maindialog.cpp 24.7 KB
Newer Older
1 2 3 4 5
#include "maindialog.h"
#include "ui_maindialog.h"
#include "../ZoomPipeline_FuncSvr/smartlink/st_message.h"
#include "../ZoomPipeline_FuncSvr/smartlink/st_msg_applayer.h"
#include <QSettings>
6
#include <time.h>
7
#include <QMessageBox>
丁劲犇's avatar
丁劲犇 已提交
8
#include <string.h>
丁劲犇's avatar
丁劲犇 已提交
9
#include <QVector>
10 11
using namespace SmartLink;
MainDialog::MainDialog(QWidget *parent) :
丁劲犇's avatar
丁劲犇 已提交
12 13
	QDialog(parent),
	ui(new Ui::MainDialog)
14
{
丁劲犇's avatar
丁劲犇 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
	ui->setupUi(this);
	m_bLogedIn = false;
	m_bUUIDGot = false;
	nTimer = startTimer(100);
	ui->listView_msg->setModel(&model);
	client = new QGHTcpClient (this);
	connect(client, SIGNAL(readyRead()),this, SLOT(new_data_recieved()));
	connect(client, SIGNAL(connected()),this, SLOT(on_client_connected()));
	connect(client, SIGNAL(disconnected()),this,SLOT(on_client_disconnected()));
	connect(client, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(displayError(QAbstractSocket::SocketError)));
	connect(client, SIGNAL(bytesWritten(qint64)), this, SLOT(on_client_trasferred(qint64)));

	m_currentReadOffset = 0;
	m_currentMessageSize = 0;


	QSettings settings("goldenhawking club","FunctionalClientTest",this);
	ui->lineEdit_ip->setText(settings.value("settings/ip","localhost").toString());
	ui->lineEdit_Port->setText(settings.value("settings/port","23456").toString());
	ui->plainTextEdit_boxSerialNum->setPlainText(settings.value("settings/box2svr_insid","Temporary Equip_id for test only, by goldenhawking@163.com.64Bts").toString());
	ui->lineEdit_username->setText(settings.value("settings/client2svr_username","debug").toString());
	ui->lineEdit_password->setText(settings.value("settings/client2svr_password","debug").toString());
	ui->plainTextEdit_box_userids->setPlainText(settings.value("settings/box2svr_uploadid","0,").toString());
38 39 40 41
}

MainDialog::~MainDialog()
{
丁劲犇's avatar
丁劲犇 已提交
42
	delete ui;
43 44 45
}
void MainDialog::on_client_trasferred(qint64 dtw)
{
丁劲犇's avatar
丁劲犇 已提交
46 47 48 49 50
	QGHTcpClient * pSock = qobject_cast<QGHTcpClient*>(sender());
	if (pSock)
	{
		displayMessage(QString("client %1 Transferrd %2 bytes.").arg((quintptr)pSock).arg(dtw));
	}
51 52 53 54

}
void MainDialog::on_client_connected()
{
丁劲犇's avatar
丁劲犇 已提交
55 56 57 58 59 60
	QGHTcpClient * pSock = qobject_cast<QGHTcpClient*>(sender());
	if (pSock)
	{
		displayMessage(QString("client %1 connected.").arg((quintptr)pSock));
		ui->pushButton_connect->setEnabled(false);
	}
61 62 63 64

}
void MainDialog::on_client_disconnected()
{
丁劲犇's avatar
丁劲犇 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
	m_bLogedIn = m_bUUIDGot = false;
	client = new QGHTcpClient (this);
	connect(client, SIGNAL(readyRead()),this, SLOT(new_data_recieved()));
	connect(client, SIGNAL(connected()),this, SLOT(on_client_connected()));
	connect(client, SIGNAL(disconnected()),this,SLOT(on_client_disconnected()));
	connect(client, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(displayError(QAbstractSocket::SocketError)));
	connect(client, SIGNAL(bytesWritten(qint64)), this, SLOT(on_client_trasferred(qint64)));

	QGHTcpClient * pSock = qobject_cast<QGHTcpClient*>(sender());
	if (pSock)
	{
		displayMessage(QString("client %1 disconnected.").arg((quintptr)pSock));
		ui->pushButton_connect->setEnabled(true);
		pSock->abort();
		pSock->deleteLater();
	}
81 82 83
}
void MainDialog::displayError(QAbstractSocket::SocketError /*err*/)
{
丁劲犇's avatar
丁劲犇 已提交
84 85 86
	QGHTcpClient * sock = qobject_cast<QGHTcpClient *> (sender());
	if (sock)
		displayMessage(QString("client %1 error msg:").arg((quintptr)sock)+sock->errorString());
87 88 89
}
void MainDialog::new_data_recieved()
{
丁劲犇's avatar
丁劲犇 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
	QTcpSocket * pSock = qobject_cast<QTcpSocket*>(sender());
	if (pSock)
	{
		QByteArray block =pSock->readAll();
		if (block.isEmpty()==false && block.size()>0)
		{

			do
			{
				m_currentReadOffset = filter_message(block,m_currentReadOffset);
			}
			while (m_currentReadOffset < block.size());
			m_currentReadOffset = 0;
		}
	}
105 106 107
}
void MainDialog::displayMessage(const QString &str)
{
丁劲犇's avatar
丁劲犇 已提交
108 109 110
	model.insertRow(0,new QStandardItem(str));
	while (model.rowCount()>=256)
		model.removeRow(model.rowCount()-1);
111
}
丁劲犇's avatar
丁劲犇 已提交
112 113
void MainDialog::saveIni()
{
丁劲犇's avatar
丁劲犇 已提交
114 115 116 117 118 119 120
	QSettings settings("goldenhawking club","FunctionalClientTest",this);
	settings.setValue("settings/ip", ui->lineEdit_ip->text());
	settings.setValue("settings/port", ui->lineEdit_Port->text());
	settings.setValue("settings/box2svr_insid", ui->plainTextEdit_boxSerialNum->toPlainText());
	settings.setValue("settings/client2svr_username", ui->lineEdit_username->text());
	settings.setValue("settings/client2svr_password", ui->lineEdit_password->text());
	settings.setValue("settings/box2svr_uploadid", ui->plainTextEdit_box_userids->toPlainText());
丁劲犇's avatar
丁劲犇 已提交
121 122
}

