BaseTCPServer v1.16.1
Base class for TCP server
Loading...
Searching...
No Matches
web::BaseTCPServer Class Referenceabstract

#include <BaseTCPServer.h>

Public Member Functions

std::string getServerIpV4 () const
 Get server IP address.
 
uint16_t getServerPortV4 () const
 Get server port.
 
 BaseTCPServer (std::string_view port, std::string_view ip="0.0.0.0", DWORD timeout=0, bool multiThreading=true, u_long listenSocketBlockingMode=0, bool freeDLL=true)
 
virtual void start (bool wait=false, const std::function< void()> &onStartServer=[]() {}, std::exception **outException=nullptr)
 Start server in separate thread.
 
virtual void stop (bool wait=true)
 Stop receiving new connections.
 
virtual void kick (const std::string &ip)
 Kick specific client.
 
virtual void kickAll ()
 Kick all clients.
 
bool isServerRunning () const
 Is server accept new connections.
 
bool isListenSocketInBlockingMode () const
 Is server's listen socket in blocking mode.
 
bool isAcceptedSocketsInBlockingMode () const
 Is client's socket in blocking mode.
 
void setAcceptedSocketsBlockingMode (bool block)
 Set blocking mode for all connected sockets.
 
size_t getNumberOfClients () const
 Number of IP addresses.
 
size_t getNumberOfConnections () const
 Number of sockets(each IP address may have few sockets)
 
std::vector< std::pair< std::string, std::vector< SOCKET > > > getClients () const
 Get all clients ip - sockets.
 
std::string_view getIp () const
 Initial passed IP.
 
std::string_view getPort () const
 Initial passed port.
 
virtual ~BaseTCPServer ()
 

Static Public Member Functions

static std::string getClientIpV4 (sockaddr address)
 Get client IP address.
 
static uint16_t getClientPortV4 (sockaddr address)
 Get client port.
 
static std::string getVersion ()
 Get BaseTCPServer version.
 

Static Public Attributes

static constexpr size_t ipV4Size = 16
 

Protected Member Functions

void createListenSocket ()
 
virtual void receiveConnections (const std::function< void()> &onStartServer, std::exception **outException)
 
virtual void serve (std::string ip, SOCKET clientSocket, sockaddr address)
 
virtual void clientConnection (const std::string &ip, SOCKET clientSocket, sockaddr address, std::function< void()> &cleanup)=0
 Serving each client connection.
 
virtual void onConnectionReceive (SOCKET clientSocket, sockaddr address)
 
virtual void onInvalidConnectionReceive ()
 
virtual bool autoCloseSocket () const
 Automatically close socket after clientConnection in cleanup function.
 

Static Protected Member Functions

template<typename DataT >
static int sendBytes (SOCKET clientSocket, const DataT *const data, int size)
 
template<typename DataT >
static int receiveBytes (SOCKET clientSocket, DataT *const data, int size)
 

Protected Attributes

ClientData data
 
std::string ip
 
std::string port
 
SOCKET listenSocket
 
DWORD timeout
 
bool freeDLL
 
bool isRunning
 
const bool multiThreading
 
std::future< void > handle
 
u_long blockingMode
 0 for blocking, non 0 for non blocking
 
u_long listenSocketBlockingMode
 0 for blocking, non 0 for non blocking
 

Constructor & Destructor Documentation

◆ BaseTCPServer()

web::BaseTCPServer::BaseTCPServer ( std::string_view port,
std::string_view ip = "0.0.0.0",
DWORD timeout = 0,
bool multiThreading = true,
u_long listenSocketBlockingMode = 0,
bool freeDLL = true )
Parameters
portServer's port
ipServer's ip
timeoutrecv function timeout in milliseconds, 0 wait for upcoming data
multiThreadingEach client in separate thread
listenSocketBlockingModeBlocking mode for listen socket (0 - blocking, non 0 - non blocking)
freeDLLUnload Ws2_32.dll in destructor(Windows only parameter)

◆ ~BaseTCPServer()

web::BaseTCPServer::~BaseTCPServer ( )
virtual

Member Function Documentation

◆ autoCloseSocket()

bool web::BaseTCPServer::autoCloseSocket ( ) const
protectedvirtual

Automatically close socket after clientConnection in cleanup function.

Returns

◆ clientConnection()

virtual void web::BaseTCPServer::clientConnection ( const std::string & ip,
SOCKET clientSocket,
sockaddr address,
std::function< void()> & cleanup )
protectedpure virtual

Serving each client connection.

Parameters
ipClient IP address
clientSocketClient socket
addressStructure used to store most addresses.
cleanupMove this function if you want made cleanup by yourself

◆ createListenSocket()

void web::BaseTCPServer::createListenSocket ( )
protected

