libhomegear-base
0.7
Base library for Homegear and Homegear family modules.
|
Class to easily create a TCP server or client. More...
#include <TcpSocket.h>
Classes | |
struct | AverageMeanData |
class | CertificateCredentials |
struct | CertificateInfo |
class | QueueEntry |
struct | TcpClientData |
struct | TcpServerInfo |
Public Types | |
typedef std::vector< uint8_t > | TcpPacket |
typedef std::shared_ptr< TcpClientData > | PTcpClientData |
typedef std::shared_ptr< CertificateInfo > | PCertificateInfo |
Public Member Functions | |
TcpSocket (BaseLib::SharedObjects *baseLib) | |
Constructor to create an empty socket object. More... | |
TcpSocket (BaseLib::SharedObjects *baseLib, std::shared_ptr< FileDescriptor > socketDescriptor) | |
Constructor to just wrap an existing socket descriptor. More... | |
TcpSocket (BaseLib::SharedObjects *baseLib, std::string hostname, std::string port) | |
Constructor to create a TCP client socket. More... | |
TcpSocket (BaseLib::SharedObjects *baseLib, std::string hostname, std::string port, bool useSsl, std::string caFile, bool verifyCertificate) | |
Constructor to create a TCP client socket with SSL enabled. More... | |
TcpSocket (BaseLib::SharedObjects *baseLib, std::string hostname, std::string port, bool useSsl, bool verifyCertificate, std::string caData) | |
Constructor to create a TCP client socket with SSL enabled. More... | |
TcpSocket (BaseLib::SharedObjects *baseLib, std::string hostname, std::string port, bool useSsl, std::string caFile, bool verifyCertificate, std::string clientCertFile, std::string clientKeyFile) | |
Constructor to create a TCP client socket with SSL enabled using certificate login. More... | |
TcpSocket (BaseLib::SharedObjects *baseLib, std::string hostname, std::string port, bool useSsl, bool verifyCertificate, std::string caData, std::string clientCertData, const std::shared_ptr< Security::SecureVector< uint8_t >> &clientKeyData) | |
Constructor to create a TCP client socket with SSL enabled using certificate login. More... | |
TcpSocket (BaseLib::SharedObjects *baseLib, std::string hostname, std::string port, bool useSsl, bool verifyCertificate, std::string caFile, std::string caData, std::string clientCertFile, std::string clientCertData, std::string clientKeyFile, const std::shared_ptr< Security::SecureVector< uint8_t >> &) | |
Constructor to create a TCP client socket with SSL enabled using certificate login. More... | |
TcpSocket (BaseLib::SharedObjects *baseLib, TcpServerInfo &serverInfo) | |
Constructor to create a TCP server. More... | |
virtual | ~TcpSocket () |
PFileDescriptor | getFileDescriptor () |
std::string | getIpAddress () |
int32_t | getPort () |
bool | getRequireClientCert () |
void | setConnectionRetries (int32_t retries) |
void | setReadTimeout (int64_t timeout) |
void | setWriteTimeout (int64_t timeout) |
void | setAutoConnect (bool autoConnect) |
void | setHostname (std::string hostname) |
void | setPort (std::string port) |
void | setUseSSL (bool useSsl) |
void | setCertificates (std::unordered_map< std::string, PCertificateInfo > &certificates) |
void | reloadCertificates () |
void | setVerifyCertificate (bool verifyCertificate) |
void | setVerifyHostname (bool verifyHostname) |
void | setVerificationHostname (const std::string &hostname) |
Only relevant for TLS connections. More... | |
std::unordered_map< std::string, std::shared_ptr< CertificateCredentials > > | getCredentials () |
bool | connected () |
Tests in this order and returns false if one of the tests fails: More... | |
double | GetPacketsPerMinuteReceived () |
double | GetPacketsPerMinuteSent () |
double | GetServerThreadLoad () |
Returns the current server thread load. More... | |
double | GetProcessingThreadLoad () |
Returns the current processing thread load. More... | |
double | GetProcessingThreadLoadMax () |
Returns the maximum load of the last minute. More... | |
int32_t | proofread (char *buffer, int32_t bufferSize) |
Use this overload when there are no socket operations outside of this class. More... | |
int32_t | proofread (char *buffer, int32_t bufferSize, bool &moreData, bool skip_poll=false) |
Use this overload of proofread when select is called outside of this class. More... | |
int32_t | proofwrite (const std::shared_ptr< std::vector< char >> &data) |
Writes data to a socket. More... | |
int32_t | proofwrite (const std::vector< char > &data) |
Writes data to a socket. More... | |
int32_t | proofwrite (const std::string &data) |
Writes data to a socket. More... | |
int32_t | proofwrite (const char *buffer, int32_t bytesToWrite) |
Writes data to a socket. More... | |
void | open () |
void | close () |
void | bindServerSocket (std::string address, std::string port, std::string &listenAddress) |
Binds a socket to an IP address and a port. More... | |
void | startPreboundServer (std::string &listenAddress, size_t processingThreads=0) |
Starts listening on the already bound socket (created with bindServerSocket()). More... | |
void | startServer (std::string address, std::string port, std::string &listenAddress, size_t processingThreads=0) |
Starts listening. More... | |
void | startServer (std::string address, std::string &listenAddress, int32_t &listenPort, size_t processingThreads=0) |
Starts listening on a dynamically assigned port. More... | |
void | stopServer () |
Starts stopping the server and returns immediately. More... | |
void | waitForServerStopped () |
Waits until the server is stopped. More... | |
bool | sendToClient (int32_t clientId, const TcpPacket &packet, bool closeConnection=false) |
Sends a response to a TCP client connected to the server. More... | |
bool | sendToClient (int32_t clientId, const std::vector< char > &packet, bool closeConnection=false) |
Sends a response to a TCP client connected to the server. More... | |
void | closeClientConnection (int32_t clientId) |
Closes the connection to a connected client. More... | |
int32_t | clientCount () |
Returns the number of clients connected to the TCP server. More... | |
uint32_t | processingQueueSize () |
Returns the size of the packet processing queue. More... | |
std::string | getClientCertDn (int32_t clientId) |
Returns the distinguished name of the client certificate. More... | |
std::string | getClientCertSerial (int32_t clientId) |
int64_t | getClientCertExpiration (int32_t clientId) |
Public Member Functions inherited from BaseLib::IQueue | |
IQueue (SharedObjects *baseLib, uint32_t queueCount, uint32_t bufferSize) | |
Constructor. More... | |
virtual | ~IQueue () |
void | startQueue (int32_t index, bool waitWhenFull, uint32_t processingThreadCount, int32_t threadPriority=0, int32_t threadPolicy=SCHED_OTHER) |
Starts the threads of a queue. More... | |
void | startQueue (int32_t index, bool waitWhenFull, uint32_t initialProcessingThreadCount, uint32_t maxProcessingThreadCount) |
Starts the threads of a queue using a thread count that can be increased later. More... | |
void | stopQueue (int32_t index) |
Stops the threads of a queue previously started with startQueue() . More... | |
bool | queueIsStarted (int32_t index) |
Checks if the specified queue has been started. More... | |
bool | addThread (int32_t index) |
Start an additional thread. More... | |
bool | enqueue (int32_t index, std::shared_ptr< IQueueEntry > &entry, bool waitWhenFull=false) |
Enqueues an item. More... | |
virtual void | processQueueEntry (int32_t index, std::shared_ptr< IQueueEntry > &entry)=0 |
This method is called by the processing threads for each item to process. More... | |
bool | queueEmpty (int32_t index) |
Checks if a queue is empty. More... | |
uint32_t | processingThreadCount (int32_t index) |
Returns the number of processing threads. More... | |
uint32_t | maxProcessingThreadCount (int32_t index) |
Returns the maximum number of processing threads. More... | |
int32_t | queueSize (int32_t index) |
Returns the number of items queued in a queue. More... | |
double | threadLoad (int32_t index) |
double | maxThreadLoad (int32_t index) |
double | maxThreadLoad1m (int32_t index) |
double | maxThreadLoad10m (int32_t index) |
double | maxThreadLoad1h (int32_t index) |
int64_t | maxWait (int32_t index) |
int64_t | maxWait1m (int32_t index) |
int64_t | maxWait10m (int32_t index) |
int64_t | maxWait1h (int32_t index) |
Public Member Functions inherited from BaseLib::IQueueBase | |
IQueueBase (SharedObjects *baseLib, uint32_t queueCount) | |
virtual | ~IQueueBase () |
void | printQueueFullError (BaseLib::Output &out, const std::string &message) |
Prints a rate limited (1 output per 10 seconds) error message and keeps count of error messages. More... | |
Static Public Member Functions | |
static PFileDescriptor | bindAndReturnSocket (FileDescriptorManager &fileDescriptorManager, const std::string &address, const std::string &port, uint32_t connectionBacklogSize, std::string &listenAddress, int32_t &listenPort) |
Protected Member Functions | |
void | getSocketDescriptor () |
void | getConnection () |
void | getSsl () |
void | initSsl () |
void | initTlsPriorityCache () |
void | autoConnect () |
void | bindSocket () |
void | serverThread (uint32_t thread_index) |
void | processQueueEntry (int32_t index, std::shared_ptr< BaseLib::IQueueEntry > &entry) override |
void | collectGarbage () |
void | collectGarbage (std::unordered_map< int32_t, PTcpClientData > &clients) |
void | initClientSsl (PTcpClientData &clientData) |
void | readClient (const PTcpClientData &client_data, std::unordered_map< int32_t, PTcpClientData > &backlog_clients) |
Protected Attributes | |
BaseLib::SharedObjects * | _bl = nullptr |
int32_t | _connectionRetries = 3 |
int64_t | _readTimeout = 15000000 |
int64_t | _writeTimeout = 15000000 |
std::atomic_bool | _connecting |
bool | _autoConnect = true |
std::string | _ipAddress |
std::string | _hostname |
std::string | _verificationHostname |
std::string | _port |
std::mutex | _readMutex |
std::mutex | _writeMutex |
std::unordered_map< std::string, PCertificateInfo > | _certificates |
bool | _verifyCertificate = true |
bool | _verifyHostname = true |
bool | _isServer = false |
uint32_t | _backlogSize = 100 |
uint32_t | _maxConnections = 10 |
std::string | _dhParamFile |
std::string | _dhParamData |
bool | _requireClientCert = false |
std::atomic< uint32_t > | server_threads_in_use_ {0} |
std::vector< AverageMeanData > | average_packets_per_minute_received_ |
std::vector< AverageMeanData > | average_packets_per_minute_sent_ |
std::function< void(int32_t clientId, std::string address, uint16_t port)> | _newConnectionCallback |
std::function< void(int32_t clientId)> | _connectionClosedCallback |
std::function< void(int32_t clientId, int32_t errorCode, const std::string &errorString)> | _connectionClosedCallbackEx |
std::function< void(int32_t clientId, TcpPacket &packet)> | _packetReceivedCallback |
std::string | _listenAddress |
std::string | _listenPort |
int32_t | _boundListenPort = -1 |
gnutls_priority_t | _tlsPriorityCache = nullptr |
std::atomic_bool | _stopServer |
std::vector< std::thread > | server_threads_ |
int64_t | _lastGarbageCollection = 0 |
std::mutex | _clientsMutex |
std::map< int32_t, PTcpClientData > | _clients |
std::mutex | _socketDescriptorMutex |
PFileDescriptor | _socketDescriptor |
bool | _useSsl = false |
std::mutex | _certificateCredentialsMutex |
std::shared_ptr< CertificateCredentials > | _currentClientCertificateCredentials |
Contains a copy of the current credentials in case the credentials are replaced while the connection is open. More... | |
std::unordered_map< std::string, std::shared_ptr< CertificateCredentials > > | _certificateCredentials |
Stores the certificate credentials so that they can be replaced at any time. More... | |
Protected Attributes inherited from BaseLib::IQueueBase | |
SharedObjects * | _bl = nullptr |
int32_t | _queueCount = 2 |
std::unique_ptr< std::atomic_bool[]> | _stopProcessingThread |
std::atomic< uint32_t > | _droppedEntries {0} |
std::atomic< int64_t > | _lastQueueFullError {0} |
Class to easily create a TCP server or client.
Save the example below in main.cpp
and compile with:
g++ -o main -std=c++11 main.cpp -lhomegear-base -lgcrypt -lgnutls
Start and connect using e. g. telnet:
./main telnet localhost 8082
Example code:
#include <homegear-base/BaseLib.h> std::shared_ptr<BaseLib::SharedObjects> _bl; std::shared_ptr<BaseLib::TcpSocket> _tcpServer; void newConnection(int32_t clientId, std::string address, uint16_t port) { std::cout << "New connection from " << address << " on port " << port << ". Client ID: " << clientId << std::endl; } void packetReceived(int32_t clientId, BaseLib::TcpSocket::TcpPacket& packet) { std::string data((char*)packet.data(), packet.size()); BaseLib::HelperFunctions::trim(data); std::cout << "Packet received from client " << clientId << ": " << data << std::endl; std::vector<uint8_t> response; response.push_back('R'); response.push_back(':'); response.push_back(' '); response.insert(response.end(), packet.begin(), packet.end()); _tcpServer->sendToClient(clientId, response); } void connectionClosed(int32_t clientId) { std::cout << "Connection to client " << clientId << " closed." << std::endl; } int main() { _bl.reset(new BaseLib::SharedObjects(false)); BaseLib::TcpSocket::TcpServerInfo serverInfo; serverInfo.newConnectionCallback = std::bind(&newConnection, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); //For class methods add `this` as first parameter: //serverInfo.newConnectionCallback = std::bind(&MyClass::newConnection, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); serverInfo.packetReceivedCallback = std::bind(&packetReceived, std::placeholders::_1, std::placeholders::_2); serverInfo.connectionClosedCallback = std::bind(&connectionClosed, std::placeholders::_1); _tcpServer = std::make_shared<BaseLib::TcpSocket>(_bl.get(), serverInfo); std::string listenAddress; _tcpServer->startServer("::", "8082", listenAddress); std::cout << "Started listening on " + listenAddress << std::endl; //Alternatively you can call `bindServerSocket()` as root first, then drop privileges and start the server: //_tcpServer->bindServerSocket("::", "8082", listenAddress); //... //Drop privileges //... //_tcpServer->startPreboundServer(listenAddress); for(int32_t i = 0; i < 300; i++) //Run for 300 seconds { std::this_thread::sleep_for(std::chrono::milliseconds(1000)); } _tcpServer->stopServer(); _tcpServer->waitForServerStopped(); }
typedef std::shared_ptr<CertificateInfo> BaseLib::TcpSocket::PCertificateInfo |
typedef std::shared_ptr<TcpClientData> BaseLib::TcpSocket::PTcpClientData |
typedef std::vector<uint8_t> BaseLib::TcpSocket::TcpPacket |
|
explicit |
Constructor to create an empty socket object.
baseLib | The base library object. |
BaseLib::TcpSocket::TcpSocket | ( | BaseLib::SharedObjects * | baseLib, |
std::shared_ptr< FileDescriptor > | socketDescriptor | ||
) |
Constructor to just wrap an existing socket descriptor.
baseLib | The base library object. |
socketDescriptor | The socket descriptor to wrap. |
BaseLib::TcpSocket::TcpSocket | ( | BaseLib::SharedObjects * | baseLib, |
std::string | hostname, | ||
std::string | port | ||
) |
Constructor to create a TCP client socket.
baseLib | The base library object. |
hostname | The host name or IP address to connect to. |
port | The port to connect to. |
BaseLib::TcpSocket::TcpSocket | ( | BaseLib::SharedObjects * | baseLib, |
std::string | hostname, | ||
std::string | port, | ||
bool | useSsl, | ||
std::string | caFile, | ||
bool | verifyCertificate | ||
) |
Constructor to create a TCP client socket with SSL enabled.
baseLib | The base library object. |
hostname | The host name or IP address to connect to. |
port | The port to connect to. |
useSsl | Set to true to enable SSL. |
caFile | Path to a file containing the PEM-encoded CA certificate used to sign the server certificate. If empty, the system's certificate store is used. |
verifyCertificate | Enable certificate verification. Only set to false for testing. |
BaseLib::TcpSocket::TcpSocket | ( | BaseLib::SharedObjects * | baseLib, |
std::string | hostname, | ||
std::string | port, | ||
bool | useSsl, | ||
bool | verifyCertificate, | ||
std::string | caData | ||
) |
Constructor to create a TCP client socket with SSL enabled.
baseLib | The base library object. |
hostname | The host name or IP address to connect to. |
port | The port to connect to. |
useSsl | Set to true to enable SSL. |
verifyCertificate | Enable certificate verification. Only set to false for testing. |
caData | The PEM-encoded CA certificate (not the path) used to sign the server certificate. If empty, the system's certificate store is used. |
BaseLib::TcpSocket::TcpSocket | ( | BaseLib::SharedObjects * | baseLib, |
std::string | hostname, | ||
std::string | port, | ||
bool | useSsl, | ||
std::string | caFile, | ||
bool | verifyCertificate, | ||
std::string | clientCertFile, | ||
std::string | clientKeyFile | ||
) |
Constructor to create a TCP client socket with SSL enabled using certificate login.
baseLib | The base library object. |
hostname | The host name or IP address to connect to. |
port | The port to connect to. |
useSsl | Set to true to enable SSL. |
caFile | Path to a file containing the PEM-encoded CA certificate used to sign the server certificate. |
verifyCertificate | Enable certificate verification. Only set to false for testing. |
clientCertFile | Path to a file containing the PEM-encoded client certificate. |
clientKeyFile | Path to a file containing the PEM-encoded client key file. |
BaseLib::TcpSocket::TcpSocket | ( | BaseLib::SharedObjects * | baseLib, |
std::string | hostname, | ||
std::string | port, | ||
bool | useSsl, | ||
bool | verifyCertificate, | ||
std::string | caData, | ||
std::string | clientCertData, | ||
const std::shared_ptr< Security::SecureVector< uint8_t >> & | clientKeyData | ||
) |
Constructor to create a TCP client socket with SSL enabled using certificate login.
baseLib | The base library object. |
hostname | The host name or IP address to connect to. |
port | The port to connect to. |
useSsl | Set to true to enable SSL. |
verifyCertificate | Enable certificate verification. Only set to false for testing. |
caData | The PEM-encoded CA certificate (not the path) used to sign the server certificate. |
clientCertData | The PEM-encoded client certificate (not the path). |
clientKeyData | The PEM-encoded client key (not the path). |
BaseLib::TcpSocket::TcpSocket | ( | BaseLib::SharedObjects * | baseLib, |
std::string | hostname, | ||
std::string | port, | ||
bool | useSsl, | ||
bool | verifyCertificate, | ||
std::string | caFile, | ||
std::string | caData, | ||
std::string | clientCertFile, | ||
std::string | clientCertData, | ||
std::string | clientKeyFile, | ||
const std::shared_ptr< Security::SecureVector< uint8_t >> & | clientKeyData | ||
) |
Constructor to create a TCP client socket with SSL enabled using certificate login.
baseLib | The base library object. |
hostname | The host name or IP address to connect to. |
port | The port to connect to. |
useSsl | Set to true to enable SSL. |
verifyCertificate | Enable certificate verification. Only set to false for testing. |
caFile | Path to a file containing the PEM-encoded CA certificate used to sign the server certificate. |
caData | The PEM-encoded CA certificate (not the path) used to sign the server certificate. |
clientCertFile | Path to a file containing the PEM-encoded client certificate. |
clientCertData | The PEM-encoded client certificate (not the path). |
clientKeyFile | Path to a file containing the PEM-encoded client key file. |
clientKeyData | The PEM-encoded client key (not the path). |
BaseLib::TcpSocket::TcpSocket | ( | BaseLib::SharedObjects * | baseLib, |
TcpServerInfo & | serverInfo | ||
) |
Constructor to create a TCP server.
|
virtual |
|
protected |
|
static |
void BaseLib::TcpSocket::bindServerSocket | ( | std::string | address, |
std::string | port, | ||
std::string & | listenAddress | ||
) |
Binds a socket to an IP address and a port.
This splits up the start process to be able to listen on ports lower than 1024 and do a privilege drop. Call startPreboundServer() to start listening. Don't call startServer() when using pre-binding as this recreates the socket.
address | The address to bind the server to (e. g. :: or 0.0.0.0 ). | |
port | The port number to bind the server to. | |
[out] | listenAddress | The IP address the server was bound to (e. g. 192.168.0.152 ). |
|
protected |
int32_t BaseLib::TcpSocket::clientCount | ( | ) |
Returns the number of clients connected to the TCP server.
void BaseLib::TcpSocket::close | ( | ) |
void BaseLib::TcpSocket::closeClientConnection | ( | int32_t | clientId | ) |
Closes the connection to a connected client.
clientId | The ID of the client to close as passed to TcpSocket::TcpServerServer::packetReceivedCallback. |
|
protected |
|
protected |
bool BaseLib::TcpSocket::connected | ( | ) |
Tests in this order and returns false if one of the tests fails:
std::string BaseLib::TcpSocket::getClientCertDn | ( | int32_t | clientId | ) |
Returns the distinguished name of the client certificate.
This method only returns a non empty string if the client certificate is valid.
clientId | The ID of the client to get the distinguished name for. |
int64_t BaseLib::TcpSocket::getClientCertExpiration | ( | int32_t | clientId | ) |
std::string BaseLib::TcpSocket::getClientCertSerial | ( | int32_t | clientId | ) |
|
protected |
|
inline |
PFileDescriptor BaseLib::TcpSocket::getFileDescriptor | ( | ) |
std::string BaseLib::TcpSocket::getIpAddress | ( | ) |
double BaseLib::TcpSocket::GetPacketsPerMinuteReceived | ( | ) |
double BaseLib::TcpSocket::GetPacketsPerMinuteSent | ( | ) |
|
inline |
double BaseLib::TcpSocket::GetProcessingThreadLoad | ( | ) |
Returns the current processing thread load.
A load of 1.0 means all threads are in use. (Queue backlog / thread count) is added to this load, so the load can be greater than 1.0.
double BaseLib::TcpSocket::GetProcessingThreadLoadMax | ( | ) |
Returns the maximum load of the last minute.
|
inline |
double BaseLib::TcpSocket::GetServerThreadLoad | ( | ) |
Returns the current server thread load.
A load of 1.0 means all threads are in use. (Queue backlog / thread count) is added to this load, so the load can be greater than 1.0.
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
void BaseLib::TcpSocket::open | ( | ) |
uint32_t BaseLib::TcpSocket::processingQueueSize | ( | ) |
Returns the size of the packet processing queue.
|
overrideprotected |
int32_t BaseLib::TcpSocket::proofread | ( | char * | buffer, |
int32_t | bufferSize | ||
) |
Use this overload when there are no socket operations outside of this class.
buffer | The buffer to fill. |
bufferSize | The size of the buffer. |
SocketOperationException | Thrown when socket is nullptr. |
SocketTimeOutException | Thrown when reading times out. |
SocketClosedException | Thrown when socket is closed. |
int32_t BaseLib::TcpSocket::proofread | ( | char * | buffer, |
int32_t | bufferSize, | ||
bool & | moreData, | ||
bool | skip_poll = false |
||
) |
Use this overload of proofread when select is called outside of this class.
buffer | The buffer to fill. | |
bufferSize | The size of the buffer. | |
[out] | moreData | If true, call proofread immediately again (without calling e. g. select first). |
skip_poll | Skips the call to select. Set this to true when you know, that data is available. |
SocketOperationException | Thrown when socket is nullptr. |
SocketTimeOutException | Thrown when reading times out. |
SocketClosedException | Thrown when socket is closed. |
int32_t BaseLib::TcpSocket::proofwrite | ( | const std::shared_ptr< std::vector< char >> & | data | ) |
Writes data to a socket.
SocketOperationException | Thrown when socket is nullptr. |
SocketDataLimitException | Thrown when trying to send more than 104857600 bytes of data. |
SocketTimeOutException | Thrown when writing times out. |
SocketClosedException | Thrown when socket is closed. |
int32_t BaseLib::TcpSocket::proofwrite | ( | const std::vector< char > & | data | ) |
Writes data to a socket.
SocketOperationException | Thrown when socket is nullptr. |
SocketDataLimitException | Thrown when trying to send more than 104857600 bytes of data. |
SocketTimeOutException | Thrown when writing times out. |
SocketClosedException | Thrown when socket is closed. |
int32_t BaseLib::TcpSocket::proofwrite | ( | const std::string & | data | ) |
Writes data to a socket.
SocketOperationException | Thrown when socket is nullptr. |
SocketDataLimitException | Thrown when trying to send more than 104857600 bytes of data. |
SocketTimeOutException | Thrown when writing times out. |
SocketClosedException | Thrown when socket is closed. |
int32_t BaseLib::TcpSocket::proofwrite | ( | const char * | buffer, |
int32_t | bytesToWrite | ||
) |
Writes data to a socket.
SocketOperationException | Thrown when socket is nullptr. |
SocketDataLimitException | Thrown when trying to send more than 104857600 bytes of data. |
SocketTimeOutException | Thrown when writing times out. |
SocketClosedException | Thrown when socket is closed. |
|
protected |
void BaseLib::TcpSocket::reloadCertificates | ( | ) |
bool BaseLib::TcpSocket::sendToClient | ( | int32_t | clientId, |
const TcpPacket & | packet, | ||
bool | closeConnection = false |
||
) |
Sends a response to a TCP client connected to the server.
clientId | The ID of the client as passed to TcpSocket::TcpServerServer::packetReceivedCallback. |
packet | The data to send. |
closeConnection | Close the connection after sending the packet. |
bool BaseLib::TcpSocket::sendToClient | ( | int32_t | clientId, |
const std::vector< char > & | packet, | ||
bool | closeConnection = false |
||
) |
Sends a response to a TCP client connected to the server.
clientId | The ID of the client as passed to TcpSocket::TcpServerServer::packetReceivedCallback. |
packet | The data to send. |
closeConnection | Close the connection after sending the packet. |
|
protected |
|
inline |
void BaseLib::TcpSocket::setCertificates | ( | std::unordered_map< std::string, PCertificateInfo > & | certificates | ) |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
Only relevant for TLS connections.
Sets the hostname the certificate's common name is verified against.
hostname | The compare the certificate's common name to. |
|
inline |
|
inline |
|
inline |
void BaseLib::TcpSocket::startPreboundServer | ( | std::string & | listenAddress, |
size_t | processingThreads = 0 |
||
) |
Starts listening on the already bound socket (created with bindServerSocket()).
This splits up the start process to be able to listen on ports lower than 1024 and do a privilege drop. Don't call startServer() when using pre-binding as this recreates the socket.
[out] | listenAddress | The IP address the server was bound to (e. g. 192.168.0.152 ). |
processingThreads | The number of processing threads to start. By default no threads are started. |
void BaseLib::TcpSocket::startServer | ( | std::string | address, |
std::string | port, | ||
std::string & | listenAddress, | ||
size_t | processingThreads = 0 |
||
) |
Starts listening.
address | The address to bind the server to (e. g. :: or 0.0.0.0 ). | |
port | The port number to bind the server to. | |
[out] | listenAddress | The IP address the server was bound to (e. g. 192.168.0.152 ). |
processingThreads | The number of processing threads to start. By default no threads are started. |
void BaseLib::TcpSocket::startServer | ( | std::string | address, |
std::string & | listenAddress, | ||
int32_t & | listenPort, | ||
size_t | processingThreads = 0 |
||
) |
Starts listening on a dynamically assigned port.
address | The address to bind the server to (e. g. :: or 0.0.0.0 ). | |
[out] | listenAddress | The IP address the server was bound to (e. g. 192.168.0.152 ). |
[out] | listenPort | The port the server was bound to (e. g. 45735 ). |
processingThreads | The number of processing threads to start. By default no threads are started. |
void BaseLib::TcpSocket::stopServer | ( | ) |
Starts stopping the server and returns immediately.
void BaseLib::TcpSocket::waitForServerStopped | ( | ) |
Waits until the server is stopped.
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
Stores the certificate credentials so that they can be replaced at any time.
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
Contains a copy of the current credentials in case the credentials are replaced while the connection is open.
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |