Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
SocketError.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4
5#include "../Common/Result.h"
6
7namespace SC
8{
11
13enum class SocketError : uint32_t
14{
15 NetworkingNotInitialized = 1,
16 UnsupportedTextEncoding,
17 InputCapacityExceeded,
18 InvalidIPAddress,
19 InvalidAddress,
20 AddressTypeMismatch,
21 AddressFamilyMismatch,
22 OperationUnsupported,
23 InvalidSocket,
24 DestinationSocketInUse,
25 InvalidShutdownType,
26 SocketCreationFailed,
27 SocketCloseFailed,
28 DescriptorQueryFailed,
29 DescriptorConfigurationFailed,
30 SocketOptionFailed,
31 ShutdownFailed,
32 BindFailed,
33 AddressInUse,
34 ListenFailed,
35 AcceptFailed,
36 ConnectFailed,
37 SendFailed,
38 ReceiveFailed,
39 IncompleteSend,
40 DatagramTruncated,
41 WouldBlock,
42 TimedOut,
43 DNSResolutionFailed,
44 OutputCapacityExceeded,
45};
46
48enum class SocketErrorDetail : uint16_t
49{
50 None = 0,
51 ParseIPv4Address,
52 ParseIPv6Address,
53 BuildUnixPathAddress,
54 BuildUnixAbstractAddress,
55 ExtractIPAddress,
56 ExtractUnixAddress,
57 CopyHostName,
58 ResolveHostName,
59 CopyResolvedAddress,
60 GetSocketAddress,
61 PosixCloseDescriptor,
62 WindowsCloseSocket,
63 PosixGetDescriptorFlags,
64 PosixSetDescriptorFlags,
65 WindowsGetHandleInformation,
66 WindowsSetHandleInformation,
67 WindowsSetBlocking,
68 CreateSocket,
69 SetTcpNoDelay,
70 SetBroadcast,
71 JoinIPv4Multicast,
72 JoinIPv6Multicast,
73 LeaveIPv4Multicast,
74 LeaveIPv6Multicast,
75 SetIPv4MulticastLoopback,
76 SetIPv6MulticastLoopback,
77 SetIPv4MulticastHops,
78 SetIPv6MulticastHops,
79 SetIPv4MulticastInterface,
80 SetIPv6MulticastInterface,
81 ShutdownSocket,
82 SendDatagram,
83 ReceiveDatagram,
84 BindSocket,
85 ListenSocket,
86 AcceptConnection,
87 ConnectSocket,
88 SendStream,
89 ReceiveStream,
90 WaitForRead,
91};
92
94enum class SocketErrorContextKind : uint16_t
95{
96 None = 0,
97 NativeError,
98 ResolverError,
99 RequiredBytes,
100 ActualBytes,
101};
102
105{
107 {
108 };
110 {
111 };
113 {
114 };
115
116 uint32_t nativeError;
117 int32_t resolverError;
118 uint32_t requiredBytes;
119 uint32_t actualBytes;
120
121 constexpr SocketErrorContext(uint32_t value = 0) : nativeError(value) {}
122 constexpr SocketErrorContext(ResolverError, int32_t value) : resolverError(value) {}
123 constexpr SocketErrorContext(RequiredBytes, uint32_t value) : requiredBytes(value) {}
124 constexpr SocketErrorContext(ActualBytes, uint32_t value) : actualBytes(value) {}
125};
126
128static constexpr ResultCategory SocketResultCategory = ResultCategory(8);
129
133struct [[nodiscard]] ResultSocket
134{
135 Result result;
136 SocketErrorDetail detail = SocketErrorDetail::None;
137 SocketErrorContextKind contextKind = SocketErrorContextKind::None;
138 SocketErrorContext context = {};
139
140 constexpr ResultSocket() : result(true) {}
141 explicit constexpr ResultSocket(bool valid) : result(valid) {}
142 constexpr ResultSocket(SocketError error, SocketErrorDetail detail = SocketErrorDetail::None)
143 : result(Result::Error(SocketResultCategory, error)), detail(detail)
144 {}
145 constexpr ResultSocket(SocketError error, SocketErrorDetail detail, SocketErrorContextKind contextKind,
146 SocketErrorContext context)
147 : result(Result::Error(SocketResultCategory, error)), detail(detail), contextKind(contextKind), context(context)
148 {}
149 constexpr ResultSocket(Result result) : result(result) {}
150
151 template <typename ResultLike>
152 constexpr ResultSocket(const ResultLike& other) : result(other.toResult())
153 {}
154
155 static constexpr ResultSocket withNativeError(SocketError error, SocketErrorDetail detail, uint32_t nativeError)
156 {
157 return {error, detail, SocketErrorContextKind::NativeError, SocketErrorContext(nativeError)};
158 }
159
160 static constexpr ResultSocket withResolverError(SocketError error, SocketErrorDetail detail, int32_t resolverError)
161 {
162 return {error, detail, SocketErrorContextKind::ResolverError,
164 }
165
166 static constexpr ResultSocket withRequiredBytes(SocketError error, SocketErrorDetail detail, uint32_t requiredBytes)
167 {
168 return {error, detail, SocketErrorContextKind::RequiredBytes,
170 }
171
172 static constexpr ResultSocket withActualBytes(SocketError error, SocketErrorDetail detail, uint32_t actualBytes)
173 {
174 return {error, detail, SocketErrorContextKind::ActualBytes,
176 }
177
178 explicit constexpr operator bool() const { return static_cast<bool>(result); }
179 constexpr operator Result() const { return result; }
180 constexpr Result toResult() const { return result; }
181 constexpr bool isError(SocketError error) const { return result.isError(SocketResultCategory, error); }
182};
183
185} // namespace SC
SocketErrorDetail
Stable operation or backend stage retained for a Socket failure.
Definition SocketError.h:49
SocketError
Stable portable failures returned by the Socket library.
Definition SocketError.h:14
SocketErrorContextKind
Kind of the scalar diagnostic context stored in ResultSocket.
Definition SocketError.h:95
Socket result retaining a stable operation detail and one scalar diagnostic context.
Definition SocketError.h:134
Definition SocketError.h:113
Definition SocketError.h:110
Definition SocketError.h:107
A typed scalar payload for a Socket error. Its interpretation is selected by SocketErrorContextKind.
Definition SocketError.h:105