Polyphase Game Engine
Loading...
Searching...
No Matches
WebSocketTypes.h
Go to the documentation of this file.
1#pragma once
2
3#include <stdint.h>
4#include <string>
5#include <utility>
6#include <vector>
7
8// ---------------------------------------------------------------------------
9// Shared value types for the WebSocket client. Header-only on purpose: the web
10// build-target addons supply their own WebSocket:: implementation and include
11// this header, so nothing here may depend on an engine TU.
12// ---------------------------------------------------------------------------
13
14// Ready state. Values match RFC 6455 / the browser WebSocket.readyState enum
15// and the WebSocketState table exposed to Lua.
16enum class WsState : uint8_t
17{
18 Connecting = 0,
19 Open = 1,
20 Closing = 2,
21 Closed = 3
22};
23
24enum class WsError : uint8_t
25{
26 None = 0,
27 BadUrl,
28 NoTransport, // wss:// on a TLS-less build, or no NET_ stack at all
29 Dns,
30 Connect,
34 Transport // socket died mid-stream
35};
36
37// Close codes we originate. Peer-supplied codes pass through untouched.
38#define WS_CLOSE_NORMAL 1000
39#define WS_CLOSE_GOING_AWAY 1001
40#define WS_CLOSE_PROTOCOL_ERROR 1002
41#define WS_CLOSE_TOO_LARGE 1009
42#define WS_CLOSE_ABNORMAL 1006 // never sent on the wire; synthesized locally
43
44// Per-direction queue cap and the largest single message we will reassemble.
45#define WS_DEFAULT_MAX_QUEUED_BYTES (1u * 1024u * 1024u)
46#define WS_MAX_MESSAGE_BYTES (16u * 1024u * 1024u)
47
48// Read chunk pulled off the socket per Tick pass.
49#define WS_RECV_CHUNK_SIZE 8192
50
51struct WsUrl
52{
53 std::string mScheme; // "ws" or "wss"
54 std::string mHost;
55 std::string mResource; // path + query, always starts with '/'
56 uint16_t mPort = 0;
57 bool mSecure = false;
58};
59
61{
62 std::string mUrl;
63 std::vector<std::string> mProtocols;
64 std::vector<std::pair<std::string, std::string> > mHeaders;
66};
67
69{
70 std::vector<uint8_t> mData;
71 bool mBinary = false;
72};
73
74inline const char* WsErrorToString(WsError err)
75{
76 switch (err)
77 {
78 case WsError::None: return "None";
79 case WsError::BadUrl: return "BadUrl";
80 case WsError::NoTransport: return "NoTransport";
81 case WsError::Dns: return "Dns";
82 case WsError::Connect: return "Connect";
83 case WsError::Handshake: return "Handshake";
84 case WsError::Protocol: return "Protocol";
85 case WsError::TooLarge: return "TooLarge";
86 case WsError::Transport: return "Transport";
87 }
88 return "Unknown";
89}
WsError
Definition WebSocketTypes.h:25
#define WS_DEFAULT_MAX_QUEUED_BYTES
Definition WebSocketTypes.h:45
WsState
Definition WebSocketTypes.h:17
const char * WsErrorToString(WsError err)
Definition WebSocketTypes.h:74
Definition WebSocketTypes.h:61
std::string mUrl
Definition WebSocketTypes.h:62
std::vector< std::string > mProtocols
Definition WebSocketTypes.h:63
std::vector< std::pair< std::string, std::string > > mHeaders
Definition WebSocketTypes.h:64
uint32_t mMaxQueuedBytes
Definition WebSocketTypes.h:65
Definition WebSocketTypes.h:69
bool mBinary
Definition WebSocketTypes.h:71
std::vector< uint8_t > mData
Definition WebSocketTypes.h:70
Definition WebSocketTypes.h:52
std::string mHost
Definition WebSocketTypes.h:54
std::string mScheme
Definition WebSocketTypes.h:53
uint16_t mPort
Definition WebSocketTypes.h:56
bool mSecure
Definition WebSocketTypes.h:57
std::string mResource
Definition WebSocketTypes.h:55