summaryrefslogtreecommitdiff
path: root/plugins/Listener/catchcopy-v0002/catchcopy-api-0002/ClientCatchcopy.cpp
blob: d7492d0e80e45d46f92f39737fce19ea3b3be769 (plain)
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/** \file ClientCatchcopy.cpp
\brief Define the catchcopy client
\author alpha_one_x86 */

#include "ClientCatchcopy.h"
#include "VariablesCatchcopy.h"
#include "ExtraSocketCatchcopy.h"

#include <QDataStream>

ClientCatchcopy::ClientCatchcopy()
{
	disconnectedFromSocket();
	error_string="Unknown error";
	detectTimeOut.setSingleShot(true);
	detectTimeOut.setInterval(CATCHCOPY_COMMUNICATION_TIMEOUT); // the max time to without send packet
	connect(&socket,	SIGNAL(connected()),					this,	SIGNAL(connected()));
	connect(&socket,	SIGNAL(disconnected()),					this,	SIGNAL(disconnected()));
	connect(&socket,	SIGNAL(disconnected()),					this,	SLOT(disconnectedFromSocket()));
	connect(&socket,	SIGNAL(stateChanged(QLocalSocket::LocalSocketState)),	this,	SIGNAL(stateChanged(QLocalSocket::LocalSocketState)));
	connect(&socket,	SIGNAL(error(QLocalSocket::LocalSocketError)),		this,	SIGNAL(errorSocket(QLocalSocket::LocalSocketError)));
	connect(&socket,	SIGNAL(readyRead()),					this,	SLOT(readyRead()));
	connect(&detectTimeOut,	SIGNAL(timeout()),					this,	SLOT(checkTimeOut()));
	connect(&socket,	SIGNAL(connected()),					this,	SLOT(socketIsConnected()));
}

void ClientCatchcopy::checkTimeOut()
{
	if(haveData)
	{
		error_string="The server is too long to send the next part of the reply";
		emit error(error_string);
		disconnectFromServer();
	}
}

const QString ClientCatchcopy::errorString()
{
	return error_string;
}

void ClientCatchcopy::socketIsConnected()
{
	orderIdFirstSendProtocol=sendProtocol();
}

void ClientCatchcopy::connectToServer()
{
	socket.connectToServer(ExtraSocketCatchcopy::pathSocket());
}

void ClientCatchcopy::disconnectFromServer()
{
	socket.abort();
	socket.disconnectFromServer();
}

const QString ClientCatchcopy::errorStringSocket()
{
	return socket.errorString();
}

/// \brief to send stream of string list
quint32 ClientCatchcopy::sendRawOrderList(const QStringList & order)
{
	if(!socket.isValid())
	{
		error_string="Socket is not valid, try send: "+order.join(";");
		emit error(error_string);
		return -1;
	}
	if(socket.state()!=QLocalSocket::ConnectedState)
	{
		error_string="Socket is not connected "+QString::number(socket.state());
		emit error(error_string);
		return -1;
	}
	do
	{
		idNextOrder++;
		if(idNextOrder>2000000000)
			idNextOrder=0;
	} while(notRepliedQuery.contains(idNextOrder));
	notRepliedQuery << idNextOrder;
	QByteArray block;
	QDataStream out(&block, QIODevice::WriteOnly);
	out.setVersion(QDataStream::Qt_4_4);
	out << int(0);
	out << idNextOrder;
	out << order;
	out.device()->seek(0);
	out << block.size();
	if(idNextOrder!=1) // drop if internal protocol send
	{
		emit dataSend(idNextOrder,block);
		emit dataSend(idNextOrder,order);
	}
	do //cut string list and send it as block of 32KB
	{
		QByteArray blockToSend;
		int byteWriten;
		blockToSend=block.left(32*1024);//32KB
		block.remove(0,blockToSend.size());
		byteWriten = socket.write(blockToSend);
		if(!socket.isValid())
		{
			error_string="Socket is not valid";
			emit error(error_string);
			return -1;
		}
		if(socket.errorString()!="Unknown error" && socket.errorString()!="")
		{
			error_string=socket.errorString();
			emit error(error_string);
			return -1;
		}
		if(blockToSend.size()!=byteWriten)
		{
			error_string="All the bytes have not be written";
			emit error(error_string);
			return -1;
		}
	}
	while(block.size());
	return idNextOrder;
}

void ClientCatchcopy::readyRead()
{
	while(socket.bytesAvailable()>0)
	{
		if(!haveData)
		{
			if(socket.bytesAvailable()<(int)sizeof(int))//int of size cuted
			{
			/*	error_string="Bytes available is not sufficient to do a int";
				emit error(error_string);
				disconnectFromServer();*/
				return;
			}
			QDataStream in(&socket);
			in.setVersion(QDataStream::Qt_4_4);
			in >> dataSize;
			dataSize-=sizeof(int);
			if(dataSize>64*1024*1024) // 64MB
			{
				error_string="Reply size is >64MB, seam corrupted";
				emit error(error_string);
				disconnectFromServer();
				return;
			}
			if(dataSize<(int)(sizeof(int) //orderId
				     + sizeof(quint32) //returnCode
				     + sizeof(quint32) //string list size
						))
			{
				error_string="Reply size is too small to have correct code";
				emit error(error_string);
				disconnectFromServer();
				return;
			}
		}
		if(dataSize<(data.size()+socket.bytesAvailable()))
			data.append(socket.read(dataSize-data.size()));
		else
			data.append(socket.readAll());
		if(dataSize==data.size())
		{
			if(!checkDataIntegrity(data))
			{
				data.clear();
				qWarning() << "Data of the reply is wrong";
				return;
			}
			QStringList returnList;
			quint32 orderId;
			quint32 returnCode;
			QDataStream in(data);
			in.setVersion(QDataStream::Qt_4_4);
			in >> orderId;
			in >> returnCode;
			in >> returnList;
			data.clear();
			if(orderId!=orderIdFirstSendProtocol)
			{
				if(!notRepliedQuery.contains(orderId))
					qWarning() << "Unknown query not replied:" << orderId;
				else
				{
					if(!parseReply(orderId,returnCode,returnList))
						emit unknowReply(orderId);
					emit newReply(orderId,returnCode,returnList);
				}
			}
			else
			{
				if(!sendProtocolReplied)
				{
					sendProtocolReplied=true;
					if(returnCode!=1000)
					{
						error_string="Protocol not supported";
						emit error(error_string);
						disconnectFromServer();
						return;
					}
				}
				else
				{
					error_string=QStringLiteral("First send protocol send with the query id %1 have been already previously replied").arg(orderIdFirstSendProtocol);
					emit error(error_string);
					disconnectFromServer();
					return;
				}
			}
		}
	}
	if(haveData)
		detectTimeOut.start();
	else
		detectTimeOut.stop();
}