123 124
void MainDialog::timerEvent(QTimerEvent * evt)
{
丁劲犇's avatar
丁劲犇 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
	static int nCount = 0;
	if (evt->timerId()==nTimer)
	{
		nCount++;
		if (nCount % 200 == 0 && client->isOpen()==true)
		{
			//send heart-beating
			QByteArray array(sizeof(SMARTLINK_HEARTBEATING),0);
			char * ptr = array.data();
			SMARTLINK_HEARTBEATING * pMsg = (SMARTLINK_HEARTBEATING *)ptr;
			pMsg->Mark = 0xBEBE;
			pMsg->tmStamp = time(0)&0x00ffff;
			//3/10 possibility to send a data block to server
			client->SendData(array);

		}
	}
142 143 144
}
void MainDialog::on_pushButton_connect_clicked()
{
丁劲犇's avatar
丁劲犇 已提交
145 146
	saveIni();
	client->connectToHost(ui->lineEdit_ip->text(),ui->lineEdit_Port->text().toUShort());
147
}
148 149
void MainDialog::on_pushButton_regisit_clicked()
{
丁劲犇's avatar
丁劲犇 已提交
150 151 152 153 154 155
	if (client->isOpen()==false)
	{
		QMessageBox::warning(this,"Please connect first!","Connect to server and test funcs");
		return;
	}
	saveIni();
156 157 158 159 160 161

	QString strSerial = ui->plainTextEdit_boxSerialNum->toPlainText();
	std::string strStdSerial = strSerial.toStdString();
	const char * pSrc = strStdSerial.c_str();
	int nMaxLen = strStdSerial.length();

丁劲犇's avatar
丁劲犇 已提交
162
	quint16 nMsgLen = sizeof(SMARTLINK_MSG_APP::tag_app_layer_header)
163
			+sizeof(stMsg_HostRegistReq) + nMaxLen /*Length Include \0*/;
丁劲犇's avatar
丁劲犇 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
	QByteArray array(sizeof(SMARTLINK_MSG) + nMsgLen - 1,0);
	char * ptr = array.data();
	SMARTLINK_MSG * pMsg = (SMARTLINK_MSG *)ptr;
	SMARTLINK_MSG_APP * pApp = (SMARTLINK_MSG_APP *)(((unsigned char *)
													  (ptr))+sizeof(SMARTLINK_MSG)-1
													 );
	pMsg->Mark = 0x55AA;
	pMsg->version = 1;
	pMsg->SerialNum = 0;
	pMsg->Priority = 1;
	pMsg->Reserved1 = 0;
	pMsg->source_id = (quint32)((quint64)(0xffffffff) & 0xffffffff );

	pMsg->destin_id = (quint32)((quint64)(0x00000001) & 0xffffffff );;

	pMsg->data_length = nMsgLen;
	pMsg->Reserved2 = 0;


	pApp->header.AskID = 0x01;
	pApp->header.MsgType = 0x1000;
	pApp->header.MsgFmtVersion = 0x01;
186 187

	for (int i=0;i<=nMaxLen;i++)
丁劲犇's avatar
丁劲犇 已提交
188 189 190 191
		pApp->MsgUnion.msg_HostRegistReq.HostSerialNum[i] =
				i<nMaxLen?pSrc[i]:0;
	//3/10 possibility to send a data block to server
	client->SendData(array);
192
}
193 194 195

