Skip to content

raisin::tcp_viewer::TcpClient ​

class · #include <rayrai/RaisimTcpCommon.hpp>

Cross-platform TCP client for the Rayrai/RaisimServer protocol.

The client owns platform socket setup, including Winsock startup on Windows. Messages are length-prefixed frames shared with raisim::RaisimServer.

Public types ​

IoStatus ​

cpp
enum class IoStatus

Status for the last I/O operation.

EnumeratorDescription
OkThe operation completed.
WouldBlockNon-blocking I/O made no or partial progress; retry later.
ErrorThe operation failed; see lastError().

Constructors & destructor ​

~TcpClient ​

cpp
~TcpClient()

Public member functions ​

connectTo ​

cpp
bool connectTo(
    const std::string &host,
    int port,
    bool verbose=true,
    int timeoutMs=0)

Connect to a server host/port.

Parameters

ParameterDescription
hostHostname or IP.
portTCP port.
verboseTrue to log connection errors.
timeoutMsConnection timeout in milliseconds; 0 or negative performs a blocking connect.

Returns True if connection succeeded.

disconnect ​

cpp
void disconnect()

Disconnect and reset internal state.

isConnected ​

cpp
bool isConnected() const

Check connection state.

Returns True if connected.

peerIsLoopback ​

cpp
bool peerIsLoopback() const

Whether the connected server runs on this computer, judged by the address the socket is connected to (127.0.0.0/8, ::1 or an IPv4-mapped 127.x.y.z), not by the host name, which may resolve anywhere.

Returns False when not connected.

sendMessage ​

cpp
bool sendMessage(const std::vector<char> &payload)

Send a length-prefixed message payload.

Prepends the int32 total frame size. Fails for an empty payload; a payload that would exceed kMaxMessageBytes fails and marks the client disconnected.

Parameters

ParameterDescription
payloadRaw payload bytes.

Returns True on success.

recvMessage ​

cpp
bool recvMessage(std::vector<char> &payload)

Receive a length-prefixed message payload.

Parameters

ParameterDescription
payloadOutput payload bytes.

Returns True only when a complete payload has been received. Returns false while a non-blocking receive is incomplete; use lastIoWouldBlock() and consumeRecvProgress() to distinguish that state from an error.

lastIoWouldBlock ​

cpp
bool lastIoWouldBlock() const

Check if the last I/O would have blocked.

Returns True if would-block was encountered.

consumeRecvProgress ​

cpp
bool consumeRecvProgress()

Consume the internal receive-progress flag.

Returns True if there was progress since last call.

lastError ​

cpp
std::string lastError() const

Get the last error string.

Returns Error message string.

RaiSim — physics for robotics and AI.