public static short ReadShort(byte[] buf, ref int offset) { short val = ByteOrderConverter.NetworkToHostOrder(BitConverter.ToInt16(buf, offset)); offset += 2; return(val); }
public static UInt32 ReadUInt32(byte[] buf, ref int offset) { UInt32 val = ByteOrderConverter.NetworkToHostOrder(BitConverter.ToUInt32(buf, offset)); offset += 4; return(val); }
public static int ReadInt(byte[] buf, ref int offset) { int val = ByteOrderConverter.NetworkToHostOrder(BitConverter.ToInt32(buf, offset)); offset += 4; return(val); }
public void HandleReceive() { lock (mutex) { do { if (offset < DataSizeOffset + DataSize) { break; } // 先读取包的size int packet_size = ByteOrderConverter.NetworkToHostOrder(BitConverter.ToInt32(buffer, DataSizeOffset)); int proto_total_len = packet_size + WrapperLen; if (offset < proto_total_len) { break; } // 读取包id int id = ByteOrderConverter.NetworkToHostOrder(BitConverter.ToInt16(buffer, HandlerIdOfffset)); // 处理具体的包协议 object proto_data = null; int proto_data_deserialize_offset = DataOffset; switch (id) { case ResponseId.JoinRoomResult: { proto_data = JoinRoomResult.Deserialize(buffer, ref proto_data_deserialize_offset); } break; case ResponseId.BattleStart: { proto_data = BattleStart.Deserialize(buffer, ref proto_data_deserialize_offset); } break; case ResponseId.Tick: { proto_data = TickList.Deserialize(buffer, ref proto_data_deserialize_offset); } break; default: break; } ; if (proto_data != null) { ProtoData data = new ProtoData(); data.id = id; data.proto = proto_data; proto_data_queue.Enqueue(data); } // 包体读取完毕 Array.Copy(buffer, proto_total_len, buffer, 0, offset - proto_total_len); offset = offset - proto_total_len; }while(true); } }