Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
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/StringViewData.h"
8#include "../Foundation/UniqueHandle.h"
9
10namespace SC
11{
12namespace detail
13{
14struct SocketDescriptorDefinition;
15}
16struct SC_COMPILER_EXPORT SocketDescriptor;
17struct SocketFlags;
18struct SocketIPAddress;
19} // namespace SC
20
28
31
33#if SC_PLATFORM_WINDOWS
34
35struct SC::detail::SocketDescriptorDefinition
36{
37 using Handle = size_t; // SOCKET
38 static Result releaseHandle(Handle& handle);
39
40 static constexpr Handle Invalid = ~static_cast<Handle>(0); // INVALID_SOCKET
41};
42
43#else
44
45struct SC::detail::SocketDescriptorDefinition
46{
47 using Handle = int; // fd
48 static Result releaseHandle(Handle& handle);
49
50 static constexpr Handle Invalid = -1; // invalid fd
51};
52
53#endif
54
57{
64
71
78
85
92
100
101 private:
102 friend struct SocketDescriptor;
103 friend struct SocketDescriptor;
104 friend struct SocketIPAddressInternal;
105 [[nodiscard]] static AddressFamily AddressFamilyFromInt(int value);
106 [[nodiscard]] static unsigned char toNative(AddressFamily family);
107 [[nodiscard]] static SocketType SocketTypeFromInt(int value);
108 [[nodiscard]] static int toNative(SocketType family);
109
110 [[nodiscard]] static ProtocolType ProtocolTypeFromInt(int value);
111 [[nodiscard]] static int toNative(ProtocolType family);
112};
113
119{
121 static constexpr int MAX_ASCII_STRING_LENGTH = 46;
122
125
129
133
136 [[nodiscard]] uint16_t getPort() const;
137
142 [[nodiscard]] Result fromAddressPort(StringViewData interfaceAddress, uint16_t port);
143
145 [[nodiscard]] uint32_t sizeOfHandle() const;
146
148 [[nodiscard]] bool isValid() const;
149
154 template <size_t N>
155 [[nodiscard]] StringViewData toString(char (&buffer)[N]) const
156 {
157 static_assert(N >= MAX_ASCII_STRING_LENGTH, "Insufficient buffer");
158 return formatAddress(buffer);
159 }
160
163
164 private:
165 [[nodiscard]] StringViewData formatAddress(Span<char> buffer) const;
166 friend struct SocketServer;
167 friend struct SocketClient;
168
169 struct Internal;
170};
171
#define SC_COMPILER_EXPORT
Macro for symbol visibility in non-MSVC compilers.
Definition Compiler.h:78
unsigned short uint16_t
Platform independent (2) bytes unsigned int.
Definition PrimitiveTypes.h:37
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
A buffer of bytes with given alignment.
Definition AlignedStorage.h:29
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:67
Low-level OS socket handle.
Definition SocketDescriptor.h:178
Result shutdown(SocketFlags::ShutdownType shutdownType)
Shuts down the socket for reading, writing, or both.
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:57
SocketType
Sets the socket type, if it's a Datagram (for UDP) or Streaming (for TCP and others)
Definition SocketDescriptor.h:81
@ SocketStream
Sets the socket type as Streaming type (for TCP and others)
Definition SocketDescriptor.h:82
@ SocketDgram
Sets the socket type as Streaming type (for UDP)
Definition SocketDescriptor.h:83
AddressFamily
Sets the address family of an IP Address (IPv4 or IPV6)
Definition SocketDescriptor.h:74
@ AddressFamilyIPV6
IP Address is IPV6.
Definition SocketDescriptor.h:76
@ AddressFamilyIPV4
IP Address is IPV4.
Definition SocketDescriptor.h:75
InheritableType
Sets the socket inheritable behaviour for child processes.
Definition SocketDescriptor.h:67
@ NonInheritable
SocketDescriptor will not be inherited by child processes.
Definition SocketDescriptor.h:68
@ Inheritable
SocketDescriptor will be inherited by child processes.
Definition SocketDescriptor.h:69
ShutdownType
Sets the type of shutdown to perform.
Definition SocketDescriptor.h:95
@ ShutdownBoth
Shuts down the socket for both reading and writing.
Definition SocketDescriptor.h:98
@ ShutdownWrite
Shuts down the socket for writing.
Definition SocketDescriptor.h:97
@ ShutdownRead
Shuts down the socket for reading.
Definition SocketDescriptor.h:96
BlockingType
Sets the socket as blocking / nonblocking mode.
Definition SocketDescriptor.h:60
@ NonBlocking
SocketDescriptor is in non-blocking mode.
Definition SocketDescriptor.h:61
@ Blocking
SocketDescriptor is in blocking mode.
Definition SocketDescriptor.h:62
ProtocolType
Sets the socket protocol type.
Definition SocketDescriptor.h:88
@ ProtocolUdp
The protocol is UDP.
Definition SocketDescriptor.h:90
@ ProtocolTcp
The protocol is TCP.
Definition SocketDescriptor.h:89
Native representation of an IP Address.
Definition SocketDescriptor.h:119
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 SocketDescriptor.h:124
AlignedStorage< 28 > handle
Handle to native OS representation of the IP Address.
Definition SocketDescriptor.h:162
static constexpr int MAX_ASCII_STRING_LENGTH
Maximum length of the ASCII representation of an IP Address.
Definition SocketDescriptor.h:121
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.
StringViewData toString(char(&buffer)[N]) const
Returns the text representation of this SocketIPAddress.
Definition SocketDescriptor.h:155
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.
Result fromAddressPort(StringViewData 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:26
View over a contiguous sequence of items (pointer + size in elements).
Definition Span.h:29
An read-only view over a string (to avoid including Strings library)
Definition StringViewData.h:31
Move only handle that has a special tag value flagging its invalid state.
Definition UniqueHandle.h:67