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 GetTcpNoDelay,
92};
93
95enum class SocketErrorContextKind : uint16_t
96{
97 None = 0,
98 NativeError,
99 ResolverError,
100 RequiredBytes,
101 ActualBytes,
102};
103
106{
108 {
109 };
111 {
112 };
114 {
115 };
116
117 uint32_t nativeError;
118 int32_t resolverError;
119 uint32_t requiredBytes;
120 uint32_t actualBytes;
121
122 constexpr SocketErrorContext(uint32_t value = 0) : nativeError(value) {}
123 constexpr SocketErrorContext(ResolverError, int32_t value) : resolverError(value) {}
124 constexpr SocketErrorContext(RequiredBytes, uint32_t value) : requiredBytes(value) {}
125 constexpr SocketErrorContext(ActualBytes, uint32_t value) : actualBytes(value) {}
126};
127
129static constexpr ResultCategory SocketResultCategory = ResultCategory(8);
130
134struct [[nodiscard]] ResultSocket
135{
136 Result result;
137 SocketErrorDetail detail = SocketErrorDetail::None;
138 SocketErrorContextKind contextKind = SocketErrorContextKind::None;
139 SocketErrorContext context = {};
140
141 constexpr ResultSocket() : result(true) {}
142 explicit constexpr ResultSocket(bool valid) : result(valid) {}
143 constexpr ResultSocket(SocketError error, SocketErrorDetail detail = SocketErrorDetail::None)
144 : result(Result::Error(SocketResultCategory, error)), detail(detail)
145 {}
146 constexpr ResultSocket(SocketError error, SocketErrorDetail detail, SocketErrorContextKind contextKind,
147 SocketErrorContext context)
148 : result(Result::Error(SocketResultCategory, error)), detail(detail), contextKind(contextKind), context(context)
149 {}
150 constexpr ResultSocket(Result result) : result(result) {}
151
152 template <typename ResultLike>
153 constexpr ResultSocket(const ResultLike& other) : result(other.toResult())
154 {}
155
156 static constexpr ResultSocket withNativeError(SocketError error, SocketErrorDetail detail, uint32_t nativeError)
157 {
158 return {error, detail, SocketErrorContextKind::NativeError, SocketErrorContext(nativeError)};
159 }
160
161 static constexpr ResultSocket withResolverError(SocketError error, SocketErrorDetail detail, int32_t resolverError)
162 {
163 return {error, detail, SocketErrorContextKind::ResolverError,
165 }
166
167 static constexpr ResultSocket withRequiredBytes(SocketError error, SocketErrorDetail detail, uint32_t requiredBytes)
168 {
169 return {error, detail, SocketErrorContextKind::RequiredBytes,
171 }
172
173 static constexpr ResultSocket withActualBytes(SocketError error, SocketErrorDetail detail, uint32_t actualBytes)
174 {
175 return {error, detail, SocketErrorContextKind::ActualBytes,
177 }
178
179 explicit constexpr operator bool() const { return static_cast<bool>(result); }
180 constexpr operator Result() const { return result; }
181 constexpr Result toResult() const { return result; }
182 constexpr bool isError(SocketError error) const { return result.isError(SocketResultCategory, error); }
183};
184
186} // 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:96
Socket result retaining a stable operation detail and one scalar diagnostic context.
Definition SocketError.h:135
Definition SocketError.h:114
Definition SocketError.h:111
Definition SocketError.h:108
A typed scalar payload for a Socket error. Its interpretation is selected by SocketErrorContextKind.
Definition SocketError.h:106