-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.cpp
441 lines (373 loc) · 13.8 KB
/
server.cpp
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#include "server.h"
Server::Server(const quint16 websocketport, const quint16 udpport)
{
_server = new QWebSocketServer("janus-server-c", QWebSocketServer::NonSecureMode, this);
connect(_server, SIGNAL(newConnection()), this, SLOT(newConnection()));
qDebug() << "JanusVR Presence Server (C++) v1.3 - process id" << QCoreApplication::applicationPid();
qDebug() << "Usage: ";
qDebug() << "\tjanus-server-c [-wsport x] [-udpport x]";
if (_server->listen(QHostAddress::Any, websocketport)) {
qDebug() << "Server::Server listening for WebSocket clients on" << _server->serverAddress() << _server->serverPort();
}
_udpport = udpport;
_udpsocket = new QUdpSocket();
if (_udpport > 0 && _udpsocket->bind(QHostAddress::Any, _udpport)) {
connect(_udpsocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
qDebug() << "Server::Server listening for UDP clients on" << _server->serverAddress() << udpport;
}
}
Server::~Server()
{
// qDebug() << "Server::~Server()";
if (_server) {
delete _server;
}
}
void Server::newConnection()
{
qDebug() << "Server::newConnection()";
// need to grab the socket
QWebSocket * socket = _server->nextPendingConnection();
if (socket) {
Session * session = new Session(socket);
connect(session, SIGNAL(socketConnected()), this, SLOT(connected()));
connect(session, SIGNAL(socketDisconnected()), this, SLOT(disconnected()));
connect(session, SIGNAL(socketBytesWritten(qint64)), this, SLOT(bytesWritten(qint64)));
connect(session, SIGNAL(socketTextMessageReceived(QString)), this, SLOT(onTextMessageReceived(QString)));
// _sessions.push_back(session);
qDebug() << "Server::newConnection() Client:" << socket->peerAddress() << socket->peerPort();
}
}
void Server::connected()
{
// qDebug() << "Server::connected() Sessions: " << _sessions.size() << " Rooms: " << _rooms.size();
}
void Server::disconnected()
{
Session * session = reinterpret_cast<Session *>(QObject::sender());
if (session) {
if (session->GetSocket()) {
qDebug() << "Server::disconnected()" << session->GetSocket()->peerAddress() << session->GetSocket()->peerPort();
}
//perform cleanup for this session
QString userId = session->GetId();
QJsonObject disconnectdata;
disconnectdata["userId"] = userId;
//broadcast out user_disconnected event to all users listening to roomId
BroadcastToRoom(session, "user_disconnected", disconnectdata);
_sessions.remove(userId); //remove connected userId
for (QVector <QPointer <Session> > & sessions : _rooms) { //remove session from listening to all roomId's
sessions.removeAll(session);
}
delete session;
// qDebug() << "Server::disconnected()" << _ipPortToUserId << _sessions;
// qDebug() << "Server::disconnected() Sessions: " << _sessions.size() << " Rooms: " << _rooms.size();
}
}
void Server::bytesWritten(qint64 )
{
// qDebug() << "Server::bytesWritten()" << bytes;
}
void Server::onTextMessageReceived(QString message)
{
Session * session = reinterpret_cast<Session *>(QObject::sender());
if (session) {
const QByteArray b = message.toLatin1();
ProcessMessage(session, b);
}
}
void Server::UpdateUDPPort(const QByteArray & b, quint16 senderPort)
{
QJsonDocument doc = QJsonDocument::fromJson(b);
QJsonObject o = doc.object();
if (!o.contains("method") || !o.contains("data")) {
qDebug(" Server::UpdateUDPPort - method or data need to be defined");
return;
}
const QJsonObject data = o["data"].toObject();
if (!data.contains("userId")) {
// qDebug() << " Server::UpdateUDPPort - userID needs to be defined within data";
return;
}
const QString userId = data["userId"].toString();
if (!_sessions.contains(userId) || _sessions[userId].isNull()) {
// qDebug() << " Server::UpdateUDPPort - no session for given userId" << userId;
return;
}
if (_sessions[userId]->GetUdpPort() == 0) {
qDebug() << "Server::UpdateUDPPort - updating" << userId << "UDP port to " << senderPort;
_sessions[userId]->SetUdpPort(senderPort);
}
}
void Server::ProcessMessage(Session * session, const QByteArray & b)
{
// get the root object
QJsonDocument doc = QJsonDocument::fromJson(b);
QJsonObject o = doc.object();
// qDebug() << " data:" << o;
if (!o.contains("method") || !o.contains("data")) {
qDebug(" Server::ProcessMessage - method or data need to be defined");
qDebug() << " Packet:" << b.left(256);
return;
}
const QString method = o["method"].toString();
const QJsonObject data = o["data"].toObject();
if (!data.contains("userId")) {
// qDebug() << " Server::ProcessMessage - userID needs to be defined within data";
return;
}
const QString userId = data["userId"].toString();
if (method == "logon" && session) {
//note: we do logon first, because we need to add it to sessions list
DoLogon(session, data);
return;
}
if (!_sessions.contains(userId)) {
// qDebug() << " Server::ProcessMessage - no active session with the supplied userId " << userId;
return;
}
else if (_sessions[userId].isNull()) {
// qDebug() << " Server::ProcessMessage - session null for given userId " << userId;
return;
}
QPointer <Session> s = _sessions[userId];
if (method == "subscribe") {
DoSubscribe(s, data);
}
else if (method == "unsubscribe") {
DoUnsubscribe(s, data);
}
else if (method == "enter_room") {
DoEnterRoom(s, data);
}
else if (method == "move") {
DoMove(s, data);
}
else if (method == "chat") {
DoChat(s, data);
}
else if (method == "portal") {
DoPortal(s, data);
}
else if (method == "users_online") {
DoUsersOnline(s, data);
}
else {
s->SendClientError("Unrecognized method");
}
}
void Server::readPendingDatagrams()
{
// qDebug() << "Server::readPendingDatagrams()";
while (_udpsocket->hasPendingDatagrams()) {
QByteArray datagram;
QHostAddress sender;
quint16 senderPort;
const int maxSize = int(_udpsocket->pendingDatagramSize());
datagram.resize(maxSize);
_udpsocket->readDatagram(datagram.data(), maxSize, &sender, &senderPort);
//v1.3 Update outbound UDP port to use for this session
UpdateUDPPort(datagram, senderPort);
ProcessMessage(nullptr, datagram);
}
}
void Server::DoLogon(Session * session, QJsonObject data)
{
// qDebug() << "Server::DoLogon()";
if (!data.contains("userId")) {
session->SendClientError("No userId specified in logon method");
}
else if (!data.contains("roomId")) {
session->SendClientError("No roomId specified in logon method");
}
else {
QString userId = data["userId"].toString();
QString roomId = data["roomId"].toString();
if (QRegExp("[^a-zA-Z0-9_]").indexIn(userId) >= 0) {
session->SendClientError("Illegal character in userId" + userId + ", only use alphanumeric and underscore");
}
else if (_sessions.contains(userId)) {
session->SendClientError("User name is already in use");
}
else {
//v1.3 - Note: only assign userId when user name is not already in use
session->SetId(userId);
session->SetRoomId(roomId);
_sessions[userId] = session;
//send our UDP port as well
QJsonObject data;
if (_udpport > 0) {
data["udp"] = _udpsocket->localPort();
}
session->SendData("okay", data);
qDebug() << "Server::DoLogon() User" << userId << "logged in. Sessions: " << _sessions.size() << " Rooms: " << _rooms.size();
}
}
}
void Server::DoSubscribe(Session * session, QJsonObject data)
{
//subscribe this session to events specified by roomId
// qDebug() << "Server::DoSubscribe()";
if (!data.contains("roomId")) {
session->SendClientError("No roomId specified in subscribe method");
}
else {
if (session) {
QString roomId = data["roomId"].toString();
_rooms[roomId].push_back(session);
session->SendData("okay");
}
}
}
void Server::DoUnsubscribe(Session *session, QJsonObject data)
{
// qDebug() << "Server::DoUnsubscribe()";
if (!data.contains("roomId")) {
session->SendClientError("No roomId specified in unsubscribe method");
}
else {
if (session) {
QString roomId = data["roomId"].toString();
_rooms[roomId].removeAll(session);
session->SendData("okay");
}
}
//Cleanup _rooms on unsubscribe
QList <QString> keys = _rooms.keys();
for (QString & s: keys) {
if (_rooms[s].isEmpty()) {
_rooms.remove(s);
}
else {
bool all_null = true;
for (int i=0; i<_rooms[s].size(); ++i) {
if (!_rooms[s][i].isNull()) {
all_null = false;
break;
}
}
if (all_null) {
_rooms.remove(s);
}
}
}
}
void Server::DoEnterRoom(Session *session, QJsonObject data)
{
// qDebug() << "Server::DoEnterRoom()";
if (!data.contains("roomId")) {
session->SendClientError("No roomId specified in enter_room method");
}
else {
QString oldRoomId = session->GetRoomId();
QString userId = session->GetId();
QJsonObject leave_data;
leave_data["roomId"] = oldRoomId;
leave_data["userId"] = userId;
BroadcastToRoom(session, "user_leave", leave_data);
QString roomId = data["roomId"].toString();
QJsonObject enter_data;
enter_data["roomId"] = roomId;
enter_data["userId"] = userId;
session->SetRoomId(roomId); //track internally (also need to subcribe to events?)
//we need to broadcast user_enter method to all users listening to roomId
BroadcastToRoom(session, "user_enter", enter_data);
}
}
void Server::DoMove(Session * session, QJsonObject data)
{
//broadcast this data to all sessions subscribed to events specified by roomId
// qDebug() << "Server::DoMove()";
QJsonObject movedata;
movedata["roomId"] = session->GetRoomId();
movedata["userId"] = session->GetId();
movedata["position"] = data;
//broadcast out user_moved event to all users listening to roomId
BroadcastToRoom(session, "user_moved", movedata);
}
void Server::DoChat(Session *session, QJsonObject data)
{
// qDebug() << "Server::DoChat()";
QJsonObject chatdata;
chatdata["roomId"] = session->GetRoomId();
chatdata["userId"] = session->GetId();
if (data.contains("message")) {
//broadcast out user_chat event to all users listening to roomId
chatdata["message"] = data["message"];
//PM a user, or broadcast to all
if (data.contains("toUserId")) {
BroadcastToUser(data["toUserId"].toString(), "user_chat", chatdata);
}
else {
BroadcastToRoom(session, "user_chat", chatdata);
}
}
}
void Server::DoPortal(Session *session, QJsonObject data)
{
// qDebug() << "Server::DoPortal()";
//broadcast out user_portal event to all users listening to roomId
data["roomId"] = session->GetRoomId();
data["userId"] = session->GetId();
session->SendData("okay");
BroadcastToRoom(session, "user_portal", data);
}
void Server::DoUsersOnline(Session *session, QJsonObject data)
{
// qDebug() << "Server::DoUsersOnline()";
if (!data.contains("roomId")) { //users online across all rooms
QStringList userIds = _sessions.keys();
QJsonObject data;
data["results"] = userIds.size();
data["users"] = QJsonArray::fromStringList(userIds);
session->SendData("users_online", data);
}
else { //users online in a specific room defined by roomId
QStringList userIds;
QString roomId = data["roomId"].toString();
QVector <QPointer <Session> > & sessions = _rooms[roomId];
for (QPointer <Session> & s : sessions) {
if (s) {
userIds.push_back(s->GetId());
}
}
QJsonObject data;
data["results"] = userIds.size();
data["users"] = QJsonArray::fromStringList(userIds);
data["roomId"] = roomId;
session->SendData("users_online", data);
}
}
void Server::BroadcastToUser(QString userId, const QString method, const QJsonObject data)
{
QJsonObject o;
o["method"] = method;
o["data"] = data;
QJsonDocument doc(o);
QByteArray b = doc.toJson(QJsonDocument::Compact) + "\n";
const bool udpPreferred = (method == "user_moved");
QPointer <Session> s;
if (_sessions.contains(userId) && _sessions[userId]) {
s = _sessions[userId];
}
if (s && s->GetSocket()) {
s->SendMessage(b, udpPreferred);
}
}
void Server::BroadcastToRoom(Session *session, const QString method, const QJsonObject data)
{
// qDebug() << "Server::BroadcastToRoom broadcasting" << method << "to" << session->GetRoomId();
QJsonObject o;
o["method"] = method;
o["data"] = data;
QJsonDocument doc(o);
QByteArray b = doc.toJson(QJsonDocument::Compact) + "\n";
const bool udpPreferred = (method == "user_moved");
QVector <QPointer <Session> > & sessions = _rooms[session->GetRoomId()];
for (QPointer <Session> & s : sessions) {
if (s && s->GetSocket() && s != session) {
// qDebug() << " broadcasting" << method << "to user" << s->GetId();
s->SendMessage(b, udpPreferred);
}
}
}