public uint Unserialize(byte[] buff, uint offset, uint buffLen) { CreateInstance(); uint buffPos = offset; uint copyLen = 0; copyLen = 2; if (copyLen > buffLen - buffPos) { return(0); } byte[] tempBytes = new byte[8]; SerializeUtil.LEMemcpy(tempBytes, 0, buff, buffPos, copyLen); uint nameLen = (uint)BitConverter.ToUInt16(tempBytes, 0); buffPos += copyLen; if (nameLen > 255) { return(0); } if (nameLen > 0) { if (nameLen > buffLen - buffPos) { return(0); } for (int i = 0; i < nameLen; ++i) { name.Add(buff[buffPos + i]); } buffPos += nameLen; } copyLen = sizeof(int); SerializeUtil.LEMemcpy(tempBytes, 0, buff, buffPos, copyLen); age = BitConverter.ToInt32(tempBytes, 0); buffPos += copyLen; copyLen = sizeof(int); SerializeUtil.LEMemcpy(tempBytes, 0, buff, buffPos, copyLen); phone = BitConverter.ToInt32(tempBytes, 0); buffPos += copyLen; return(buffPos); }
public uint Serialize(byte[] buff, uint offset, uint buffLen) { uint buffPos = offset; uint copyLen = 0; copyLen = 2; SerializeUtil.LEMemcpy(buff, buffPos, BitConverter.GetBytes((ushort)list.Count), 0, copyLen); buffPos += copyLen; if (list.Count > 1000) { return(0); } if (list.Count > 0) { for (int i = 0; i < list.Count; ++i) { copyLen = list[i].Serialize(buff, buffPos, buffLen - (buffPos - offset)); buffPos += copyLen; } } copyLen = stru.Serialize(buff, buffPos, buffLen - (buffPos - offset)); buffPos += copyLen; copyLen = 2; SerializeUtil.LEMemcpy(buff, buffPos, BitConverter.GetBytes((ushort)numList.Count), 0, copyLen); buffPos += copyLen; if (numList.Count > 1000) { return(0); } if (numList.Count > 0) { copyLen = sizeof(int); for (int i = 0; i < numList.Count; ++i) { SerializeUtil.LEMemcpy(buff, buffPos, BitConverter.GetBytes(numList[i]), 0, copyLen); buffPos += copyLen; } } return(buffPos - offset); }
protected bool Invoke(FileDataBuff buff) { if (buff == null || _rpcCallbackArray == null) { return(false); } if (buff._dataLen < 4) { return(false); } uint readLen = 0; uint msgType = 0; uint msgLen = 0; byte[] ushortBytes = new byte[2]; readLen = 2; SerializeUtil.LEMemcpy(ushortBytes, 0, buff._data, buff.GetReadPos(), readLen); buff._readPos += readLen; msgType = BitConverter.ToUInt16(ushortBytes, 0); if (msgType >= _rpcCallbackNum) { return(false); } readLen = 2; SerializeUtil.LEMemcpy(ushortBytes, 0, buff._data, buff.GetReadPos(), readLen); buff._readPos += readLen; msgLen = BitConverter.ToUInt16(ushortBytes, 0); if (buff.GetUnreadLen() != msgLen) { return(false); } return(_rpcCallbackArray[msgType](buff._data, buff.GetReadPos(), msgLen)); }
public uint Unserialize(byte[] buff, uint offset, uint buffLen) { CreateInstance(); uint buffPos = offset; uint copyLen = 0; copyLen = 2; if (copyLen > buffLen - buffPos) { return(0); } byte[] tempBytes = new byte[8]; SerializeUtil.LEMemcpy(tempBytes, 0, buff, buffPos, copyLen); uint strMsgLen = (uint)BitConverter.ToUInt16(tempBytes, 0); buffPos += copyLen; if (strMsgLen > 1000) { return(0); } if (strMsgLen > 0) { if (strMsgLen > buffLen - buffPos) { return(0); } for (int i = 0; i < strMsgLen; ++i) { strMsg.Add(buff[buffPos + i]); } buffPos += strMsgLen; } return(buffPos); }
public uint Serialize(byte[] buff, uint offset, uint buffLen) { uint buffPos = offset; uint copyLen = 0; copyLen = 2; SerializeUtil.LEMemcpy(buff, buffPos, BitConverter.GetBytes((ushort)strMsg.Count), 0, copyLen); buffPos += copyLen; if (strMsg.Count > 1000) { return(0); } if (strMsg.Count > 0) { for (int i = 0; i < strMsg.Count; ++i) { buff[buffPos + i] = strMsg[i]; } buffPos += (uint)strMsg.Count; } return(buffPos - offset); }
public uint Unserialize(byte[] buff, uint offset, uint buffLen) { CreateInstance(); uint buffPos = offset; uint copyLen = 0; copyLen = 2; if (copyLen > buffLen - buffPos) { return(0); } byte[] tempBytes = new byte[8]; SerializeUtil.LEMemcpy(tempBytes, 0, buff, buffPos, copyLen); uint listLen = (uint)BitConverter.ToUInt16(tempBytes, 0); buffPos += copyLen; if (listLen > 1000) { return(0); } if (listLen > 0) { for (int i = 0; i < listLen; ++i) { SampleStruct item = new SampleStruct(); copyLen = item.Unserialize(buff, buffPos, buffLen - (buffPos - offset)); list.Add(item); buffPos += copyLen; } } copyLen = stru.Unserialize(buff, buffPos, buffLen - (buffPos - offset)); buffPos += copyLen; copyLen = 2; if (copyLen > buffLen - buffPos) { return(0); } SerializeUtil.LEMemcpy(tempBytes, 0, buff, buffPos, copyLen); uint numListLen = (uint)BitConverter.ToUInt16(tempBytes, 0); buffPos += copyLen; if (numListLen > 1000) { return(0); } if (numListLen > 0) { copyLen = sizeof(int); if (numListLen * copyLen > buffLen - buffPos) { return(0); } for (int i = 0; i < numListLen; ++i) { SerializeUtil.LEMemcpy(tempBytes, 0, buff, buffPos, copyLen); int item = BitConverter.ToInt32(tempBytes, 0); numList.Add(item); buffPos += copyLen; } } return(buffPos); }
public void PushSendData(ushort msgType, ISerializableObj data) { if (!IsConnected()) { return; } uint dataLen = data == null ? 0 : data.CalcDataLen(); uint needLen = dataLen + 4; uint copyLen = 0; DataBuff buff = null; List <DataBuff> sendBuffList = new List <DataBuff>(); if (needLen > FileDataBuff.MAX_FILE_SIZE) { return; } if (needLen > DataBuff.MAX_BUFF_LEN) { buff = ObjectPool <DataBuff> .Pop(); if (buff == null) { return; } sendBuffList.Add(buff); copyLen = 2; SerializeUtil.LEMemcpy(buff._data, buff.GetWritePos(), BitConverter.GetBytes(msgType), 0, copyLen); if (_bEncode) { Encode(buff._data, buff.GetWritePos(), copyLen); } buff._dataLen += copyLen; copyLen = 2; SerializeUtil.LEMemcpy(buff._data, buff.GetWritePos(), BitConverter.GetBytes((ushort)dataLen), 0, copyLen); if (_bEncode) { Encode(buff._data, buff.GetWritePos(), copyLen); } buff._dataLen += copyLen; if (data != null) { _sendFileBuff.Clear(); data.Serialize(_sendFileBuff._data, _sendFileBuff.GetWritePos(), FileDataBuff.MAX_FILE_SIZE); while (_sendFileBuff.GetUnreadLen() > 0) { if (buff.GetUnwriteLen() <= 0) { buff = ObjectPool <DataBuff> .Pop(); if (buff == null) { return; } _sendBuffList.Add(buff); } copyLen = Math.Min(buff.GetUnwriteLen(), _sendFileBuff.GetUnreadLen()); SerializeUtil.Memcpy(buff._data, buff.GetWritePos(), _sendFileBuff._data, _sendFileBuff.GetReadPos(), copyLen); if (_bEncode) { Encode(buff._data, buff.GetWritePos(), copyLen); } buff._dataLen += copyLen; _sendFileBuff._readPos += copyLen; } } } else { lock (_sendLocker) { if (_sendBuffList.Count > 0) { int tailIndex = _sendBuffList.Count - 1; sendBuffList.Add(_sendBuffList[tailIndex]); _sendBuffList.RemoveAt(tailIndex); } } bool needNew = sendBuffList.Count <= 0 || (DataBuff.MAX_BUFF_LEN - sendBuffList[sendBuffList.Count - 1]._dataLen < needLen); if (needNew) { buff = ObjectPool <DataBuff> .Pop(); } else { buff = sendBuffList[sendBuffList.Count - 1]; } if (buff != null) { sendBuffList.Add(buff); copyLen = 2; SerializeUtil.LEMemcpy(buff._data, buff.GetWritePos(), BitConverter.GetBytes(msgType), 0, copyLen); if (_bEncode) { Encode(buff._data, buff.GetWritePos(), copyLen); } buff._dataLen += copyLen; copyLen = 2; SerializeUtil.LEMemcpy(buff._data, buff.GetWritePos(), BitConverter.GetBytes((ushort)dataLen), 0, copyLen); if (_bEncode) { Encode(buff._data, buff.GetWritePos(), copyLen); } buff._dataLen += copyLen; if (data != null) { copyLen = data.Serialize(buff._data, buff.GetWritePos(), DataBuff.MAX_BUFF_LEN - buff._dataLen); if (_bEncode) { Encode(buff._data, buff.GetWritePos(), copyLen); } buff._dataLen += copyLen; } } } if (sendBuffList.Count > 0) { lock (_sendLocker) { _sendBuffList.AddRange(sendBuffList); sendBuffList.Clear(); } } if (!IsConnected()) { ClearSendBuffList(); } else { SendBytes(); } }
public FileDataBuff PopRecvData() { if (!IsConnected()) { return(null); } List <DataBuff> recvBuffList = new List <DataBuff>(); lock (_recvLocker) { List <DataBuff> tempList = _recvBuffList; _recvBuffList = recvBuffList; recvBuffList = tempList; } uint copyLen = 0; DataBuff buff = null; while (_recvFileBuff._dataLen < 4) { if (recvBuffList.Count <= 0) { return(null); } buff = recvBuffList[0]; copyLen = Math.Min(4 - _recvFileBuff._dataLen, buff.GetUnreadLen()); SerializeUtil.Memcpy(_recvFileBuff._data, _recvFileBuff.GetWritePos(), buff._data, buff.GetReadPos(), copyLen); buff._readPos += copyLen; _recvFileBuff._dataLen += copyLen; if (buff.GetUnreadLen() <= 0) { buff.Clear(); ObjectPool <DataBuff> .Push(buff); recvBuffList.RemoveAt(0); } } byte[] ushortBytes = new byte[2]; SerializeUtil.LEMemcpy(ushortBytes, 0, _recvFileBuff._data, 2, 2); uint msgLen = BitConverter.ToUInt16(ushortBytes, 0); if (msgLen + 4 > FileDataBuff.MAX_FILE_SIZE) { return(null); } uint needLen = msgLen - (_recvFileBuff._dataLen - 4); while (needLen > 0) { if (recvBuffList.Count <= 0) { return(null); } buff = recvBuffList[0]; copyLen = Math.Min(needLen, buff.GetUnreadLen()); SerializeUtil.Memcpy(_recvFileBuff._data, _recvFileBuff.GetWritePos(), buff._data, buff.GetReadPos(), copyLen); buff._readPos += copyLen; _recvFileBuff._dataLen += copyLen; if (buff.GetUnreadLen() <= 0) { buff.Clear(); ObjectPool <DataBuff> .Push(buff); recvBuffList.RemoveAt(0); } needLen -= copyLen; } if (recvBuffList.Count > 0) { lock (_recvLocker) { recvBuffList.AddRange(_recvBuffList); _recvBuffList.Clear(); _recvBuffList = recvBuffList; } } if (!IsConnected()) { ClearRecvBuffList(); } return(_recvFileBuff); }