Nets v3.0.0
Socket Struct Referencefinal

Network socket instance handle. More...

#include <socket.hpp>

Public Member Functions

 Socket (const Socket &)=delete
 Socket (Socket &&r) noexcept
Socket & operator= (Socket &)=delete
Socket & operator= (Socket &&r) noexcept
 Socket () noexcept
 Creates a new empty netwown socket.
 Socket (SocketType type, SocketFamily family, SocketAddressView localAddress, bool isBlocking=true, bool isOnlyIPv6=false, SslContextView sslContext=nullptr)
 Creates a new network socket instance.
 ~Socket ()
 Destroys network socket instance.
void destroy () noexcept
 Destroys network socket instance.
Socket_TgetInstance () const noexcept
 Returns socket SSL context view instance.
SocketType getType () const noexcept
 Returns socket communication protocol type.
SocketFamily getFamily () const noexcept
 Returns socket internet protocol (IP) address family type.
bool isBlocking () const noexcept
 Returns true if socket is in blocking mode.
bool isOnlyIPv6 () const noexcept
 Returns true if IPv6 socket is not accepting IPv4 connections.
void getLocalAddress (SocketAddressView socketAddress) const
 Gets local socket IP address.
void getRemoteAddress (SocketAddressView socketAddress) const
 Gets remote socket IP address.
SslContextView getSslContext () const noexcept
 Returns socket SSL context instance.
void * getHandle () const noexcept
 Returns socket internal handle.
bool isNoDelay () const noexcept
 Returns true if stream socket sends without caching.
void setNoDelay (bool value) noexcept
 Sets socket no delay mode.
bool isListening () const noexcept
 Returns true if socket is in the listening state.
size_t getQueueSize () const noexcept
 Returns socket pending connections queue size.
NetsResult listen (size_t queueSize=256) noexcept
 Puts socket in a listening state.
NetsResult accept (SocketView &accepted) noexcept
 Accepts a new socket connection.
NetsResult acceptSsl () noexcept
 Accepts socket SSL connection.
NetsResult connect (SocketAddressView remoteAddress) noexcept
 Connects socket to the specified remote address.
NetsResult connectSsl (const char *hostname=nullptr) noexcept
 Establishes socket SSL connection.
NetsResult shutdown (SocketShutdown shutdown=RECEIVE_SEND_SOCKET_SHUTDOWN) noexcept
 Shutdowns part of the full-duplex connection.
NetsResult receive (void *receiveBuffer, size_t bufferSize, size_t &byteCount) noexcept
 Receives pending socket data.
NetsResult send (const void *data, size_t byteCount)
 Sends specified data to the remote socket.
template<class T>
NetsResult send (const T &data)
 Sends specified data to the remote socket.
NetsResult receiveFrom (SocketAddressView remoteAddress, void *receiveBuffer, size_t bufferSize, size_t &byteCount) noexcept
 Receives pending data from the remote socket.
NetsResult sendTo (const void *data, size_t byteCount, SocketAddressView remoteAddress) noexcept
 Sends specified data to the remote socket.
template<class T>
NetsResult sendTo (const T &data, SocketAddressView remoteAddress) noexcept
 Sends specified data to the remote socket.
 operator bool () const noexcept
 Returns true if socket view is not null.

Static Public Member Functions

static void disableSigpipe () noexcept
 Disablea SIGPIPE signals on Linux for a current thread.

Protected Attributes

Socket_Tinstance = nullptr

Detailed Description

Network socket instance handle.

See the socket.h

Constructor & Destructor Documentation

◆ Socket()

Socket ( SocketType type,
SocketFamily family,
SocketAddressView localAddress,
bool isBlocking = true,
bool isOnlyIPv6 = false,
SslContextView sslContext = nullptr )
inline

Creates a new network socket instance.

See the createSocket().

Parameters
typesocket communication protocol type
familyinternet protocol address family
localAddresssocket local bind address instance
isBlockingcreate socket in blocking mode
isOnlyIPv6create socket in IPv6 only mode
sslContextsocket SSL context instance or nullptr
Exceptions
Errorwith a NetsResult string on failure.

◆ ~Socket()

~Socket ( )
inline

Destroys network socket instance.

See the destroySocket().

Member Function Documentation

◆ destroy()

void destroy ( )
inlinenoexceptinherited

Destroys network socket instance.

See the destroySocket().

◆ getType()

SocketType getType ( ) const
inlinenoexceptinherited

Returns socket communication protocol type.

See the getSocketType().

◆ getFamily()

SocketFamily getFamily ( ) const
inlinenoexceptinherited

Returns socket internet protocol (IP) address family type.

See the getSocketFamily().

◆ isBlocking()

bool isBlocking ( ) const
inlinenoexceptinherited

Returns true if socket is in blocking mode.

See the isSocketBlocking().

◆ isOnlyIPv6()

bool isOnlyIPv6 ( ) const
inlinenoexceptinherited

Returns true if IPv6 socket is not accepting IPv4 connections.

See the isSocketOnlyIPv6().

◆ getLocalAddress()

void getLocalAddress ( SocketAddressView socketAddress) const
inlineinherited

Gets local socket IP address.

