Thrill  0.1

Detailed Description

Socket is a light-weight wrapper around the BSD socket API.

Functions all have plain return values and do not through exceptions.

Not all functions in this class follow the normal naming conventions, because they are wrappers around the equally named functions of the socket API.

Definition at line 50 of file socket.hpp.

#include <socket.hpp>

Public Member Functions

Close
bool close ()
 Close socket. More...
 
Connect, Bind and Accept Functions
bool bind (const SocketAddress &sa)
 Bind socket to given SocketAddress for listening or connecting. More...
 
int connect (const SocketAddress &sa)
 Initial socket connection to address. More...
 
bool listen (int backlog=0)
 Turn socket into listener state to accept incoming connections. More...
 
Socket accept () const
 Wait on socket until a new connection comes in. More...
 
Send and Recv Functions
ssize_t send_one (const void *data, size_t size, int flags=0)
 
ssize_t send (const void *data, size_t size, int flags=0)
 Send (data,size) to socket, retry sends if short-sends occur. More...
 
ssize_t sendto (const void *data, size_t size, int flags, const SocketAddress &dest)
 Send (data,size) to destination. More...
 
ssize_t recv_one (void *out_data, size_t max_size, int flags=0)
 Recv (out_data,max_size) from socket (BSD socket API function wrapper) More...
 
ssize_t recv (void *out_data, size_t size, int flags=0)
 Receive (data,size) from socket, retry recvs if short-reads occur. More...
 
ssize_t recvfrom (void *out_data, size_t max_size, int flags=0, SocketAddress *out_source=nullptr)
 
Socket Options and Accelerations
int getsockopt (int level, int optname, void *optval, socklen_t *optlen) const
 Perform raw getsockopt() operation on socket. More...
 
int setsockopt (int level, int optname, const void *optval, socklen_t optlen)
 Perform raw setsockopt() operation on socket. More...
 
void SetKeepAlive (bool activate=true)
 Enable sending of keep-alive messages on connection-oriented sockets. More...
 
void SetReuseAddr (bool activate=true)
 
void SetNoDelay (bool activate=true)
 
void SetSndBuf (size_t size)
 Set SO_SNDBUF socket option. More...
 
void SetRcvBuf (size_t size)
 Set SO_RCVBUF socket option. More...
 

Static Private Member Functions

static std::string MaybeHexdump (const void *data, size_t size)
 return hexdump or just [data] if not debugging More...
 

Private Attributes

int fd_
 the file descriptor of the socket. More...
 
bool non_blocking_ = false
 flag whether the socket is set to non-blocking More...
 

Static Private Attributes

static constexpr bool debug = false
 
static constexpr bool debug_data = false
 

Creation

 Socket (int fd, bool loopback_socket=false)
 construct new Socket object from existing file descriptor. More...
 
 Socket ()
 default constructor: invalid socket. More...
 
 Socket (const Socket &)=delete
 non-copyable: delete copy-constructor More...
 
Socketoperator= (const Socket &)=delete
 non-copyable: delete assignment operator More...
 
 Socket (Socket &&s) noexcept
 move-constructor: move file descriptor More...
 
Socketoperator= (Socket &&s)
 move-assignment operator: move file descriptor More...
 
 ~Socket ()
 
static Socket Create ()
 Create a new stream socket. More...
 
static std::pair< Socket, SocketCreatePair ()
 

Status

bool IsValid () const
 Check whether the contained file descriptor is valid. More...
 
int fd () const
 Return the associated file descriptor. More...
 
int GetError () const
 Query socket for its current error state. More...
 
bool SetNonBlocking (bool non_blocking)
 Turn socket into non-blocking state. More...
 
SocketAddress GetLocalAddress () const
 Return the current local socket address. More...
 
SocketAddress GetPeerAddress () const
 Return the current peer socket address. More...
 
static bool SetNonBlocking (int fd, bool non_blocking)
 Turn socket into non-blocking state. More...
 

Constructor & Destructor Documentation

◆ Socket() [1/4]

Socket ( int  fd,
bool  loopback_socket = false 
)
inlineexplicit

construct new Socket object from existing file descriptor.

Definition at line 60 of file socket.hpp.

References Socket::SetNoDelay(), Socket::SetRcvBuf(), and Socket::SetSndBuf().

◆ Socket() [2/4]

Socket ( )
inline

default constructor: invalid socket.

Definition at line 71 of file socket.hpp.

References Socket::operator=().

Referenced by Socket::accept(), Socket::Create(), and Socket::CreatePair().

◆ Socket() [3/4]

Socket ( const Socket )
delete

non-copyable: delete copy-constructor

◆ Socket() [4/4]

