4#include "../Common/CompilerMacrosExport.h"
5#include "../Common/PlatformMacrosType.h"
6#ifndef SC_EXPORT_LIBRARY_SOCKET
7#define SC_EXPORT_LIBRARY_SOCKET 0
9#define SC_SOCKET_EXPORT SC_COMPILER_LIBRARY_EXPORT(SC_EXPORT_LIBRARY_SOCKET)
11#include "../Common/AlignedStorage.h"
12#include "../Common/Assert.h"
13#include "../Common/Result.h"
14#include "../Common/StringSpan.h"
15#include "../Common/UniqueHandle.h"
19SC_DECLARE_ASSERT_PROVIDER(SocketAssert, SC_SOCKET_EXPORT);
21#define SC_SOCKET_ASSERT_RELEASE(e) SC_ASSERT_PROVIDER_RELEASE(SC::SocketAssert, e)
22#define SC_SOCKET_ASSERT_DEBUG(e) SC_ASSERT_PROVIDER_DEBUG(SC::SocketAssert, e)
23#define SC_SOCKET_TRUST_RESULT(expression) SC_SOCKET_ASSERT_RELEASE(expression)
32#if SC_PLATFORM_WINDOWS
34struct SC_SOCKET_EXPORT SocketDescriptorDefinition
36 using Handle = size_t;
37 static Result releaseHandle(Handle& handle);
39 static constexpr Handle Invalid = ~static_cast<Handle>(0);
44struct SC_SOCKET_EXPORT SocketDescriptorDefinition
47 static Result releaseHandle(Handle& handle);
49 static constexpr Handle Invalid = -1;
101 friend struct SocketIPAddressInternal;
102 [[nodiscard]]
static AddressFamily AddressFamilyFromInt(
int value);
103 [[nodiscard]]
static unsigned char toNative(AddressFamily family);
105 [[nodiscard]]
static int toNative(SocketType family);
106 [[nodiscard]]
static int toNative(ProtocolType family);
116 static constexpr int MAX_ASCII_STRING_LENGTH = 46;
149 [[nodiscard]]
bool toString(Span<char> inputSpan, StringSpan& outputSpan)
const;
152 AlignedStorage<28> handle = {};
166struct SC_SOCKET_EXPORT
SocketDescriptor :
public UniqueHandle<detail::SocketDescriptorDefinition>
252 enum class BindReuseAddress : uint8_t
258 enum class BindStatus : uint8_t
278 BindStatus* outStatus =
nullptr);
284 Result
listen(uint32_t numberOfWaitingConnections);
317 Result
connect(StringSpan address, uint16_t port);
327 Result
write(Span<const char> data);
333 Result
read(Span<char> data, Span<char>& readData);
359 [[nodiscard]]
static Result
resolveDNS(StringSpan host, Span<char>& ipAddress);
Use a SocketDescriptor as a client (example a TCP or UDP socket client).
Definition Socket.h:307
Result read(Span< char > data, Span< char > &readData)
Read bytes from this socket blocking until they're actually received.
SocketClient(const SocketDescriptor &socket)
Constructs this SocketClient from a SocketDescriptor (already created with SocketDescriptor::create)
Definition Socket.h:310
Result connect(StringSpan address, uint16_t port)
Connect to a given address and port combination.
Result connect(SocketIPAddress ipAddress)
Connect to a given address and port combination.
Result readWithTimeout(Span< char > data, Span< char > &readData, int64_t timeout)
Read bytes from this socket blocking until they're actually received or timeout occurs.
Result write(Span< const char > data)
Writes bytes to this socket.
Synchronous DNS Resolution.
Definition Socket.h:351
static Result resolveDNS(StringSpan host, Span< char > &ipAddress)
Resolve an host string to an ip address (blocking until DNS response arrives)
Low-level OS socket handle.
Definition Socket.h:167
Result setMulticastHops(SocketFlags::AddressFamily addressFamily, int hops)
Specifies the default TTL or hop limit for outgoing multicast datagrams.
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 setMulticastOutboundInterface(const SocketIPAddress &interfaceAddress)
Specifies the default local network interface for outgoing multicast datagrams.
Result isInheritable(bool &value) const
Check if socket is inheritable by child processes.
Result setBlocking(bool value)
Changes the blocking flag for this socket (if IO reads / writes should be blocking or not)
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 setInheritable(bool value)
Changes the inheritable flag for this socket.
Result joinMulticastGroup(const SocketIPAddress &multicastAddress, const SocketIPAddress &interfaceAddress)
Joins an IPv4 or IPv6 multicast group on a specific interface.
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 getAddressFamily(SocketFlags::AddressFamily &addressFamily) const
Get address family (IPV4 / IPV6) of this socket.
Flags for SocketDescriptor (Blocking / Inheritable, IPVx, SocketType)
Definition Socket.h:57
SocketType
Sets the socket type, if it's a Datagram (for UDP) or Streaming (for TCP and others)
Definition Socket.h:81
@ SocketStream
Sets the socket type as Streaming type (for TCP and others)
Definition Socket.h:82
AddressFamily
Sets the address family of an IP Address (IPv4 or IPV6)
Definition Socket.h:74
@ AddressFamilyIPV6
IP Address is IPV6.
Definition Socket.h:76
@ AddressFamilyIPV4
IP Address is IPV4.
Definition Socket.h:75
InheritableType
Sets the socket inheritable behaviour for child processes.
Definition Socket.h:67
@ NonInheritable
SocketDescriptor will not be inherited by child processes.
Definition Socket.h:68
ShutdownType
Sets the type of shutdown to perform.
Definition Socket.h:95
BlockingType
Sets the socket as blocking / nonblocking mode.
Definition Socket.h:60
@ NonBlocking
SocketDescriptor is in non-blocking mode.
Definition Socket.h:61
ProtocolType
Sets the socket protocol type.
Definition Socket.h:88
@ ProtocolUdp
The protocol is UDP.
Definition Socket.h:90
@ ProtocolTcp
The protocol is TCP.
Definition Socket.h:89
Native representation of an IP Address.
Definition Socket.h:114
bool toString(Span< char > inputSpan, StringSpan &outputSpan) const
Returns the text representation of this SocketIPAddress.
uint16_t getPort() const
Get port of this ip address.
char[MAX_ASCII_STRING_LENGTH] AsciiBuffer
Buffer for storing the ASCII representation of an IP Address.
Definition Socket.h:119
Result fromAddressPort(StringSpan interfaceAddress, uint16_t port)
Builds this SocketIPAddress parsing given address string and port.
SocketFlags::AddressFamily getAddressFamily() const
Get Address family of this ip address (IPV4 or IPV6)
uint32_t sizeOfHandle() const
Size of the native IP Address representation.
SocketIPAddress(SocketFlags::AddressFamily addressFamily=SocketFlags::AddressFamilyIPV4)
Constructs an ip address from a given family (IPV4 or IPV6)
bool isValid() const
Checks if this is a valid IPV4 or IPV6 address.
Networking globals initialization (Winsock2 WSAStartup)
Definition Socket.h:364
static void initNetworking()
Initializes Winsock2 on Windows (WSAStartup)
static void shutdownNetworking()
Shutdowns Winsock2 on Windows (WSAStartup)
static bool isNetworkingInited()
Check if initNetworking has been previously called.
Use a SocketDescriptor as a Server (example TCP or UDP Socket Server).
Definition Socket.h:251
Result listen(uint32_t numberOfWaitingConnections)
Start listening for incoming connections at a specific address / port combination (after bind)
SocketServer(SocketDescriptor &socket)
Build a SocketServer from a SocketDescriptor (already created with SocketDescriptor::create)
Definition Socket.h:266
Result bind(SocketIPAddress nativeAddress, BindReuseAddress reuseAddress=BindReuseAddress::Enabled, BindStatus *outStatus=nullptr)
Binds this socket to a given address / port combination.
Result close()
Calls SocketDescriptor::close.
Result accept(SocketFlags::AddressFamily addressFamily, SocketDescriptor &newClient)
Accepts a new client, blocking while waiting for it.