void MainDialog::on_pushButton_Login_clicked()
{
丁劲犇's avatar
丁劲犇 已提交
196 197 198 199 200 201
	if (client->isOpen()==false)
	{
		QMessageBox::warning(this,"Please connect first!","Connect to server and test funcs");
		return;
	}
	saveIni();
202 203 204 205 206 207 208

	QString strSerial = ui->plainTextEdit_boxSerialNum->toPlainText();
	std::string strStdSerial = strSerial.toStdString();
	const char * pSrc = strStdSerial.c_str();
	int nMaxLen = strStdSerial.length();


丁劲犇's avatar
丁劲犇 已提交
209
	quint16 nMsgLen = sizeof(SMARTLINK_MSG_APP::tag_app_layer_header)
210
			+sizeof(stMsg_HostLogonReq) + nMaxLen /*\0 included*/;
丁劲犇's avatar
丁劲犇 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
	QByteArray array(sizeof(SMARTLINK_MSG) + nMsgLen - 1,0);
	char * ptr = array.data();
	SMARTLINK_MSG * pMsg = (SMARTLINK_MSG *)ptr;
	SMARTLINK_MSG_APP * pApp = (SMARTLINK_MSG_APP *)(((unsigned char *)
													  (ptr))+sizeof(SMARTLINK_MSG)-1
													 );
	pMsg->Mark = 0x55AA;
	pMsg->version = 1;
	pMsg->SerialNum = 0;
	pMsg->Priority = 1;
	pMsg->Reserved1 = 0;
	pMsg->source_id = (quint32)((quint64)(0xffffffff) & 0xffffffff );

	pMsg->destin_id = (quint32)((quint64)(0x00000001) & 0xffffffff );;

	pMsg->data_length = nMsgLen;
	pMsg->Reserved2 = 0;


	pApp->header.AskID = 0x01;
	pApp->header.MsgType = 0x1001;
	pApp->header.MsgFmtVersion = 0x01;
233 234

	for (int i=0;i<=nMaxLen;i++)
丁劲犇's avatar
丁劲犇 已提交
235 236 237 238 239 240
		pApp->MsgUnion.msg_HostLogonReq.HostSerialNum[i] =
				i<nMaxLen?pSrc[i]:0;
	pApp->MsgUnion.msg_HostLogonReq.ID = ui->lineEdit_boxid->text().toUInt();

	//3/10 possibility to send a data block to server
	client->SendData(array);
241
}
丁劲犇's avatar
丁劲犇 已提交
242 243
void MainDialog::on_pushButton_clientLogin_clicked()
{
丁劲犇's avatar
丁劲犇 已提交
244 245 246 247 248 249
	if (client->isOpen()==false)
	{
		QMessageBox::warning(this,"Please connect first!","Connect to server and test funcs");
		return;
	}
	saveIni();
250 251 252 253 254 255 256 257 258
	QString strUserName = ui->lineEdit_username->text();
	QString strPassWd = ui->lineEdit_password->text();
	std::string strStdUserName = strUserName.toStdString();
	const char * pSrcUsername = strStdUserName.c_str();
	int nMaxLenUserName = strStdUserName.length();
	std::string strStdPassword = strPassWd.toStdString();
	const char * pSrcPassword = strStdPassword.c_str();
	int nMaxLenPassword = strStdPassword.length();

丁劲犇's avatar
丁劲犇 已提交
259
	quint16 nMsgLen = sizeof(SMARTLINK_MSG_APP::tag_app_layer_header)
260
			+sizeof(stMsg_ClientLoginReq)+nMaxLenUserName+nMaxLenPassword+1;
丁劲犇's avatar
丁劲犇 已提交
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
	QByteArray array(sizeof(SMARTLINK_MSG) + nMsgLen - 1,0);
	char * ptr = array.data();
	SMARTLINK_MSG * pMsg = (SMARTLINK_MSG *)ptr;
	SMARTLINK_MSG_APP * pApp = (SMARTLINK_MSG_APP *)(((unsigned char *)
													  (ptr))+sizeof(SMARTLINK_MSG)-1
													 );
	pMsg->Mark = 0x55AA;
	pMsg->version = 1;
	pMsg->SerialNum = 0;
	pMsg->Priority = 1;
	pMsg->Reserved1 = 0;
	pMsg->source_id = (quint32)((quint64)(0xffffffff) & 0xffffffff );

	pMsg->destin_id = (quint32)((quint64)(0x00000001) & 0xffffffff );;

	pMsg->data_length = nMsgLen;
	pMsg->Reserved2 = 0;


	pApp->header.AskID = 0x01;
	pApp->header.MsgType = 0x3000;
	pApp->header.MsgFmtVersion = 0x01;


285 286 287 288 289 290 291 292 293

	for (int i=0;i<=nMaxLenUserName;i++)
		pApp->MsgUnion.msg_ClientLoginReq.UserNameAndPasswd[i] =
				i<nMaxLenUserName?pSrcUsername[i]:0;


	for (int i=0;i<=nMaxLenPassword;i++)
		pApp->MsgUnion.msg_ClientLoginReq.UserNameAndPasswd[i+nMaxLenUserName+1] =
				i<nMaxLenPassword?pSrcPassword[i]:0;
丁劲犇's avatar
丁劲犇 已提交
294 295 296 297 298

	pApp->MsgUnion.msg_ClientLoginReq.ClientVersion = 0;

	//3/10 possibility to send a data block to server
	client->SendData(array);
丁劲犇's avatar
丁劲犇 已提交
299
}
丁劲犇's avatar
丁劲犇 已提交
300 301
void MainDialog::on_pushButton_CrTime_clicked()
{
丁劲犇's avatar
丁劲犇 已提交
302 303 304 305 306 307 308 309 310 311 312 313
	if (client->isOpen()==false)
	{
		QMessageBox::warning(this,"Please connect first!","Connect to server and test funcs");
		return;
	}
	if (m_bLogedIn==false)
	{
		QMessageBox::warning(this,"Please log in the host first!","get uuid and login");
		return;
	}
	saveIni();
	quint16 nMsgLen = sizeof(SMARTLINK_MSG_APP::tag_app_layer_header)
314
			/*+sizeof(stMsg_HostTimeCorrectReq)*/;
丁劲犇's avatar
丁劲犇 已提交
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
	QByteArray array(sizeof(SMARTLINK_MSG) + nMsgLen - 1,0);
	char * ptr = array.data();
	SMARTLINK_MSG * pMsg = (SMARTLINK_MSG *)ptr;
	SMARTLINK_MSG_APP * pApp = (SMARTLINK_MSG_APP *)(((unsigned char *)
													  (ptr))+sizeof(SMARTLINK_MSG)-1
													 );
	pMsg->Mark = 0x55AA;
	pMsg->version = 1;
	pMsg->SerialNum = 0;
	pMsg->Priority = 1;
	pMsg->Reserved1 = 0;
	pMsg->source_id = (quint32)((quint64)(ui->lineEdit_boxid->text().toUInt()) & 0xffffffff );

	pMsg->destin_id = (quint32)((quint64)(0x00000001) & 0xffffffff );;

	pMsg->data_length = nMsgLen;
	pMsg->Reserved2 = 0;


	pApp->header.AskID = 0x01;
	pApp->header.MsgType = 0x1002;
	pApp->header.MsgFmtVersion = 0x01;

	//3/10 possibility to send a data block to server
	client->SendData(array);
丁劲犇's avatar
丁劲犇 已提交
340
}
丁劲犇's avatar
丁劲犇 已提交
341 342
void MainDialog::on_pushButton_box_upload_uid_clicked()
{
丁劲犇's avatar
丁劲犇 已提交
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 380 381 382 383 384 385 386 387 388 389 390 391
	if (client->isOpen()==false)
	{
		QMessageBox::warning(this,"Please connect first!","Connect to server and test funcs");
		return;
	}
	if (m_bLogedIn==false)
	{
		QMessageBox::warning(this,"Please log in the host first!","get uuid and login");
		return;
	}
	saveIni();
	QStringList lst = ui->plainTextEdit_box_userids->toPlainText().split(",");
	QVector<quint32> vecInt;
	foreach (QString item,lst)
	{
		vecInt.push_back(item.toUInt());
	}

	quint16 nMsgLen = sizeof(SMARTLINK_MSG_APP::tag_app_layer_header)
			+sizeof(stMsg_UploadUserListReq)+ sizeof(quint32)*vecInt.size() - sizeof(quint32);
	QByteArray array(sizeof(SMARTLINK_MSG) + nMsgLen - 1,0);
	char * ptr = array.data();
	SMARTLINK_MSG * pMsg = (SMARTLINK_MSG *)ptr;
	SMARTLINK_MSG_APP * pApp = (SMARTLINK_MSG_APP *)(((unsigned char *)
													  (ptr))+sizeof(SMARTLINK_MSG)-1
													 );
	pMsg->Mark = 0x55AA;
	pMsg->version = 1;
	pMsg->SerialNum = 0;
	pMsg->Priority = 1;
	pMsg->Reserved1 = 0;
	pMsg->source_id = (quint32)((quint64)(ui->lineEdit_boxid->text().toUInt()) & 0xffffffff );

	pMsg->destin_id = (quint32)((quint64)(0x00000001) & 0xffffffff );;

	pMsg->data_length = nMsgLen;
	pMsg->Reserved2 = 0;


	pApp->header.AskID = 0x01;
	pApp->header.MsgType = 0x1003;
	pApp->header.MsgFmtVersion = 0x01;

	pApp->MsgUnion.msg_UploadUserListReq.UserNum = (quint16)(vecInt.size() & 0x00ffff);
	for (int i=0;i< vecInt.size();i++)
		pApp->MsgUnion.msg_UploadUserListReq.pUserIDList[i] = vecInt[i];

	//3/10 possibility to send a data block to server
	client->SendData(array);
丁劲犇's avatar
丁劲犇 已提交
392 393 394 395
}

