|
STTNet
|
WebSocketServer server operation class. More...
#include <sttnet_English.h>
Public Member Functions | |
| void | putTask (const std::function< int(WebSocketServerFDHandler &, WebSocketFDInformation &)> &fun, WebSocketServerFDHandler &k, WebSocketFDInformation &inf) |
| Submit a task to the worker thread pool. More... | |
| WebSocketServer (const unsigned long long &maxFD=1000000, const int &buffer_size=256, const bool &security_open=true, const int &connectionNumLimit=5, const int &connectionSecs=10, const int &connectionTimes=3, const int &requestSecs=1, const int &requestTimes=10, const int &checkFrequency=60, const int &connectionTimeout=120) | |
| Constructor. More... | |
| void | setSecuritySendBackFun (std::function< void(WebSocketServerFDHandler &k, WebSocketFDInformation &inf)> fc) |
| Set the callback invoked when an information security policy is violated. More... | |
| void | setGlobalSolveFunction (std::function< bool(WebSocketServerFDHandler &, WebSocketFDInformation &)> fc) |
| Set the global fallback handler. More... | |
| void | setStartFunction (std::function< bool(WebSocketServerFDHandler &, WebSocketFDInformation &)> fccc) |
| Set the callback invoked immediately after a WebSocket connection is established. More... | |
| void | setJudgeFunction (std::function< bool(WebSocketFDInformation &)> fcc) |
| Set the handshake validation callback. More... | |
| void | setFunction (const std::string &key, std::function< int(WebSocketServerFDHandler &, WebSocketFDInformation &)> fc) |
| Register a message handler callback for a specific key. More... | |
| void | setGetKeyFunction (std::function< int(WebSocketServerFDHandler &, WebSocketFDInformation &)> parseKeyFun) |
| Set the key parsing callback. More... | |
| void | setTimeOutTime (const int &seca) |
| Set heartbeat interval. More... | |
| void | setHBTimeOutTime (const int &secb) |
| Set heartbeat response timeout. More... | |
| bool | closeFD (const int &fd, const std::string &closeCodeAndMessage) |
| Close a WebSocket connection using an encoded close payload. More... | |
| bool | closeFD (const int &fd, const short &code=1000, const std::string &message="bye") |
| Close a WebSocket connection using standard RFC 6455 format. More... | |
| bool | sendMessage (const int &fd, const std::string &msg, const std::string &type="0001") |
| Send a WebSocket message to a specific client. More... | |
| bool | close () |
| Close all connections and stop listening. More... | |
| bool | close (const int &fd) |
| Close the connection of a certain socket More... | |
| bool | startListen (const int &port, const int &threads=8) |
| Start the WebSocket server. More... | |
| void | sendMessage (const std::string &msg, const std::string &type="0001") |
| Broadcast a WebSocket message to all connected clients. More... | |
| ~WebSocketServer () | |
| Destructor. More... | |
Public Member Functions inherited from stt::network::TcpServer | |
| void | putTask (const std::function< int(TcpFDHandler &k, TcpInformation &inf)> &fun, TcpFDHandler &k, TcpInformation &inf) |
| Add a task to a worker thread pool and have it completed by worker threads. More... | |
| TcpServer (const unsigned long long &maxFD=1000000, const int &buffer_size=256, const bool &security_open=true, const int &connectionNumLimit=20, const int &connectionSecs=1, const int &connectionTimes=6, const int &requestSecs=1, const int &requestTimes=40, const int &checkFrequency=60, const int &connectionTimeout=60) | |
| Constructor. 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... | |
| void | setSecuritySendBackFun (std::function< void(TcpFDHandler &k, TcpInformation &inf)> fc) |
| Set the callback invoked when an information security policy is violated. More... | |
| void | setGlobalSolveFunction (std::function< bool(TcpFDHandler &k, TcpInformation &inf)> fc) |
| Set global fallback function. More... | |
| void | setFunction (const std::string &key, std::function< int(TcpFDHandler &k, TcpInformation &inf)> fc) |
| Register a callback function for a specific key. More... | |
| void | setGetKeyFunction (std::function< int(TcpFDHandler &k, TcpInformation &inf)> parseKeyFun) |
| Set the callback function used to parse the key. More... | |
| bool | stopListen () |
| Stop listening. More... | |
| bool | close () |
| Close listening and all connected sockets. More... | |
| void | setCloseFun (std::function< void(const int &fd)> closeFun) |
| set function after tcp connection close 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... | |
Additional Inherited Members | |
Protected Attributes inherited from stt::network::TcpServer | |
| std::queue< WorkerMessage > | finishQueue |
| stt::system::WorkerPool * | workpool |
| unsigned long | buffer_size |
| unsigned long long | maxFD |
| security::ConnectionLimiter | connectionLimiter |
| TcpFDInf * | clientfd |
| int | flag1 =true |
| bool | unblock |
| SSL_CTX * | ctx =nullptr |
| bool | TLS =false |
| bool | security_open |
| int | workerEventFD |
| int | serverType |
| int | connectionSecs |
| int | connectionTimes |
| int | requestSecs |
| int | requestTimes |
| int | checkFrequency |
WebSocketServer server operation class.
|
inline |
Constructor.
By default, allows up to 1,000,000 concurrent connections, allocates a maximum receive buffer of 256 KB per connection, and enables the security module.
| maxFD | Maximum number of connections this server instance can accept. Default: 1,000,000. |
| buffer_size | Maximum amount of data a single connection is allowed to receive, in kilobytes (KB). Default: 256 KB. |
| security_open | Whether to enable the security module.
|
| connectionNumLimit | Maximum number of concurrent connections allowed per IP. Default: 5. |
| connectionSecs | Time window (in seconds) for connection rate limiting. Default: 10 seconds. |
| connectionTimes | Maximum number of connection attempts allowed within connectionSecs seconds. Default: 3. |
| requestSecs | Time window (in seconds) for request/message rate limiting. Default: 1 second. |
| requestTimes | Maximum number of requests/messages allowed within requestSecs seconds. Default: 10. |
| checkFrequency | Frequency (in seconds) for checking zombie/idle connections. -1 disables zombie connection detection. Default: 60 seconds. |
| connectionTimeout | If a connection has no activity for this many seconds, it is considered a zombie connection. -1 means no timeout (infinite). Default: 120 seconds. |
|
inline |
Destructor.
| bool stt::network::WebSocketServer::close | ( | ) |
Close all connections and stop listening.
|
virtual |
Close the connection of a certain socket
Reimplemented from stt::network::TcpServer.
| bool stt::network::WebSocketServer::closeFD | ( | const int & | fd, |
| const std::string & | closeCodeAndMessage | ||
| ) |
Close a WebSocket connection using an encoded close payload.
| fd | Socket file descriptor. |
| closeCodeAndMessage | Encoded close payload (2 bytes close code + optional UTF-8 reason). |
| bool stt::network::WebSocketServer::closeFD | ( | const int & | fd, |
| const short & | code = 1000, |
||
| const std::string & | message = "bye" |
||
| ) |
Close a WebSocket connection using standard RFC 6455 format.
| fd | Socket file descriptor. |
| code | WebSocket close code (default: 1000). |
| message | Optional close reason. |
| void stt::network::WebSocketServer::putTask | ( | const std::function< int(WebSocketServerFDHandler &, WebSocketFDInformation &)> & | fun, |
| WebSocketServerFDHandler & | k, | ||
| WebSocketFDInformation & | inf | ||
| ) |
Submit a task to the worker thread pool.
| fun | The callable task to be executed by the worker thread pool.
|
| k | Reference to the socket handler. |
| inf | Reference to the client information. |
|
inline |
Send a WebSocket message to a specific client.
| fd | Client socket file descriptor. |
| msg | Message payload. |
| type | Frame opcode type (default: text frame). |
| void stt::network::WebSocketServer::sendMessage | ( | const std::string & | msg, |
| const std::string & | type = "0001" |
||
| ) |
Broadcast a WebSocket message to all connected clients.
| msg | Message payload. |
| type | Frame opcode type. |
|
inline |
Register a message handler callback for a specific key.
| key | Message key. |
| fc | Callback function.
|
|
inline |
Set the key parsing callback.
| parseKeyFun | Key parsing function. |
|
inline |
Set the global fallback handler.
| fc | Fallback function to process incoming messages.
|
|
inline |
Set heartbeat response timeout.
| secb | Time to wait after sending a heartbeat (seconds). |
|
inline |
Set the handshake validation callback.
| fcc | Validation function.
|
|
inline |
Set the callback invoked when an information security policy is violated.
This callback is executed when a WebSocket client violates a security rule (e.g. rate limiting, abnormal message behavior, protocol abuse, etc.). After the callback is executed, the connection will be closed.
| fc | A function or function object used to handle the response sent back to the client upon a security violation. |
Callback parameters:
|
inline |
Set the callback invoked immediately after a WebSocket connection is established.
| fccc | Callback executed after successful WebSocket handshake.
|
|
inline |
Set heartbeat interval.
| seca | Heartbeat interval in minutes (default: 20 minutes). |
|
inline |
Start the WebSocket server.
| port | Listening port. |
| threads | Number of worker threads (default: 8). |
1.8.5