private void readPacketData(ByteArray data) { int num = this._expectedLength - (int)this.packetStream.Length; bool flag = data.Length >= num; try { if (flag) { resizeByteArray(data, num); this.packetStream.Flush(); this.dispatchRequest(this.packetStream); this.readState = PacketReadState.WAIT_NEW_PACKET; resizeByteArray(data, data.Length - num); } else { resizeByteArray(data, data.Length); } } catch (Exception ex) { Debugger.LogException("Error handling data ", ex); this._skipBytes = num; this.readState = PacketReadState.INVALID_DATA; } }
/// <summary> /// 开始读取新包 /// </summary> /// <param name="data"></param> private void readNewPacket(ByteArray data) { this.packetStream.Seek(0, SeekOrigin.Begin); this.packetStream.SetLength(0); this._expectedLength = -1; this.readState = PacketReadState.WAIT_DATA_SIZE; }
/// <summary> /// 非法数据读取 /// </summary> /// <param name="data"></param> /// <returns></returns> private void readInvalidData(ByteArray data) { if (this._skipBytes == 0) { this.readState = PacketReadState.WAIT_NEW_PACKET; return; } int pos = Math.Min(data.Length, this._skipBytes); resizeByteArray(data, data.Length - pos); this._skipBytes = this._skipBytes - pos; }
/// <summary> /// 读取数据的大小 /// </summary> /// <param name="msg"></param> private void readMsgSize(ByteArray msg) { int num = msg.ReadInt(); int pos = PacketMsg.INT_BYTE_SIZE; if (num != -1) { this._expectedLength = num; resizeByteArray(msg, msg.Length - pos); this.readState = PacketReadState.WAIT_DATA; return; } this.readState = PacketReadState.WAIT_DATA_SIZE_FRAGMENT; resizeByteArray(msg, msg.Length); }
/// <summary> /// 读取消息的数据片段 /// </summary> /// <param name="msg"></param> private void readMsgFragment(ByteArray msg) { int num1 = PacketMsg.INT_BYTE_SIZE - (int)this.packetStream.Length; if (msg.Length >= num1) { resizeByteArray(msg, num1); if (msg.Length > num1) { resizeByteArray(msg, msg.Length - num1); } this.readState = PacketReadState.WAIT_DATA; return; } resizeByteArray(msg, msg.Length); }
public PacketMsg() { packetStream = new MemoryStream(); readState = PacketReadState.WAIT_NEW_PACKET; }