private void Client_PacketReceieved(object sender, EventArgs e) { Client client = (Client)sender; if (client.Packet.ID == PacketID.Ping) { Packet pong = new Packet(PacketID.Ping); client.BeginSend(pong); } else if (client.Packet.ID == Packets.PacketID.Unknown) { Console.WriteLine("Unknown packet receieved. that should not happen :D"); } else { if (Enum.IsDefined(typeof(PacketID), client.Packet.ID)) { PacketReceieved?.Invoke(client, null); } else { Console.WriteLine("unhandled packet receieved. disconnecting client."); Disconnect(client); } } }
void EndReceive(IAsyncResult ar) { try { int bytesRead = socket.EndReceive(ar); lock (syncRoot) { totalBytesDown += (UInt32)bytesRead; bytesSecDown += (UInt16)bytesRead; } if (bytesRead > 0) { packet = null; using (MemoryStream stream = new MemoryStream(buffer)) { BinaryFormatter formatter = new BinaryFormatter(); try { packet = (IPacket)formatter.Deserialize(stream); } catch (Exception E) { Console.WriteLine(E.Message); Console.WriteLine("deserialization problem"); } } if (packet != null) { PacketReceieved?.Invoke(this, null); } } else { Disconnect(); } BeginReceive(); } catch// (Exception E) { // noom: dont need an errormessage here //Console.WriteLine(E.Message); //Console.WriteLine("endreceieve error"); Disconnect(); } }