void MainDialog::on_pushButton_box_download_uid_clicked()
{
丁劲犇's avatar
丁劲犇 已提交
396 397 398 399 400 401 402 403 404 405 406 407
	if (client->isOpen()==false)
	{
		QMessageBox::warning(this,"Please connect first!","Connect to server and test funcs");
		return;
	}
	if (m_bLogedIn==false)
	{
		QMessageBox::warning(this,"Please log in the host first!","get uuid and login");
		return;
	}
	saveIni();
	quint16 nMsgLen = sizeof(SMARTLINK_MSG_APP::tag_app_layer_header)
408
			/*+sizeof(stMsg_DownloadUserListReq)*/;
丁劲犇's avatar
丁劲犇 已提交
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
	QByteArray array(sizeof(SMARTLINK_MSG) + nMsgLen - 1,0);
	char * ptr = array.data();
	SMARTLINK_MSG * pMsg = (SMARTLINK_MSG *)ptr;
	SMARTLINK_MSG_APP * pApp = (SMARTLINK_MSG_APP *)(((unsigned char *)
													  (ptr))+sizeof(SMARTLINK_MSG)-1
													 );
	pMsg->Mark = 0x55AA;
	pMsg->version = 1;
	pMsg->SerialNum = 0;
	pMsg->Priority = 1;
	pMsg->Reserved1 = 0;
	pMsg->source_id = (quint32)((quint64)(ui->lineEdit_boxid->text().toUInt()) & 0xffffffff );

	pMsg->destin_id = (quint32)((quint64)(0x00000001) & 0xffffffff );;

	pMsg->data_length = nMsgLen;
	pMsg->Reserved2 = 0;


	pApp->header.AskID = 0x01;
	pApp->header.MsgType = 0x1004;
	pApp->header.MsgFmtVersion = 0x01;

	//3/10 possibility to send a data block to server
	client->SendData(array);
丁劲犇's avatar
丁劲犇 已提交
434 435 436
}
void  MainDialog::on_pushButton_client_downHost_clicked()
{
丁劲犇's avatar
丁劲犇 已提交
437 438 439 440 441 442 443 444 445 446 447 448
	if (client->isOpen()==false)
	{
		QMessageBox::warning(this,"Please connect first!","Connect to server and test funcs");
		return;
	}
	if (m_bLogedIn==false)
	{
		QMessageBox::warning(this,"Please log in the host first!","get uuid and login");
		return;
	}
	saveIni();
	quint16 nMsgLen = sizeof(SMARTLINK_MSG_APP::tag_app_layer_header)
449
			/*+sizeof(stMsg_GetHostListReq)*/;
丁劲犇's avatar
丁劲犇 已提交
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
	QByteArray array(sizeof(SMARTLINK_MSG) + nMsgLen - 1,0);
	char * ptr = array.data();
	SMARTLINK_MSG * pMsg = (SMARTLINK_MSG *)ptr;
	SMARTLINK_MSG_APP * pApp = (SMARTLINK_MSG_APP *)(((unsigned char *)
													  (ptr))+sizeof(SMARTLINK_MSG)-1
													 );
	pMsg->Mark = 0x55AA;
	pMsg->version = 1;
	pMsg->SerialNum = 0;
	pMsg->Priority = 1;
	pMsg->Reserved1 = 0;
	pMsg->source_id = (quint32)((quint64)(ui->lineEdit_userid->text().toUInt()) & 0xffffffff );

	pMsg->destin_id = (quint32)((quint64)(0x00000001) & 0xffffffff );;

	pMsg->data_length = nMsgLen;
	pMsg->Reserved2 = 0;


	pApp->header.AskID = 0x01;
	pApp->header.MsgType = 0x3002;
	pApp->header.MsgFmtVersion = 0x01;
	//3/10 possibility to send a data block to server
	client->SendData(array);
丁劲犇's avatar
丁劲犇 已提交
474 475 476 477
}

