Polyphase Game Engine
Loading...
Searching...
No Matches
WebSocketFraming.h
Go to the documentation of this file.
1#pragma once
2
3#include "PolyphaseAPI.h"
5
6// ---------------------------------------------------------------------------
7// RFC 6455 wire format. Pure computation — no sockets, no engine singletons —
8// so this is unit-testable and safe to share with any transport.
9// ---------------------------------------------------------------------------
10
11#define WS_OP_CONTINUATION 0x0
12#define WS_OP_TEXT 0x1
13#define WS_OP_BINARY 0x2
14#define WS_OP_CLOSE 0x8
15#define WS_OP_PING 0x9
16#define WS_OP_PONG 0xA
17
18// Appends one complete, masked, unfragmented client frame to outBytes. RFC 6455
19// 5.3 requires every client-to-server frame to be masked; the key is a fresh 4
20// bytes per frame from a cheap PRNG (masking is not a security measure).
21POLYPHASE_API void WsEncodeFrame(uint8_t opcode, const uint8_t* payload, uint32_t size, std::vector<uint8_t>& outBytes);
22
23// Convenience for the Close handshake: builds the 2-byte code + UTF-8 reason
24// payload and frames it.
25POLYPHASE_API void WsEncodeClose(uint16_t code, const char* reason, std::vector<uint8_t>& outBytes);
26
27// Incremental frame decoder. Feed() whatever the socket handed over, then pump
28// Next() until it returns false.
30{
31public:
32
33 struct Event
34 {
35 enum Type
36 {
37 None = 0,
41 Close
42 };
43
44 Type mType = None;
45 std::vector<uint8_t> mData;
46 bool mBinary = false;
47 uint16_t mCloseCode = 0;
48 std::string mCloseReason;
49 };
50
51 void Feed(const uint8_t* data, uint32_t size);
52 bool Next(Event& outEvent);
53
54 void SetMaxMessageBytes(uint32_t bytes) { mMaxMessageBytes = bytes; }
55
56 bool HasError() const { return mError != WsError::None; }
57 WsError GetError() const { return mError; }
58 const std::string& GetErrorMessage() const { return mErrorMessage; }
59
60private:
61
62 void Fail(WsError err, const char* message);
63
64 std::vector<uint8_t> mBuffer;
65 size_t mReadPos = 0;
66
67 // Fragmentation reassembly.
68 std::vector<uint8_t> mFragment;
69 uint8_t mFragmentOpcode = 0;
70 bool mFragmenting = false;
71
72 uint32_t mMaxMessageBytes = WS_MAX_MESSAGE_BYTES;
73 WsError mError = WsError::None;
74 std::string mErrorMessage;
75};
76
77// SHA-1 and base64, used by the opening handshake. Implemented here so the
78// subsystem carries no crypto dependency onto console toolchains.
79POLYPHASE_API void WsSha1(const uint8_t* data, uint32_t size, uint8_t outDigest[20]);
80POLYPHASE_API std::string WsBase64Encode(const uint8_t* data, uint32_t size);
81
82// xorshift32 PRNG. Used for mask keys and the handshake nonce only.
83POLYPHASE_API uint32_t WsRandom32();
Export macros for Polyphase Engine symbols.
#define POLYPHASE_API
Definition PolyphaseAPI.h:31
POLYPHASE_API void WsEncodeFrame(uint8_t opcode, const uint8_t *payload, uint32_t size, std::vector< uint8_t > &outBytes)
Definition WebSocketFraming.cpp:47
POLYPHASE_API uint32_t WsRandom32()
Definition WebSocketFraming.cpp:35
POLYPHASE_API void WsSha1(const uint8_t *data, uint32_t size, uint8_t outDigest[20])
Definition WebSocketFraming.cpp:351
POLYPHASE_API void WsEncodeClose(uint16_t code, const char *reason, std::vector< uint8_t > &outBytes)
Definition WebSocketFraming.cpp:96
POLYPHASE_API std::string WsBase64Encode(const uint8_t *data, uint32_t size)
Definition WebSocketFraming.cpp:430
WsError
Definition WebSocketTypes.h:25
#define WS_MAX_MESSAGE_BYTES
Definition WebSocketTypes.h:46
Definition WebSocketFraming.h:30
bool HasError() const
Definition WebSocketFraming.h:56
void SetMaxMessageBytes(uint32_t bytes)
Definition WebSocketFraming.h:54
const std::string & GetErrorMessage() const
Definition WebSocketFraming.h:58
WsError GetError() const
Definition WebSocketFraming.h:57
Definition WebSocketFraming.h:34
std::string mCloseReason
Definition WebSocketFraming.h:48
Type
Definition WebSocketFraming.h:36
@ Message
Definition WebSocketFraming.h:38
@ Pong
Definition WebSocketFraming.h:40
@ Ping
Definition WebSocketFraming.h:39
std::vector< uint8_t > mData
Definition WebSocketFraming.h:45