sptk2 logo
SPTK Home Page
Public Member Functions | Protected Member Functions | Friends | List of all members
sptk::TCPServer Class Reference

#include <TCPServer.h>

Inheritance diagram for sptk::TCPServer:
sptk::ThreadPool sptk::ThreadEvent sptk::WSListener

Public Member Functions

 TCPServer (const String &listenerName, ServerConnection::Type connectionType, size_t threadLimit=16, LogEngine *logEngine=nullptr, const LogDetails &logDetails=LogDetails())
 
 ~TCPServer () override
 
const Hosthost () const
 
void listen (uint16_t port)
 
void stop () override
 
bool active () const
 
void log (LogPriority priority, const String &message) const
 
const LogDetailslogDetails () const
 
void setSSLKeys (std::shared_ptr< SSLKeys > sslKeys)
 
const SSLKeysgetSSLKeys () const
 
virtual void onConnection (const ServerConnection::Function &function)
 
- Public Member Functions inherited from sptk::ThreadPool
 ThreadPool (uint32_t threadLimit, std::chrono::milliseconds threadIdleSeconds, const String &threadName, LogEngine *logEngine)
 
virtual void execute (const SRunable &task)
 
void threadEvent (Thread *thread, ThreadEvent::Type eventType, SRunable runable) override
 
virtual void stop ()
 
size_t size () const
 
- Public Member Functions inherited from sptk::ThreadEvent
virtual void threadEvent (Thread *thread, Type eventType, SRunable runable)=0
 Thread event callback function. More...
 
virtual ~ThreadEvent ()=default
 Destructor.
 

Protected Member Functions

void host (const Host &host)
 
virtual bool allowConnection (sockaddr_in *connectionRequest)
 
virtual SServerConnection createConnection (SOCKET connectionSocket, const sockaddr_in *peer)
 
void threadEvent (Thread *thread, ThreadEvent::Type eventType, SRunable runable) override
 

Friends

class TCPServerListener
 
class ServerConnection
 

Additional Inherited Members

- Public Types inherited from sptk::ThreadEvent
enum class  Type : uint8_t {
  THREAD_STARTED , THREAD_FINISHED , RUNABLE_STARTED , RUNABLE_FINISHED ,
  IDLE_TIMEOUT
}
 Thread event type. More...
 

Detailed Description

TCP server

For every incoming connection, creates connection thread.

Constructor & Destructor Documentation

◆ TCPServer()

sptk::TCPServer::TCPServer ( const String listenerName,
ServerConnection::Type  connectionType,
size_t  threadLimit = 16,
LogEngine logEngine = nullptr,
const LogDetails logDetails = LogDetails() 
)

Constructor

Parameters
listenerNameLogical name of the listener
threadLimitNumber of worker threads in thread pool
logEngineOptional log engine

◆ ~TCPServer()

sptk::TCPServer::~TCPServer ( )
override

Destructor

Member Function Documentation

◆ active()

bool sptk::TCPServer::active ( ) const
inline

Returns server state

◆ allowConnection()

virtual bool sptk::TCPServer::allowConnection ( sockaddr_in *  connectionRequest)
protectedvirtual

Screens incoming connection request

Method is called right after connection request is accepted, and allows ignoring unwanted connections. By default simply returns true (allow).

Parameters
connectionRequestIncoming connection information

◆ createConnection()

virtual SServerConnection sptk::TCPServer::createConnection ( SOCKET  connectionSocket,
const sockaddr_in *  peer 
)
protectedvirtual

Creates connection thread derived from TCPServerConnection or SSLServerConnection

Application should override this method to create concrete connection object. Created connection object is maintained by CTCPServer.

Parameters
connectionSocketAlready accepted incoming connection socket
peerIncoming connection information

Reimplemented in sptk::WSListener.

◆ getSSLKeys()

const SSLKeys & sptk::TCPServer::getSSLKeys ( ) const

Get SSL keys for SSL connections (encrypted mode only)

Returns
SSL keys info

Referenced by sptk::SSLServerConnection::SSLServerConnection().

◆ host() [1/2]

const Host & sptk::TCPServer::host ( ) const

Get current host of the server

Returns

◆ host() [2/2]

void sptk::TCPServer::host ( const Host host)
protected

Modify server host. If listener is already active, don't modify exiting server host.

Parameters
hostServer host

◆ listen()

void sptk::TCPServer::listen ( uint16_t  port)

Starts listener

Parameters
portListener port number

◆ log()

void sptk::TCPServer::log ( LogPriority  priority,
const String message 
) const
inline

Server operation log

Parameters
priorityLog message priority
messageLog message

◆ onConnection()

virtual void sptk::TCPServer::onConnection ( const ServerConnection::Function &  function)
virtual

Creates connection thread derived from TCPServerConnection or SSLServerConnection

Application should override this method to create concrete connection object. Created connection object is maintained by CTCPServer.

Parameters
functionUser-defined function that is called upon client connection to server

◆ setSSLKeys()

void sptk::TCPServer::setSSLKeys ( std::shared_ptr< SSLKeys sslKeys)

Set SSL keys for SSL connections (encrypted mode only)

Parameters
sslKeysSSL keys info

◆ stop()

void sptk::TCPServer::stop ( )
overridevirtual

Stops listener

Reimplemented from sptk::ThreadPool.

◆ threadEvent()

void sptk::TCPServer::threadEvent ( Thread thread,
ThreadEvent::Type  eventType,
SRunable  runable 
)
overrideprotectedvirtual

Thread event callback function

Receives events that occur in the threads

Parameters
threadThread where event occured
eventTypeThread event type
runableRelated runable (if any)

Implements sptk::ThreadEvent.


The documentation for this class was generated from the following file:

Fri Oct 14 2022 09:58:46: SPTK 5.4.1