Tcp server class.
More...
#include <sttnet_English.h>
|
| | TcpServer (const int &maxFD=10000, const bool &security_open=true, const int &connectionNumLimit=20, const int &connectionRateLimit=6, const int &buffer_size=8, const int &requestRate=12, const int &checkFrequency=1, const int &connectionTimeout=1800) |
| | Constructor, which is enabled by default. Limit the maximum number of connections to an IP address to 20; The fastest connection speed per second for the same IP address is 6. More...
|
| |
| bool | startListen (const int &port, const int &threads=8) |
| | Start the TCP server listening program. More...
|
| |
| bool | setTLS (const char *cert, const char *key, const char *passwd, const char *ca) |
| | Enable TLS encryption and configure server-side certificate and key. More...
|
| |
| void | redrawTLS () |
| | Revoke TLS encryption, CA certificate, etc. More...
|
| |
| bool | setFunction (std::function< bool(TcpFDHandler &k, TcpFDInf &inf)> fc) |
| | Set the callback function after receiving a message from the client Register a callback function. More...
|
| |
| bool | stopListen () |
| | Stop listening. More...
|
| |
| bool | close () |
| | Close listening and all connected sockets. More...
|
| |
| bool | close (const int &fd) |
| | Close the connection of a specific socket. More...
|
| |
| bool | isListen () |
| | Return the listening status of the object. More...
|
| |
| SSL * | getSSL (const int &fd) |
| | Query the connection with the server, pass in the socket, and return the encrypted SSL handle. More...
|
| |
| | ~TcpServer () |
| | Destructor of TcpServer class. More...
|
| |
Tcp server class.
- Note
- The default underlying implementation is epoll edge trigger + socket non-blocking mode
| stt::network::TcpServer::TcpServer |
( |
const int & |
maxFD = 10000, |
|
|
const bool & |
security_open = true, |
|
|
const int & |
connectionNumLimit = 20, |
|
|
const int & |
connectionRateLimit = 6, |
|
|
const int & |
buffer_size = 8, |
|
|
const int & |
requestRate = 12, |
|
|
const int & |
checkFrequency = 1, |
|
|
const int & |
connectionTimeout = 1800 |
|
) |
| |
|
inline |
Constructor, which is enabled by default. Limit the maximum number of connections to an IP address to 20; The fastest connection speed per second for the same IP address is 6.
- Note
- Turning on the security module has a performance impact
- Parameters
-
| maxFD | service object can accept the maximum number of connections(default 10000) |
| security_open | true: enable the security module false: disable the security module (enabled by default) |
| connectionNumLimit | The maximum number of connections from the same IP address |
| connectionRateLimit | The maximum number of connections per second to the same IP address |
| buffer_size | The maximum amount of data allowed to be transferred over the same connection (in KB) is 8KB by default |
| requestRatte | The maximum number of requests allowed for the same connection within one second (the default is 12 times) |
| checkFrequency | The frequency of checking zombie connections (in minutes) The default is 1 minute -1 means no check |
| connectionTimeout | The number of seconds that a connection is considered a zombie connection if there is no response (in seconds) The default is 60 seconds -1 means no limit |
| stt::network::TcpServer::~TcpServer |
( |
| ) |
|
|
inline |
Destructor of TcpServer class.
- Note
- Calls the close function to close
| bool stt::network::TcpServer::allowRequest |
( |
const int & |
cclientfd | ) |
|
|
protected |
| bool stt::network::TcpServer::close |
( |
| ) |
|
Close listening and all connected sockets.
- Note
- Closes listening and all connected sockets, registered callback functions and TLS will not be deleted or redrawn
-
Will block until all closures are complete
- Returns
- true: Closed successfully, false: Failed to close
| bool stt::network::TcpServer::close |
( |
const int & |
fd | ) |
|
Close the connection of a specific socket.
- Parameters
-
- Returns
- true: Closed successfully, false: Failed to close
| void stt::network::TcpServer::connectionDetect |
( |
| ) |
|
|
protected |
| SSL* stt::network::TcpServer::getSSL |
( |
const int & |
fd | ) |
|
Query the connection with the server, pass in the socket, and return the encrypted SSL handle.
- Returns
- Encrypted SSL pointer; returns nullptr if this fd does not exist or there is no encryption
| bool stt::network::TcpServer::isListen |
( |
| ) |
|
|
inline |
Return the listening status of the object.
- Returns
- true: Listening, false: Not listening
| void stt::network::TcpServer::redrawTLS |
( |
| ) |
|
Revoke TLS encryption, CA certificate, etc.
| bool stt::network::TcpServer::setFunction |
( |
std::function< bool(TcpFDHandler &k, TcpFDInf &inf)> |
fc | ) |
|
|
inline |
Set the callback function after receiving a message from the client Register a callback function.
- Parameters
-
| fc | A function or function object for processing logic after receiving a message from the client
- Parameter: TcpFDHandler &k - Reference to the socket connected to the client TcpFDInf &inf - client information, processing progress, state machine information, etc.
- Return: bool - true for successful processing, false for processing failure
|
- Note
- The passed function should have the signature bool func(TcpFDHandler &k)
-
If processing fails, the TCP connection will be closed
| bool stt::network::TcpServer::setTLS |
( |
const char * |
cert, |
|
|
const char * |
key, |
|
|
const char * |
passwd, |
|
|
const char * |
ca |
|
) |
| |
Enable TLS encryption and configure server-side certificate and key.
This function initializes OpenSSL and enables TLS (SSL/TLSv1 protocol family) support for the TCP server. It loads the server-side certificate, private key, and an optional CA root certificate for peer verification.
If TLS is already enabled, the context will be automatically rebuilt (reloaded).
- Parameters
-
| cert | Server certificate chain file path (usually PEM format, including intermediate certificates) |
| key | Private key file path (matching PEM format key for the certificate) |
| passwd | Password for the private key file (can be an empty string if the key is not encrypted) |
| ca | CA root certificate path for verifying client certificates (PEM format) |
- Note
- The protocol method used is
SSLv23_method(), which actually supports SSLv3/TLSv1/TLSv1.1/TLSv1.2 and higher versions (depending on the OpenSSL version and configuration)
-
The certificate verification policy uses
SSL_VERIFY_FAIL_IF_NO_PEER_CERT, meaning:
- If the client does not provide a certificate, the handshake fails (safer, recommended)
- If the certificate is invalid or verification fails, the handshake is also terminated
- Returns
- true if TLS is enabled successfully, server is in encrypted state
-
false if enabling fails (specific error will be logged)
- Warning
- After enabling TLS, all incoming connections must follow the TLS handshake process, otherwise communication will fail
- See Also
- redrawTLS() If a TLS context already exists, it will be released and rebuilt first (can be used for hot updating certificates)
| bool stt::network::TcpServer::startListen |
( |
const int & |
port, |
|
|
const int & |
threads = 8 |
|
) |
| |
Start the TCP server listening program.
- Parameters
-
| port | Port to listen on |
| threads | Number of consumer threads (default is 8) |
- Returns
- true: Listening started successfully, false: Failed to start listening
| bool stt::network::TcpServer::stopListen |
( |
| ) |
|
Stop listening.
- Warning
- Only stops listening (but the socket can no longer receive, it depends on listening and consumers, so this function is of little significance)
- Returns
- true: Stopped successfully, false: Failed to stop
| unsigned long stt::network::TcpServer::buffer_size |
|
protected |
| int stt::network::TcpServer::checkFrequency |
|
protected |
| TcpFDInf* stt::network::TcpServer::clientfd |
|
protected |
| int stt::network::TcpServer::connectionTimeout |
|
protected |
| int stt::network::TcpServer::consumerNum |
|
protected |
| SSL_CTX* stt::network::TcpServer::ctx = nullptr |
|
protected |
| std::condition_variable* stt::network::TcpServer::cv |
|
protected |
| std::queue<QueueFD>* stt::network::TcpServer::fdQueue |
|
protected |
| int stt::network::TcpServer::flag1 = true |
|
protected |
| std::mutex stt::network::TcpServer::lco1 |
|
protected |
| std::mutex* stt::network::TcpServer::lq1 |
|
protected |
| int stt::network::TcpServer::maxFD |
|
protected |
| int stt::network::TcpServer::requestRate |
|
protected |
| bool stt::network::TcpServer::security_open |
|
protected |
| bool stt::network::TcpServer::TLS = false |
|
protected |
| bool stt::network::TcpServer::unblock |
|
protected |
The documentation for this class was generated from the following file: