RAII helper class that manages a socket and closes it automatically when it goes out of scope.

Synopsis

Declared in <util/sock.h>

class Sock;

Types

Name

Description

EqualSharedPtrSock

Equality comparator for shared_ptr<const Sock> based on the underlying socket descriptor.

Events

Auxiliary requested/occurred events to wait for in WaitMany().

HashSharedPtrSock

Hasher for a shared_ptr<const Sock> keyed on the underlying socket descriptor.

Type Aliases

Name

Description

Event

Bitmask type for the I/O readiness events passed to and returned from Wait()/`WaitMany()`.

EventsPerSock

On which socket to wait for what events in WaitMany(). The shared_ptr is copied into the map to ensure that the Sock object is not destroyed (its destructor would close the underlying socket). If this happens shortly before or after we call poll(2) and a new socket gets created under the same file descriptor number then the report from WaitMany() will be bogus.

Member Functions

Name

Description

Sock [constructor]

Constructors

~Sock [destructor] [virtual]

Destructor, close the socket or do nothing if empty.

operator=

Assignment operators

Accept [virtual]

accept(2) wrapper. Equivalent to std::make_unique<Sock>(accept(m_socket, addr, addr_len)). Code that uses this wrapper can be unit tested if this method is overridden by a mock Sock implementation. The returned unique_ptr is empty if accept() failed in which case errno will be set.

Bind [virtual]

bind(2) wrapper. Equivalent to bind(m_socket, addr, addr_len). Code that uses this wrapper can be unit tested if this method is overridden by a mock Sock implementation.

Connect [virtual]

connect(2) wrapper. Equivalent to connect(m_socket, addr, addrlen). Code that uses this wrapper can be unit tested if this method is overridden by a mock Sock implementation.

GetSockName [virtual]

getsockname(2) wrapper. Equivalent to getsockname(m_socket, name, name_len). Code that uses this wrapper can be unit tested if this method is overridden by a mock Sock implementation.

GetSockOpt [virtual]

getsockopt(2) wrapper. Equivalent to getsockopt(m_socket, level, opt_name, opt_val, opt_len). Code that uses this wrapper can be unit tested if this method is overridden by a mock Sock implementation.

IsConnected [virtual]

Check if still connected.

IsSelectable [virtual]

Check if the underlying socket can be used for select(2) (or the Wait() method).

Listen [virtual]

listen(2) wrapper. Equivalent to listen(m_socket, backlog). Code that uses this wrapper can be unit tested if this method is overridden by a mock Sock implementation.

Recv [virtual]

recv(2) wrapper. Equivalent to recv(m_socket, buf, len, flags);. Code that uses this wrapper can be unit tested if this method is overridden by a mock Sock implementation.

RecvUntilTerminator [virtual]

Read from socket until a terminator character is encountered. Will never consume bytes past the terminator from the socket.

Send [virtual]

send(2) wrapper. Equivalent to send(m_socket, data, len, flags);. Code that uses this wrapper can be unit tested if this method is overridden by a mock Sock implementation.

SendComplete

SendComplete overloads

SetNonBlocking [virtual]

Set the non‐blocking option on the socket.

SetSockOpt [virtual]

setsockopt(2) wrapper. Equivalent to setsockopt(m_socket, level, opt_name, opt_val, opt_len). Code that uses this wrapper can be unit tested if this method is overridden by a mock Sock implementation.

Wait [virtual]

Wait for readiness for input (recv) or output (send).

WaitMany [virtual]

Same as Wait(), but wait on many sockets within the same timeout.

operator==

Check if the internal socket is equal to s. Use only in tests.

Static Data Members

Name

Description

ErrorEvent

Ignored if passed to Wait(), but could be set in the occurred events if an exceptional condition has occurred on the socket or if it has been disconnected.

RecvEvent

If passed to Wait(), then it will wait for readiness to read from the socket.

SendEvent

If passed to Wait(), then it will wait for readiness to send to the socket.

Protected Data Members

Name

Description

m_socket

Contained socket. INVALID_SOCKET designates the object is empty.

Non-Member Functions

Name

Description

ConnectDirectly

Create a socket and try to connect to the specified service, using the provided timeout.

ConnectDirectly

Create a socket and try to connect to the specified service.

ConnectThroughProxy

Connect to a specified destination service through a SOCKS5 proxy by first connecting to the SOCKS5 proxy.

CreateSockOS

Create a real socket from the operating system.

GetBindAddress

Get the bind address for a socket as CService.

Created with MrDocs