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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
/*************************************************************************/
/* lws_server.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#ifndef JAVASCRIPT_ENABLED
#include "lws_server.h"
#include "core/os/os.h"
#include "core/project_settings.h"
Error LWSServer::listen(int p_port, PoolVector<String> p_protocols, bool gd_mp_api) {
ERR_FAIL_COND_V(context != NULL, FAILED);
_is_multiplayer = gd_mp_api;
struct lws_context_creation_info info;
memset(&info, 0, sizeof info);
// Prepare lws protocol structs
_lws_make_protocols(this, &LWSServer::_lws_gd_callback, p_protocols, &_lws_ref);
info.port = p_port;
info.user = _lws_ref;
info.protocols = _lws_ref->lws_structs;
info.gid = -1;
info.uid = -1;
//info.ws_ping_pong_interval = 5;
context = lws_create_context(&info);
if (context == NULL) {
_lws_free_ref(_lws_ref);
_lws_ref = NULL;
ERR_EXPLAIN("Unable to create LWS context");
ERR_FAIL_V(FAILED);
}
return OK;
}
bool LWSServer::is_listening() const {
return context != NULL;
}
int LWSServer::get_max_packet_size() const {
return (1 << _out_buf_size) - PROTO_SIZE;
}
int LWSServer::_handle_cb(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len) {
LWSPeer::PeerData *peer_data = (LWSPeer::PeerData *)user;
switch (reason) {
case LWS_CALLBACK_HTTP:
// no http for now
// closing immediately returning -1;
return -1;
case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION:
// check header here?
break;
case LWS_CALLBACK_ESTABLISHED: {
int32_t id = _gen_unique_id();
Ref<LWSPeer> peer = Ref<LWSPeer>(memnew(LWSPeer));
peer->set_wsi(wsi, _in_buf_size, _in_pkt_size, _out_buf_size, _out_pkt_size);
_peer_map[id] = peer;
peer_data->peer_id = id;
peer_data->force_close = false;
peer_data->clean_close = false;
_on_connect(id, lws_get_protocol(wsi)->name);
break;
}
case LWS_CALLBACK_WS_PEER_INITIATED_CLOSE: {
if (peer_data == NULL)
return 0;
int32_t id = peer_data->peer_id;
if (_peer_map.has(id)) {
int code;
Ref<LWSPeer> peer = _peer_map[id];
String reason2 = peer->get_close_reason(in, len, code);
peer_data->clean_close = true;
_on_close_request(id, code, reason2);
}
return 0;
}
case LWS_CALLBACK_CLOSED: {
if (peer_data == NULL)
return 0;
int32_t id = peer_data->peer_id;
bool clean = peer_data->clean_close;
if (_peer_map.has(id)) {
_peer_map[id]->close();
_peer_map.erase(id);
}
_on_disconnect(id, clean);
return 0; // we can end here
}
case LWS_CALLBACK_RECEIVE: {
int32_t id = peer_data->peer_id;
if (_peer_map.has(id)) {
static_cast<Ref<LWSPeer> >(_peer_map[id])->read_wsi(in, len);
if (_peer_map[id]->get_available_packet_count() > 0)
_on_peer_packet(id);
}
break;
}
case LWS_CALLBACK_SERVER_WRITEABLE: {
int id = peer_data->peer_id;
if (peer_data->force_close) {
if (_peer_map.has(id)) {
Ref<LWSPeer> peer = _peer_map[id];
peer->send_close_status(wsi);
}
return -1;
}
if (_peer_map.has(id))
static_cast<Ref<LWSPeer> >(_peer_map[id])->write_wsi();
break;
}
default:
break;
}
return 0;
}
void LWSServer::stop() {
if (context == NULL)
return;
_peer_map.clear();
destroy_context();
context = NULL;
}
bool LWSServer::has_peer(int p_id) const {
return _peer_map.has(p_id);
}
Ref<WebSocketPeer> LWSServer::get_peer(int p_id) const {
ERR_FAIL_COND_V(!has_peer(p_id), NULL);
return _peer_map[p_id];
}
IP_Address LWSServer::get_peer_address(int p_peer_id) const {
ERR_FAIL_COND_V(!has_peer(p_peer_id), IP_Address());
return _peer_map[p_peer_id]->get_connected_host();
}
int LWSServer::get_peer_port(int p_peer_id) const {
ERR_FAIL_COND_V(!has_peer(p_peer_id), 0);
return _peer_map[p_peer_id]->get_connected_port();
}
void LWSServer::disconnect_peer(int p_peer_id, int p_code, String p_reason) {
ERR_FAIL_COND(!has_peer(p_peer_id));
get_peer(p_peer_id)->close(p_code, p_reason);
}
Error LWSServer::set_buffers(int p_in_buffer, int p_in_packets, int p_out_buffer, int p_out_packets) {
ERR_EXPLAIN("Buffers sizes can only be set before listening or connecting");
ERR_FAIL_COND_V(context != NULL, FAILED);
_in_buf_size = nearest_shift(p_in_buffer - 1) + 10;
_in_pkt_size = nearest_shift(p_in_packets - 1);
_out_buf_size = nearest_shift(p_out_buffer - 1) + 10;
_out_pkt_size = nearest_shift(p_out_packets - 1);
return OK;
}
LWSServer::LWSServer() {
_in_buf_size = nearest_shift((int)GLOBAL_GET(WSS_IN_BUF) - 1) + 10;
_in_pkt_size = nearest_shift((int)GLOBAL_GET(WSS_IN_PKT) - 1);
_out_buf_size = nearest_shift((int)GLOBAL_GET(WSS_OUT_BUF) - 1) + 10;
_out_pkt_size = nearest_shift((int)GLOBAL_GET(WSS_OUT_PKT) - 1);
context = NULL;
_lws_ref = NULL;
}
LWSServer::~LWSServer() {
invalidate_lws_ref(); // we do not want any more callbacks
stop();
}
#endif // JAVASCRIPT_ENABLED
|