Sane C++ Libraries
C++ Platform Abstraction Libraries
SocketDescriptor.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Foundation/AlignedStorage.h"
5#include "../Foundation/Result.h"
6#include "../Foundation/Span.h"
7#include "../Foundation/UniqueHandle.h"
8
9namespace SC
10{
11namespace detail
12{
13struct SocketDescriptorDefinition;
14}
15struct SC_COMPILER_EXPORT SocketDescriptor;
16struct SocketFlags;
17struct SocketIPAddress;
18} // namespace SC
19
27
30
32#if SC_PLATFORM_WINDOWS
33
34struct SC::detail::SocketDescriptorDefinition
35{
36 using Handle = size_t; // SOCKET
37 static Result releaseHandle(Handle& handle);
38
39 static constexpr Handle Invalid = ~static_cast<Handle>(0); // INVALID_SOCKET
40};
41
42#else
43
44struct SC::detail::SocketDescriptorDefinition
45{
46 using Handle = int; // fd
47 static Result releaseHandle(Handle& handle);
48
49 static constexpr Handle Invalid = -1; // invalid fd
50};
51
52#endif
53
56{
59 {
62 };
63
66 {
69 };
70
73 {
76 };
77
80 {
83 };
84
87 {
90 };
91
92 private:
93 friend struct SocketDescriptor;
94 friend struct SocketDescriptor;
95 friend struct SocketIPAddressInternal;
96 [[nodiscard]] static AddressFamily AddressFamilyFromInt(int value);
97 [[nodiscard]] static unsigned char toNative(AddressFamily family);
98 [[nodiscard]] static SocketType SocketTypeFromInt(int value);
99 [[nodiscard]] static int toNative(SocketType family);
100
101 [[nodiscard]] static ProtocolType ProtocolTypeFromInt(int value);
102 [[nodiscard]] static int toNative(ProtocolType family);
103};
104
110{
114 : addressFamily(addressFamily)
115 {}
116
119 [[nodiscard]] SocketFlags::AddressFamily getAddressFamily() { return addressFamily; }
120
125 [[nodiscard]] Result fromAddressPort(SpanStringView interfaceAddress, uint16_t port);
126
129
132
133 private:
134 friend struct SocketServer;
135 friend struct SocketClient;
136
138 struct Internal;
139};
140
146struct SC::SocketDescriptor : public UniqueHandle<detail::SocketDescriptorDefinition>
147{
155 [[nodiscard]] Result create(SocketFlags::AddressFamily addressFamily,
160
164 [[nodiscard]] Result isInheritable(bool& value) const;
165
169 [[nodiscard]] Result setInheritable(bool value);
170
174 [[nodiscard]] Result setBlocking(bool value);
175
179 [[nodiscard]] Result getAddressFamily(SocketFlags::AddressFamily& addressFamily) const;
180};
#define SC_COMPILER_EXPORT
Macro for symbol visibility in non-MSVC compilers.
Definition: Compiler.h:78
unsigned long size_t
Platform independent unsigned size type.
Definition: PrimitiveTypes.h:56
unsigned int uint32_t
Platform independent (4) bytes unsigned int.
Definition: PrimitiveTypes.h:38
unsigned short uint16_t
Platform independent (2) bytes unsigned int.
Definition: PrimitiveTypes.h:37
A buffer of bytes with given alignment.
Definition: AlignedStorage.h:25
An ascii string used as boolean result. SC_TRY macro forwards errors to caller.
Definition: Result.h:12
Use a SocketDescriptor as a client (example a TCP or UDP socket client).
Definition: Socket.h:66
Low-level OS socket handle.
Definition: SocketDescriptor.h:147
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 setInheritable(bool value)
Changes the inheritable flag for this socket.
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: SocketDescriptor.h:56
SocketType
Sets the socket type, if it's a Datagram (for UDP) or Streaming (for TCP and others)
Definition: SocketDescriptor.h:80
@ SocketStream
Sets the socket type as Streaming type (for TCP and others)
Definition: SocketDescriptor.h:81
@ SocketDgram
Sets the socket type as Streaming type (for UDP)
Definition: SocketDescriptor.h:82
AddressFamily
Sets the address family of an IP Address (IPv4 or IPV6)
Definition: SocketDescriptor.h:73
@ AddressFamilyIPV6
IP Address is IPV6.
Definition: SocketDescriptor.h:75
@ AddressFamilyIPV4
IP Address is IPV4.
Definition: SocketDescriptor.h:74
InheritableType
Sets the socket inheritable behaviour for child processes.
Definition: SocketDescriptor.h:66
@ NonInheritable
SocketDescriptor will not be inherited by child processes.
Definition: SocketDescriptor.h:67
@ Inheritable
SocketDescriptor will be inherited by child processes.
Definition: SocketDescriptor.h:68
BlockingType
Sets the socket as blocking / nonblocking mode.
Definition: SocketDescriptor.h:59
@ NonBlocking
SocketDescriptor is in non-blocking mode.
Definition: SocketDescriptor.h:60
@ Blocking
SocketDescriptor is in blocking mode.
Definition: SocketDescriptor.h:61
ProtocolType
Sets the socket protocol type.
Definition: SocketDescriptor.h:87
@ ProtocolUdp
The protocol is UDP.
Definition: SocketDescriptor.h:89
@ ProtocolTcp
The protocol is TCP.
Definition: SocketDescriptor.h:88
Native representation of an IP Address.
Definition: SocketDescriptor.h:110
AlignedStorage< 28 > handle
Handle to native OS representation of the IP Address.
Definition: SocketDescriptor.h:131
SocketFlags::AddressFamily getAddressFamily()
Get Address family of this ip address (IPV4 or IPV6)
Definition: SocketDescriptor.h:119
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)
Definition: SocketDescriptor.h:113
Result fromAddressPort(SpanStringView interfaceAddress, uint16_t port)
Builds this SocketIPAddress parsing given address string and port.
Use a SocketDescriptor as a Server (example TCP or UDP Socket Server).
Definition: Socket.h:25
An read-only view over an ASCII string (to avoid including Strings library)
Definition: Span.h:249
Move only handle that has a special tag value flagging its invalid state.
Definition: UniqueHandle.h:67