-
Notifications
You must be signed in to change notification settings - Fork 0
/
myfortunethread.cpp
143 lines (125 loc) · 4 KB
/
myfortunethread.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
#include "myfortunethread.h"
MyFortuneThread::MyFortuneThread(int socketDescriptor, const QString &fortune, QObject *parent)
: QThread(parent), socketDescriptor(socketDescriptor), text(fortune)
{
/*QObject *parent) :
QThread(parent)*/
}
void MyFortuneThread::run()
{
stopped=0;
tcpSocket = new QTcpSocket();
//emit dataFromSocketSignal(QString::number(QThread::idealThreadCount()));
if (!tcpSocket->setSocketDescriptor(socketDescriptor)) {
emit error(tcpSocket->error());
return;
}
connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(slotStop()));
connect(tcpSocket, SIGNAL(readyRead()), this,SLOT(readyRead()));
if(modeSendRec){
QTextStream os(tcpSocket);
os.setAutoDetectUnicode(true);
os <<text;
}else {
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0); //Qt_4_0);
out << (quint16)0;
out << text;
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));
tcpSocket->write(block);
}
/* while (true) {
tcpSocket->waitForReadyRead(3000);
if(modeSendRec){
QString readArr=tcpSocket->readAll();
for (int i=0; readArr.right(2)!="\r\n" && i<25; i++)
{
tcpSocket->waitForReadyRead(25);
readArr+=tcpSocket->readAll();
}
if(!readArr.isEmpty())
emit dataFromSocketSignal(readArr);
}else{
quint16 blockSize;
QDataStream in(tcpSocket);
// in.setVersion(QDataStream::Qt_5_0);
in >> blockSize;
while (tcpSocket->bytesAvailable() < blockSize) {
if (!tcpSocket->waitForReadyRead(5000)) {
qDebug() << QString::fromUtf8("socket.error() ggg");
// emit error(socket.error(), socket.errorString());
return;
}
}
QString fortune;
in >> fortune;
if(!fortune.isEmpty())
emit dataFromSocketSignal(fortune);
}
}*/
exec();
//tcpSocket->waitForDisconnected();
}
void MyFortuneThread::readyRead()
{
if(modeSendRec){
QString readArr=tcpSocket->readAll();
for (int i=0; readArr.right(2)!="\r\n" && i<25; i++)
{
tcpSocket->waitForReadyRead(25);
readArr+=tcpSocket->readAll();
}
if(!readArr.isEmpty())
emit dataFromSocketSignal(readArr);
}else{
quint16 blockSize;
QDataStream in(tcpSocket);
// in.setVersion(QDataStream::Qt_5_0);
in >> blockSize;
while (tcpSocket->bytesAvailable() < blockSize) {
if (!tcpSocket->waitForReadyRead(5000)) {
qDebug() << QString::fromUtf8("socket.error() ggg");
// emit error(socket.error(), socket.errorString());
return;
}
}
QString fortune;
in >> fortune;
if(!fortune.isEmpty())
emit dataFromSocketSignal(fortune);
}
}
void MyFortuneThread::slotStop()
{
this->stopped=1;
tcpSocket->disconnectFromHost();
qDebug() << QString::fromUtf8("stopped");
exit(0);
emit zypynyvs();
// this->terminate();
// tcpSocket->disconnectFromHost();
// tcpSocket->waitForDisconnected();
// this->exit();
}
void MyFortuneThread::writeToSockets(QString arg1)
{
if(!tcpSocket->isOpen())
return;
if(modeSendRec){
QTextStream os(tcpSocket);
os.setAutoDetectUnicode(true);
os <<arg1;
//tcpSocket->write(arg1.toLocal8Bit());
}else {
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0); //Qt_4_0);
out << (quint16)0;
out << arg1;
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));
tcpSocket->write(block);
}
}