Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
SC::SocketDescriptor Struct Reference

Low-level OS socket handle. More...

#include <Socket.h>

Inheritance diagram for SC::SocketDescriptor:

Public Member Functions

Result create (SocketFlags::AddressFamily addressFamily, SocketFlags::SocketType socketType=SocketFlags::SocketStream, SocketFlags::ProtocolType protocol=SocketFlags::ProtocolTcp, SocketFlags::BlockingType blocking=SocketFlags::Blocking, SocketFlags::InheritableType inheritable=SocketFlags::NonInheritable)
 Creates a new SocketDescriptor Descriptor of given family, type, protocol.
 
Result isInheritable (bool &value) const
 Check if socket is inheritable by child processes.
 
Result setInheritable (bool value)
 Changes the inheritable flag for this socket.
 
Result setBlocking (bool value)
 Changes the blocking flag for this socket (if IO reads / writes should be blocking or not)
 
Result getAddressFamily (SocketFlags::AddressFamily &addressFamily) const
 Get the address family of this socket.
 
Result shutdown (SocketFlags::ShutdownType shutdownType)
 Shuts down the socket for reading, writing, or both.
 
Result setTcpNoDelay (bool tcpNoDelay)
 Disables Nagle's algorithm for low-latency communication.
 
Result setBroadcast (bool enableBroadcast)
 Enables or disables broadcast on this socket (for UDP)
 
Result joinMulticastGroup (const SocketIPAddress &multicastAddress, const SocketIPAddress &interfaceAddress)
 Joins an IPv4 or IPv6 multicast group on a specific interface.
 
Result leaveMulticastGroup (const SocketIPAddress &multicastAddress, const SocketIPAddress &interfaceAddress)
 Leaves a previously joined IPv4 or IPv6 multicast group on a specific interface.
 
Result setMulticastLoopback (SocketFlags::AddressFamily addressFamily, bool enableLoopback)
 Controls whether multicast traffic is sent back to the local host.
 
Result setMulticastHops (SocketFlags::AddressFamily addressFamily, int hops)
 Specifies the default TTL or hop limit for outgoing multicast datagrams.
 
Result setMulticastOutboundInterface (const SocketIPAddress &interfaceAddress)
 Specifies the default local network interface for outgoing multicast datagrams.
 
Result sendTo (Span< const char > data, const SocketAddress &destination)
 Sends a datagram to the given destination address.
 
Result sendTo (Span< const char > data, const SocketIPAddress &destination)
 IP-address compatibility overload for sendTo.
 
Result receiveFrom (Span< char > buffer, Span< char > &receivedData, SocketAddress &sourceAddress)
 Receives a datagram, reporting its source address.
 
Result receiveFrom (Span< char > buffer, Span< char > &receivedData, SocketIPAddress &sourceAddress)
 IP-address compatibility overload for receiveFrom.
 

Detailed Description

Low-level OS socket handle.

It also allow querying inheritability and changing it (and blocking mode)
Example (extracted from unit test):

bool isInheritable;
// We are testing only the inheritable because on windows there is no reliable
// way of checking if a non-connected socket is in non-blocking mode
SC_TEST_EXPECT(socket.create(SocketFlags::AddressFamilyIPV4, SocketFlags::SocketStream, SocketFlags::ProtocolTcp,
SocketFlags::NonBlocking, SocketFlags::NonInheritable));
SC_TEST_EXPECT(socket.isValid());
isInheritable = false;
SC_TEST_EXPECT(socket.isInheritable(isInheritable));
SC_TEST_EXPECT(not isInheritable);
SC_TEST_EXPECT(socket.close());
SC_TEST_EXPECT(socket.create(SocketFlags::AddressFamilyIPV4, SocketFlags::SocketStream, SocketFlags::ProtocolTcp,
SocketFlags::Blocking, SocketFlags::NonInheritable));
SC_TEST_EXPECT(socket.isValid());
isInheritable = false;
SC_TEST_EXPECT(socket.isInheritable(isInheritable));
SC_TEST_EXPECT(not isInheritable);
SC_TEST_EXPECT(socket.close());
SC_TEST_EXPECT(socket.create(SocketFlags::AddressFamilyIPV4, SocketFlags::SocketStream, SocketFlags::ProtocolTcp,
SocketFlags::Blocking, SocketFlags::Inheritable));
SC_TEST_EXPECT(socket.isValid());
isInheritable = false;
SC_TEST_EXPECT(socket.isInheritable(isInheritable));
SC_TEST_EXPECT(isInheritable);
// Test TCP_NODELAY
SC_TEST_EXPECT(socket.setTcpNoDelay(false));
SC_TEST_EXPECT(socket.close());

Member Function Documentation

◆ create()

Creates a new SocketDescriptor Descriptor of given family, type, protocol.

Parameters
addressFamilyAddress family (IPv4, IPv6, or Unix-domain)
socketTypeSocketDescriptor type (Stream or Dgram)
protocolProtocol (TCP, UDP, or the family/type default)
blockingIf the socket should be created in blocking mode
inheritableIf the socket should be inheritable by child processes
Returns
Valid Result if a socket with the requested options has been successfully created

◆ getAddressFamily()

Result SC::SocketDescriptor::getAddressFamily ( SocketFlags::AddressFamily & addressFamily) const

Get the address family of this socket.

Parameters
[out]addressFamilyThe address family of this socket (if Result is valid)
Returns
Valid Result the address family for this socket has been queried successfully

