Socket

#include <lthread_cpp/socket.h>
using namespace lthread::net;
class Socket

A wrapper around lthread’s socket calls. Socket instance is returned by TcpConnect() and TcpListener and cannot be constructed on its own.

Member Functions

These functions reflect their lthread equivalent and must be called inside lthreads.

size_t Send(const char* buf)

Sends a C-style string over a socket.

Parameters const char* buf:
 NULL-terminated buffer.
Returns:Number of bytes sent.
Throws:SocketException on socket failure.
size_t Send(const char* buf, size_t length)

Sends length bytes over a socket.

Parameters:
  • const char* buf – Ptr to buffer containing data to send.
  • size_t length – Number of bytes to send from buf.
Returns:

Number of bytes sent.

Throws:

SocketException on socket failure.

size_t Recv(char* buf, size_t length, int timeout_ms=1000)

Receives up to length bytes over a socket.

Parameters:
  • char* buf – Buffer to read data into.
  • size_t length – Buffer size to fill.
  • timeout_ms(optional, default=1000) – Milliseconds to wait before timing out.
Throws:

SocketException on socket failure.

Throws:

SocketTimeout if a timeout occured. timeout_ms=0 waits indefinitely.

void Close()

Closes the network socket.

size_t Writev(struct iovec* v, int iovcnt)

Sends an iovec over a socket.

Parameters:
  • struct iovec* v – iovec pointing to one or more ptr/size entries.
  • int iovcnt – Number of entries in the iovec.
Throws:

SocketException on socket failure.

size_t RecvExact(char* buf, size_t length, int timeout_ms=1000)

Receives exactly length bytes into buf.

Parameters:
  • char* buf – Buffer to read data into.
  • size_t length – Buffer size to fill.
  • timeout_ms(optional, default=1000) – Milliseconds to wait before timing out.
Throws:

SocketException on socket failure.

Throws:

SocketTimeout if it timed out before receiving the full number of bytes.

void WaitWrite(int timeout_ms=1000) const

Waits until the socket is writable.

Parameters timeout_ms(optional, default=1000):
 Milliseconds to wait before timing out.
Throws:SocketException on socket failure.
Throws:SocketTimeout if timeout occured.
void WaitRead(int timeout_ms=1000) const

Waits until the socket is readable.

Parameters timeout_ms(optional, default=1000):
 Milliseconds to wait before timing out.
Throws:SocketException on socket failure.
Throws:SocketTimeout if timeout occured.
bool IsConnected() const

Returns true if socket is connected.

int fd() const

Returns the fd wrapped in the Socket instance.

std::string Ip() const

Returns the remote IP Address as a string.

Returns:string containing IP address.
std::string Desc() const

Returns remote_ip:ephemeral_port as a string

Socket& operator=(Socket&& rr_c)

Moves a socket from one instance to another.

Note

Socket objects are movable but not copyable.

Exceptions

SocketTimeout

class SocketTimeout

Empty class raised on socket timeout operations.

SocketException

class SocketException

Inherits std::exception(), raised on socket errors.