-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonsockettestserver.h
142 lines (107 loc) · 3.32 KB
/
jsonsockettestserver.h
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
#ifndef JSONSOCKETTESTSERVER_H
#define JSONSOCKETTESTSERVER_H
#include <QObject>
#include <QTcpServer>
#include "Dependency/qt_json_socket_lib.h"
#include <QDebug>
class TestServer: public QObject
{
Q_OBJECT
public:
TestServer()
{
server = QSharedPointer<QTcpServer>::create();
connect(server.data(), SIGNAL(newConnection()), this, SLOT(onConnect()));
}
~TestServer()
{
server.clear();
disconnect(server.data(), SIGNAL(newConnection()), this, SLOT(onConnect()));
}
QSharedPointer<QTcpSocket> GetSocket()
{
return socket;
}
signals:
void OnConnectSignal();
void OnRecvSignal(QByteArray);
void OnSendSingal();
void OnDisconnectSignal();
public slots:
inline void onStart()
{
server->listen(QHostAddress::Any, 7071);
}
inline void onFinished()
{
if(socket.isNull() == false)
{
socket->disconnectFromHost();
socket.clear();
}
server->close();
}
inline void onConnect()
{
socket = QSharedPointer<QTcpSocket>(server->nextPendingConnection());
if(socket.isNull() )
{
qDebug()<<"QTCpSocket Alloc Error";
return;
}
connect(socket.data(), SIGNAL(readyRead()), this, SLOT(onRecv()));
connect(socket.data(), SIGNAL(disconnected()), this, SLOT(onDisconnect()));
emit OnConnectSignal();
qDebug()<<"Connect is clear";
}
inline void onRecv()
{
QByteArray ReadBase64Size = socket->read(sizeof(unsigned int));
qDebug()<<"Read Size : " + QString::number (ReadBase64Size.toInt());
unsigned int ReadSize = static_cast<unsigned int>(ReadBase64Size.toInt());
QByteArray Base64Source = socket->read(ReadSize);
emit OnRecvSignal(Base64Source);
if(Base64Source.isNull())
{
qDebug()<<"Can't Read Base64Source : " + socket->errorString();
return;
}
QByteArray ReadJsonSource = QByteArray::fromBase64(Base64Source);
if(ReadJsonSource.isEmpty())
{
qDebug()<<"[Debug] : Recv QByteArray is fail : " + socket->errorString();
return;
}
QJsonDocument RecvJsonDoc = QJsonDocument::fromJson(ReadJsonSource);
if(RecvJsonDoc.isEmpty())
{
qDebug()<<"[Debug] : Recv JsonDoc is fail : " + socket->errorString();
return;
}
RecvRequest = RecvJsonDoc.object();
if(RecvRequest.find("Reply") == RecvRequest.end())
{
qDebug()<<"[Debug] :RecvByteArray : " + RecvJsonDoc.toJson();
return;
}
if(RecvRequest["Reply"].toBool() == true)
{
qDebug()<<"[Debug] :Send Reply : " + RecvJsonDoc.toJson();
if (socket->write(QString::number(Base64Source.size()).toUtf8(), sizeof(unsigned int)) < 0
|| socket->write(Base64Source) < 0)
{
qDebug()<<"[Debug] : Socket Error" + socket->errorString();
}
emit OnSendSingal();
}
}
inline void onDisconnect()
{
emit OnDisconnectSignal();
}
private:
QJsonObject RecvRequest;
QSharedPointer<QTcpServer> server;
QSharedPointer<QTcpSocket> socket;
};
#endif // JSONSOCKETTESTSERVER_H