Bond's TCP Library  1.0
Bond's TCP Client/Server Library
tcp::Session Class Reference

Represents a TCP connection accepted by the Server. More...

#include <tcpserver.h>

Collaboration diagram for tcp::Session:

Public Member Functions

Serverserver () const
 Returns a reference to the Server that owns this Session.
 
in_port_t peer_port () const
 Returns the peer port number used to connect to this Session.
 
in_addr_t peer_address () const
 Returns the peer address used to connect to this Session.
 
bool connected () const
 Returns true if the session is connected to a peer.
 
void disconnect () override
 Shutdown the socket and close the session. More...
 
- Public Member Functions inherited from tcp::DataSocket
 DataSocket (EPoll &epoll, const int domain=AF_INET, const int socket=0, const bool blocking=false, const int events=(EPOLLIN|EPOLLRDHUP))
 
size_t available ()
 Returns the number of bytes available in the inputBuffer.
 
size_t read (void *buffer, size_t size)
 Reads up to size bytes from inputBuffer into buffer. More...
 
size_t write (const void *buffer, size_t size)
 Writes the contents of buffer to the outputBuffer. More...
 
- Public Member Functions inherited from tcp::Socket
 Socket (EPoll &epoll, const int domain=AF_INET, const int socket=0, const bool blocking=false, const int events=(EPOLLIN|EPOLLRDHUP))
 Construct a blocking or non-blocking socket handle that responds to certain epoll events. More...
 
 ~Socket ()
 Closes and destroys the socket. More...
 
int socket () const
 Return the linux socket handle.
 
int domain () const
 Return the socket domain (AF_INET or AF_INET6)
 

Protected Member Functions

 Session (EPoll &epoll, Server &server, const int socket, const struct sockaddr_in peer_addr)
 Creates a new session. More...
 
virtual ~Session ()
 The destructor is protected and is called by the disconnect() or disconnected() methods.
 
virtual void accepted ()
 Called by the Server::acceptConnection after a connection has been accepted. More...
 
void disconnected () override
 Called when a tcp connection is dropped. More...
 
- Protected Member Functions inherited from tcp::DataSocket
void readToInputBuffer ()
 Reads all available data from the socket into inputBuffer.
 
void sendOutputBuffer ()
 Writes all available data from the outputBuffer to the socket. More...
 
void canSend (bool value)
 Sets the epoll event flags. More...
 
void handleEvents (uint32_t events) override
 Called by the EPoll class when the listening socket recieves an epoll event. More...
 
void disconnect () override
 Shuts down any SSL connection gracefully. More...
 
void disconnected () override
 Called when a connection is disconnected. More...
 
virtual void dataAvailable ()=0
 Called whenever new data is appended to the inputBuffer. More...
 
virtual SSLcreateSSL (SSLContext *context)
 Factory method for returning an SSL object. More...
 
- Protected Member Functions inherited from tcp::Socket
bool setEvents (int events)
 Changes which epoll events the socket listens for. More...
 
EPollepoll ()
 Returns a reference to the epoll instance used by this socket.
 

Friends

class SSL
 
class Server
 

Additional Inherited Members

- Protected Attributes inherited from tcp::DataSocket
SSLssl_ {nullptr}
 Exposes the underlying SSL record used for openSSL calls to descendant classes.
 
- Protected Attributes inherited from tcp::Socket
recursive_mutex mtx
 The mutex used to provide exclusive access to the socket.
 
SocketState state_
 Descendant classes can manipulate the socket state directly.
 

Detailed Description

Represents a TCP connection accepted by the Server.

A Session descendant class is instantiated by the server using the Session::createSession() method. It is destroyed by calling disconnect() or disconnected()

Definition at line 117 of file tcpserver.h.

Constructor & Destructor Documentation

◆ Session()

tcp::Session::Session ( EPoll epoll,
Server server,
const int  socket,
const struct sockaddr_in  peer_addr 
)
inlineprotected

Creates a new session.

The constructor is protected and is called by the Server::createSession() method

Definition at line 145 of file tcpserver.h.

146  : DataSocket(epoll,server.domain(),socket), server_(server), port_(peer_addr.sin_port), addr_(peer_addr.sin_addr.s_addr) { }

Member Function Documentation

◆ accepted()

void tcp::Session::accepted ( )
protectedvirtual

Called by the Server::acceptConnection after a connection has been accepted.

