Bond's TCP Library  1.0
Bond's TCP Client/Server Library
tcpsocket.h
Go to the documentation of this file.
1 
13 #ifndef TCP_SOCKET_H
14 #define TCP_SOCKET_H
15 
16 #include <iostream>
17 #include <deque>
18 #include <map>
19 #include <vector>
20 #include <thread>
21 #include <mutex>
22 #include <arpa/inet.h>
23 #include <sys/epoll.h>
24 #include "tcpssl.h"
25 
27 namespace tcp {
28 
29 using namespace std;
30 
33 void setLogStream(ostream *os);
34 
36 void error(string msg);
37 
39 void error(string label, string msg);
40 
42 void warning(string msg);
43 
45 void warning(string label, string msg);
46 
48 void log(string msg);
49 
51 void log(string label, string msg);
52 
53 class Socket;
54 class SSLContext;
55 
58 enum class SocketState {UNCONNECTED=0, LISTENING, CONNECTING, CONNECTED, DISCONNECTED};
59 
72 class EPoll {
73  public:
75  EPoll();
76 
78  ~EPoll();
79 
82  void poll(int timeout);
83  private:
84  static const int MAX_EVENTS = 10;
85  bool add(Socket& socket, int events);
86  bool update(Socket& socket, int events);
87  bool remove(Socket& socket);
88  void handleEvents(uint32_t events, int fd);
89  int handle_;
90  epoll_event events[MAX_EVENTS];
91  std::map<int,tcp::Socket*> sockets;
92  friend class Socket;
93 };
94 
96 class Socket {
97  public:
98 
108  Socket(EPoll &epoll, const int domain = AF_INET, const int socket = 0, const bool blocking = false, const int events = (EPOLLIN | EPOLLRDHUP));
109 
112  ~Socket();
113 
115  int socket() const { return socket_; }
116 
118  int domain() const { return domain_; }
119 
121  virtual void disconnect();
122 
123  protected:
124 
128  bool setEvents(int events);
129 
133  virtual void handleEvents(uint32_t events) = 0;
134 
138  virtual void disconnected();
139 
141  recursive_mutex mtx;
142 
144  EPoll &epoll() { return epoll_; }
145 
148 
149  private:
150  EPoll &epoll_;
151  int events_;
152  int domain_;
153  int socket_;
154  friend class EPoll;
155 };
156 
159 class DataSocket : public Socket {
160  public:
161  DataSocket(EPoll &epoll, const int domain = AF_INET, const int socket = 0, const bool blocking = false, const int events = (EPOLLIN | EPOLLRDHUP)) :
162  Socket(epoll,domain,socket,blocking,events) {}
163 
165  size_t available() { return inputBuffer.size(); }
166 
169  size_t read(void *buffer, size_t size);
170 
173  size_t write(const void *buffer, size_t size);
174 
175  protected:
176 
178  void readToInputBuffer();
179 
183  void sendOutputBuffer();
184 
187  void canSend(bool value);
188 
192  void handleEvents(uint32_t events) override;
193 
196  void disconnect() override;
197 
201  void disconnected() override;
202 
206  virtual void dataAvailable() = 0;
207 
210  virtual SSL *createSSL(SSLContext *context);
211 
213  SSL *ssl_ {nullptr};
214 
215  private:
216  size_t read_(void *buffer, size_t size);
217  size_t write_(const void *buffer, size_t size);
218  deque<uint8_t> inputBuffer;
219  deque<uint8_t> outputBuffer;
220  friend class SSL;
221 };
222 
227 int getDomainFromHostAndPort(const char* host, const char* port, int def_domain = AF_INET);
228 
229 } // namespace tcp
230 
231 #endif // include guard
tcp::setLogStream
void setLogStream(ostream *os)
Set the output stream used by the library for log, warning and error messages.
Definition: tcpsocket.cpp:17
tcp::Socket::epoll
EPoll & epoll()
Returns a reference to the epoll instance used by this socket.
Definition: tcpsocket.h:144
tcp::error
void error(string msg)
Send an error message to the log stream.
Definition: tcpsocket.cpp:18
tcp::Socket::mtx
recursive_mutex mtx
The mutex used to provide exclusive access to the socket.
Definition: tcpsocket.h:141
tcp::getDomainFromHostAndPort
int getDomainFromHostAndPort(const char *host, const char *port, int def_domain)
Tries to determine which address family to use from a host and port string.
Definition: tcpsocket.cpp:357
tcp::SocketState
SocketState
Determines the state of a socket.
Definition: tcpsocket.h:58
tcp::warning
void warning(string msg)
Send a warning message to the log stream.
Definition: tcpsocket.cpp:20
tcp::DataSocket
Represents a buffered socket that can send and receive data using optional SSL encryption.
Definition: tcpsocket.h:159
tcp::SSL
Encapsulates an SSL connection data structure.
Definition: tcpssl.h:109
tcp::Socket::state_
SocketState state_
Descendant classes can manipulate the socket state directly.
Definition: tcpsocket.h:147
tcp::DataSocket::available
size_t available()
Returns the number of bytes available in the inputBuffer.
Definition: tcpsocket.h:165
tcp::SSLContext
Encapsulates an openSSL SSL_CTX record.
Definition: tcpssl.h:41
tcp::Socket::socket
int socket() const
Return the linux socket handle.
Definition: tcpsocket.h:115
tcp::EPoll
Encapsulates the EPoll interface.
Definition: tcpsocket.h:72
tcpssl.h
Provides ssl client and server functionality.
tcp::Socket::domain
int domain() const
Return the socket domain (AF_INET or AF_INET6)
Definition: tcpsocket.h:118
tcp::log
void log(string msg)
Send an log message to the log stream.
Definition: tcpsocket.cpp:22
tcp
A tcp client/server library for linux that supports openSSL and EPoll.
Definition: tcpclient.cpp:5
tcp::Socket
Encapsulates a socket handle that is capable of recieving epoll events.
Definition: tcpsocket.h:96