-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserverlink.cpp
More file actions
231 lines (165 loc) · 6.68 KB
/
serverlink.cpp
File metadata and controls
231 lines (165 loc) · 6.68 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
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
#include "serverlink.h"
#include "logging.h"
#include "settings.h"
#include "authentification.h"
#include <QDataStream>
#include <QJsonDocument>
#include <QJsonObject>
namespace Pico{
namespace Server{
Pico::Logging::Funcs logging;
Pico::Settings::FD_CONFIG FDC;
Funcs::Funcs(QObject *parent) :
QObject(parent)
{
///
/// Connecting the wires for async system
///
qDebug() << connect(&socket,
SIGNAL(connected()),
SLOT(OnConnected()));
qDebug() << connect(&socket,
SIGNAL(error(QAbstractSocket::SocketError)),
SLOT(OnSocketError(QAbstractSocket::SocketError)));
qDebug() << connect(&socket,
SIGNAL(readyRead()),
SLOT(OnReceivedData()));
qDebug() << connect(&socket,
SIGNAL(disconnected()),
SLOT(OnClosedConnection()));
logging.Write("INITIALIZE_CONNECTION => Connecting to "+QString::fromStdString(FDC.SERVER_ADDRESS)+":"+QString::number(FDC.SERVER_PORT)+"...");
socket.connectToHost(QString::fromStdString(FDC.SERVER_ADDRESS), (quint16) FDC.SERVER_PORT);
socket.setSocketOption(QAbstractSocket::KeepAliveOption, 1);
}
bool Funcs::IsConnected(){
return (socket.state() == QTcpSocket::ConnectedState);
}
void Funcs::CloseConnection(QString errorMsg){
socket.close();
if (errorMsg.length() >0){
emit StatusChanged("Closed connection : "+errorMsg);
}
logging.Write("CLOSE_CONNECTION => Connection closed with error message : "+errorMsg+".");
}
void Funcs::SendData(QString data){
QTcpSocket* socketPointer = &socket;
QDataStream socketStream{socketPointer};
socketStream.setVersion(QDataStream::Qt_4_2);
socketStream << (quint32) data.size()*2 + 4 << data;
socket.flush();
logging.Write("SEND_DATA => Data sent");
}
void Funcs::OnSocketError(QAbstractSocket::SocketError error){
logging.Write("ON_SOCKET_ERROR => Error : "+socket.errorString());
}
void Funcs::OnReceivedData(){
QString msg = IsolateMessage();
if (msg.length() > 0){
//logging.Write(QString("ON_RECEIVED_DATA => Received non-empty message [") + QString::number(msg.length()) + QString("]: ") + msg);
}
else{
return;
}
if (msg == "PING"){
logging.Write("ON_RECEIVED_DATA => Pong-ing back.");
SendData("PONG");
}
QJsonObject jsonMsg = JsonDecode(msg);
QString command = GetCommand(jsonMsg);
if (command == "session"){
LogIn(jsonMsg);
}
else if (command == "authentication_failed"){
QString errorMsg = jsonMsg.value("text").toString();
CloseConnection(errorMsg);
}
else if (command == "game_info"){
QString state = jsonMsg.value("state").toString();
int key = jsonMsg.value("uid").toInt();
if (state == "open"){
emit AddGame(key, jsonMsg);
}
else{
emit DeleteGame(key);
}
}
}
void Funcs::OnClosedConnection(){
emit Disconnected();
logging.Write("ON_CLOSED_CONNECTION => Connection closed.");
}
void Funcs::OnConnected(){
logging.Write("ON_CONNECTED => Connected to "+QString::fromStdString(FDC.SERVER_ADDRESS)+"!");
emit Connected();
emit StatusChanged("Connected to the server");
QString helloMessage = MakeHelloMessage();
SendData(helloMessage);
logging.Write(QString("ON_CONNECTED => Sending ") + helloMessage);
}
////
/// LOG IN
////
void Funcs::LogIn(QJsonObject serverData){
double sessionId = serverData.value("session").toDouble();
logging.Write("LOG_IN => Generating UID...");
QString sessionUid = Pico::Auth::GetUid(sessionId);
if (sessionUid == "-1"){
logging.Write(QString::fromStdString("LOG_IN => COULD NOT GENERATE UID. CLOSING CONNECTION."));
emit StatusChanged("Could not generate UID. Closed connection");
CloseConnection();
}
QJsonObject responseObject{
{"command", "hello"},
{"login", QString::fromStdString(FDC.LOGIN)},
{"unique_id", sessionUid},
{"session", sessionId},
{"password", QString::fromStdString(FDC.HASHWORD)} };
QJsonDocument responseDocument(responseObject);
SendData(QString(responseDocument.toJson()));
emit StatusChanged("Logged in");
}
////
/// Returns wellformed hello message
////
QString Funcs::MakeHelloMessage(){
QJsonObject helloObject{
{"command", "ask_session"},
{"version", QString::fromStdString(Pico::Settings::HC_CONFIG.CLIENT_VERSION)},
{"user_agent", "faf-client"}
};
QJsonDocument helloDocument(helloObject);
QString helloMessage = helloDocument.toJson();
return helloMessage;
}
////
/// Isolates COMMAND from the servers gibberish
////
QString Funcs::GetCommand(QJsonObject serverData){
QString command = serverData.value("command").toString();
return command;
}
////
/// Json string to QJsonObject
////
QJsonObject Funcs::JsonDecode(QString jsonString){
QJsonDocument jsonDocument = QJsonDocument::fromJson(jsonString.toLocal8Bit());
QJsonObject jsonObject = jsonDocument.object();
return jsonObject;
}
///
/// Given the socket is readable, return its content as a string.
///
QString Funcs::IsolateMessage(){
QString msg;
if(socket.bytesAvailable() >= 4){
QTcpSocket* socketPointer = &socket;
QDataStream socketStream{socketPointer};
socketStream.setVersion(QDataStream::Qt_4_2);
quint32 bs;
socketStream >> bs;
socketStream >> msg;
}
return msg;
}
}
}