void  MainDialog::on_pushButton_clientLogout_clicked()
{
丁劲犇's avatar
丁劲犇 已提交
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
	if (client->isOpen()==false)
	{
		QMessageBox::warning(this,"Please connect first!","Connect to server and test funcs");
		return;
	}
	if (m_bLogedIn==false)
	{
		QMessageBox::warning(this,"Please log in the host first!","get uuid and login");
		return;
	}
	saveIni();
	quint16 nMsgLen = sizeof(SMARTLINK_MSG_APP::tag_app_layer_header)
			+sizeof(stMsg_ClientLogoutReq);
	QByteArray array(sizeof(SMARTLINK_MSG) + nMsgLen - 1,0);
	char * ptr = array.data();
	SMARTLINK_MSG * pMsg = (SMARTLINK_MSG *)ptr;
	SMARTLINK_MSG_APP * pApp = (SMARTLINK_MSG_APP *)(((unsigned char *)
													  (ptr))+sizeof(SMARTLINK_MSG)-1
													 );
	pMsg->Mark = 0x55AA;
	pMsg->version = 1;
	pMsg->SerialNum = 0;
	pMsg->Priority = 1;
	pMsg->Reserved1 = 0;
	pMsg->source_id = (quint32)((quint64)(ui->lineEdit_userid->text().toUInt()) & 0xffffffff );

	pMsg->destin_id = (quint32)((quint64)(0x00000001) & 0xffffffff );;

	pMsg->data_length = nMsgLen;
	pMsg->Reserved2 = 0;


	pApp->header.AskID = 0x01;
	pApp->header.MsgType = 0x3001;
	pApp->header.MsgFmtVersion = 0x01;


	pApp->MsgUnion.msg_ClientLogoutReq.UserName[0] = 0;


	//3/10 possibility to send a data block to server
	client->SendData(array);
丁劲犇's avatar
丁劲犇 已提交
520
}
521

