[#Sock]
= Sock
:mrdocs:

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

== Synopsis

Declared in `&lt;util&sol;sock&period;h&gt;`

[source,cpp,subs="verbatim,replacements,macros,-callouts"]
----
class Sock;
----

== Types

[cols="1,4"]
|===
| Name| Description
| xref:Sock/EqualSharedPtrSock.adoc[`EqualSharedPtrSock`] 
| Equality comparator for `shared&lowbar;ptr&lt;const Sock&gt;` based on the underlying socket descriptor&period;
| xref:Sock/Events.adoc[`Events`] 
| Auxiliary requested&sol;occurred events to wait for in `WaitMany()`&period;
| xref:Sock/HashSharedPtrSock.adoc[`HashSharedPtrSock`] 
| Hasher for a `shared&lowbar;ptr&lt;const Sock&gt;` keyed on the underlying socket descriptor&period;
|===


== Type Aliases

[cols="1,4"]
|===
| Name| Description
| xref:Sock/Event.adoc[`Event`] 
| Bitmask type for the I&sol;O readiness events passed to and returned from `Wait()`&sol;`WaitMany()`&period;
| xref:Sock/EventsPerSock.adoc[`EventsPerSock`] 
| On which socket to wait for what events in `WaitMany()`&period; The `shared&lowbar;ptr` is copied into the map to ensure that the `Sock` object is not destroyed (its destructor would close the underlying socket)&period; 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&period;
|===


== Member Functions

[cols="1,4"]
|===
| Name| Description
| xref:Sock/2constructor-07.adoc[`Sock`]         [.small]#[constructor]#
| Constructors
| xref:Sock/2destructor.adoc[`&#126;Sock`] [.small]#[destructor]# [.small]#[virtual]#
| Destructor, close the socket or do nothing if empty&period;
| xref:Sock/operator_assign-0e.adoc[`operator&equals;`] 
| Assignment operators
| xref:Sock/Accept.adoc[`Accept`]  [.small]#[virtual]#
| accept(2) wrapper&period; Equivalent to `std&colon;&colon;make&lowbar;unique&lt;Sock&gt;(accept(m&lowbar;socket, addr, addr&lowbar;len))`&period; Code that uses this wrapper can be unit tested if this method is overridden by a mock Sock implementation&period; The returned unique&lowbar;ptr is empty if `accept()` failed in which case errno will be set&period;
| xref:Sock/Bind.adoc[`Bind`]  [.small]#[virtual]#
| bind(2) wrapper&period; Equivalent to `bind(m&lowbar;socket, addr, addr&lowbar;len)`&period; Code that uses this wrapper can be unit tested if this method is overridden by a mock Sock implementation&period;
| xref:Sock/Connect.adoc[`Connect`]  [.small]#[virtual]#
| connect(2) wrapper&period; Equivalent to `connect(m&lowbar;socket, addr, addrlen)`&period; Code that uses this wrapper can be unit tested if this method is overridden by a mock Sock implementation&period;
| xref:Sock/GetSockName.adoc[`GetSockName`]  [.small]#[virtual]#
| getsockname(2) wrapper&period; Equivalent to `getsockname(m&lowbar;socket, name, name&lowbar;len)`&period; Code that uses this wrapper can be unit tested if this method is overridden by a mock Sock implementation&period;
| xref:Sock/GetSockOpt.adoc[`GetSockOpt`]  [.small]#[virtual]#
| getsockopt(2) wrapper&period; Equivalent to `getsockopt(m&lowbar;socket, level, opt&lowbar;name, opt&lowbar;val, opt&lowbar;len)`&period; Code that uses this wrapper can be unit tested if this method is overridden by a mock Sock implementation&period;
| xref:Sock/IsConnected.adoc[`IsConnected`]  [.small]#[virtual]#
| Check if still connected&period;
| xref:Sock/IsSelectable.adoc[`IsSelectable`]  [.small]#[virtual]#
| Check if the underlying socket can be used for `select(2)` (or the `Wait()` method)&period;
| xref:Sock/Listen.adoc[`Listen`]  [.small]#[virtual]#
| listen(2) wrapper&period; Equivalent to `listen(m&lowbar;socket, backlog)`&period; Code that uses this wrapper can be unit tested if this method is overridden by a mock Sock implementation&period;
| xref:Sock/Recv.adoc[`Recv`]  [.small]#[virtual]#
| recv(2) wrapper&period; Equivalent to `recv(m&lowbar;socket, buf, len, flags)&semi;`&period; Code that uses this wrapper can be unit tested if this method is overridden by a mock Sock implementation&period;
| xref:Sock/RecvUntilTerminator.adoc[`RecvUntilTerminator`]  [.small]#[virtual]#
| Read from socket until a terminator character is encountered&period; Will never consume bytes past the terminator from the socket&period;
| xref:Sock/Send.adoc[`Send`]  [.small]#[virtual]#
| send(2) wrapper&period; Equivalent to `send(m&lowbar;socket, data, len, flags)&semi;`&period; Code that uses this wrapper can be unit tested if this method is overridden by a mock Sock implementation&period;
| xref:Sock/SendComplete-0bd3.adoc[`SendComplete`] 
| `SendComplete` overloads
| xref:Sock/SetNonBlocking.adoc[`SetNonBlocking`]  [.small]#[virtual]#
| Set the non&hyphen;blocking option on the socket&period;
| xref:Sock/SetSockOpt.adoc[`SetSockOpt`]  [.small]#[virtual]#
| setsockopt(2) wrapper&period; Equivalent to `setsockopt(m&lowbar;socket, level, opt&lowbar;name, opt&lowbar;val, opt&lowbar;len)`&period; Code that uses this wrapper can be unit tested if this method is overridden by a mock Sock implementation&period;
| xref:Sock/Wait.adoc[`Wait`]  [.small]#[virtual]#
| Wait for readiness for input (recv) or output (send)&period;
| xref:Sock/WaitMany.adoc[`WaitMany`]  [.small]#[virtual]#
| Same as `Wait()`, but wait on many sockets within the same timeout&period;
| xref:Sock/operator_eq.adoc[`operator&equals;&equals;`] 
| Check if the internal socket is equal to `s`&period; Use only in tests&period;
|===


== Static Data Members

[cols="1,4"]
|===
| Name| Description
| xref:Sock/ErrorEvent.adoc[`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&period;
| xref:Sock/RecvEvent.adoc[`RecvEvent`] 
| If passed to `Wait()`, then it will wait for readiness to read from the socket&period;
| xref:Sock/SendEvent.adoc[`SendEvent`] 
| If passed to `Wait()`, then it will wait for readiness to send to the socket&period;
|===


== Protected Data Members

[cols="1,4"]
|===
| Name| Description
| xref:Sock/m_socket.adoc[`m&lowbar;socket`] 
| Contained socket&period; `INVALID&lowbar;SOCKET` designates the object is empty&period;
|===


== Non-Member Functions

[cols="1,4"]
|===
| Name| Description
| xref:ConnectDirectly-0c.adoc[`ConnectDirectly`]
| Create a socket and try to connect to the specified service, using the provided timeout&period;
| xref:ConnectDirectly-0e.adoc[`ConnectDirectly`]
| Create a socket and try to connect to the specified service&period;
| xref:ConnectThroughProxy.adoc[`ConnectThroughProxy`]
| Connect to a specified destination service through a SOCKS5 proxy by first connecting to the SOCKS5 proxy&period;
| xref:CreateSockOS.adoc[`CreateSockOS`]
| Create a real socket from the operating system&period;
| xref:GetBindAddress.adoc[`GetBindAddress`]
| Get the bind address for a socket as CService&period;
|===


[.small]#Created with https://www.mrdocs.com[MrDocs]#
