Tamer
C++ language extensions for event-driven programming
Loading...
Searching...
No Matches
websocket.hh
1#ifndef TAMER_WEBSOCKET_HH
2#define TAMER_WEBSOCKET_HH 1
3#include "http.hh"
4namespace tamer {
5
6class websocket_handshake {
7 public:
8 static bool request(http_message& req, std::string& key);
9 static bool is_request(const http_message& req);
10 static bool response(http_message& resp, const http_message& req);
11 static bool is_response(const http_message& resp, const std::string& key);
12};
13
14enum websocket_opcode {
15 WEBSOCKET_CONTINUATION = 0,
16 WEBSOCKET_TEXT = 1,
17 WEBSOCKET_BINARY = 2,
18 WEBSOCKET_CLOSE = 8,
19 WEBSOCKET_PING = 9,
20 WEBSOCKET_PONG = 10
21};
22
23class websocket_message {
24 public:
25 inline websocket_message();
26
27 inline bool ok() const;
28 inline bool operator!() const;
29 inline enum http_errno error() const;
30
31 inline enum websocket_opcode opcode() const;
32 inline bool text() const;
33 inline bool binary() const;
34 inline bool incomplete() const;
35 inline const std::string& body() const;
36 inline std::string& body();
37
38 inline websocket_message& clear();
39
40 inline websocket_message& error(enum http_errno e);
41 inline websocket_message& incomplete(bool c);
42 inline websocket_message& opcode(enum websocket_opcode o);
43 inline websocket_message& body(std::string body);
44 inline websocket_message& text(std::string body);
45 inline websocket_message& binary(std::string body);
46
47 private:
48 unsigned error_ : 8;
49 unsigned incomplete_ : 4;
50 unsigned opcode_ : 4;
51 std::string body_;
52};
53
54class websocket_parser : public tamed_class {
55 public:
56 inline websocket_parser(enum http_parser_type type);
57
58 inline bool ok() const;
59 inline bool operator!() const;
60 inline bool closed() const;
61
62 void receive_any(fd f, websocket_message& ctrl, websocket_message& data, event<int> done);
63 void receive(fd f, event<websocket_message> done);
64 void send(fd f, websocket_message m, event<> done);
65 inline void send_text(fd f, std::string text, event<> done);
66 inline void send_binary(fd f, std::string text, event<> done);
67 void close(fd f, uint16_t code, std::string reason, event<> done);
68 inline void close(fd f, uint16_t code, event<> done);
69
70 private:
71 enum http_parser_type type_;
72 uint8_t closed_;
73 uint16_t close_code_;
74 std::string close_reason_;
75
76 class closure__receive_any__2fdR17websocket_messageR17websocket_messageQi_;
77 void receive_any(closure__receive_any__2fdR17websocket_messageR17websocket_messageQi_&);
78 class closure__receive__2fdQ17websocket_message_;
79 void receive(closure__receive__2fdQ17websocket_message_&);
80 class closure__send__2fd17websocket_messageQ_;
81 void send(closure__send__2fd17websocket_messageQ_&);
82};
83
84inline websocket_message::websocket_message()
85 : error_(0), incomplete_(0), opcode_(0) {
86}
87
88inline bool websocket_message::ok() const {
89 return error_ == 0;
90}
91
92inline bool websocket_message::operator!() const {
93 return !ok();
94}
95
96inline enum http_errno websocket_message::error() const {
97 return http_errno(error_);
98}
99
100inline enum websocket_opcode websocket_message::opcode() const {
101 return websocket_opcode(opcode_);
102}
103
104inline bool websocket_message::text() const {
105 return websocket_opcode(opcode_) == WEBSOCKET_TEXT;
106}
107
108inline bool websocket_message::binary() const {
109 return websocket_opcode(opcode_) == WEBSOCKET_BINARY;
110}
111
112inline bool websocket_message::incomplete() const {
113 return incomplete_;
114}
115
116inline const std::string& websocket_message::body() const {
117 return body_;
118}
119
120inline std::string& websocket_message::body() {
121 return body_;
122}
123
124inline websocket_message& websocket_message::clear() {
125 error_ = 0;
126 incomplete_ = 0;
127 opcode_ = 0;
128 body_.clear();
129 return *this;
130}
131
132inline websocket_message& websocket_message::error(enum http_errno e) {
133 error_ = e;
134 return *this;
135}
136
137inline websocket_message& websocket_message::incomplete(bool c) {
138 incomplete_ = c;
139 return *this;
140}
141
142inline websocket_message& websocket_message::opcode(enum websocket_opcode o) {
143 opcode_ = o;
144 return *this;
145}
146
147inline websocket_message& websocket_message::body(std::string s) {
148 body_ = TAMER_MOVE(s);
149 return *this;
150}
151
152inline websocket_message& websocket_message::text(std::string s) {
153 opcode_ = WEBSOCKET_TEXT;
154 body_ = TAMER_MOVE(s);
155 return *this;
156}
157
158inline websocket_message& websocket_message::binary(std::string s) {
159 opcode_ = WEBSOCKET_BINARY;
160 body_ = TAMER_MOVE(s);
161 return *this;
162}
163
164inline websocket_parser::websocket_parser(enum http_parser_type type)
165 : type_(type), closed_(0), close_code_(0) {
166}
167
168inline bool websocket_parser::ok() const {
169 return !closed_;
170}
171
172inline bool websocket_parser::operator!() const {
173 return closed_;
174}
175
176inline bool websocket_parser::closed() const {
177 return closed_;
178}
179
180inline void websocket_parser::send_text(fd f, std::string s, event<> done) {
181 send(f, websocket_message().text(s), done);
182}
183
184inline void websocket_parser::send_binary(fd f, std::string s, event<> done) {
185 send(f, websocket_message().binary(s), done);
186}
187
188inline void websocket_parser::close(fd f, uint16_t close_code, event<> done) {
189 close(f, close_code, std::string(), done);
190}
191
192}
193#endif
Definition rendezvous.hh:463
Namespace containing public Tamer classes and functions for the Tamer core.
Definition adapter.hh:17