522 523 524 525
//!deal one message, affect m_currentRedOffset,m_currentMessageSize,m_currentHeader
//!return bytes Used.
int MainDialog::filter_message(const QByteArray & block, int offset)
{
丁劲犇's avatar
丁劲犇 已提交
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
	const int blocklen = block.length();
	while (blocklen>offset)
	{
		const char * dataptr = block.constData();

		//Recieve First 2 byte
		while (m_currentMessageSize<2 && blocklen>offset )
		{
			m_currentBlock.push_back(dataptr[offset++]);
			m_currentMessageSize++;
		}
		if (m_currentMessageSize < 2) //First 2 byte not complete
			continue;

		if (m_currentMessageSize==2)
		{
			const char * headerptr = m_currentBlock.constData();
			memcpy((void *)&m_currentHeader,headerptr,2);
		}

		const char * ptrCurrData = m_currentBlock.constData();
		//Heart Beating
		if (m_currentHeader.Mark == 0xBEBE)
		{
			while (m_currentMessageSize< sizeof(SMARTLINK_HEARTBEATING) && blocklen>offset )
			{
				m_currentBlock.push_back(dataptr[offset++]);
				m_currentMessageSize++;
			}
			if (m_currentMessageSize < sizeof(SMARTLINK_HEARTBEATING)) //Header not completed.
				continue;
			const SMARTLINK_HEARTBEATING * ptr = (const SMARTLINK_HEARTBEATING *)m_currentBlock.constData();
			displayMessage(tr("Recieved Heart-beating msg sended %1 sec(s) ago.").
						   arg((time(0)&0x00ffff)-(ptr->tmStamp)));
			//This Message is Over. Start a new one.
			m_currentMessageSize = 0;
			m_currentBlock = QByteArray();

			continue;
		}
		else if (m_currentHeader.Mark == 0x55AA)
			//Trans Message
		{
			while (m_currentMessageSize< sizeof(SMARTLINK_MSG)-1 && blocklen>offset)
			{
				m_currentBlock.push_back(dataptr[offset++]);
				m_currentMessageSize++;
			}
			if (m_currentMessageSize < sizeof(SMARTLINK_MSG)-1) //Header not completed.
				continue;
			else if (m_currentMessageSize == sizeof(SMARTLINK_MSG)-1)//Header just  completed.
			{
				const char * headerptr = m_currentBlock.constData();
				memcpy((void *)&m_currentHeader,headerptr,sizeof(SMARTLINK_MSG)-1);

				//continue reading if there is data left behind
				if (block.length()>offset)
				{
					qint32 bitLeft = m_currentHeader.data_length + sizeof(SMARTLINK_MSG) - 1
							-m_currentMessageSize ;
					while (bitLeft>0 && blocklen>offset)
					{
						m_currentBlock.push_back(dataptr[offset++]);
						m_currentMessageSize++;
						bitLeft--;
					}
					//deal block, may be send data as soon as possible;
					deal_current_message_block();
					if (bitLeft>0)
						continue;
					//This Message is Over. Start a new one.
					m_currentMessageSize = 0;
					m_currentBlock = QByteArray();
					continue;
				}
			}
			else
			{
				if (block.length()>offset)
				{
					qint32 bitLeft = m_currentHeader.data_length + sizeof(SMARTLINK_MSG) - 1
							-m_currentMessageSize ;
					while (bitLeft>0 && blocklen>offset)
					{
						m_currentBlock.push_back(dataptr[offset++]);
						m_currentMessageSize++;
						bitLeft--;
					}
					//deal block, may be processed as soon as possible;
					deal_current_message_block();
					if (bitLeft>0)
						continue;
					//This Message is Over. Start a new one.
					m_currentMessageSize = 0;
					m_currentBlock = QByteArray();
					continue;
				}
			} // end if there is more bytes to append
		} //end deal trans message
		else
		{
			displayMessage(tr("Client Send a unknown start Header %1 %2. Close client immediately.")
丁劲犇's avatar
丁劲犇 已提交
628
						   .arg((int)(ptrCurrData[0])).arg((int)(ptrCurrData[1])));
丁劲犇's avatar
丁劲犇 已提交
629 630 631 632 633 634 635 636 637
			m_currentMessageSize = 0;
			m_currentBlock = QByteArray();
			offset = blocklen;

			this->client->abort();
		}
	} // end while block len > offset

	return offset;
638 639 640 641
}
//!deal current message
int MainDialog::deal_current_message_block()
{
丁劲犇's avatar
丁劲犇 已提交
642 643 644 645 646 647
	//The bytes left to recieve.
	qint32 bytesLeft = m_currentHeader.data_length + sizeof(SMARTLINK_MSG) - 1
			-m_currentMessageSize ;
	if (bytesLeft)
		return 0;

丁劲犇's avatar
丁劲犇 已提交
648 649 650

	const char * ptr = m_currentBlock.constData();

丁劲犇's avatar
丁劲犇 已提交
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
	SMARTLINK_MSG_APP * pApp = (SMARTLINK_MSG_APP *)(((unsigned char *)
													  (ptr))+sizeof(SMARTLINK_MSG)-1
													 );

	if (pApp->header.MsgType==0x1800)
	{
		ui->lineEdit_boxid->setText(QString("%1").arg(pApp->MsgUnion.msg_HostRegistRsp.ID));
		displayMessage(tr("Res = %1, ID = %2")
					   .arg(pApp->MsgUnion.msg_HostRegistRsp.DoneCode)
					   .arg(pApp->MsgUnion.msg_HostRegistRsp.ID)
					   );
		if (pApp->MsgUnion.msg_HostRegistRsp.DoneCode!=2)
			m_bUUIDGot = true;
	}
	else if (pApp->header.MsgType==0x1801)
	{
		if (pApp->MsgUnion.msg_HostLogonRsp.DoneCode==0)
		{
			m_bLogedIn = true;
			QMessageBox::information(this,tr("Succeed!"),tr("Log in succeed!"));
		}
		else
			QMessageBox::information(this,tr("Failed!"),tr("Log in Failed!"));
		displayMessage(tr("Res = %1")
					   .arg(pApp->MsgUnion.msg_HostLogonRsp.DoneCode)
					   );
	}
	else if (pApp->header.MsgType==0x1802)
	{
		if (pApp->MsgUnion.msg_HostTimeCorrectRsp.DoneCode==0)
			QMessageBox::information(this,tr("Succeed!"),tr("Time Corrected!"));
		else
			QMessageBox::information(this,tr("Failed!"),tr("Time Correct Failed!"));
		displayMessage(tr("Res = %1")
					   .arg(pApp->MsgUnion.msg_HostTimeCorrectRsp.DoneCode)
					   );
		QString tm = QString("%1-%2-%3 %4:%5:%6")
				.arg(pApp->MsgUnion.msg_HostTimeCorrectRsp.DateTime.Year)
				.arg(pApp->MsgUnion.msg_HostTimeCorrectRsp.DateTime.Month)
				.arg(pApp->MsgUnion.msg_HostTimeCorrectRsp.DateTime.Day)
				.arg(pApp->MsgUnion.msg_HostTimeCorrectRsp.DateTime.Hour)
				.arg(pApp->MsgUnion.msg_HostTimeCorrectRsp.DateTime.Minute)
				.arg(pApp->MsgUnion.msg_HostTimeCorrectRsp.DateTime.Second)
				;
		ui->lineEdit_box_time->setText(tm);

	}
	else if (pApp->header.MsgType==0x3800)
	{
		if (pApp->MsgUnion.msg_ClientLoginRsp.DoneCode==0)
		{
			m_bLogedIn = true;
			QMessageBox::information(this,tr("Succeed!"),tr("Log in succeed!"));
		}
		else
			QMessageBox::information(this,tr("Failed!"),tr("Log in Failed!"));
		displayMessage(tr("Res = %1")
					   .arg(pApp->MsgUnion.msg_ClientLoginRsp.DoneCode)
					   );
		ui->lineEdit_userid->setText(QString ("%1").arg(pApp->MsgUnion.msg_ClientLoginRsp.UserID));
	}
	else if (pApp->header.MsgType==0x1803)
	{
		if (pApp->MsgUnion.msg_UploadUserListRsp.DoneCode==0)
			QMessageBox::information(this,tr("Succeed!"),tr("upload succeed!"));
		else
			QMessageBox::information(this,tr("Failed!"),tr("upload in Failed!"));
		displayMessage(tr("Res = %1")
					   .arg(pApp->MsgUnion.msg_UploadUserListRsp.DoneCode)
					   );

	}
	else if (pApp->header.MsgType==0x1804)
	{
		if (pApp->MsgUnion.msg_DownloadUserListRsp.DoneCode==0)
		{
			QMessageBox::information(this,tr("Succeed!"),tr("download succeed!"));
			QString strRes;
			for (quint16 i = 0;i<pApp->MsgUnion.msg_DownloadUserListRsp.UserNum;i++)
			{
				strRes += QString("%1,").arg(pApp->MsgUnion.msg_DownloadUserListRsp.pUserIDList[i]);
			}
			ui->plainTextEdit_box_userids->setPlainText(strRes);
		}
		else
			QMessageBox::information(this,tr("Failed!"),tr("download in Failed!"));
		displayMessage(tr("Res = %1")
					   .arg(pApp->MsgUnion.msg_DownloadUserListRsp.DoneCode)
丁劲犇's avatar
丁劲犇 已提交
739
					   );
丁劲犇's avatar
丁劲犇 已提交
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767

	}
	else if (pApp->header.MsgType==0x3801)
	{
		if (pApp->MsgUnion.msg_ClientLogoutRsp.DoneCode==0)
			QMessageBox::information(this,tr("Succeed!"),tr("log out succeed!"));
		else
			QMessageBox::information(this,tr("Failed!"),tr("download in Failed!"));
		displayMessage(tr("Res = %1")
					   .arg(pApp->MsgUnion.msg_ClientLogoutRsp.DoneCode)
					   );
		this->client->abort();
	}
	else if (pApp->header.MsgType==0x3802)
	{
		if (pApp->MsgUnion.msg_GetHostListRsp.HostNum>0)
		{
			QMessageBox::information(this,tr("Succeed!"),tr("download succeed!"));
			QString strRes;
			for (quint16 i = 0;i<pApp->MsgUnion.msg_GetHostListRsp.HostNum;i++)
			{
				strRes += QString("%1").arg(pApp->MsgUnion.msg_GetHostListRsp.pHostIDList[i]);
			}
			ui->plainTextEdit_hostsIds->setPlainText(strRes);
		}
		else
			QMessageBox::information(this,tr("Failed!"),tr("download in Failed!"));
	}
丁劲犇's avatar
丁劲犇 已提交
768 769 770 771 772 773 774 775 776 777 778
	else
	{
		QString str;
		int nLen =  m_currentHeader.data_length;
		for (int i=0;i<nLen;i++)
		{
			str += ptr[i+ sizeof(SMARTLINK_MSG) - 1];
		}
		ui->plainTextEdit_msg_recieved->setPlainText(str);

	}
丁劲犇's avatar
丁劲犇 已提交
779 780 781 782
	m_currentBlock = QByteArray();


	return 0;
783
}
丁劲犇's avatar
丁劲犇 已提交
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821