◆ getClientIpV4()

string web::BaseTCPServer::getClientIpV4 ( sockaddr address)
static

Get client IP address.

Parameters
address
Returns

◆ getClientPortV4()

uint16_t web::BaseTCPServer::getClientPortV4 ( sockaddr address)
static

Get client port.

Parameters
address
Returns

◆ getClients()

vector< pair< string, vector< SOCKET > > > web::BaseTCPServer::getClients ( ) const

Get all clients ip - sockets.

Returns

◆ getIp()

string_view web::BaseTCPServer::getIp ( ) const

Initial passed IP.

Returns

◆ getNumberOfClients()

size_t web::BaseTCPServer::getNumberOfClients ( ) const

Number of IP addresses.

Returns

◆ getNumberOfConnections()

size_t web::BaseTCPServer::getNumberOfConnections ( ) const

Number of sockets(each IP address may have few sockets)

Returns

◆ getPort()

string_view web::BaseTCPServer::getPort ( ) const

Initial passed port.

Returns

◆ getServerIpV4()

string web::BaseTCPServer::getServerIpV4 ( ) const

Get server IP address.

Returns

◆ getServerPortV4()

uint16_t web::BaseTCPServer::getServerPortV4 ( ) const

Get server port.

Returns

◆ getVersion()

string web::BaseTCPServer::getVersion ( )
static

Get BaseTCPServer version.

Returns

◆ isAcceptedSocketsInBlockingMode()

bool web::BaseTCPServer::isAcceptedSocketsInBlockingMode ( ) const

Is client's socket in blocking mode.

Returns

◆ isListenSocketInBlockingMode()

bool web::BaseTCPServer::isListenSocketInBlockingMode ( ) const

Is server's listen socket in blocking mode.

Returns

◆ isServerRunning()

bool web::BaseTCPServer::isServerRunning ( ) const

Is server accept new connections.

Returns

◆ kick()

void web::BaseTCPServer::kick ( const std::string & ip)
virtual

Kick specific client.

Parameters
ip

◆ kickAll()

void web::BaseTCPServer::kickAll ( )
virtual

Kick all clients.

◆ onConnectionReceive()

void web::BaseTCPServer::onConnectionReceive ( SOCKET clientSocket,
sockaddr address )
protectedvirtual

◆ onInvalidConnectionReceive()

void web::BaseTCPServer::onInvalidConnectionReceive ( )
protectedvirtual

◆ receiveBytes()

template<typename DataT >
int web::BaseTCPServer::receiveBytes ( SOCKET clientSocket,
DataT *const data,
int size )
staticprotected

◆ receiveConnections()

void web::BaseTCPServer::receiveConnections ( const std::function< void()> & onStartServer,
std::exception ** outException )
protectedvirtual

◆ sendBytes()

template<typename DataT >
int web::BaseTCPServer::sendBytes ( SOCKET clientSocket,
const DataT *const data,
int size )
staticprotected

◆ serve()

void web::BaseTCPServer::serve ( std::string ip,
SOCKET clientSocket,
sockaddr address )
protectedvirtual

◆ setAcceptedSocketsBlockingMode()

void web::BaseTCPServer::setAcceptedSocketsBlockingMode ( bool block)

Set blocking mode for all connected sockets.

Parameters
block

◆ start()

void web::BaseTCPServer::start ( bool wait = false,
const std::function< void()> & onStartServer = []() {},
std::exception ** outException = nullptr )
virtual

Start server in separate thread.

Parameters
waitWait server serving in current thread
onStartServerCall function before accept first connection

◆ stop()

void web::BaseTCPServer::stop ( bool wait = true)
virtual

Stop receiving new connections.

Parameters
waitWait all clients tasks

Member Data Documentation

◆ blockingMode

u_long web::BaseTCPServer::blockingMode
protected

0 for blocking, non 0 for non blocking

◆ data

ClientData web::BaseTCPServer::data
protected

◆ freeDLL

bool web::BaseTCPServer::freeDLL
protected

◆ handle

std::future<void> web::BaseTCPServer::handle
protected

◆ ip

std::string web::BaseTCPServer::ip
protected

◆ ipV4Size

size_t web::BaseTCPServer::ipV4Size = 16
staticconstexpr

◆ isRunning

bool web::BaseTCPServer::isRunning
protected

◆ listenSocket

SOCKET web::BaseTCPServer::listenSocket
protected

◆ listenSocketBlockingMode

u_long web::BaseTCPServer::listenSocketBlockingMode
protected

0 for blocking, non 0 for non blocking

◆ multiThreading

const bool web::BaseTCPServer::multiThreading
protected

◆ port

std::string web::BaseTCPServer::port
protected

◆ timeout

DWORD web::BaseTCPServer::timeout
protected

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