bool OnDataReceived(NFNetClient xNetClient, byte[] bytes, UInt32 bytesCount) { if (bytes.Length == bytesCount) { byte[] headBytes = new byte[ConstDefine.NF_PACKET_HEAD_SIZE]; Array.Copy(bytes, 0, headBytes, 0, ConstDefine.NF_PACKET_HEAD_SIZE); MsgHead head = new MsgHead(); if (head.DeCode(headBytes) && head.unDataLen == bytesCount) { Int32 nBodyLen = (Int32)bytesCount - (Int32)ConstDefine.NF_PACKET_HEAD_SIZE; if (nBodyLen > 0) { byte[] body = new byte[nBodyLen]; Array.Copy(bytes, ConstDefine.NF_PACKET_HEAD_SIZE, body, 0, nBodyLen); OnMessageEvent(head, body); return(true); } else { //space packet } } } return(false); }
void OnDataReceived() { //ensure the length > size of MsgHead at all time if (mnPacketSize >= ConstDefine.NF_PACKET_HEAD_SIZE) { byte[] headBytes = new byte[ConstDefine.NF_PACKET_HEAD_SIZE]; Array.Copy(mPacket, 0, headBytes, 0, ConstDefine.NF_PACKET_HEAD_SIZE); MsgHead head = new MsgHead(); if (head.DeCode(headBytes)) { if (head.unDataLen == mnPacketSize) { byte[] body_head = new byte[head.unDataLen]; Array.Copy(mPacket, 0, body_head, 0, head.unDataLen); mnPacketSize = 0; if (false == OnDataReceived(mxClientNet, body_head, head.unDataLen)) { OnClientDisconnect(new NFNetEventParams()); } } else if (mnPacketSize > head.unDataLen) { UInt32 nNewLen = mnPacketSize - head.unDataLen; byte[] newpacket = new byte[nNewLen]; Array.Copy(mPacket, head.unDataLen, newpacket, 0, nNewLen); byte[] body_head = new byte[head.unDataLen]; Array.Copy(mPacket, 0, body_head, 0, head.unDataLen); //memset 0 Array.Clear(mPacket, 0, ConstDefine.MAX_PACKET_LEN); mnPacketSize = nNewLen; Array.Copy(newpacket, 0, mPacket, 0, nNewLen); if (false == OnDataReceived(mxClientNet, body_head, head.unDataLen)) { OnClientDisconnect(new NFNetEventParams()); } OnDataReceived(); } } } }