Socket ( Socket &&  s)
inlinenoexcept

move-constructor: move file descriptor

Definition at line 78 of file socket.hpp.

◆ ~Socket()

~Socket ( )
inline

Definition at line 88 of file socket.hpp.

References Socket::close(), and Socket::fd_.

Member Function Documentation

◆ accept()

Socket accept ( ) const
inline

Wait on socket until a new connection comes in.

Definition at line 324 of file socket.hpp.

References Socket::fd_, Socket::IsValid(), LOG, and Socket::Socket().

◆ bind()

bool bind ( const SocketAddress sa)
inline

Bind socket to given SocketAddress for listening or connecting.

Definition at line 267 of file socket.hpp.

References Socket::fd_, SocketAddress::IsValid(), Socket::IsValid(), LOG, SocketAddress::sockaddr(), and SocketAddress::socklen().

◆ close()

bool close ( )
inline

Close socket.

Definition at line 241 of file socket.hpp.

References Socket::fd_, Socket::IsValid(), and LOG.

Referenced by Connection::Close(), Socket::operator=(), and Socket::~Socket().

◆ connect()

int connect ( const SocketAddress sa)
inline

Initial socket connection to address.

Definition at line 285 of file socket.hpp.

References Socket::fd_, SocketAddress::IsValid(), Socket::IsValid(), LOG, SocketAddress::sockaddr(), and SocketAddress::socklen().

◆ Create()

static Socket Create ( )
inlinestatic

Create a new stream socket.

Definition at line 93 of file socket.hpp.

References Socket::fd(), LOG, and Socket::Socket().

◆ CreatePair()

static std::pair<Socket, Socket> CreatePair ( )
inlinestatic

Create a pair of connected stream sockets. Use this for internal local test connection pairs.

Definition at line 117 of file socket.hpp.

References LOG1, and Socket::Socket().

Referenced by Group::ConstructLoopbackMesh().

◆ fd()

int fd ( ) const
inline

◆ GetError()

int GetError ( ) const
inline

Query socket for its current error state.

Definition at line 157 of file socket.hpp.

References Socket::getsockopt().

Referenced by Connection::GetError().

◆ GetLocalAddress()

SocketAddress GetLocalAddress ( ) const
inline

Return the current local socket address.

Definition at line 198 of file socket.hpp.

References Socket::fd_, Socket::IsValid(), and LOG.

◆ GetPeerAddress()

SocketAddress GetPeerAddress ( ) const
inline

Return the current peer socket address.

Definition at line 217 of file socket.hpp.

References Socket::fd_, Socket::IsValid(), and LOG.

Referenced by Connection::GetPeerAddress().

◆ getsockopt()

int getsockopt ( int  level,
int  optname,
void *  optval,
socklen_t *  optlen 
) const
inline

Perform raw getsockopt() operation on socket.

Definition at line 545 of file socket.hpp.

References Socket::fd_, Socket::IsValid(), and LOG.

Referenced by Socket::GetError().

◆ IsValid()

◆ listen()

bool listen ( int  backlog = 0)
inline

Turn socket into listener state to accept incoming connections.

Definition at line 304 of file socket.hpp.

References Socket::fd_, Socket::IsValid(), and LOG.

◆ MaybeHexdump()

static std::string MaybeHexdump ( const void *  data,
size_t  size 
)
inlinestaticprivate

return hexdump or just [data] if not debugging

Definition at line 609 of file socket.hpp.

References tlx::hexdump().

Referenced by Socket::recv(), Socket::recv_one(), Socket::recvfrom(), Socket::send(), Socket::send_one(), and Socket::sendto().

◆ operator=() [1/2]

Socket& operator= ( const Socket )
delete

non-copyable: delete assignment operator

Referenced by Socket::Socket().

◆ operator=() [2/2]

Socket& operator= ( Socket &&  s)
inline

move-assignment operator: move file descriptor

Definition at line 80 of file socket.hpp.

References Socket::close(), and Socket::fd_.

◆ recv()

ssize_t recv ( void *  out_data,
size_t  size,
int  flags = 0 
)
inline

Receive (data,size) from socket, retry recvs if short-reads occur.

Definition at line 467 of file socket.hpp.

References Socket::fd_, Socket::IsValid(), LOG, and Socket::MaybeHexdump().

Referenced by Socket::recv_one(), and Connection::SyncRecv().

◆ recv_one()

ssize_t recv_one ( void *  out_data,
size_t  max_size,
int  flags = 0 
)
inline

Recv (out_data,max_size) from socket (BSD socket API function wrapper)

Definition at line 440 of file socket.hpp.

