void ReadPacket(byte opcode) { reader.Skip(1); // remove opcode lastOpcode = (Opcode)opcode; Action handler = handlers[opcode]; lastPacket = DateTime.UtcNow; if (handler == null) { throw new NotImplementedException("Unsupported packet:" + (Opcode)opcode); } handler(); }
public override void Tick(ScheduledTask task) { if (Disconnected) { return; } if (connecting) { TickConnect(); return; } if ((DateTime.UtcNow - lastPacket).TotalSeconds >= 30) { CheckDisconnection(task.Interval); } if (Disconnected) { return; } try { reader.ReadPendingData(); } catch (SocketException ex) { ErrorHandler.LogError("reading packets", ex); game.Disconnect("&eLost connection to the server", "I/O error when reading packets"); return; } while ((reader.size - reader.index) > 0) { byte opcode = reader.buffer[reader.index]; // Workaround for older D3 servers which wrote one byte too many for HackControl packets. if (cpeData.needD3Fix && lastOpcode == Opcode.CpeHackControl && (opcode == 0x00 || opcode == 0xFF)) { Utils.LogDebug("Skipping invalid HackControl byte from D3 server."); reader.Skip(1); LocalPlayer player = game.LocalPlayer; player.physics.jumpVel = 0.42f; // assume default jump height player.physics.serverJumpVel = player.physics.jumpVel; continue; } if (opcode >= handlers.Length) { game.Disconnect("Disconnected!", "Server sent invalid packet " + opcode + "!"); return; } if ((reader.size - reader.index) < packetSizes[opcode]) { break; } reader.Skip(1); // remove opcode lastOpcode = opcode; lastPacket = DateTime.UtcNow; Action handler = handlers[opcode]; if (handler == null) { game.Disconnect("Disconnected!", "Server sent invalid packet " + opcode + "!"); return; } handler(); } reader.RemoveProcessed(); // Network is ticked 60 times a second. We only send position updates 20 times a second. if ((netTicks % 3) == 0) { CoreTick(); } netTicks++; }
public override void Tick(ScheduledTask task) { if (Disconnected) { return; } if ((DateTime.UtcNow - lastPacket).TotalSeconds >= 30) { CheckDisconnection(task.Interval); } if (Disconnected) { return; } LocalPlayer player = game.LocalPlayer; this.task = task; try { reader.ReadPendingData(); } catch (SocketException ex) { ErrorHandler.LogError("reading packets", ex); game.Disconnect("&eLost connection to the server", "I/O error when reading packets"); Dispose(); return; } while ((reader.size - reader.index) > 0) { byte opcode = reader.buffer[reader.index]; // Workaround for older D3 servers which wrote one byte too many for HackControl packets. if (cpeData.needD3Fix && lastOpcode == Opcode.CpeHackControl && (opcode == 0x00 || opcode == 0xFF)) { Utils.LogDebug("Skipping invalid HackControl byte from D3 server."); reader.Skip(1); player.physics.jumpVel = 0.42f; // assume default jump height player.physics.serverJumpVel = player.physics.jumpVel; continue; } if (opcode > maxHandledPacket) { ErrorHandler.LogError("NetworkProcessor.Tick", "received an invalid opcode of " + opcode); reader.Skip(1); continue; } if ((reader.size - reader.index) < packetSizes[opcode]) { break; } ReadPacket(opcode); } reader.RemoveProcessed(); if (receivedFirstPosition) { SendPosition(player.Position, player.HeadY, player.HeadX); } CheckAsyncResources(); wom.Tick(); }