-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebsocketProtocolParser.h
68 lines (55 loc) · 2.01 KB
/
WebsocketProtocolParser.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//
// Created by Administrator on 2019/9/23 0023.
//
#ifndef WEBSOCKET_WEBSOCKETPROTOCOLPARSER_H
#define WEBSOCKET_WEBSOCKETPROTOCOLPARSER_H
#include "HttpProtocolParser.h"
#include "EventCallback.h"
#include "HttpResponse.h"
#include "MessageHandler.h"
#include "WebsocketResponse.h"
class WebsocketProtocolParser : public HttpProtocolParser, public EventCallback {
protected:
string header_upgrade;
string header_connection;
string header_sec_webSocket_key;
string header_sec_webSocket_protocol;
string header_sec_webSocket_version;
string header_host;
string header_origin;
bool handshake_finished;//check if handshake is finished
HttpResponse response;//used for handshake with client
string guid = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
bool message_fragmented = false;//Check if message has been fragmented,because message is too large
int64_t message_length;//pending message size
bool message_length_calculated = false;
bool is_last_message = false;
ushort op_code;
bool is_mask;
ushort payload_length_type;
string mask_key;
bool base_parsed = false;
bool mask_key_parsed = false;
int last_parse_index = 0;
string last_message;
MessageHandler *message_handler;
WebsocketResponse websocket_response;
EventLoop *event_loop;
bool is_handler_switched = false;
void ProcessHttpHeader(string &name, string &value) override;
bool Handle(bool socket_should_close, void *event_loop) override;
void ShakeHandWithClient(EventLoop *eventLoop);
bool CheckHttpProtocol();
string CalculateKey();
bool ProcessClientMessage();
void ProxyMessage();
void SendMessageToClient(string &resp);
public:
explicit WebsocketProtocolParser(int socket, MessageHandler *handler) : HttpProtocolParser(socket) {
handshake_finished = false;
message_handler = handler;
response.BindSocket(socket);
websocket_response.BindSocket(socket);
}
};
#endif //WEBSOCKET_WEBSOCKETPROTOCOLPARSER_H