Thrill
0.1
|
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... | |
Socket & | operator= (const Socket &)=delete |
non-copyable: delete assignment operator More... | |
Socket (Socket &&s) noexcept | |
move-constructor: move file descriptor More... | |
Socket & | operator= (Socket &&s) |
move-assignment operator: move file descriptor More... | |
~Socket () | |
static Socket | Create () |
Create a new stream socket. More... | |
static std::pair< Socket, Socket > | CreatePair () |
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... | |
|
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().
|
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().
move-constructor: move file descriptor
Definition at line 78 of file socket.hpp.
|
inline |
Definition at line 88 of file socket.hpp.
References Socket::close(), and Socket::fd_.
|
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().
|
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().
|
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().
|
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().
|
inlinestatic |
Create a new stream socket.
Definition at line 93 of file socket.hpp.
References Socket::fd(), LOG, and Socket::Socket().
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().
|
inline |
Return the associated file descriptor.
Definition at line 154 of file socket.hpp.
References Socket::fd_.
Referenced by SelectDispatcher::AddRead(), SelectDispatcher::AddWrite(), SelectDispatcher::Cancel(), Socket::Create(), Connection::operator==(), Connection::OutputOstream(), and SelectDispatcher::SetExcept().
|
inline |
Query socket for its current error state.
Definition at line 157 of file socket.hpp.
References Socket::getsockopt().
Referenced by Connection::GetError().
|
inline |
Return the current local socket address.
Definition at line 198 of file socket.hpp.
References Socket::fd_, Socket::IsValid(), and LOG.
|
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().
|
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().
|
inline |
Check whether the contained file descriptor is valid.
Definition at line 150 of file socket.hpp.
References Socket::fd_.
Referenced by Socket::accept(), Socket::bind(), Socket::close(), Socket::connect(), Socket::GetLocalAddress(), Socket::GetPeerAddress(), Socket::getsockopt(), Connection::IsValid(), Socket::listen(), Socket::recv(), Socket::recv_one(), Socket::recvfrom(), Socket::send(), Socket::send_one(), Socket::sendto(), Socket::SetKeepAlive(), Socket::SetNoDelay(), Socket::SetNonBlocking(), Socket::SetRcvBuf(), Socket::SetReuseAddr(), Socket::SetSndBuf(), and Socket::setsockopt().
|
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.
|
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().
non-copyable: delete assignment operator
Referenced by Socket::Socket().
move-assignment operator: move file descriptor
Definition at line 80 of file socket.hpp.
References Socket::close(), and Socket::fd_.
|
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().
|
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().
|
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().
|
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().
|
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().
|
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().
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().
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().
|
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().
|
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().
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().
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().
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().
|
inline |
Perform raw setsockopt() operation on socket.
Definition at line 564 of file socket.hpp.
References Socket::fd_, Socket::IsValid(), LOG, Socket::SetKeepAlive(), Socket::SetNoDelay(), Socket::SetRcvBuf(), Socket::SetReuseAddr(), and Socket::SetSndBuf().
Referenced by Socket::SetKeepAlive(), Socket::SetNoDelay(), Socket::SetRcvBuf(), Socket::SetReuseAddr(), and Socket::SetSndBuf().
|
staticprivate |
Definition at line 52 of file socket.hpp.
|
staticprivate |
Definition at line 53 of file socket.hpp.
|
private |
the file descriptor of the socket.
Definition at line 603 of file socket.hpp.
Referenced by Socket::accept(), Socket::bind(), Socket::close(), Socket::connect(), Socket::fd(), Socket::GetLocalAddress(), Socket::GetPeerAddress(), Socket::getsockopt(), Socket::IsValid(), Socket::listen(), Socket::operator=(), Socket::recv(), Socket::recv_one(), Socket::recvfrom(), Socket::send(), Socket::send_one(), Socket::sendto(), Socket::SetKeepAlive(), Socket::SetNoDelay(), Socket::SetNonBlocking(), Socket::SetRcvBuf(), Socket::SetReuseAddr(), Socket::SetSndBuf(), Socket::setsockopt(), and Socket::~Socket().
|
private |
flag whether the socket is set to non-blocking
Definition at line 606 of file socket.hpp.
Referenced by Socket::SetNonBlocking().