Bond's TCP Library  1.0
Bond's TCP Client/Server Library
tcpserver.h
Go to the documentation of this file.
1 
10 #ifndef TCP_SERVER_H
11 #define TCP_SERVER_H
12 
13 #include <iostream>
14 #include <map>
15 #include <deque>
16 #include <string.h>
17 #include "tcpsocket.h"
18 #include "tcpssl.h"
19 
20 namespace tcp {
21 
22 using namespace std;
23 
24 // Forward declarations:
25 
26 class Server;
27 class Session;
28 
35 class Server : public Socket {
36  public:
40  Server(EPoll &epoll, SSLContext *ctx, const int domain = AF_INET) : Socket(epoll,domain,0,false,EPOLLIN), useSSL_(ctx), ctx_(ctx) {}
41 
46  virtual ~Server();
47 
57  void start(in_port_t port, string bindaddress, bool useSSL = false, int backlog = 64);
61  void start(in_port_t port, char *bindaddress, bool useSSL = false, int backlog = 64);
62 
64  void stop();
65 
71  bool listening() { return state_ == SocketState::LISTENING; }
72 
74  static bool printifaddrs();
75 
77  SSLContext *ctx() { return ctx_; }
78 
79  protected:
80 
85  void handleEvents(uint32_t events) override;
86 
93  virtual Session* createSession(const int socket, const sockaddr_in peer_address) = 0;
94 
96  bool findifaddr(const string ifname, sockaddr *addr);
97 
101  std::map<int,tcp::Session*> sessions;
102 
103  private:
104  bool bindToAddress(sockaddr *addr, socklen_t len);
105  bool startListening(int backlog);
106  bool acceptConnection();
107  bool useSSL_ {false};
108  SSLContext *ctx_;
109  struct sockaddr_storage addr_;
110  friend class Session;
111 };
112 
117 class Session : public DataSocket {
118  public:
119 
121  Server &server() const { return server_; }
122 
124  in_port_t peer_port() const { return port_; }
125 
127  in_addr_t peer_address() const { return addr_; }
128 
130  bool connected() const { return state_ == SocketState::CONNECTED; }
131 
138  void disconnect() override;
139 
140  protected:
141 
145  Session(EPoll &epoll, Server& server, const int socket, const struct sockaddr_in peer_addr)
146  : DataSocket(epoll,server.domain(),socket), server_(server), port_(peer_addr.sin_port), addr_(peer_addr.sin_addr.s_addr) { }
147 
149  virtual ~Session();
150 
155  virtual void accepted();
156 
164  void disconnected() override;
165 
166  friend class SSL;
167  private:
168  void connectionMessage(string action);
169  Server& server_;
170  in_port_t port_;
171  in_addr_t addr_;
172  friend class Server;
173 };
174 
175 }
176 
177 #endif
tcp::Server::ctx
SSLContext * ctx()
Get the SSL context for the server.
Definition: tcpserver.h:77
tcp::Session::Session
Session(EPoll &epoll, Server &server, const int socket, const struct sockaddr_in peer_addr)
Creates a new session.
Definition: tcpserver.h:145
tcp::Session::connected
bool connected() const
Returns true if the session is connected to a peer.
Definition: tcpserver.h:130
tcpsocket.h
Shared base classes for tcpclient.h and tcpserver.h.
tcp::Session::peer_port
in_port_t peer_port() const
Returns the peer port number used to connect to this Session.
Definition: tcpserver.h:124
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::Server::listening
bool listening()
Determine if the server is listening.
Definition: tcpserver.h:71
tcp::Server::sessions
std::map< int, tcp::Session * > sessions
Maps socket handles to their corresponding tcp::Session objects.
Definition: tcpserver.h:101
tcp::SSLContext
Encapsulates an openSSL SSL_CTX record.
Definition: tcpssl.h:41
tcp::Server::Server
Server(EPoll &epoll, SSLContext *ctx, const int domain=AF_INET)
Construct a server instance.
Definition: tcpserver.h:40
tcp::EPoll
Encapsulates the EPoll interface.
Definition: tcpsocket.h:72
tcp::Server
Listens for TCP connections and establishes Sessions.
Definition: tcpserver.h:35
tcpssl.h
Provides ssl client and server functionality.
tcp::Session::peer_address
in_addr_t peer_address() const
Returns the peer address used to connect to this Session.
Definition: tcpserver.h:127
tcp::Session::server
Server & server() const
Returns a reference to the Server that owns this Session.
Definition: tcpserver.h:121
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
tcp::Session
Represents a TCP connection accepted by the Server.
Definition: tcpserver.h:117