STTNet
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Public Member Functions | Public Attributes | Protected Attributes | List of all members
stt::network::TcpFDHandler Class Reference

TCP socket operation class. More...

#include <sttnet_English.h>

Inheritance diagram for stt::network::TcpFDHandler:
stt::network::HttpServerFDHandler stt::network::TcpClient stt::network::WebSocketServerFDHandler stt::network::HttpClient stt::network::WebSocketClient

Public Member Functions

void setFD (const int &fd, SSL *ssl, const bool &flag1=false, const bool &flag2=false, const int &sec=-1)
 Initialize the object with a socket. More...
 
int getFD ()
 Get the socket of this object. More...
 
SSL * getSSL ()
 Get the encrypted SSL handle of this object. More...
 
void close (const bool &cle=true)
 Close the object. More...
 
void blockSet (const int &sec=-1)
 Set the socket in the object to blocking mode. More...
 
void unblockSet ()
 Set the socket in the object to non-blocking mode. More...
 
bool multiUseSet ()
 Set the socket in the object to SO_REUSEADDR mode. More...
 
bool isConnect ()
 Determine if the object has a socket bound. More...
 
int sendData (const std::string &data, const bool &block=true)
 Send string data to the connected socket. More...
 
int sendData (const char *data, const uint64_t &length, const bool &block=true)
 Send a specified length of binary data to the connected socket. More...
 
int recvDataByLength (std::string &data, const uint64_t &length, const int &sec=2)
 Blocking receive of specified length of data from a connected socket into a string. More...
 
int recvDataByLength (char *data, const uint64_t &length, const int &sec=2)
 Blocking receive of specified length of data from a connected socket into a char* container. More...
 
int recvData (std::string &data, const uint64_t &length)
 Receive data once from a connected socket into a string container. More...
 
int recvData (char *data, const uint64_t &length)
 Receive data once from a connected socket into a char* container. More...
 

Public Attributes

bool flag3 = false
 if the connection lost while using sendData function with block=true,this flag will be set to true More...
 

Protected Attributes

int fd = -1
 
bool flag1 = false
 
bool flag2 = false
 
SSL * ssl = nullptr
 
int sec =-1
 

Detailed Description

TCP socket operation class.

Member Function Documentation

void stt::network::TcpFDHandler::blockSet ( const int &  sec = -1)

Set the socket in the object to blocking mode.

Parameters
secBlocking timeout, no longer block if blocking exceeds this time, default is -1, i.e., infinite wait
void stt::network::TcpFDHandler::close ( const bool &  cle = true)

Close the object.

Parameters
cletrue: Close the object and the socket connection, false: Only close the object (default is true)
int stt::network::TcpFDHandler::getFD ( )
inline

Get the socket of this object.

Returns
The socket of this object
SSL* stt::network::TcpFDHandler::getSSL ( )
inline

Get the encrypted SSL handle of this object.

Returns
The encrypted SSL handle. Returns nullptr if no encrypted SSL handle exists.
bool stt::network::TcpFDHandler::isConnect ( )
inline

Determine if the object has a socket bound.

Returns
true: The object has a socket bound, false: The object has no socket bound
bool stt::network::TcpFDHandler::multiUseSet ( )

Set the socket in the object to SO_REUSEADDR mode.

int stt::network::TcpFDHandler::recvData ( std::string &  data,
const uint64_t &  length 
)

Receive data once from a connected socket into a string container.

Parameters
dataData container for received data (string type)
lengthMaximum reception length
Returns
  • > 0: Number of bytes successfully received;
  • = 0: Connection closed;
  • < 0: Reception failed;
    • -99: Object not bound to a socket;
    • -100: No data in non-blocking mode
Note
Whether reception blocks depends on the fd's blocking state
int stt::network::TcpFDHandler::recvData ( char *  data,
const uint64_t &  length 
)

Receive data once from a connected socket into a char* container.

Parameters
dataData container for received data (char* type)
lengthMaximum reception length
Returns
  • > 0: Number of bytes successfully received;
  • = 0: Connection closed;
  • < 0: Reception failed;
    • -99: Object not bound to a socket;
    • -100: No data in non-blocking mode
Note
Whether reception blocks depends on the fd's blocking state
int stt::network::TcpFDHandler::recvDataByLength ( std::string &  data,
const uint64_t &  length,
const int &  sec = 2 
)

Blocking receive of specified length of data from a connected socket into a string.