Server calls this method to signal the start of the session Override accepted() to perform initial actions when a session starts.

Override accepted to perform operations when a session is first established. In the base class, accepted() prints a message to clog.

Definition at line 248 of file tcpserver.cpp.

248  {
249  mtx.lock();
250  connectionMessage("accepted");
251  if (server().useSSL_) {
252  ssl_ = createSSL(server().ctx());
253  ssl_->setfd(socket());
254  if (ssl_->accept())
255  state_ = SocketState::CONNECTED;
256  else
257  disconnected();
258  } else {
259  state_ = SocketState::CONNECTED;
260  }
261  mtx.unlock();
262 }

◆ disconnect()

void tcp::Session::disconnect ( )
overridevirtual

Shutdown the socket and close the session.

Starts a graceful shutdown of the session Override disconnect() to send any last messages required before the session is terminated.

Internally calls disconnect()

Override disconnect() to perform additional cleanup operations before a session is intentionally closed.

Warning
disconnect() causes the Session to be destroyed

Be sure to call flush() to ensure the data is actually written to the write buffer.

Reimplemented from tcp::Socket.

Definition at line 267 of file tcpserver.cpp.

267  {
268  if (ssl_) {
269  mtx.lock();
270  ssl_->shutdown();
271  printSSLErrors();
272  mtx.unlock();
273  }
274  disconnected();
275 }
Here is the call graph for this function:

◆ disconnected()

void tcp::Session::disconnected ( )
overrideprotectedvirtual

Called when a tcp connection is dropped.

Called in response to a disconnected TCP Connection Override disconnected() to perform cleanup operations when a connection is unexpectedly lost.

Shuts down the network socket, removes itself from Server.sessions[], then destroys itself.

An application can override disconnected() to perform additional cleanup operations before the underlying TCP connection gets torn down.

Remarks
To intentionally close a Session, call disconnect() instead
Warning
disconnected() destroys the Session

Reimplemented from tcp::Socket.

Definition at line 279 of file tcpserver.cpp.

279  {
280  if (connected()) {
281  mtx.lock();
282  if (ssl_) {
283  delete ssl_;
284  ssl_ = nullptr;
285  printSSLErrors();
286  }
287  state_ = SocketState::DISCONNECTED;
288  connectionMessage("disconnected");
289  delete this;
290  mtx.unlock();
291  }
292 }
Here is the call graph for this function:

The documentation for this class was generated from the following files:
tcp::printSSLErrors
void printSSLErrors()
This method logs openSSL errors to cerr.
Definition: tcpssl.cpp:56
tcp::DataSocket::createSSL
virtual SSL * createSSL(SSLContext *context)
Factory method for returning an SSL object.
Definition: tcpsocket.cpp:348
tcp::Socket::epoll
EPoll & epoll()
Returns a reference to the epoll instance used by this socket.
Definition: tcpsocket.h:144
tcp::Socket::mtx
recursive_mutex mtx
The mutex used to provide exclusive access to the socket.
Definition: tcpsocket.h:141
tcp::Session::connected
bool connected() const
Returns true if the session is connected to a peer.
Definition: tcpserver.h:130
tcp::Socket::state_
SocketState state_
Descendant classes can manipulate the socket state directly.
Definition: tcpsocket.h:147
tcp::SSL::setfd
bool setfd(int socket)
Sets the socket file descripter for this SSL object.
Definition: tcpssl.cpp:472
tcp::SSL::shutdown
void shutdown()
Closes the SSL connection gracefully.
Definition: tcpssl.cpp:641
tcp::Socket::socket
int socket() const
Return the linux socket handle.
Definition: tcpsocket.h:115
tcp::DataSocket::ssl_
SSL * ssl_
Exposes the underlying SSL record used for openSSL calls to descendant classes.
Definition: tcpsocket.h:213
tcp::Socket::domain
int domain() const
Return the socket domain (AF_INET or AF_INET6)
Definition: tcpsocket.h:118
tcp::Session::server
Server & server() const
Returns a reference to the Server that owns this Session.
Definition: tcpserver.h:121
tcp::Session::disconnected
void disconnected() override
Called when a tcp connection is dropped.
Definition: tcpserver.cpp:279
tcp::SSL::accept
bool accept()
Starts the SSL server handshake sequence.
Definition: tcpssl.cpp:576