public void FlushReceiveBuffer() { lock (ClientState.ReceiveLock) { try { ClientPacket packet; while (ClientState.ReceiveBufferTake(out packet)) { if (packet.ShouldEncrypt) { packet.Decrypt(this); } if (packet.Opcode == 0x39 || packet.Opcode == 0x3A) { packet.DecryptDialog(); } try { if (Server is Lobby) { GameLog.DebugFormat("Lobby: 0x{0:X2}", packet.Opcode); var handler = (Server as Lobby).PacketHandlers[packet.Opcode]; handler.Invoke(this, packet); GameLog.DebugFormat("Lobby packet done"); UpdateLastReceived(); } else if (Server is Login) { GameLog.Debug($"Login: 0x{packet.Opcode:X2}"); var handler = (Server as Login).PacketHandlers[packet.Opcode]; handler.Invoke(this, packet); GameLog.DebugFormat("Login packet done"); UpdateLastReceived(); } else { UpdateLastReceived(packet.Opcode != 0x45 && packet.Opcode != 0x75); GameLog.Debug($"Queuing: 0x{packet.Opcode:X2}"); //packet.DumpPacket(); // Check for throttling var throttleResult = Server.PacketThrottleCheck(this, packet); if (throttleResult == ThrottleResult.OK || throttleResult == ThrottleResult.ThrottleEnd || throttleResult == ThrottleResult.SquelchEnd) { World.MessageQueue.Add(new HybrasylClientMessage(packet, ConnectionId)); } else if (packet.Opcode == 0x06) { World.ControlMessageQueue.Add(new HybrasylControlMessage(ControlOpcodes.TriggerRefresh, ConnectionId)); } } } catch (Exception e) { Game.ReportException(e); GameLog.ErrorFormat("EXCEPTION IN HANDLING: 0x{0:X2}: {1}", packet.Opcode, e); } } } catch (Exception e) { Game.ReportException(e); Console.WriteLine(e); throw; } } }
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(); } }