Skip to content
This repository was archived by the owner on Sep 13, 2025. It is now read-only.

Commit 94c7c68

Browse files
author
Blue Staggo
committed
Server now correctly sends chunks to clients :D + Quick and dirty fix for crashes on disconnection
1 parent 4d4bcfa commit 94c7c68

6 files changed

Lines changed: 29 additions & 28 deletions

File tree

VoxelThing.Client/src/ClientPacketHandler.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ void SConnectionAccepted(SConnectionAccepted packet)
4444

4545
void SDisconnect(SDisconnect packet)
4646
{
47-
Client.DisconnectFromServer();
4847
Client.ExitWorld();
4948
Client.CurrentScreen = new MultiplayerDisconnectionScreen(Client, packet.Reason);
5049
}

VoxelThing.Client/src/Game.cs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class Game : GameWindow
3939
public const double TickRate = 1.0 / TicksPerSecond;
4040
public const int HandshakeRate = TicksPerSecond;
4141
public const int MaxTimeWithoutHandshake = TicksPerSecond * 5;
42-
public const bool EnableCursorGrab = false;
42+
public const bool EnableCursorGrab = true;
4343
public const bool OpenGlDebugging = SharedConstants.Debug; // Only enable this if your drivers support OpenGL 4.3+
4444

4545
public static double TimeElapsed => GLFW.GetTime();
@@ -237,24 +237,23 @@ protected override void OnUpdateFrame(FrameEventArgs args)
237237
}
238238
Profiler.Pop();
239239

240-
if (PacketHandler is not null)
240+
if (PacketHandler is null) continue;
241+
242+
if (Ticks > PacketHandler.TimeSinceLastHandshake
243+
&& Ticks - PacketHandler.TimeSinceLastHandshake > MaxTimeWithoutHandshake)
241244
{
242-
if (Ticks > PacketHandler.TimeSinceLastHandshake
243-
&& Ticks - PacketHandler.TimeSinceLastHandshake > MaxTimeWithoutHandshake)
244-
{
245-
PacketHandler.HandlePacket(new SDisconnect("Timed out"));
246-
}
247-
else
248-
{
249-
if (Ticks % HandshakeRate == 0)
250-
PacketHandler.Server.SendPacket(CHandshake.Instance);
245+
PacketHandler.HandlePacket(new SDisconnect("Timed out"));
246+
}
247+
else
248+
{
249+
if (Ticks % HandshakeRate == 0)
250+
PacketHandler.Server.SendPacket(CHandshake.Instance);
251251

252-
if (Player is not null)
253-
PacketHandler.Server.SendPacket(new CUpdatePosition(Player));
252+
if (Player is not null)
253+
PacketHandler.Server.SendPacket(new CUpdatePosition(Player));
254254

255-
while (PacketHandler.Server.PendingPackets.TryDequeue(out IPacket? packet))
256-
PacketHandler.HandlePacket(packet);
257-
}
255+
while (PacketHandler.Server.PendingPackets.TryDequeue(out IPacket? packet))
256+
PacketHandler.HandlePacket(packet);
258257
}
259258
}
260259

@@ -474,11 +473,9 @@ public void ExitWorld()
474473
if (World is null)
475474
return;
476475

477-
if (Player is not null)
478-
{
476+
if (Player is not null && !World.Remote)
479477
World.SaveHandler.SaveData("player", (CompoundItem)Player.Serialize());
480-
Player = null;
481-
}
478+
Player = null;
482479

483480
World.Close();
484481
World = null;

VoxelThing.Client/src/Gui/Screens/PauseScreen.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public PauseScreen(Game game) : base(game)
4141
exitButton.OnClick += (_, _) =>
4242
{
4343
game.ExitWorld();
44+
game.DisconnectFromServer();
4445
game.CurrentScreen = Parent;
4546
};
4647
}

VoxelThing.Game/src/Networking/Connection.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public class Connection : IDisposable
2222
private readonly BinaryReader networkBinaryReader;
2323
private readonly BinaryWriter networkBinaryWriter;
2424

25+
private bool disposed;
26+
2527
public Connection(TcpClient tcpClient, IPEndPoint ipEndPoint, PacketSide side)
2628
{
2729
this.tcpClient = tcpClient;
@@ -41,6 +43,8 @@ public void StartListening()
4143

4244
public void SendPacket(IPacket packet)
4345
{
46+
if (disposed) return;
47+
4448
try
4549
{
4650
if (SharedConstants.PrintPackets)
@@ -51,6 +55,7 @@ public void SendPacket(IPacket packet)
5155
{
5256
Disconnected?.Invoke(this, EventArgs.Empty);
5357
}
58+
catch (ObjectDisposedException) { }
5459
}
5560

5661
private void ListenForPackets()
@@ -72,6 +77,7 @@ private void ListenForPackets()
7277

7378
public void Dispose()
7479
{
80+
disposed = true;
7581
GC.SuppressFinalize(this);
7682
cancellationTokenSource.Cancel();
7783
tcpClient.Dispose();

VoxelThing.Game/src/Networking/IPacket.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public interface IPacket
1515
if (!PacketManager.ForSide(senderSide).TryGetPacketTypeFromId(id, out Type? type))
1616
return null;
1717

18-
ushort length = reader.ReadUInt16();
18+
int length = reader.Read7BitEncodedInt();
1919
if (length == 0)
2020
return null;
2121

@@ -38,11 +38,9 @@ void IPacket.Write(BinaryWriter writer)
3838
if (id == 0)
3939
return;
4040
byte[] serialized = MemoryPackSerializer.Serialize((T)this);
41-
if (serialized.Length > ushort.MaxValue)
42-
return;
4341

4442
writer.Write(id);
45-
writer.Write((ushort)serialized.Length);
43+
writer.Write7BitEncodedInt(serialized.Length);
4644
writer.Write(MemoryPackSerializer.Serialize((T)this));
4745
}
4846
}

VoxelThing.Server/src/Client.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@ public class Client : IDisposable
1616

1717
public IPEndPoint IpEndPoint => Connection.IpEndPoint;
1818

19-
private readonly PlayerIsland Island;
19+
private readonly PlayerIsland island;
2020

2121
public Client(GameServer server, Connection connection)
2222
{
2323
Connection = connection;
2424
Server = server;
2525
PacketHandler = new(server, this);
26-
Island = new(this);
26+
island = new(this);
2727
}
2828

2929
public void Update()
3030
{
31-
Island.Update();
31+
island.Update();
3232
}
3333

3434
public void SendPacket(IPacket packet) => Connection.SendPacket(packet);

0 commit comments

Comments
 (0)