st_clientnode_app_imp.cpp 7.7 KB
Newer Older
丁劲犇's avatar
丁劲犇 已提交
1
#include "st_clientnode_applayer.h"
2 3 4 5
#include "st_client_table.h"
#include <QSqlDatabase>
#include <QSqlQuery>
#include <assert.h>
丁劲犇's avatar
丁劲犇 已提交
6 7 8 9
#include <QCoreApplication>
#include <QMutexLocker>
#include <QSettings>
#include <QSqlError>
10
#include <QSet>
11
namespace ExampleServer{
丁劲犇's avatar
丁劲犇 已提交
12 13 14

	bool st_clientNodeAppLayer::LoginClient()
	{
15 16
		const EXAMPLE_MSG_APP * pAppLayer =
				(const EXAMPLE_MSG_APP *)(
丁劲犇's avatar
丁劲犇 已提交
17
					((const char *)(m_currentBlock.constData()))
18 19 20 21 22
					+sizeof(EXAMPLE_TRANS_MSG)-1);
		int nAppLen = m_currentBlock.length()- (sizeof(EXAMPLE_TRANS_MSG)-1)- sizeof(tag_example_app_layer::tag_app_layer_header) - sizeof (quint32);
		QString strPasswd ;
		quint32 UserID = pAppLayer->MsgUnion.msg_ClientLoginReq.user_id;

23
		int nSwim = 0;
24 25
		while (  nSwim < 65 && nSwim <nAppLen && pAppLayer->MsgUnion.msg_ClientLoginReq.Passwd[nSwim]!=0 )
			strPasswd+= pAppLayer->MsgUnion.msg_ClientLoginReq.Passwd[nSwim++];
26 27


丁劲犇's avatar
丁劲犇 已提交
28
		//form Msgs
29
		quint16 nMsgLen = sizeof(EXAMPLE_MSG_APP::tag_app_layer_header)
丁劲犇's avatar
丁劲犇 已提交
30
				+sizeof(stMsg_ClientLoginRsp);
31
		QByteArray array(sizeof(EXAMPLE_TRANS_MSG) + nMsgLen - 1,0);
丁劲犇's avatar
丁劲犇 已提交
32
		char * ptr = array.data();
33 34 35
		EXAMPLE_TRANS_MSG * pMsg = (EXAMPLE_TRANS_MSG *)ptr;
		EXAMPLE_MSG_APP * pApp = (EXAMPLE_MSG_APP *)(((unsigned char *)
														  (ptr))+sizeof(EXAMPLE_TRANS_MSG)-1
丁劲犇's avatar
丁劲犇 已提交
36 37 38 39 40 41 42 43
														 );
		pMsg->Mark = 0x55AA;
		pMsg->source_id = (quint32)((quint64)(m_currentHeader.destin_id) & 0xffffffff );

		pMsg->destin_id = (quint32)((quint64)(m_currentHeader.source_id) & 0xffffffff );;

		pMsg->data_length = nMsgLen;

44
		pApp->header.MsgType = 0x7FFE;
丁劲犇's avatar
丁劲犇 已提交
45 46 47 48 49 50 51 52 53 54 55 56

		stMsg_ClientLoginRsp & reply = pApp->MsgUnion.msg_ClientLoginRsp;

		//Check the database, find current equipment info
		QSqlDatabase db = m_pClientTable->dbRes()->databse(m_pClientTable->Database_UserAcct());

		reply.DoneCode = 3;
		//strcpy(reply.TextInfo,"Unknown error");
		if (db.isValid()==true && db.isOpen()==true )
		{
			QSqlQuery query(db);

57
			QString sql = "select user_id,password from users where user_id = ? and password = ?;";
丁劲犇's avatar
丁劲犇 已提交
58
			query.prepare(sql);
59
			query.addBindValue(UserID);
丁劲犇's avatar
丁劲犇 已提交
60 61 62 63 64 65 66 67 68 69
			query.addBindValue(strPasswd);

			if (true==query.exec())
			{
				if (query.next())
				{
					bool bOk = false;
					quint32 ncurrid = query.value(1).toUInt(&bOk);
					if (bOk==true)
					{
70
						if (this->bIsValidUserId(ncurrid))
丁劲犇's avatar
丁劲犇 已提交
71
						{
72

丁劲犇's avatar
丁劲犇 已提交
73 74 75 76 77 78 79 80 81 82
							reply.DoneCode = 0;
							reply.UserID = ncurrid;
							m_bLoggedIn = true;
							m_bUUIDRecieved = true;
							m_uuid = ncurrid;
							m_pClientTable->regisitClientUUID(this);
							if (false == loadRelations())
							{
								reply.DoneCode = 3;
							}
83 84 85 86 87
							//Cluster-Balance.
							if (m_pClientTable->NeedRedirect(reply.Address_Redirect,&reply.port_Redirect))
							{
								reply.DoneCode = 1;
							}
丁劲犇's avatar
丁劲犇 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
						}
						// else
						// strcpy(reply.TextInfo,"UserID Is Invalid.Accunt locked by svr");
					}
					//else
					// strcpy(reply.TextInfo,"UserID Is Invalid.Accunt locked by svr");
				}
				else
				{
					// No such device
					//strcpy(reply.TextInfo,"No such user or password.");
				}
			}
			else
			{
				//strcpy(reply.TextInfo,"Server Access Error.");
丁劲犇's avatar
丁劲犇 已提交
104
				emit evt_Message(this,tr("Database Access Error :")+query.lastError().text());
丁劲犇's avatar
丁劲犇 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118
			}
		}
		else
		{
			//Server db is currently not accessable, wait.
			//strcpy(reply.TextInfo,"Server Not Accessable Now.");
		}


		//Send back
		emit evt_SendDataToClient(this->sock(),array);



119
		return reply.DoneCode==3?false:true;
丁劲犇's avatar
丁劲犇 已提交
120 121 122
	}
	bool st_clientNodeAppLayer::Box2Svr_UploadUserTable()
	{
123 124
		const EXAMPLE_MSG_APP * pAppLayer =
				(const EXAMPLE_MSG_APP *)(
丁劲犇's avatar
丁劲犇 已提交
125
					((const char *)(m_currentBlock.constData()))
126
					+sizeof(EXAMPLE_TRANS_MSG)-1);
丁劲犇's avatar
丁劲犇 已提交
127

128 129
		if (m_currentMessageSize!=sizeof(EXAMPLE_TRANS_MSG) - 1
				+ sizeof (EXAMPLE_MSG_APP::tag_app_layer_header)
丁劲犇's avatar
丁劲犇 已提交
130 131 132 133
				+ sizeof (stMsg_UploadUserListReq) - sizeof(quint32)
				+ sizeof (quint32) * (pAppLayer->MsgUnion.msg_UploadUserListReq.UserNum)
				)
		{
丁劲犇's avatar
丁劲犇 已提交
134
			emit evt_Message(this,tr("Broken Message stMsg_UploadUserListReq, size not correct."));
丁劲犇's avatar
丁劲犇 已提交
135 136 137 138
			return false;
		}

		//form Msgs
139
		quint16 nMsgLen = sizeof(EXAMPLE_MSG_APP::tag_app_layer_header)
丁劲犇's avatar
丁劲犇 已提交
140
				+sizeof(stMsg_UploadUserListRsp);
141
		QByteArray array(sizeof(EXAMPLE_TRANS_MSG) + nMsgLen - 1,0);
丁劲犇's avatar
丁劲犇 已提交
142
		char * ptr = array.data();
143 144 145
		EXAMPLE_TRANS_MSG * pMsg = (EXAMPLE_TRANS_MSG *)ptr;
		EXAMPLE_MSG_APP * pApp = (EXAMPLE_MSG_APP *)(((unsigned char *)
														  (ptr))+sizeof(EXAMPLE_TRANS_MSG)-1
丁劲犇's avatar
丁劲犇 已提交
146 147 148 149 150 151 152 153
														 );
		pMsg->Mark = 0x55AA;
		pMsg->source_id = (quint32)((quint64)(m_currentHeader.destin_id) & 0xffffffff );

		pMsg->destin_id = (quint32)((quint64)(m_currentHeader.source_id) & 0xffffffff );;

		pMsg->data_length = nMsgLen;

154
		pApp->header.MsgType = 0x7FFC;
丁劲犇's avatar
丁劲犇 已提交
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

		stMsg_UploadUserListRsp & reply = pApp->MsgUnion.msg_UploadUserListRsp;

		reply.DoneCode = 1;
		//strcpy(reply.TextInfo,"Unknown error");
		if (loadRelations()==true )
		{
			for (quint32 i = 0; i <pAppLayer->MsgUnion.msg_UploadUserListReq.UserNum && i<4096;i++)
			{
				if (bIsValidUserId(pAppLayer->MsgUnion.msg_UploadUserListReq.pUserIDList[i])==false)
					continue;
				m_matched_nodes.insert(pAppLayer->MsgUnion.msg_UploadUserListReq.pUserIDList[i]);
			}
			if (true == saveRelations())
			{
				reply.DoneCode = 0;
				//strcpy(reply.TextInfo,"Succeeded.");
			}
		}
		else
		{
			//Server db is currently not accessable, wait.
			//strcpy(reply.TextInfo,"Failed to load current relations from db.");
		}


		//Send back
		emit evt_SendDataToClient(this->sock(),array);



		return reply.DoneCode==0?true:false;
	}
	bool st_clientNodeAppLayer::Box2Svr_DownloadUserTable()
	{
		bool res = true;
		//form Msgs
192
		quint16 nMsgLen = sizeof(EXAMPLE_MSG_APP::tag_app_layer_header)
丁劲犇's avatar
丁劲犇 已提交
193 194 195 196 197 198 199 200 201 202 203
				+sizeof(stMsg_DownloadUserListRsp) - sizeof(quint32);
		int nSz = 0;
		if (loadRelations()==true )
		{
			nSz = m_matched_nodes.size();
			nMsgLen += nSz * sizeof(quint32);
		}
		else
			res = false;

		//form Msgs
204
		QByteArray array(sizeof(EXAMPLE_TRANS_MSG) + nMsgLen - 1,0);
丁劲犇's avatar
丁劲犇 已提交
205
		char * ptr = array.data();
206 207 208
		EXAMPLE_TRANS_MSG * pMsg = (EXAMPLE_TRANS_MSG *)ptr;
		EXAMPLE_MSG_APP * pApp = (EXAMPLE_MSG_APP *)(((unsigned char *)
														  (ptr))+sizeof(EXAMPLE_TRANS_MSG)-1
丁劲犇's avatar
丁劲犇 已提交
209 210 211 212 213 214 215 216
														 );
		pMsg->Mark = 0x55AA;
		pMsg->source_id = (quint32)((quint64)(m_currentHeader.destin_id) & 0xffffffff );

		pMsg->destin_id = (quint32)((quint64)(m_currentHeader.source_id) & 0xffffffff );;

		pMsg->data_length = nMsgLen;

217
		pApp->header.MsgType = 0x7FFB;
丁劲犇's avatar
丁劲犇 已提交
218 219 220 221 222

		stMsg_DownloadUserListRsp & reply = pApp->MsgUnion.msg_DownloadUserListRsp;

		reply.DoneCode = res==true?0:1;
		//if (res==false)
丁劲犇's avatar
丁劲犇 已提交
223
		//strcpy(reply.TextInfo,"load Relation failed");
丁劲犇's avatar
丁劲犇 已提交
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
		int ii = 0;
		foreach (quint32 it, m_matched_nodes)
		{
			if (ii < nSz && ii<32768)
			{
				reply.pUserIDList[ii] = it;
				reply.UserNum = (quint16) ii+1;
			}
			++ii;
		}


		//Send back
		emit evt_SendDataToClient(this->sock(),array);



		return reply.DoneCode==0?true:false;
	}
	bool st_clientNodeAppLayer::ClientLogout()
	{
		bool res = true;
		//form Msgs
247
		quint16 nMsgLen = sizeof(EXAMPLE_MSG_APP::tag_app_layer_header)
丁劲犇's avatar
丁劲犇 已提交
248 249 250
				+sizeof(stMsg_ClientLogoutRsp);
		//int nSz = 0;
		//form Msgs
251
		QByteArray array(sizeof(EXAMPLE_TRANS_MSG) + nMsgLen - 1,0);
丁劲犇's avatar
丁劲犇 已提交
252
		char * ptr = array.data();
253 254 255
		EXAMPLE_TRANS_MSG * pMsg = (EXAMPLE_TRANS_MSG *)ptr;
		EXAMPLE_MSG_APP * pApp = (EXAMPLE_MSG_APP *)(((unsigned char *)
														  (ptr))+sizeof(EXAMPLE_TRANS_MSG)-1
丁劲犇's avatar
丁劲犇 已提交
256 257 258 259 260 261 262 263 264
														 );
		pMsg->Mark = 0x55AA;
		pMsg->source_id = (quint32)((quint64)(m_currentHeader.destin_id) & 0xffffffff );

		pMsg->destin_id = (quint32)((quint64)(m_currentHeader.source_id) & 0xffffffff );;

		pMsg->data_length = nMsgLen;


265
		pApp->header.MsgType = 0x7FFD;
丁劲犇's avatar
丁劲犇 已提交
266 267 268 269 270

		stMsg_ClientLogoutRsp & reply = pApp->MsgUnion.msg_ClientLogoutRsp;

		reply.DoneCode = res==true?0:1;
		//if (res==false)
丁劲犇's avatar
丁劲犇 已提交
271
		//strcpy(reply.TextInfo,"load Relation failed");
丁劲犇's avatar
丁劲犇 已提交
272 273 274 275
		//Send back
		emit evt_SendDataToClient(this->sock(),array);
		return reply.DoneCode==0?true:false;
	}
276
}