public void ReadCallback(IAsyncResult ar) { ClientState state = (ClientState)ar.AsyncState; Client client; SocketError errorCode; int bytesRead = 0; if (this is Login) { Logger.InfoFormat("This is a login ReadCallback"); } try { bytesRead = state.WorkSocket.EndReceive(ar, out errorCode); if (errorCode != SocketError.Success) { bytesRead = 0; } } catch (SocketException) { state.WorkSocket.Close(); } catch (ObjectDisposedException) { state.WorkSocket.Close(); } if (!GlobalConnectionManifest.ConnectedClients.TryGetValue(state.Id, out client)) { // Is this a redirect? Redirect redirect; if (!GlobalConnectionManifest.TryGetRedirect(state.Id, out redirect)) { Logger.ErrorFormat("Receive: data from unknown client (id {0}, closing connection", state.Id); state.WorkSocket.Close(); state.WorkSocket.Dispose(); return; } client = redirect.Client; client.ClientState = state; GlobalConnectionManifest.RegisterClient(client); } if (bytesRead > 0) { if (this is Login) { Logger.InfoFormat("Dafuq bro"); } var inboundBytes = state.ReceiveBufferTake(bytesRead).ToArray(); if (inboundBytes[0] != 0xAA) { Logger.DebugFormat("cid {0}: client is wat", client.ConnectionId); state.ResetReceive(); } else { while (inboundBytes.Length > 3) { Logger.InfoFormat("Inside while loop"); var packetLength = ((int)inboundBytes[1] << 8) + (int)inboundBytes[2] + 3; if (packetLength > inboundBytes.Length) { // We haven't received the entire packet yet; read more bytes break; } else { // We've received an intact packet, pop it off ClientPacket receivedPacket = new ClientPacket(state.ReceiveBufferPop(packetLength).ToArray()); // Also remove it from our local buffer...this seems kinda gross to me inboundBytes = new List <byte>(inboundBytes).GetRange(packetLength, inboundBytes.Length - packetLength) .ToArray(); if (receivedPacket.ShouldEncrypt) { receivedPacket.Decrypt(client); } if (receivedPacket.Opcode == 0x39 || receivedPacket.Opcode == 0x3A) { receivedPacket.DecryptDialog(); } try { if (this is Lobby) { Logger.InfoFormat("Lobby: 0x{0:X2}", receivedPacket.Opcode); var handler = (this as Lobby).PacketHandlers[receivedPacket.Opcode]; handler.Invoke(client, receivedPacket); Logger.InfoFormat("Lobby packet done"); client.UpdateLastReceived(); } else if (this is Login) { Logger.InfoFormat("Login: 0x{0:X2}", receivedPacket.Opcode); var handler = (this as Login).PacketHandlers[receivedPacket.Opcode]; handler.Invoke(client, receivedPacket); Logger.InfoFormat("Login packet done"); client.UpdateLastReceived(); } else { client.UpdateLastReceived(receivedPacket.Opcode != 0x45 && receivedPacket.Opcode != 0x75); Logger.InfoFormat("Queuing: 0x{0:X2}", receivedPacket.Opcode); World.MessageQueue.Add(new HybrasylClientMessage(receivedPacket, client.ConnectionId)); Logger.InfoFormat("World packet done"); } } catch (Exception e) { Logger.ErrorFormat("EXCEPTION IN HANDLING: 0x{0:X2}: {1}", receivedPacket.Opcode, e); } } } } } else { Logger.DebugFormat("cid {0}: client is disconnected or corrupt packets received", client.ConnectionId); client.Disconnect(); return; } // Continue getting dem bytes try { state.WorkSocket.BeginReceive(state.Buffer, 0, state.Buffer.Length, 0, new AsyncCallback(this.ReadCallback), state); Logger.InfoFormat("Triggering receive callback"); } catch (ObjectDisposedException) { //client.Disconnect(); state.WorkSocket.Close(); } catch (SocketException) { client.Disconnect(); } }