Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions Source/Client/Networking/State/ClientPlayingState.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
using Ionic.Zlib;
using Multiplayer.Common;
using RimWorld;
using System;
using System.Collections.Generic;
using UnityEngine;
using Verse;

Expand Down Expand Up @@ -32,14 +32,20 @@ public void HandleTimeControl(ByteReader data)
[PacketHandler(Packets.Server_KeepAlive)]
public void HandleKeepAlive(ByteReader data)
{
var padding = data.ReadRaw(8);
int id = data.ReadInt32();
var ending = data.ReadRaw(8);
int ticksBehind = TickPatch.tickUntil - TickPatch.Timer;

connection.Send(
Packets.Client_KeepAlive,
ByteWriter.GetBytes(id, ticksBehind, TickPatch.Simulating, TickPatch.workTicks),
false
);
var writer = new ByteWriter();
writer.WriteRaw(padding);
writer.WriteInt32(id);
writer.WriteInt32(ticksBehind);
writer.WriteBool(TickPatch.Simulating);
writer.WriteInt32(TickPatch.workTicks);
writer.WriteRaw(ending);

connection.Send(Packets.Client_KeepAlive, writer.ToArray(), false);
}

[PacketHandler(Packets.Server_Command)]
Expand Down
2 changes: 2 additions & 0 deletions Source/Common/Networking/State/ServerPlayingState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,12 @@ public void HandlePing(ByteReader data)
[PacketHandler(Packets.Client_KeepAlive)]
public void HandleClientKeepAlive(ByteReader data)
{
var padding = data.ReadRaw(8);
int id = data.ReadInt32();
int ticksBehind = data.ReadInt32();
var simulating = data.ReadBool();
var workTicks = data.ReadInt32();
var ending = data.ReadRaw(8);

Player.ticksBehind = ticksBehind;
Player.ticksBehindReceivedAt = Server.gameTimer;
Expand Down
14 changes: 12 additions & 2 deletions Source/Common/ServerPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class ServerPlayer : IChatSource

public int lastCursorTick = -1;

public int keepAliveId;
public int keepAliveId = 0xCAFE;
public Stopwatch keepAliveTimer = new();
public int keepAliveAt;

Expand Down Expand Up @@ -82,7 +82,17 @@ public void SendKeepAlivePacket()
{
keepAliveTimer.Start();
}
SendPacket(Packets.Server_KeepAlive, ByteWriter.GetBytes(keepAliveId), false);

var bytes = new byte[8];
var bytes2 = new byte[8];
var random = new Random();
random.NextBytes(bytes);
random.NextBytes(bytes2);
var writer = new ByteWriter(20);
writer.WriteRaw(bytes);
writer.WriteInt32(keepAliveId);
writer.WriteRaw(bytes2);
SendPacket(Packets.Server_KeepAlive, writer.ToArray(), false);
}

public void SendPacket(Packets packet, byte[] data, bool reliable = true)
Expand Down
Loading