See the getSocketLocalAddress().

Exceptions
Errorwith a result string on failure.
Parameters
[out]socketAddresssocket IP address instance

◆ getRemoteAddress()

void getRemoteAddress ( SocketAddressView socketAddress) const
inlineinherited

Gets remote socket IP address.

See the getSocketRemoteAddress().

Exceptions
Errorwith a result string on failure.
Parameters
[out]socketAddresssocket IP address instance

◆ getSslContext()

SslContextView getSslContext ( ) const
inlinenoexceptinherited

Returns socket SSL context instance.

See the getSocketSslContext().

◆ getHandle()

void * getHandle ( ) const
inlinenoexceptinherited

Returns socket internal handle.

See the getSocketHandle().

◆ isNoDelay()

bool isNoDelay ( ) const
inlinenoexceptinherited

Returns true if stream socket sends without caching.

See the isSocketNoDelay().

◆ setNoDelay()

void setNoDelay ( bool value)
inlinenoexceptinherited

Sets socket no delay mode.

Does stream socket sends without caching.

Parameters
valueno delay mode value

◆ isListening()

bool isListening ( ) const
inlinenoexceptinherited

Returns true if socket is in the listening state.

See the isSocketListening().

◆ getQueueSize()

size_t getQueueSize ( ) const
inlinenoexceptinherited

Returns socket pending connections queue size.

See the getSocketQueueSize().

◆ listen()

NetsResult listen ( size_t queueSize = 256)
inlinenoexceptinherited

Puts socket in a listening state.

See the listenSocket().

Returns
The operation NetsResult code.
Parameters
queueSizepending connections queue size

◆ accept()

NetsResult accept ( SocketView & accepted)
inlinenoexceptinherited

Accepts a new socket connection.

Note
You should destroy accepted socket instance manually!

See the acceptSocket().

Returns
The operation NetsResult code.
Parameters
[out]acceptedreference to the accepted socket

◆ acceptSsl()

NetsResult acceptSsl ( )
inlinenoexceptinherited

Accepts socket SSL connection.

See the acceptSslSocket().

Returns
The operation NetsResult code.

◆ connect()

NetsResult connect ( SocketAddressView remoteAddress)
inlinenoexceptinherited

Connects socket to the specified remote address.

See the connectSocket().

Returns
The operation NetsResult code.
Parameters
remoteAddressremote socket IP address instance

◆ connectSsl()

NetsResult connectSsl ( const char * hostname = nullptr)
inlinenoexceptinherited

Establishes socket SSL connection.

See the connectSslSocket().

Returns
The operation NetsResult code.
Parameters
[in]hostnameremote socket SNI hostname or NULL

◆ shutdown()

NetsResult shutdown ( SocketShutdown shutdown = RECEIVE_SEND_SOCKET_SHUTDOWN)
inlinenoexceptinherited

Shutdowns part of the full-duplex connection.

See the shutdownSocket().

Returns
The operation NetsResult code.
Parameters
shutdownsocket connection shutdown mode

◆ receive()

NetsResult receive ( void * receiveBuffer,
size_t bufferSize,
size_t & byteCount )
inlinenoexceptinherited

Receives pending socket data.

See the socketReceive().

Returns
The operation NetsResult code.
Parameters
[out]receiveBufferdata receive buffer
bufferSizedata receive buffer size in bytes
[out]byteCountreference to the received byte count

◆ send() [1/2]

NetsResult send ( const void * data,
size_t byteCount )
inlineinherited

Sends specified data to the remote socket.

See the socketSend().

Returns
The operation NetsResult code.
Parameters
[in]datatarget data to send
byteCountdata byte count to send

◆ send() [2/2]

template<class T>
NetsResult send ( const T & data)
inlineinherited

Sends specified data to the remote socket.

See the socketSend().

Returns
The operation NetsResult code.
Template Parameters
Ttype of the send data
Parameters
[in]datatarget data to send

◆ receiveFrom()

NetsResult receiveFrom ( SocketAddressView remoteAddress,
void * receiveBuffer,
size_t bufferSize,
size_t & byteCount )
inlinenoexceptinherited

Receives pending data from the remote socket.

See the socketReceiveFrom().

Returns
The operation NetsResult code.
Parameters
[out]remoteAddressremote socket IP address
[out]receiveBufferdata receive buffer
bufferSizedata receive buffer size in bytes
[out]byteCountreference to the received byte count

◆ sendTo() [1/2]

NetsResult sendTo ( const void * data,
size_t byteCount,
SocketAddressView remoteAddress )
inlinenoexceptinherited

Sends specified data to the remote socket.

See the socketSendTo().

Returns
The operation NetsResult code.
Parameters
[in]datasend data buffer
byteCountdata byte count to send
remoteAddressdestination remote socket IP address

◆ sendTo() [2/2]

template<class T>
NetsResult sendTo ( const T & data,
SocketAddressView remoteAddress )
inlinenoexceptinherited

Sends specified data to the remote socket.

See the socketSendTo().

Returns
The operation NetsResult code.
Template Parameters
Ttype of the send data
Parameters
[in]datatarget data to send
remoteAddressdestination remote socket IP address