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

Low-level OS socket handle. More...

#include <SocketDescriptor.h>

Inheritance diagram for SC::SocketDescriptor:
SC::UniqueHandle< detail::SocketDescriptorDefinition >

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 address family (IPV4 / IPV6) of this socket.
 

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);
SC_TEST_EXPECT(socket.close());

Member Function Documentation

◆ create()

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

Parameters
addressFamilyAddress family (IPV4 / IPV6)
socketTypeSocketDescriptor type (Stream or Dgram)
protocolProtocol (TCP or UDP)
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
nodiscard

Get address family (IPV4 / IPV6) 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
nodiscard

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

◆ setBlocking()

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

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

◆ setInheritable()

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

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

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