-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsession.cpp
120 lines (99 loc) · 2.56 KB
/
session.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
#include "session.h"
Session::Session(QWebSocket * socket) :
_udpPort(0)
{
// qDebug() << "Session::Session";
_socket = socket;
connect(socket, SIGNAL(connected()),this, SLOT(connected()));
connect(socket, SIGNAL(disconnected()),this, SLOT(disconnected()));
connect(socket, SIGNAL(bytesWritten(qint64)),this, SLOT(bytesWritten(qint64)));
connect(socket, SIGNAL(textMessageReceived(QString)),this, SLOT(textMessageReceived(QString)));
_udpSocket = new QUdpSocket();
}
Session::~Session()
{
delete _udpSocket;
}
QWebSocket * Session::GetSocket()
{
return _socket;
}
void Session::SendClientError(const QString error)
{
if (_socket) {
QJsonObject o;
o["method"] = QString("error");
QJsonObject od;
od["message"] = error;
o["data"] = od;
QJsonDocument doc(o);
QByteArray b = doc.toJson(QJsonDocument::Compact) + "\n";
SendMessage(b, false);
qDebug() << "Session::SendClientError()" << error;
}
}
void Session::SendData(const QString method, const QJsonObject data)
{
if (_socket) {
QJsonObject o;
o["method"] = method;
o["data"] = data;
QJsonDocument doc(o);
QByteArray b = doc.toJson(QJsonDocument::Compact) + "\n";
SendMessage(b, false);
// qDebug() << "Session::SendData()" << b.size();
}
}
void Session::connected()
{
// qDebug() << "Session::connected()";
emit socketConnected();
}
void Session::disconnected()
{
// qDebug() << "Session::disconnected()";
emit socketDisconnected();
}
void Session::bytesWritten(qint64 bytes)
{
// qDebug() << "Session::bytesWritten()";
emit socketBytesWritten(bytes);
}
void Session::textMessageReceived(QString message)
{
emit socketTextMessageReceived(message);
}
void Session::SetId(QString id)
{
_id = id;
}
QString Session::GetId()
{
return _id;
}
void Session::SetRoomId(QString roomId)
{
_roomId = roomId;
}
QString Session::GetRoomId()
{
return _roomId;
}
void Session::SetUdpPort(const quint16 i)
{
_udpPort = i;
}
quint16 Session::GetUdpPort() const
{
return _udpPort;
}
void Session::SendMessage(const QByteArray b, const bool udpPreferred)
{
if (udpPreferred && _udpPort > 0 && b.size() <= 4096) {
// qDebug() << "Session::SendMessage sending UDP bytes to" << _socket->peerAddress() << _udpPort << ":" << _udpSocket->writeDatagram(b, _socket->peerAddress(), _udpPort);
_udpSocket->writeDatagram(b, _socket->peerAddress(), _udpPort);
}
else {
_socket->sendTextMessage(b);
}
}