bool OnDataReceived(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); }
private void OnMessageEvent(MsgHead head, MemoryStream ms) { if (mhtMsgDelegation.ContainsKey(head.unMsgID)) { MsgDelegation myDelegationHandler = (MsgDelegation)mhtMsgDelegation[head.unMsgID]; ms.Position = 0; myDelegationHandler(head.unMsgID, ms); } else { Debug.LogError("ReciveMsg:" + head.unMsgID + " and no handler!!!!"); } }
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(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(body_head, head.unDataLen)) { OnClientDisconnect(new NFNetEventParams()); } OnDataReceived(); } } } }
private void OnMessageEvent(MsgHead head, byte[] bytes) { if (head.unDataLen != bytes.Length + ConstDefine.NF_PACKET_HEAD_SIZE) { Debug.LogError("ReciveMsg:" + head.unMsgID + " Size:" + head.unDataLen); return; } if (mhtMsgDelegation.ContainsKey(head.unMsgID)) { MsgDelegation myDelegationHandler = (MsgDelegation)mhtMsgDelegation[head.unMsgID]; myDelegationHandler(head.unMsgID, new MemoryStream(bytes)); } else { Debug.LogError("ReciveMsg:" + head.unMsgID + " and no handler!!!!"); } }