Parameters
dataData container for received data (string type)
lengthReception length
secWaiting time in seconds, -1 for infinite blocking (default is 2s)
Returns
  • > 0: Number of bytes successfully received;
  • = 0: Connection closed;
  • < 0: Reception failed;
    • -99: Object not bound to a socket;
    • -100: Timeout
Note
If the specified data size is not received, it will block until timeout or error
int stt::network::TcpFDHandler::recvDataByLength ( char *  data,
const uint64_t &  length,
const int &  sec = 2 
)

Blocking receive of specified length of data from a connected socket into a char* container.

Parameters
dataData container for received data (char* type)
lengthReception length
secWaiting time in seconds, -1 for infinite blocking (default is 2s)
Returns
  • > 0: Number of bytes successfully received;
  • = 0: Connection closed;
  • < 0: Reception failed;
    • -99: Object not bound to a socket;
    • -100: Timeout
Note
If the specified data size is not received, it will block until timeout or error
int stt::network::TcpFDHandler::sendData ( const std::string &  data,
const bool &  block = true 
)

Send string data to the connected socket.

Parameters
dataData content to be sent (std::string type).
blockWhether to send in blocking mode (default true).
  • true: Will block until all data is sent successfully unless an error occurs (regardless of socket blocking state); Check the flag3 flag to determine if the connection is disconnected.
  • false: Blocking depends on socket state.
Returns
  • Return value > 0: Number of bytes successfully sent;
  • Return value = 0: Connection closed (block=false) or 0 bytes sent successfully (block=true);
  • Return value < 0 (only when block=false): Sending failed;
    • -99: Object not bound to a socket;
    • -100: Send buffer full in non-blocking mode.
Note
If block is true, it will continue to block until all data is sent unless an error occurs (regardless of socket blocking state), and the return value is always >= 0, suitable for scenarios requiring complete sending. Check the flag3 flag to determine if the connection is disconnected. If block is false, blocking depends on socket state. The return value may be less than the expected length, requiring manual handling of remaining data.
int stt::network::TcpFDHandler::sendData ( const char *  data,
const uint64_t &  length,
const bool &  block = true 
)

Send a specified length of binary data to the connected socket.

Parameters
dataPointer to the data buffer to be sent.
lengthData length (bytes).
blockWhether to send in blocking mode (default true).
  • true: Will block until all data is sent successfully unless an error occurs (regardless of socket blocking state); Check the flag3 flag to determine if the connection is disconnected.
  • false: Blocking depends on socket state.
Returns
  • Return value > 0: Number of bytes successfully sent;
  • Return value = 0: Connection closed (block=false) or 0 bytes sent successfully (block=true);
  • Return value < 0 (only when block=false): Sending failed;
    • -99: Object not bound to a socket;
    • -100: Send buffer full in non-blocking mode.
Note
If block is true, it will continue to block until all data is sent unless an error occurs (regardless of socket blocking state), and the return value is always >= 0, suitable for scenarios requiring complete sending. Check the flag3 flag to determine if the connection is disconnected. If block is false, blocking depends on socket state. The return value may be less than length, requiring manual handling of remaining data.
void stt::network::TcpFDHandler::setFD ( const int &  fd,
SSL *  ssl,
const bool &  flag1 = false,
const bool &  flag2 = false,
const int &  sec = -1 
)

Initialize the object with a socket.

Note
Can choose to set the socket to blocking or non-blocking, SO_REUSEADDR mode (only applicable to server sockets), and can also choose the SSL handle after encrypting the socket
Parameters
fdSocket descriptor
sslSSL handle after TLS encryption of the fd (can be nullptr if none)
flag1true: Enable non-blocking mode, false: Enable blocking mode (default is false, i.e., blocking mode)
flag2true: Enable SO_REUSEADDR mode, false: Do not enable SO_REUSEADDR mode (default is false)
secblocking timeout Blocking beyond this time will not be blocked again The default is -1, that is, infinite waiting
void stt::network::TcpFDHandler::unblockSet ( )

Set the socket in the object to non-blocking mode.

Member Data Documentation

int stt::network::TcpFDHandler::fd = -1
protected
bool stt::network::TcpFDHandler::flag1 = false
protected
bool stt::network::TcpFDHandler::flag2 = false
protected
bool stt::network::TcpFDHandler::flag3 = false

if the connection lost while using sendData function with block=true,this flag will be set to true

int stt::network::TcpFDHandler::sec =-1
protected
SSL* stt::network::TcpFDHandler::ssl = nullptr
protected

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