◆ isInheritable()

Result SC::SocketDescriptor::isInheritable ( bool & value) const

Check if socket is inheritable by child processes.

Parameters
[out]valueif set to true indicates that this socket is inheritable by child processes
Returns
Valid Result if the inheritable status for this socket has been queried successfully

◆ joinMulticastGroup()

Result SC::SocketDescriptor::joinMulticastGroup ( const SocketIPAddress & multicastAddress,
const SocketIPAddress & interfaceAddress )

Joins an IPv4 or IPv6 multicast group on a specific interface.

Parameters
multicastAddressThe multicast group address to join
interfaceAddressThe local interface address to join on
Returns
Valid Result if the socket has successfully joined the multicast group

◆ leaveMulticastGroup()

Result SC::SocketDescriptor::leaveMulticastGroup ( const SocketIPAddress & multicastAddress,
const SocketIPAddress & interfaceAddress )

Leaves a previously joined IPv4 or IPv6 multicast group on a specific interface.

Parameters
multicastAddressThe multicast group address to leave
interfaceAddressThe local interface address to leave from
Returns
Valid Result if the socket has successfully left the multicast group

◆ receiveFrom() [1/2]

Result SC::SocketDescriptor::receiveFrom ( Span< char > buffer,
Span< char > & receivedData,
SocketAddress & sourceAddress )

Receives a datagram, reporting its source address.

Parameters
[in,out]bufferSpan of memory that will receive the datagram and may be modified on truncation
[out]receivedDataA sub-Span of buffer containing the received bytes; unchanged on failure
[out]sourceAddressThe source address; unchanged on failure
Returns
Valid Result if a complete datagram has been received successfully
Note
On non-blocking sockets, an unsuccessful Result is also returned when no datagram is immediately available
An oversized datagram is consumed and returns an unsuccessful Result. buffer may contain a truncated prefix

◆ receiveFrom() [2/2]

Result SC::SocketDescriptor::receiveFrom ( Span< char > buffer,
Span< char > & receivedData,
SocketIPAddress & sourceAddress )

IP-address compatibility overload for receiveFrom.

◆ sendTo() [1/2]

Result SC::SocketDescriptor::sendTo ( Span< const char > data,
const SocketAddress & destination )

Sends a datagram to the given destination address.

Parameters
dataBytes to send as a single datagram; the whole span must be sent for success
destinationThe destination address of the datagram
Returns
Valid Result if the whole datagram has been sent successfully
Note
On non-blocking sockets, an unsuccessful Result is also returned when the operation would block

◆ sendTo() [2/2]

Result SC::SocketDescriptor::sendTo ( Span< const char > data,
const SocketIPAddress & destination )

IP-address compatibility overload for sendTo.

◆ setBlocking()

Result SC::SocketDescriptor::setBlocking ( bool value)

Changes the blocking flag for this socket (if IO reads / writes should be blocking or not)

Parameters
valuetrue if this socket should be made blocking, false for non-blocking
Returns
Valid Result if it has been possible changing the blocking status of this socket

◆ setBroadcast()

Result SC::SocketDescriptor::setBroadcast ( bool enableBroadcast)

Enables or disables broadcast on this socket (for UDP)

Parameters
enableBroadcasttrue to enable broadcast (set SO_BROADCAST), false to disable
Returns
Valid Result if the SO_BROADCAST option has been set successfully

◆ setInheritable()

Result SC::SocketDescriptor::setInheritable ( bool value)

Changes the inheritable flag for this socket.

Parameters
valuetrue if this socket should be made inheritable, false for non-inheritable
Returns
Valid Result if it has been possible changing the inheritable status of this socket

◆ setMulticastHops()

Result SC::SocketDescriptor::setMulticastHops ( SocketFlags::AddressFamily addressFamily,
int hops )

Specifies the default TTL or hop limit for outgoing multicast datagrams.

Parameters
addressFamilyAddress family of the socket
hopsThe hop limit (0-255)
Returns
Valid Result if the multicast hop limit has been set successfully

◆ setMulticastLoopback()

Result SC::SocketDescriptor::setMulticastLoopback ( SocketFlags::AddressFamily addressFamily,
bool enableLoopback )

Controls whether multicast traffic is sent back to the local host.

Parameters
addressFamilyAddress family of the socket
enableLoopbacktrue to enable multicast loopback, false to disable
Returns
Valid Result if the multicast loopback option has been set successfully

◆ setMulticastOutboundInterface()

Result SC::SocketDescriptor::setMulticastOutboundInterface ( const SocketIPAddress & interfaceAddress)

Specifies the default local network interface for outgoing multicast datagrams.

Parameters
interfaceAddressThe local interface address
Returns
Valid Result if the multicast outgoing interface has been set successfully

◆ setTcpNoDelay()

Result SC::SocketDescriptor::setTcpNoDelay ( bool tcpNoDelay)

Disables Nagle's algorithm for low-latency communication.

Parameters
tcpNoDelaytrue to disable Nagle's algorithm (set TCP_NODELAY), false to enable it
Returns
Valid Result if the TCP_NODELAY option has been set successfully

◆ shutdown()

Result SC::SocketDescriptor::shutdown ( SocketFlags::ShutdownType shutdownType)

Shuts down the socket for reading, writing, or both.

Parameters
shutdownTypeThe type of shutdown to perform
Returns
Valid Result if the socket has been successfully shut down

The documentation for this struct was generated from the following file: