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

UDP operation class Pass in the socket for UDP protocol operations. More...

#include <sttnet_English.h>

Inheritance diagram for stt::network::UdpFDHandler:
stt::network::UdpClient stt::network::UdpServer

Public Member Functions

void setFD (const int &fd, const bool &flag1=false, const int &sec=-1, const bool &flag2=false)
 Set fd. More...
 
void blockSet (const int &sec=-1)
 Set to blocking mode. More...
 
void unblockSet ()
 Set to non-blocking mode. More...
 
bool multiUseSet ()
 Set SO_REUSEADDR mode. More...
 
int getFD ()
 Return fd. More...
 
void close (const bool &cle=true)
 Empty the object and close the socket. More...
 
int sendData (const std::string &data, const std::string &ip, const int &port, const bool &block=true)
 Send string data to the target. More...
 
int sendData (const char *data, const uint64_t &length, const std::string &ip, const int &port, const bool &block=true)
 Send a specified length of binary data to the target. More...
 
int recvData (std::string &data, const uint64_t &length, std::string &ip, int &port)
 Receive data once into a string container. More...
 
int recvData (char *data, const uint64_t &length, std::string &ip, int &port)
 Receive data once into a char* container. More...
 

Protected Attributes

int fd = -1
 
bool flag1 = false
 
bool flag2 = false
 
int sec = -1
 

Detailed Description

UDP operation class Pass in the socket for UDP protocol operations.

Member Function Documentation

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

Set to blocking mode.

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

Empty the object and close the socket.

Parameters
cletrue: Empty the object and close the socket, false: Only empty the object, do not close the socket
int stt::network::UdpFDHandler::getFD ( )
inline

Return fd.

bool stt::network::UdpFDHandler::multiUseSet ( )

Set SO_REUSEADDR mode.

Returns
true: Set successfully, false: Set failed
int stt::network::UdpFDHandler::recvData ( std::string &  data,
const uint64_t &  length,
std::string &  ip,
int &  port 
)

Receive data once into a string container.

Parameters
dataData container for received data (string type)
lengthMaximum reception length
ipRecord the source ip of the sender
portRecord the source port of the sender
Returns
  • Return value > 0: Number of bytes successfully received;
  • Return value = 0: Connection closed;
  • Return value < 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::UdpFDHandler::recvData ( char *  data,
const uint64_t &  length,
std::string &  ip,
int &  port 
)

Receive data once into a char* container.

Parameters
dataData container for received data (char* type)
lengthMaximum reception length
ipRecord the source ip of the sender
portRecord the source port of the sender
Returns
  • Return value > 0: Number of bytes successfully received;
  • Return value = 0: Connection closed;
  • Return value < 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::UdpFDHandler::sendData ( const std::string &  data,
const std::string &  ip,
const int &  port,
const bool &  block = true 
)

Send string data to the target.

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);
  • false: Blocking depends on socket state.
Returns
  • Return value > 0: Number of bytes successfully sent;
  • Return value <= 0: Sending failed;
    • -98: Target error
    • -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), suitable for scenarios where complete sending is required. If block is false, blocking depends on socket state. The return value may be less than length, requiring manual handling of remaining data.
int stt::network::UdpFDHandler::sendData ( const char *  data,
const uint64_t &  length,
const std::string &  ip,
const int &  port,
const bool &  block = true 
)

Send a specified length of binary data to the target.

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);
  • false: Blocking depends on socket state.
Returns
  • Return value > 0: Number of bytes successfully sent;
  • Return value <= 0: Sending failed;
    • -98: Target error
    • -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. 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::UdpFDHandler::setFD ( const int &  fd,
const bool &  flag1 = false,
const int &  sec = -1,
const bool &  flag2 = false 
)

Set fd.

Parameters
fdSocket fd to be passed in
flag1true: Set to non-blocking mode, false: Set to blocking mode (default is blocking mode)
secSet blocking timeout (seconds) (default is -1, i.e., infinite wait)
flag2true: Set SO_REUSEADDR mode, false: Do not set SO_REUSEADDR mode
void stt::network::UdpFDHandler::unblockSet ( )

Set to non-blocking mode.

Member Data Documentation

int stt::network::UdpFDHandler::fd = -1
protected
bool stt::network::UdpFDHandler::flag1 = false
protected
bool stt::network::UdpFDHandler::flag2 = false
protected
int stt::network::UdpFDHandler::sec = -1
protected

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