bool ClientCatchcopy::checkDataIntegrity(QByteArray data)
{
	quint32 orderId;
	qint32 replyCode;
	qint32 listSize;
	QDataStream in(data);
	in.setVersion(QDataStream::Qt_4_4);
	in >> orderId;
	in >> replyCode;
	in >> listSize;
	if(listSize>65535)
	{
		emit error("List size is wrong");
		qWarning() << "List size is wrong";
		return false;
	}
	int index=0;
	while(index<listSize)
	{
		qint32 stringSize;
		in >> stringSize;
		if(stringSize>65535)
		{
			emit error("String size is wrong");
			qWarning() << "String size is wrong";
			return false;
		}
		if(stringSize>(in.device()->size()-in.device()->pos()))
		{
			emit error(QStringLiteral("String size is greater than the data: %1>(%2-%3)").arg(stringSize).arg(in.device()->size()).arg(in.device()->pos()));
			qWarning() << QStringLiteral("String size is greater than the data: %1>(%2-%3)").arg(stringSize).arg(in.device()->size()).arg(in.device()->pos());
			return false;
		}
		in.device()->seek(in.device()->pos()+stringSize);
		index++;
	}
	if(in.device()->size()!=in.device()->pos())
	{
		emit error("Remaining data after string list parsing");
		qWarning() << "Remaining data after string list parsing";
		return false;
	}
	return true;
}

QLocalSocket::LocalSocketState ClientCatchcopy::state()
{
	return socket.state();
}

void ClientCatchcopy::disconnectedFromSocket()
{
	haveData		= false;
	orderIdFirstSendProtocol= 0;
	idNextOrder		= 0;
	sendProtocolReplied	= false;
	notRepliedQuery.clear();
}

/// \brief to send the protocol version used
quint32 ClientCatchcopy::sendProtocol()
{
	return sendRawOrderList(QStringList() << "protocol" << CATCHCOPY_PROTOCOL_VERSION);
}

quint32 ClientCatchcopy::askServerName()
{
	return sendRawOrderList(QStringList() << "server" << "name?");
}

quint32 ClientCatchcopy::setClientName(const QString & name)
{
	return sendRawOrderList(QStringList() << "client" << name);
}

quint32 ClientCatchcopy::checkProtocolExtension(const QString & name)
{
	return sendRawOrderList(QStringList() << "protocol extension" << name);
}

quint32 ClientCatchcopy::checkProtocolExtension(const QString & name,const QString & version)
{
	return sendRawOrderList(QStringList() << "protocol extension" << name << version);
}

quint32 ClientCatchcopy::addCopyWithDestination(const QStringList & sources,const QString & destination)
{
	return sendRawOrderList(QStringList() << "cp" << sources << destination);
}

quint32 ClientCatchcopy::addCopyWithoutDestination(const QStringList & sources)
{
	return sendRawOrderList(QStringList() << "cp-?" << sources);
}

quint32 ClientCatchcopy::addMoveWithDestination(const QStringList & sources,const QString & destination)
{
	return sendRawOrderList(QStringList() << "mv" << sources << destination);
}

quint32 ClientCatchcopy::addMoveWithoutDestination(const QStringList & sources)
{
	return sendRawOrderList(QStringList() << "mv-?" << sources);
}

bool ClientCatchcopy::parseReply(quint32 orderId,quint32 returnCode,QStringList returnList)
{
	switch(returnCode)
	{
		case 1000:
			emit protocolSupported(orderId);
		break;
		case 1001:
		case 1002:
			if(returnCode==1001)
				emit protocolExtensionSupported(orderId,true);
			else
				emit protocolExtensionSupported(orderId,false);
		break;
		case 1003:
			emit clientRegistered(orderId);
		break;
		case 1004:
			if(returnList.size()!=1)
				emit unknowOrder(orderId);
			else
				emit serverName(orderId,returnList.last());
		break;
		case 1005:
		case 1006:
			if(returnCode==1005)
				emit copyFinished(orderId,false);
			else
				emit copyFinished(orderId,true);
		break;
		case 1007:
			emit copyCanceled(orderId);
		break;
		case 5000:
			emit incorrectArgumentListSize(orderId);
		break;
		case 5001:
			emit incorrectArgument(orderId);
		break;
		case 5002:
			emit unknowOrder(orderId); //the server have not understand the order
		break;
		case 5003:
			emit protocolNotSupported(orderId);
		break;
		default:
			return false;
	}
	return true;
}