int DisptachPacket(byte[] packet, long size) { MemoryStream stream = new MemoryStream(packet); BinaryReader br = new BinaryReader(stream); int bytesConsumed = 0; while (stream.Position + 24 < size) { uint networkID = br.ReadUInt32(); string packetID = Utils.ReadString(br, 12); uint length = br.ReadUInt32(); uint checksum = br.ReadUInt32(); if (stream.Position + length > size) { break; } //Console.WriteLine(packetID); if (networkID == mNetworkID) { byte[] payload = br.ReadBytes((int)length); byte[] hash = Utils.GenerateHash(payload); if (CompareChecksum(checksum, hash)) { // valid packet, get the handler if (mPacketProcessors.ContainsKey(packetID)) { PacketProcessor p = mPacketProcessors[packetID]; p(payload); } else { Console.WriteLine("Unhandled packet: " + packetID); } } else { Console.WriteLine("Failed checksum!"); } } else { Console.WriteLine("Failed ID Check, Network ID: " + networkID); } bytesConsumed += 24 + (int)length; } br.Close(); return(bytesConsumed); }
void PH_Alert(byte[] payload) { MemoryStream stream = new MemoryStream(payload); BinaryReader br = new BinaryReader(stream); UInt64 msgLen = Utils.ReadVarInt(br); string msg = Utils.ReadString(br, (int)msgLen); UInt64 sigLen = Utils.ReadVarInt(br); string sig = Utils.ReadString(br, (int)sigLen); Console.WriteLine("Alert: " + msg + " from: " + sig); br.Close(); }