11 #include <mrpt/config.h> 46 #if MRPT_WORD_SIZE == 64 65 size_t Read(
void* Buffer,
size_t Count)
override;
73 size_t Write(
const void* Buffer,
size_t Count)
override;
95 const std::string& remotePartAddress,
unsigned short remotePartTCPPort,
96 unsigned int timeout_ms = 0);
135 void* Buffer,
const size_t Count,
const int timeoutStart_ms = -1,
136 const int timeoutBetween_ms = -1);
150 const void* Buffer,
const size_t Count,
const int timeout_ms = -1);
159 template <
class MESSAGE>
160 bool sendMessage(
const MESSAGE& outMsg,
const int timeout_ms = -1)
163 const char* magic =
"MRPTMessage";
164 uint32_t toWrite = strlen(magic);
165 uint32_t written =
writeAsync(magic, toWrite, timeout_ms);
166 if (written != toWrite)
return false;
168 toWrite =
sizeof(outMsg.type);
169 written =
writeAsync(&outMsg.type, toWrite, timeout_ms);
170 if (written != toWrite)
return false;
172 uint32_t contentLen = outMsg.content.size();
173 toWrite =
sizeof(contentLen);
174 written =
writeAsync(&contentLen, toWrite, timeout_ms);
175 if (written != toWrite)
return false;
177 toWrite = contentLen;
178 written =
writeAsync(&outMsg.content[0], toWrite, timeout_ms);
179 if (written != toWrite)
return false;
193 template <
class MESSAGE>
195 MESSAGE& inMsg,
const unsigned int timeoutStart_ms = 100,
196 const unsigned int timeoutBetween_ms = 1000)
200 uint32_t toRead = 11;
202 readAsync(magic, toRead, timeoutStart_ms, timeoutBetween_ms);
203 if (actRead != toRead)
return false;
206 if (0 != ::strcmp(
"MRPTMessage", magic))
return false;
208 toRead =
sizeof(inMsg.type);
210 &inMsg.type, toRead, timeoutBetween_ms, timeoutBetween_ms);
211 if (actRead != toRead)
return false;
214 toRead =
sizeof(contentLen);
216 &contentLen, toRead, timeoutBetween_ms, timeoutBetween_ms);
217 if (actRead != toRead)
return false;
218 inMsg.content.resize(contentLen);
222 &inMsg.content[0], toRead, timeoutBetween_ms, timeoutBetween_ms);
223 if (actRead != toRead)
return false;
static unsigned int DNS_LOOKUP_TIMEOUT_MS
See description of CClientTCPSocket.
int setSOSendBufffer(int newValue)
Set the size of the SO send buffer.
bool sendMessage(const MESSAGE &outMsg, const int timeout_ms=-1)
Send a message through the TCP stream.
void connect(const std::string &remotePartAddress, unsigned short remotePartTCPPort, unsigned int timeout_ms=0)
Establishes a connection with a remote part.
size_t Write(const void *Buffer, size_t Count) override
Introduces a virtual method responsible for writing to the stream.
bool isConnected()
Returns true if this objects represents a successfully connected socket.
bool receiveMessage(MESSAGE &inMsg, const unsigned int timeoutStart_ms=100, const unsigned int timeoutBetween_ms=1000)
Waits for an incoming message through the TCP stream.
uint64_t Seek(int64_t off, CStream::TSeekOrigin org=sFromBeginning) override
This virtual method has no effect in this implementation over a TCP socket, and its use raises an exc...
int getSOSendBufffer()
Return the current size of the SO send buffer.
size_t Read(void *Buffer, size_t Count) override
Introduces a virtual method responsible for reading from the stream (This method BLOCKS) This method ...
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
uint64_t getTotalBytesCount() const override
This virtual method has no effect in this implementation over a TCP socket, and its use raises an exc...
std::string m_remotePartIP
The IP address of the remote part of the connection.
~CClientTCPSocket() override
Destructor.
A TCP socket that can be wait for client connections to enter.
CClientTCPSocket()
Default constructor.
int getTCPNoDelay()
Return the value of the TCPNoDelay option.
std::string getLastErrorStr()
Returns a description of the last Sockets error.
size_t writeAsync(const void *Buffer, const size_t Count, const int timeout_ms=-1)
A method for writing to the socket with optional timeouts.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
int setTCPNoDelay(int newValue)
Set the TCP no delay option of the protocol (Nagle algorithm).
uint64_t getPosition() const override
This virtual method has no effect in this implementation over a TCP socket, and its use raises an exc...
A TCP socket that can be connected to a TCP server, implementing MRPT's CStream interface for passing...
void close()
Closes the connection.
unsigned short m_remotePartPort
The TCP port of the remote part of the connection.
uint32_t m_hSock
The handle for the connected TCP socket, or INVALID_SOCKET.
size_t getReadPendingBytes()
Return the number of bytes already in the receive queue (they can be read without waiting) ...
size_t readAsync(void *Buffer, const size_t Count, const int timeoutStart_ms=-1, const int timeoutBetween_ms=-1)
A method for reading from the socket with an optional timeout.
void sendString(const std::string &str)
Writes a string to the socket.