References Socket::fd_, Socket::IsValid(), LOG, Socket::MaybeHexdump(), and Socket::recv().

Referenced by Connection::RecvOne().

◆ recvfrom()

ssize_t recvfrom ( void *  out_data,
size_t  max_size,
int  flags = 0,
SocketAddress out_source = nullptr 
)
inline

Recv (out_data,max_size) and source address from socket (BSD socket API function wrapper)

Definition at line 510 of file socket.hpp.

References Socket::fd_, Socket::IsValid(), LOG, and Socket::MaybeHexdump().

◆ send()

ssize_t send ( const void *  data,
size_t  size,
int  flags = 0 
)
inline

Send (data,size) to socket, retry sends if short-sends occur.

Definition at line 375 of file socket.hpp.

References Socket::fd_, Socket::IsValid(), LOG, and Socket::MaybeHexdump().

Referenced by Socket::send_one(), and Connection::SyncSend().

◆ send_one()

ssize_t send_one ( const void *  data,
size_t  size,
int  flags = 0 
)
inline

Send (data,size) to socket (BSD socket API function wrapper), for blocking sockets one should probably use send() instead of this lower-layer functions.

Definition at line 354 of file socket.hpp.

References Socket::fd_, Socket::IsValid(), LOG, Socket::MaybeHexdump(), and Socket::send().

Referenced by Connection::SendOne().

◆ sendto()

ssize_t sendto ( const void *  data,
size_t  size,
int  flags,
const SocketAddress dest 
)
inline

Send (data,size) to destination.

Definition at line 416 of file socket.hpp.

References Socket::fd_, Socket::IsValid(), LOG, Socket::MaybeHexdump(), SocketAddress::sockaddr(), and SocketAddress::socklen().

◆ SetKeepAlive()

void SetKeepAlive ( bool  activate = true)

Enable sending of keep-alive messages on connection-oriented sockets.

Definition at line 23 of file socket.cpp.

References Socket::fd_, Socket::IsValid(), LOG, and Socket::setsockopt().

Referenced by Socket::setsockopt().

◆ SetNoDelay()

void SetNoDelay ( bool  activate = true)

If set, disable the Nagle algorithm. This means that segments are always sent as soon as possible, even if there is only a small amount of data.

Definition at line 60 of file socket.cpp.

References Socket::fd_, Socket::IsValid(), LOG, and Socket::setsockopt().

Referenced by Socket::setsockopt(), and Socket::Socket().

◆ SetNonBlocking() [1/2]

static bool SetNonBlocking ( int  fd,
bool  non_blocking 
)
inlinestatic

Turn socket into non-blocking state.

Definition at line 165 of file socket.hpp.

References LOG.

Referenced by SelectDispatcher::SelectDispatcher(), Connection::SetNonBlocking(), and Socket::SetNonBlocking().

◆ SetNonBlocking() [2/2]

bool SetNonBlocking ( bool  non_blocking)
inline

Turn socket into non-blocking state.

Definition at line 185 of file socket.hpp.

References Socket::fd_, Socket::IsValid(), Socket::non_blocking_, and Socket::SetNonBlocking().

◆ SetRcvBuf()

void SetRcvBuf ( size_t  size)

Set SO_RCVBUF socket option.

Definition at line 106 of file socket.cpp.

References Socket::fd_, Socket::IsValid(), LOG, and Socket::setsockopt().

Referenced by Socket::setsockopt(), and Socket::Socket().

◆ SetReuseAddr()

void SetReuseAddr ( bool  activate = true)

Enable SO_REUSEADDR, which allows the socket to be bound more quickly to previously used ports.

Definition at line 37 of file socket.cpp.

References Socket::fd_, Socket::IsValid(), LOG, and Socket::setsockopt().

Referenced by Socket::setsockopt().

◆ SetSndBuf()

void SetSndBuf ( size_t  size)

Set SO_SNDBUF socket option.

Definition at line 81 of file socket.cpp.

References Socket::fd_, Socket::IsValid(), LOG, and Socket::setsockopt().

Referenced by Socket::setsockopt(), and Socket::Socket().

◆ setsockopt()

int setsockopt ( int  level,
int  optname,
const void *  optval,
socklen_t  optlen 
)
inline

Member Data Documentation

◆ debug

constexpr bool debug = false
staticprivate

Definition at line 52 of file socket.hpp.

◆ debug_data

constexpr bool debug_data = false
staticprivate

Definition at line 53 of file socket.hpp.

◆ fd_

◆ non_blocking_

bool non_blocking_ = false
private

flag whether the socket is set to non-blocking

Definition at line 606 of file socket.hpp.

Referenced by Socket::SetNonBlocking().


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