-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtTcpSocket.cpp
182 lines (154 loc) · 5.31 KB
/
tTcpSocket.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
#include "tTcpSocket.h"
#include <QDebug>
#include <QDataStream>
static inline qint32 ArrayToInt(QByteArray source);
static inline QByteArray IntToArray(qint32 source);
tTcpSocket::tTcpSocket(QObject *parent) :
QObject(parent)
{
this->socket = NULL;
this->server = NULL;
}
bool tTcpSocket::connectToHost(const QHostAddress &addr, quint16 port)
{
this->socket = new QTcpSocket(this);
connect(this->socket, SIGNAL(connected()), this, SLOT(connected()));
connect(this->socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
connect(this->socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
QByteArray *buffer = new QByteArray();
qint32 *s = new qint32(0);
buffers.insert(socket, buffer);
sizes.insert(socket, s);
qDebug() << "Connecting to " << addr.toString() << ":" << port;
// this is not blocking call
this->socket->connectToHost(addr, port);
return this->socket->waitForConnected(3000);
}
void tTcpSocket::listenSocket(const QHostAddress &addr, quint16 port)
{
this->server = new QTcpServer(this);
connect(this->server, SIGNAL(newConnection()), this, SLOT(newConnection()));
this->server->listen(addr, port);
qDebug() << "Listening...";
}
void tTcpSocket::newConnection()
{
while (this->server->hasPendingConnections())
{
this->socket = server->nextPendingConnection();
connect(this->socket, SIGNAL(readyRead()), SLOT(readyRead()));
connect(this->socket, SIGNAL(disconnected()), SLOT(disconnected()));
QByteArray *buffer = new QByteArray();
qint32 *s = new qint32(0);
buffers.insert(this->socket, buffer);
sizes.insert(this->socket, s);
qDebug() << "New Connection...";
}
}
void tTcpSocket::writeData(QByteArray data)
{
if(this->socket->state() == QAbstractSocket::ConnectedState)
{
this->socket->write(IntToArray(data.size())); //write size of data
this->socket->write(data); //write the data itself
this->socket->flush();
qDebug() << "Write Data: " << data;
}
}
void tTcpSocket::disconnect()
{
if (this->socket != NULL)
this->socket->close();
if (this->server != NULL)
this->server->close();
}
void tTcpSocket::connected()
{
qDebug() << "connected...";
}
void tTcpSocket::disconnected()
{
QTcpSocket *socket_ = static_cast<QTcpSocket*>(sender());
QByteArray *buffer_ = buffers.value(socket_);
qint32 *s = sizes.value(socket_);
socket_->deleteLater();
delete buffer_;
delete s;
qDebug() << "disconnected...";
}
void tTcpSocket::readyRead()
{
QTcpSocket *socket_ = static_cast<QTcpSocket*>(sender());
QByteArray *buffer = buffers.value(socket_);
qint32 *s = sizes.value(socket_);
qint32 size = *s;
qDebug() << "ready Read..." << socket_->bytesAvailable() << " " << this->socket->bytesAvailable();
while (socket_->bytesAvailable() > 0)
{
buffer->append(socket_->readAll());
while ((size == 0 && buffer->size() >= 4) || (size > 0 && buffer->size() >= size)) //While can process data, process it
{
if (size == 0 && buffer->size() >= 4) //if size of data has received completely, then store it on our global variable
{
size = ArrayToInt(buffer->mid(0, 4));
*s = size;
buffer->remove(0, 4);
}
if (size > 0 && buffer->size() >= size) // If data has received completely, then emit our SIGNAL with the data
{
QByteArray data = buffer->mid(0, size);
buffer->remove(0, size);
size = 0;
*s = size;
qDebug() << "emit dataReceived()...";
emit dataReceived(data);
}
}
}
}
bool tTcpSocket::readData(QByteArray *data_)
{
QTcpSocket *socket_ = static_cast<QTcpSocket*>(sender());
QByteArray *buffer = buffers.value(socket_);
qint32 *s = sizes.value(socket_);
qint32 size = *s;
qDebug() << "Read data, byte available: " << socket_->bytesAvailable();
if (socket_->bytesAvailable() > 0)
{
buffer->append(socket_->readAll());
while ((size == 0 && buffer->size() >= 4) || (size > 0 && buffer->size() >= size)) //While can process data, process it
{
if (size == 0 && buffer->size() >= 4) //if size of data has received completely, then store it on our global variable
{
size = ArrayToInt(buffer->mid(0, 4));
*s = size;
buffer->remove(0, 4);
}
if (size > 0 && buffer->size() >= size) // If data has received completely, then emit our SIGNAL with the data
{
QByteArray data = buffer->mid(0, size);
buffer->remove(0, size);
size = 0;
*s = size;
*data_ = data;
return true;
}
}
}
return false;
}
qint32 ArrayToInt(QByteArray source)
{
qint32 temp;
QDataStream data(&source, QIODevice::ReadWrite);
data >> temp;
return temp;
}
QByteArray IntToArray(qint32 source) //Use qint32 to ensure that the number have 4 bytes
{
//Avoid use of cast, this is the Qt way to serialize objects
QByteArray temp;
QDataStream data(&temp, QIODevice::ReadWrite);
data << source;
return temp;
}