void MainDialog::on_pushButton_sendToClient_clicked()
{
	if (client->isOpen()==false)
	{
		QMessageBox::warning(this,"Please connect first!","Connect to server and test funcs");
		return;
	}
	if (m_bLogedIn==false)
	{
		QMessageBox::warning(this,"Please log in the host first!","get uuid and login");
		return;
	}
	saveIni();
	QString strMsg = ui->plainTextEdit_msg_to_client->toPlainText();
	QByteArray arrMsg(strMsg.toStdString().c_str());
	QByteArray array(sizeof(SMARTLINK_MSG) - 1,0);
	char * ptr = array.data();
	SMARTLINK_MSG * pMsg = (SMARTLINK_MSG *)ptr;

	pMsg->Mark = 0x55AA;
	pMsg->version = 1;
	pMsg->SerialNum = 0;
	pMsg->Priority = 1;
	pMsg->Reserved1 = 0;

	pMsg->source_id = (quint32)((quint64)(ui->lineEdit_boxid->text().toUInt()) & 0xffffffff );

	pMsg->destin_id = (quint32)((quint64)(ui->lineEdit_client_uuid->text().toUInt()) & 0xffffffff );;

	pMsg->data_length = arrMsg.size();
	pMsg->Reserved2 = 0;

	array.append(arrMsg);

	//3/10 possibility to send a data block to server
	client->SendData(array);
}