st_clientnode_applayer.cpp 8.1 KB
Newer Older
丁劲犇's avatar
丁劲犇 已提交
1 2
#include "st_clientnode_applayer.h"
#include "st_client_table.h"
3 4 5
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
6
namespace ExampleServer{
丁劲犇's avatar
丁劲犇 已提交
7 8 9
	st_clientNodeAppLayer::st_clientNodeAppLayer(st_client_table * pClientTable, QObject * pClientSock ,QObject *parent) :
		st_clientNode_baseTrans(pClientTable,pClientSock,parent)
	{
丁劲犇's avatar
丁劲犇 已提交
10

丁劲犇's avatar
丁劲犇 已提交
11
		m_bLoggedIn= false;
12
		memset(&m_current_app_header,0,sizeof(EXAMPLE_MSG_APP));
丁劲犇's avatar
丁劲犇 已提交
13
	}
丁劲犇's avatar
丁劲犇 已提交
14

丁劲犇's avatar
丁劲犇 已提交
15 16 17 18 19
	bool st_clientNodeAppLayer::loadRelations()
	{
		QSqlDatabase db = m_pClientTable->dbRes()->databse(m_pClientTable->Database_UserAcct());
		if (db.isValid()==true && db.isOpen()==true )
		{
20 21
			QString sql = "select friend_id from relations where user_id = ?;";

丁劲犇's avatar
丁劲犇 已提交
22 23 24 25 26
			QSqlQuery query(db);
			query.prepare(sql);
			query.addBindValue((quint32)m_uuid);
			if (false== query.exec())
			{
丁劲犇's avatar
丁劲犇 已提交
27
				emit evt_Message(this,tr("try to get relations Failed! ")+ query.lastError().text());
28
				db.close();
丁劲犇's avatar
丁劲犇 已提交
29 30
				return false;
			}
31

丁劲犇's avatar
丁劲犇 已提交
32 33 34 35 36 37 38 39 40 41 42
			m_matched_nodes.clear();
			while (query.next())
			{
				quint32 val = query.value(0).toUInt();
				m_matched_nodes.insert(val);
			}
			return true;
		}
		else
		{
			//Server db is currently not accessable, wait.
丁劲犇's avatar
丁劲犇 已提交
43
			emit evt_Message(this,"Server Not Accessable Now.");
丁劲犇's avatar
丁劲犇 已提交
44 45 46
		}
		return false;
	}
47

丁劲犇's avatar
丁劲犇 已提交
48 49 50 51 52
	bool st_clientNodeAppLayer::saveRelations()
	{
		QSqlDatabase db = m_pClientTable->dbRes()->databse(m_pClientTable->Database_UserAcct());
		if (db.isValid()==true && db.isOpen()==true )
		{
53 54 55 56 57 58
			QSqlQuery query(db);
			QString sql = "delete from relations where user_id = ?;";

			query.prepare(sql);
			query.addBindValue((quint32)m_uuid);
			if (false== query.exec())
丁劲犇's avatar
丁劲犇 已提交
59
			{
60
				emit evt_Message(this,tr("try to del old relations Failed! ")+ query.lastError().text());
61
				db.close();
丁劲犇's avatar
丁劲犇 已提交
62 63
				return false;
			}
64

65 66 67


			sql = "insert into relations (user_id,friend_id) values (?,?);";
丁劲犇's avatar
丁劲犇 已提交
68 69 70
			//the forigen key can automatic avoid non-existing values.
			foreach(quint32 nodeid,m_matched_nodes)
			{
71

丁劲犇's avatar
丁劲犇 已提交
72
				QSqlQuery query(db);
73 74 75 76 77
				if (bIsValidUserId(nodeid)==false)
					continue;
				query.prepare(sql);
				query.addBindValue((quint32)m_uuid);
				query.addBindValue(nodeid);
78

丁劲犇's avatar
丁劲犇 已提交
79 80
				if (false== query.exec())
				{
丁劲犇's avatar
丁劲犇 已提交
81
					emit evt_Message(this,tr("try to insert new relations Failed! ")+ query.lastError().text());
82
					db.close();
丁劲犇's avatar
丁劲犇 已提交
83 84 85 86 87 88 89 90
					return false;
				}
			}
			return true;
		}
		else
		{
			//Server db is currently not accessable, wait.
丁劲犇's avatar
丁劲犇 已提交
91
			emit evt_Message(this,"Server Not Accessable Now.");
丁劲犇's avatar
丁劲犇 已提交
92 93 94
		}
		return false;
	}
丁劲犇's avatar
丁劲犇 已提交
95

丁劲犇's avatar
丁劲犇 已提交
96 97 98 99
	//!deal current message
	int st_clientNodeAppLayer::deal_current_message_block()
	{
		//then , Start deal to-server messages
100 101 102 103 104
		//Uncomment these codes to enable debug msg dump.
//		if (m_currentBlock.length()>=64)
//			emit evt_Message(this,"Debug:" + m_currentBlock.toHex().left(64) + "..." + m_currentBlock.toHex().right(64));
//		else
//			emit evt_Message(this,"Debug:" + m_currentBlock.toHex());
丁劲犇's avatar
丁劲犇 已提交
105 106 107 108 109 110 111 112
		if (m_currentHeader.destin_id==0x00000001)
		{
			if (this->m_bLoggedIn==false || this->m_bUUIDRecieved==false)
			{
				//To-Server Messages does not wait for message-block completes
				if (false==Deal_ToServer_Handshakes())
				{
					m_currentBlock = QByteArray();
丁劲犇's avatar
丁劲犇 已提交
113
					emit evt_Message(this,tr("To-server Message Failed."));
丁劲犇's avatar
丁劲犇 已提交
114 115 116 117 118
					emit evt_close_client(this->sock());
				}
			}
			else
			{
119
				if (bIsValidUserId(m_currentHeader.source_id) )
丁劲犇's avatar
丁劲犇 已提交
120 121 122 123 124
				{
					//Deal Client->Svr Msgs
					if (false==Deal_Node2Svr_Msgs())
					{
						m_currentBlock = QByteArray();
丁劲犇's avatar
丁劲犇 已提交
125
						emit evt_Message(this,tr("Client To Server Message Failed."));
丁劲犇's avatar
丁劲犇 已提交
126 127 128 129 130 131
						emit evt_close_client(this->sock());
					}
				}
				else if (m_currentHeader.source_id==0xFFFFFFFF)
				{
					m_currentBlock = QByteArray();
丁劲犇's avatar
丁劲犇 已提交
132
					emit evt_Message(this,tr("warning, UUID 0xFFFFFFFF.ignore"));
丁劲犇's avatar
丁劲犇 已提交
133

丁劲犇's avatar
丁劲犇 已提交
134 135 136 137
				}
				else
				{
					m_currentBlock = QByteArray();
丁劲犇's avatar
丁劲犇 已提交
138
					emit evt_Message(this,tr("Bad UUID %1. Client Kicked out").arg(m_currentHeader.source_id));
丁劲犇's avatar
丁劲犇 已提交
139 140 141
					emit evt_close_client(this->sock());
				}
			}
丁劲犇's avatar
丁劲犇 已提交
142

丁劲犇's avatar
丁劲犇 已提交
143 144 145 146 147 148 149 150
		}
		else
		{
			//find Destin Client using Hash.
			st_clientNode_baseTrans * destin_node = m_pClientTable->clientNodeFromUUID(m_currentHeader.destin_id);
			if (destin_node==NULL)
			{
				//need server-to-server channels to re-post this message.
151 152 153 154 155 156
				QString svr = m_pClientTable->cross_svr_find_uuid(m_currentHeader.destin_id);
				if (svr.length()<=0)
					emit evt_Message(this,tr("Destin ID ") + QString("%1").arg(m_currentHeader.destin_id) + tr(" is not currently logged in."));
				else
					m_pClientTable->cross_svr_send_data(svr,m_currentBlock);

丁劲犇's avatar
丁劲犇 已提交
157

丁劲犇's avatar
丁劲犇 已提交
158 159 160 161 162
			}
			else
			{
				emit evt_SendDataToClient(destin_node->sock(),m_currentBlock);
				m_currentBlock = QByteArray();
丁劲犇's avatar
丁劲犇 已提交
163

丁劲犇's avatar
丁劲犇 已提交
164
			}
丁劲犇's avatar
丁劲犇 已提交
165

丁劲犇's avatar
丁劲犇 已提交
166 167 168 169 170
		}
		if (bytesLeft()==0)
			m_current_app_header.header.MsgType = 0x00;
		return     st_clientNode_baseTrans::deal_current_message_block();
	}
丁劲犇's avatar
丁劲犇 已提交
171

丁劲犇's avatar
丁劲犇 已提交
172 173 174 175
	//deal to-server handshakes
	bool st_clientNodeAppLayer::Deal_ToServer_Handshakes()
	{
		bool res = true;
176 177
		//qDebug()<<m_currentHeader.data_length<<"\n";
		//qDebug()<<this->m_currentBlock.toHex()<<"\n";
178
		if (m_currentHeader.data_length < sizeof (EXAMPLE_MSG_APP::tag_app_layer_header))
丁劲犇's avatar
丁劲犇 已提交
179
			return false;
180
		if (m_currentMessageSize < sizeof(EXAMPLE_TRANS_MSG) - 1 + sizeof (EXAMPLE_MSG_APP::tag_app_layer_header))
丁劲犇's avatar
丁劲犇 已提交
181 182 183 184 185 186 187
		{
			// header is not complete, return
			return true;
		}
		//Catch the header
		if (m_current_app_header.header.MsgType==0x00)
			memcpy((void *)&this->m_current_app_header,
188 189
				   ((unsigned char *)this->m_currentBlock.constData()) + sizeof(EXAMPLE_TRANS_MSG) - 1,
				   sizeof (EXAMPLE_MSG_APP::tag_app_layer_header)
丁劲犇's avatar
丁劲犇 已提交
190
				   );
丁劲犇's avatar
丁劲犇 已提交
191
		//qDebug()<<m_current_app_header.header.MsgType<<"\n";
丁劲犇's avatar
丁劲犇 已提交
192 193
		switch (m_current_app_header.header.MsgType)
		{
194
		case 0x0001:
丁劲犇's avatar
丁劲犇 已提交
195 196 197
			if (bytesLeft()>0)
				// message is not complete, return
				return true;
198
			if (m_currentMessageSize>
199 200
					sizeof(EXAMPLE_TRANS_MSG) - 1
					+ sizeof (EXAMPLE_MSG_APP::tag_app_layer_header)
201
					+ sizeof (stMsg_ClientLoginReq)+66)
丁劲犇's avatar
丁劲犇 已提交
202
			{
丁劲犇's avatar
丁劲犇 已提交
203
				emit evt_Message(this,tr("Broken Message stMsg_ClientLoginReq, size not correct."));
丁劲犇's avatar
丁劲犇 已提交
204 205 206 207 208 209
				res = false;
			}
			else
				res = this->LoginClient();
			break;
		default:
丁劲犇's avatar
丁劲犇 已提交
210
			emit evt_Message(this,tr("Message type not supported."));
丁劲犇's avatar
丁劲犇 已提交
211 212 213
			res = false;
			break;
		}
丁劲犇's avatar
丁劲犇 已提交
214

丁劲犇's avatar
丁劲犇 已提交
215
		m_currentBlock.clear();
丁劲犇's avatar
丁劲犇 已提交
216 217


丁劲犇's avatar
丁劲犇 已提交
218 219
		return res;
	}
丁劲犇's avatar
丁劲犇 已提交
220

丁劲犇's avatar
丁劲犇 已提交
221 222 223 224
	//Deal Box2Svr Msgs
	bool st_clientNodeAppLayer::Deal_Node2Svr_Msgs()
	{
		bool res = true;
丁劲犇's avatar
丁劲犇 已提交
225

226
		if (m_currentHeader.data_length < sizeof (EXAMPLE_MSG_APP::tag_app_layer_header))
丁劲犇's avatar
丁劲犇 已提交
227
			return false;
228
		if (m_currentMessageSize < sizeof(EXAMPLE_TRANS_MSG) - 1 + sizeof (EXAMPLE_MSG_APP::tag_app_layer_header))
丁劲犇's avatar
丁劲犇 已提交
229 230 231 232 233 234 235
		{
			// header is not complete, return
			return true;
		}
		//Catch the header
		if (m_current_app_header.header.MsgType==0x00)
			memcpy((void *)&this->m_current_app_header,
236 237
				   ((unsigned char *)this->m_currentBlock.constData()) + sizeof(EXAMPLE_TRANS_MSG) - 1,
				   sizeof (EXAMPLE_MSG_APP::tag_app_layer_header)
丁劲犇's avatar
丁劲犇 已提交
238 239 240 241
				   );
		//do
		switch (m_current_app_header.header.MsgType)
		{
242

丁劲犇's avatar
丁劲犇 已提交
243 244 245
		case 0x1003:
			if (bytesLeft()>0)
				return true;
丁劲犇's avatar
丁劲犇 已提交
246

丁劲犇's avatar
丁劲犇 已提交
247
			if (m_currentMessageSize<
248 249
					sizeof(EXAMPLE_TRANS_MSG) - 1
					+ sizeof (EXAMPLE_MSG_APP::tag_app_layer_header)
丁劲犇's avatar
丁劲犇 已提交
250 251
					+ sizeof (stMsg_UploadUserListReq) - sizeof(quint32))
			{
丁劲犇's avatar
丁劲犇 已提交
252
				emit evt_Message(this,tr("Broken Message stMsg_UploadUserListReq, size not correct."));
丁劲犇's avatar
丁劲犇 已提交
253 254 255 256 257 258 259 260 261 262
				res = false;
			}
			else
				res = this->Box2Svr_UploadUserTable();
			break;
		case 0x1004:
			if (bytesLeft()>0)
				// message is not complete, return
				return true;
			if (m_currentMessageSize!=
263 264
					sizeof(EXAMPLE_TRANS_MSG) - 1
					+ sizeof (EXAMPLE_MSG_APP::tag_app_layer_header)
265
					/*+ sizeof (stMsg_DownloadUserListReq)*/)
丁劲犇's avatar
丁劲犇 已提交
266
			{
丁劲犇's avatar
丁劲犇 已提交
267
				emit evt_Message(this,tr("Broken Message stMsg_DownloadUserListReq, size not correct."));
丁劲犇's avatar
丁劲犇 已提交
268 269 270 271 272
				res = false;
			}
			else
				res = this->Box2Svr_DownloadUserTable();
			break;
273
		case 0x1002:
丁劲犇's avatar
丁劲犇 已提交
274 275 276 277
			if (bytesLeft()>0)
				// message is not complete, return
				return true;
			if (m_currentMessageSize!=
278 279
					sizeof(EXAMPLE_TRANS_MSG) - 1
					+ sizeof (EXAMPLE_MSG_APP::tag_app_layer_header)
丁劲犇's avatar
丁劲犇 已提交
280 281
					+ sizeof (stMsg_ClientLogoutReq))
			{
丁劲犇's avatar
丁劲犇 已提交
282
				emit evt_Message(this,tr("Broken Message stMsg_ClientLogoutReq, size not correct."));
丁劲犇's avatar
丁劲犇 已提交
283 284 285 286 287
				res = false;
			}
			else
				res = this->ClientLogout();
			break;
288

丁劲犇's avatar
丁劲犇 已提交
289
		default:
丁劲犇's avatar
丁劲犇 已提交
290
			emit evt_Message(this,tr("Message type not supported."));
丁劲犇's avatar
丁劲犇 已提交
291 292 293
			res = false;
			break;
		}
丁劲犇's avatar
丁劲犇 已提交
294

丁劲犇's avatar
丁劲犇 已提交
295
		m_currentBlock.clear();
丁劲犇's avatar
丁劲犇 已提交
296 297


丁劲犇's avatar
丁劲犇 已提交
298 299
		return res;
	}
丁劲犇's avatar
丁劲犇 已提交
300

丁劲犇's avatar
丁劲犇 已提交
301
}