forked from networknt/microservices-framework-benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHub.cpp
More file actions
159 lines (130 loc) · 5.53 KB
/
Hub.cpp
File metadata and controls
159 lines (130 loc) · 5.53 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "Hub.h"
#include "HTTPSocket.h"
#include <openssl/sha.h>
static const int INFLATE_LESS_THAN_ROUGHLY = 16777216;
namespace uWS {
char *Hub::inflate(char *data, size_t &length) {
dynamicInflationBuffer.clear();
inflationStream.next_in = (Bytef *) data;
inflationStream.avail_in = length;
int err;
do {
inflationStream.next_out = (Bytef *) inflationBuffer;
inflationStream.avail_out = LARGE_BUFFER_SIZE;
err = ::inflate(&inflationStream, Z_FINISH);
if (!inflationStream.avail_in) {
break;
}
dynamicInflationBuffer.append(inflationBuffer, LARGE_BUFFER_SIZE - inflationStream.avail_out);
} while (err == Z_BUF_ERROR && dynamicInflationBuffer.length() <= INFLATE_LESS_THAN_ROUGHLY);
inflateReset(&inflationStream);
if ((err != Z_BUF_ERROR && err != Z_OK) || dynamicInflationBuffer.length() > INFLATE_LESS_THAN_ROUGHLY) {
length = 0;
return nullptr;
}
if (dynamicInflationBuffer.length()) {
dynamicInflationBuffer.append(inflationBuffer, LARGE_BUFFER_SIZE - inflationStream.avail_out);
length = dynamicInflationBuffer.length();
return (char *) dynamicInflationBuffer.data();
}
length = LARGE_BUFFER_SIZE - inflationStream.avail_out;
return inflationBuffer;
}
void Hub::onServerAccept(uS::Socket s) {
uS::SocketData *socketData = s.getSocketData();
s.enterState<HttpSocket<SERVER>>(new HttpSocket<SERVER>::Data(socketData));
((Group<SERVER> *) socketData->nodeData)->addHttpSocket(s);
((Group<SERVER> *) socketData->nodeData)->httpConnectionHandler(s);
s.setNoDelay(true);
delete socketData;
}
void Hub::onClientConnection(uS::Socket s, bool error) {
HttpSocket<CLIENT>::Data *httpSocketData = (HttpSocket<CLIENT>::Data *) s.getSocketData();
if (error) {
((Group<CLIENT> *) httpSocketData->nodeData)->errorHandler(httpSocketData->httpUser);
delete httpSocketData;
} else {
s.enterState<HttpSocket<CLIENT>>(s.getSocketData());
HttpSocket<CLIENT>(s).upgrade(nullptr, nullptr, 0, nullptr, 0, nullptr);
}
}
bool Hub::listen(int port, uS::TLS::Context sslContext, int options, Group<SERVER> *eh) {
if (!eh) {
eh = (Group<SERVER> *) this;
}
if (uS::Node::listen<onServerAccept>(port, sslContext, options, (uS::NodeData *) eh, nullptr)) {
eh->errorHandler(port);
return false;
}
return true;
}
void Hub::connect(std::string uri, void *user, int timeoutMs, Group<CLIENT> *eh, std::string subprotocol) {
if (!eh) {
eh = (Group<CLIENT> *) this;
}
size_t offset = 0;
std::string protocol = uri.substr(offset, uri.find("://")), hostname, portStr, path;
if ((offset += protocol.length() + 3) < uri.length()) {
hostname = uri.substr(offset, uri.find_first_of(":/", offset) - offset);
offset += hostname.length();
if (uri[offset] == ':') {
offset++;
portStr = uri.substr(offset, uri.find("/", offset) - offset);
}
offset += portStr.length();
if (uri[offset] == '/') {
path = uri.substr(++offset);
}
}
if (hostname.length()) {
int port = 80;
bool secure = false;
if (protocol == "wss") {
port = 443;
secure = true;
} else if (protocol != "ws") {
eh->errorHandler(user);
}
if (portStr.length()) {
port = stoi(portStr);
}
uS::SocketData socketData((uS::NodeData *) eh);
HttpSocket<CLIENT>::Data *httpSocketData = new HttpSocket<CLIENT>::Data(&socketData);
std::string optionalSubprotocol;
if (!subprotocol.empty()) {
optionalSubprotocol = "Sec-WebSocket-Protocol: " + subprotocol + "\r\n";
}
httpSocketData->httpUser = user;
httpSocketData->httpBuffer = "GET /" + path + " HTTP/1.1\r\n"
"Upgrade: websocket\r\n"
"Connection: Upgrade\r\n"
"Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\n"
"Host: " + hostname + "\r\n"
+ optionalSubprotocol +
"Sec-WebSocket-Version: 13\r\n\r\n";
uS::Socket s = uS::Node::connect<onClientConnection>(hostname.c_str(), port, secure, httpSocketData);
if (s) {
s.startTimeout<HttpSocket<CLIENT>::onEnd>(timeoutMs);
// getGroup<CLIENT>(s)->addHttpSocket(s);
}
} else {
eh->errorHandler(user);
}
}
void Hub::upgrade(uv_os_sock_t fd, const char *secKey, SSL *ssl, const char *extensions, size_t extensionsLength, const char *subprotocol, size_t subprotocolLength, Group<SERVER> *serverGroup) {
if (!serverGroup) {
serverGroup = &getDefaultGroup<SERVER>();
}
uS::Socket s = uS::Socket::init((uS::NodeData *) serverGroup, fd, ssl);
uS::SocketData *socketData = s.getSocketData();
HttpSocket<SERVER>::Data *temporaryHttpData = new HttpSocket<SERVER>::Data(socketData);
delete socketData;
s.enterState<HttpSocket<SERVER>>(temporaryHttpData);
bool perMessageDeflate;
HttpSocket<SERVER>(s).upgrade(secKey, extensions, extensionsLength, subprotocol, subprotocolLength, &perMessageDeflate);
s.enterState<WebSocket<SERVER>>(new WebSocket<SERVER>::Data(perMessageDeflate, s.getSocketData()));
serverGroup->addWebSocket(s);
serverGroup->connectionHandler(WebSocket<SERVER>(s), HttpRequest({}));
delete temporaryHttpData;
}
}