nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
websocket.h
Go to the documentation of this file.
1#ifndef NANOMMO_GATEWAY_WEBSOCKET_H_
2#define NANOMMO_GATEWAY_WEBSOCKET_H_
3
4#include <cstddef>
5#include <cstdint>
6#include <optional>
7#include <string>
8#include <string_view>
9
10#include "absl/status/status.h"
11#include "absl/status/statusor.h"
12
13namespace nanommo {
14
15// The server's side of WebSocket (RFC 6455): what a browser's page opens to
16// the gateway, which carries the game's own messages to the servers.
17
18/// What a client's opening handshake asks for.
19struct Handshake {
20 std::string path;
21 std::string key;
22};
23
24/// The client's opening handshake, an HTTP GET up to its blank line, or why it
25/// isn't one: the upgrade to websocket, version 13 and a key, as RFC 6455 has
26/// them.
27absl::StatusOr<Handshake> ParseHandshake(std::string_view request);
28
29/// The value of Sec-WebSocket-Accept for the client's key: the SHA-1 of the
30/// key and the protocol's GUID, in base 64, which shows the client that the
31/// server read its handshake.
32std::string AcceptKey(std::string_view key);
33
34/// The server's answer that switches the connection over to WebSocket.
35std::string Accept(std::string_view key);
36
37/// An HTTP answer that turns the connection down, with why in its body.
38std::string Refuse(int status, std::string_view why);
39
40enum class Opcode : uint8_t {
42 kText = 0x1,
43 kBinary = 0x2,
44 kClose = 0x8,
45 kPing = 0x9,
46 kPong = 0xA,
47};
48
49/// A whole message, or a ping, a pong or a close.
50struct Message {
52 std::string payload;
53};
54
55/// A frame as the server sends it: whole, and never masked.
56std::string ServerFrame(Opcode opcode, std::string_view payload);
57
58/// A frame as a client sends it, masked with the mask: the tests', and
59/// anything else that talks to the gateway as a browser would.
60std::string ClientFrame(Opcode opcode, std::string_view payload, uint32_t mask,
61 bool fin = true);
62
63/// Reads a client's frames, which each have to be masked, and puts each
64/// message together from its fragments. Control frames (ping, pong and close)
65/// come out as they arrive, between a message's fragments too.
67 public:
68 /// A message larger than this, all its fragments together, is refused.
69 explicit FrameDecoder(std::size_t max_message) : max_(max_message) {}
70
71 void Append(std::string_view bytes);
72
73 /// The next message, or nothing until more bytes come. After a frame that
74 /// breaks the protocol, returns nothing more, and status() says why.
75 std::optional<Message> Next();
76
77 const absl::Status& status() const { return status_; }
78
79 private:
80 std::optional<Message> Fail(std::string_view why);
81
82 std::size_t max_;
83 std::string buffer_;
84 std::optional<Opcode> fragmented_;
85 std::string fragments_;
86 absl::Status status_;
87};
88
89} // namespace nanommo
90
91#endif // NANOMMO_GATEWAY_WEBSOCKET_H_
const absl::Status & status() const
Definition websocket.h:77
std::optional< Message > Next()
Definition websocket.cc:183
FrameDecoder(std::size_t max_message)
A message larger than this, all its fragments together, is refused.
Definition websocket.h:69
void Append(std::string_view bytes)
Definition websocket.cc:170
Definition chat.cc:9
absl::StatusOr< Handshake > ParseHandshake(std::string_view request)
Definition websocket.cc:77
std::string ClientFrame(Opcode opcode, std::string_view payload, uint32_t mask, bool fin)
Definition websocket.cc:154
std::string Refuse(int status, std::string_view why)
An HTTP answer that turns the connection down, with why in its body.
Definition websocket.cc:139
std::string AcceptKey(std::string_view key)
Definition websocket.cc:123
std::string ServerFrame(Opcode opcode, std::string_view payload)
A frame as the server sends it: whole, and never masked.
Definition websocket.cc:145
std::string Accept(std::string_view key)
The server's answer that switches the connection over to WebSocket.
Definition websocket.cc:132
What a client's opening handshake asks for.
Definition websocket.h:19
std::string path
Definition websocket.h:20
std::string key
Definition websocket.h:21
A whole message, or a ping, a pong or a close.
Definition websocket.h:50
std::string payload
Definition websocket.h:52