GCC Code Coverage Report


Directory: ./
File: pdserv/src/TCP.h
Date: 2023-11-12 04:06:57
Exec Total Coverage
Lines: 2 2 100.0%
Branches: 0 0 -%

Line Branch Exec Source
1 /*****************************************************************************
2 *
3 * $Id$
4 *
5 * Copyright 2017 Richard Hacker (lerichi at gmx dot net)
6 *
7 * This file is part of the pdserv library.
8 *
9 * The pdserv library is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published
11 * by the Free Software Foundation, either version 3 of the License, or (at
12 * your option) any later version.
13 *
14 * The pdserv library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17 * License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with the pdserv library. If not, see <http://www.gnu.org/licenses/>.
21 *
22 *****************************************************************************/
23
24 #ifndef TCP_H
25 #define TCP_H
26
27 #include <stdint.h>
28 #include <unistd.h> // ssize_t
29 #include <ostream>
30 #include <vector>
31 #include <string>
32 #include <sys/socket.h> // struct sockaddr
33 #include <netinet/tcp.h> // setsockopt()
34
35 #include "PThread.h"
36
37 namespace net {
38
39 // Open a server socket.
40 class TCPSocket {
41 public:
42 // Returns: true if file descriptor is valid
43 operator bool() const;
44
45 void setSockOpt(int level, int name, int val);
46
47 // return: string <IP><sep><port> of listen socket
48 std::string getAddr(char sep = ':') const;
49
50 // select between multiple sockets. returns first available socket or NULL
51 static TCPSocket* select(std::vector<TCPSocket*> const& sockets, int msec = -1);
52
53 protected:
54 /// Constructor
55 TCPSocket();
56 ~TCPSocket();
57
58 void close();
59
60 // Returns success
61 // throws std::string() on error
62 bool listenTo(
63 const std::string& interface, const std::string& port,
64 int backlog);
65
66 // return: string of remote host
67 // thows: std::string on error
68 std::string accept(TCPSocket* server);
69
70 std::string reject();
71
72 bool readable(struct timeval *timeout) const;
73 ssize_t readData ( void *buf, size_t len);
74 ssize_t writeData(const void *buf, size_t len);
75
76 private:
77 int fd;
78 struct sockaddr addr;
79 };
80
81 188 class TCPServer: public TCPSocket {
82 public:
83 TCPServer(int backlog);
84
85 // Returns success
86 // throws std::string() on error
87 bool listenTo(
88 const std::string& interface, const std::string& port);
89
90 bool isPendingConnection(int msec = -1);
91 std::string reject();
92
93 private:
94 int backlog;
95 };
96
97 class TCPSession: public pthread::Thread, public TCPSocket {
98 public:
99 // Accept a connection
100 // throws: std::string on error
101 TCPSession(TCPServer *);
102
103 // return: reverse DNS lookup peer name
104 std::string getPeerName() const;
105
106 // Wait for socket read to become ready
107 bool isPendingRead(int msec) const;
108
109 // read() from socket
110 //
111 // return: >=0: bytes read from socket
112 // <0: -errno on error
113 ssize_t readData ( void *buf, size_t len);
114
115 // write() to socket
116 //
117 // return: >=0: bytes written to socket
118 // <0: -errno on error
119 ssize_t writeData(const void *buf, size_t len);
120
121 protected:
122 124 ~TCPSession() {}
123
124 private:
125 std::string peerName;
126 };
127 }
128
129 #endif // TCP_H
130