st_clientnode_basetrans.cpp 7.3 KB
Newer Older
丁劲犇's avatar
丁劲犇 已提交
1
#include "st_clientnode_basetrans.h"
2
#include "st_client_table.h"
3
namespace ExampleServer{
丁劲犇's avatar
丁劲犇 已提交
4 5 6 7 8 9 10 11 12 13 14 15
	st_clientNode_baseTrans::st_clientNode_baseTrans(st_client_table * pClientTable, QObject * pClientSock ,QObject *parent) :
		zp_plTaskBase(parent)
	{
		m_bUUIDRecieved = false;
		m_currentReadOffset = 0;
		m_currentMessageSize = 0;
		m_pClientSock = pClientSock;
		m_uuid = 0xffffffff;//Not Valid
		m_pClientTable = pClientTable;
		bTermSet = false;
		m_last_Report = QDateTime::currentDateTime();
	}
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
	quint32 st_clientNode_baseTrans::uuid()
	{
		return m_uuid;
	}
	QObject * st_clientNode_baseTrans::sock()
	{
		return m_pClientSock;
	}
	bool st_clientNode_baseTrans::uuidValid()
	{
		return m_bUUIDRecieved;
	}
	QDateTime st_clientNode_baseTrans::lastActiveTime()
	{
		return m_last_Report;
	}
	qint32 st_clientNode_baseTrans::bytesLeft()
	{
34
		return m_currentHeader.data_length + sizeof(EXAMPLE_TRANS_MSG) - 1
35 36 37 38 39
				-m_currentMessageSize ;
	}
	//judge whether id is valid.
	bool st_clientNode_baseTrans::bIsValidUserId(quint32 id)
	{
丁劲犇's avatar
丁劲犇 已提交
40
		return id >=(unsigned int)0x00000002 && id <=(unsigned int)0xFFFFFFFE;
41 42
	}

丁劲犇's avatar
丁劲犇 已提交
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
	//The main functional method, will run in thread pool
	int st_clientNode_baseTrans::run()
	{
		if (bTermSet==true)
		{
			//qDebug()<<QString("%1(%2) Node Martked Deleted, return.\n").arg((unsigned int)this).arg(ref());
			return 0;
		}
		int nCurrSz = -1;
		int nMessage = m_nMessageBlockSize;
		while (--nMessage>=0 && nCurrSz!=0  )
		{
			QByteArray block;
			m_mutex_rawData.lock();
			if (m_list_RawData.size())
				block =  *m_list_RawData.begin();
			m_mutex_rawData.unlock();
			if (block.isEmpty()==false && block.isNull()==false)
			{
				m_currentReadOffset = filter_message(block,m_currentReadOffset);
				if (m_currentReadOffset >= block.size())
				{
					m_mutex_rawData.lock();
					m_list_RawData.pop_front();
					m_currentReadOffset = 0;
					m_mutex_rawData.unlock();
				}
			}
			else
			{
				m_mutex_rawData.lock();
				//pop empty cabs
				if (m_list_RawData.empty()==false)
					m_list_RawData.pop_front();
				m_mutex_rawData.unlock();
			}
			m_mutex_rawData.lock();
			nCurrSz = m_list_RawData.size();
			m_mutex_rawData.unlock();
		}
		m_mutex_rawData.lock();
		nCurrSz = m_list_RawData.size();
		m_mutex_rawData.unlock();
		if (nCurrSz==0)
			return 0;
		return -1;
	}
90

丁劲犇's avatar
丁劲犇 已提交
91 92 93 94 95
	//push new binary data into queue
	int st_clientNode_baseTrans::push_new_data(const  QByteArray &  dtarray)
	{
		int res = 0;
		m_mutex_rawData.lock();
96

丁劲犇's avatar
丁劲犇 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110
		m_list_RawData.push_back(dtarray);
		res = m_list_RawData.size();
		m_mutex_rawData.unlock();
		m_last_Report = QDateTime::currentDateTime();
		return res;
	}
	//!deal one message, affect m_currentRedOffset,m_currentMessageSize,m_currentHeader
	//!return bytes Used.
	int st_clientNode_baseTrans::filter_message(const QByteArray & block, int offset)
	{
		const int blocklen = block.length();
		while (blocklen>offset)
		{
			const char * dataptr = block.constData();
111

丁劲犇's avatar
丁劲犇 已提交
112 113 114 115 116 117 118 119
			//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;
120

丁劲犇's avatar
丁劲犇 已提交
121 122 123 124 125
			if (m_currentMessageSize==2)
			{
				const char * headerptr = m_currentBlock.constData();
				memcpy((void *)&m_currentHeader,headerptr,2);
			}
126

丁劲犇's avatar
丁劲犇 已提交
127 128 129 130
			const char * ptrCurrData = m_currentBlock.constData();
			//Heart Beating
			if (m_currentHeader.Mark == 0xBEBE)
			{
131
				while (m_currentMessageSize< sizeof(EXAMPLE_HEARTBEATING) && blocklen>offset )
丁劲犇's avatar
丁劲犇 已提交
132 133 134 135
				{
					m_currentBlock.push_back(dataptr[offset++]);
					m_currentMessageSize++;
				}
136
				if (m_currentMessageSize < sizeof(EXAMPLE_HEARTBEATING)) //Header not completed.
丁劲犇's avatar
丁劲犇 已提交
137 138 139 140 141 142 143 144 145 146 147
					continue;
				//Send back
				emit evt_SendDataToClient(this->sock(),m_currentBlock);
				//This Message is Over. Start a new one.
				m_currentMessageSize = 0;
				m_currentBlock = QByteArray();
				continue;
			}
			else if (m_currentHeader.Mark == 0x55AA)
				//Trans Message
			{
148
				while (m_currentMessageSize< sizeof(EXAMPLE_TRANS_MSG)-1 && blocklen>offset)
丁劲犇's avatar
丁劲犇 已提交
149 150 151 152
				{
					m_currentBlock.push_back(dataptr[offset++]);
					m_currentMessageSize++;
				}
153
				if (m_currentMessageSize < sizeof(EXAMPLE_TRANS_MSG)-1) //Header not completed.
丁劲犇's avatar
丁劲犇 已提交
154
					continue;
155
				else if (m_currentMessageSize == sizeof(EXAMPLE_TRANS_MSG)-1)//Header just  completed.
丁劲犇's avatar
丁劲犇 已提交
156 157
				{
					const char * headerptr = m_currentBlock.constData();
158
					memcpy((void *)&m_currentHeader,headerptr,sizeof(EXAMPLE_TRANS_MSG)-1);
159

丁劲犇's avatar
丁劲犇 已提交
160 161 162
					//continue reading if there is data left behind
					if (block.length()>offset)
					{
163
						qint32 bitLeft = m_currentHeader.data_length + sizeof(EXAMPLE_TRANS_MSG) - 1
丁劲犇's avatar
丁劲犇 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
								-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)
					{
185
						qint32 bitLeft = m_currentHeader.data_length + sizeof(EXAMPLE_TRANS_MSG) - 1
丁劲犇's avatar
丁劲犇 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
								-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
			{
丁劲犇's avatar
丁劲犇 已提交
206
				emit evt_Message(this,tr("Client Send a unknown start Header %1 %2. Close client immediately.")
丁劲犇's avatar
丁劲犇 已提交
207 208 209 210 211 212 213
								 .arg((int)(ptrCurrData[0])).arg((int)(ptrCurrData[1])));
				m_currentMessageSize = 0;
				m_currentBlock = QByteArray();
				offset = blocklen;
				emit evt_close_client(this->sock());
			}
		} // end while block len > offset
214

丁劲犇's avatar
丁劲犇 已提交
215 216 217 218 219 220 221 222
		return offset;
	}
	//in Trans-Level, do nothing.
	int st_clientNode_baseTrans::deal_current_message_block()
	{
		//First, get uuid as soon as possible
		if (m_bUUIDRecieved==false)
		{
223
			if (bIsValidUserId( m_currentHeader.source_id) )
丁劲犇's avatar
丁劲犇 已提交
224 225 226 227 228 229 230 231 232 233 234 235
			{
				m_bUUIDRecieved = true;
				m_uuid =  m_currentHeader.source_id;
				//regisit client node to hash-table;
				m_pClientTable->regisitClientUUID(this);
			}
			else if (m_currentHeader.source_id==0xffffffff)
			{
				//New clients
			}
			else //Invalid
			{
丁劲犇's avatar
丁劲犇 已提交
236
				emit evt_Message(this,tr("Client ID is invalid! Close client immediatly."));
丁劲犇's avatar
丁劲犇 已提交
237 238 239 240 241 242
				m_currentBlock = QByteArray();
				emit evt_close_client(this->sock());
			}
		}
		else
		{
243
			if (!( bIsValidUserId(m_currentHeader.source_id)
丁劲犇's avatar
丁劲犇 已提交
244 245 246 247
				  ||
				  (m_currentHeader.source_id==0xffffffff)
				  ))
			{
丁劲犇's avatar
丁劲犇 已提交
248
				emit evt_Message(this,tr("Client ID is invalid! Close client immediatly."));
丁劲犇's avatar
丁劲犇 已提交
249 250 251
				m_currentBlock = QByteArray();
				emit evt_close_client(this->sock());
			}
252

丁劲犇's avatar
丁劲犇 已提交
253
		}
254

丁劲犇's avatar
丁劲犇 已提交
255 256 257 258 259 260 261 262
		return 0;
	}
	void st_clientNode_baseTrans::CheckHeartBeating()
	{
		QDateTime dtm = QDateTime::currentDateTime();
		qint64 usc = this->m_last_Report.secsTo(dtm);
		if (usc >=m_pClientTable->heartBeatingThrd())
		{
丁劲犇's avatar
丁劲犇 已提交
263
			emit evt_Message(this,tr("Client ") + QString("%1").arg((unsigned int)((quint64)this)) + tr(" is dead, kick out."));
丁劲犇's avatar
丁劲犇 已提交
264 265 266
			emit evt_close_client(this->sock());
		}
	}
267
}