From a97b2568e5d25ef87a69fc18018ff028e06b3325 Mon Sep 17 00:00:00 2001 From: teascade Date: Sat, 25 Nov 2017 13:20:59 +0200 Subject: [PATCH] Fix NPEs --- scripts/net/ConnectionList.cs | 2 +- scripts/net/Peer.cs | 2 +- scripts/net/syncing/PacketDistributor.cs | 10 ++++++---- scripts/util/Optional.cs | 3 +++ 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/scripts/net/ConnectionList.cs b/scripts/net/ConnectionList.cs index e3aa642..b3e6594 100644 --- a/scripts/net/ConnectionList.cs +++ b/scripts/net/ConnectionList.cs @@ -44,7 +44,7 @@ namespace Network { return inList; } } - return null; + return Optional.None; } } } \ No newline at end of file diff --git a/scripts/net/Peer.cs b/scripts/net/Peer.cs index 01e731c..baefb6d 100644 --- a/scripts/net/Peer.cs +++ b/scripts/net/Peer.cs @@ -24,7 +24,7 @@ namespace Network { PacketPeer = packetPeer; IsServer = isServer; ConnectionList = new ConnectionList(this); - PacketDistributor = new PacketDistributor(this, 0.5f); + PacketDistributor = new PacketDistributor(this, 2); } public void Update(float delta) { diff --git a/scripts/net/syncing/PacketDistributor.cs b/scripts/net/syncing/PacketDistributor.cs index 11695c9..2d30e7c 100644 --- a/scripts/net/syncing/PacketDistributor.cs +++ b/scripts/net/syncing/PacketDistributor.cs @@ -1,4 +1,5 @@  +using Godot; using Network.PacketHandling; using System.Collections.Generic; using Util; @@ -24,7 +25,7 @@ namespace Network.Syncing { public void Update(float delta) { TimeSinceLastSync += delta; if (TimeSinceLastSync > Interval) { - TimeSinceLastSync -= Interval; + TimeSinceLastSync = 0; foreach (ConnectionHandler Handler in HandlerList) { Handler.SerializeAndSendQueues(); @@ -35,9 +36,10 @@ namespace Network.Syncing { public void HandleRawPacket(PacketBuffer packetBuffer, Connection connection) { var Handler = GetHandlerFor(connection); if (Handler.isEmpty) { - var Temp = new ConnectionHandler(Peer, connection); - HandlerList.Add(Temp); + Handler = new ConnectionHandler(Peer, connection); + HandlerList.Add(Handler); } + Handler.Value.ReceiveBuffer(packetBuffer); } public void AddHandler(Connection connection) { @@ -77,7 +79,7 @@ namespace Network.Syncing { return Curr; } } - return null; + return Optional.None; } } diff --git a/scripts/util/Optional.cs b/scripts/util/Optional.cs index 823540a..47109dc 100644 --- a/scripts/util/Optional.cs +++ b/scripts/util/Optional.cs @@ -16,6 +16,8 @@ namespace Util { } } + public Optional() { } + public Optional(T val) { Value = val; } @@ -38,5 +40,6 @@ namespace Util { if (opt.HasValue == HasValue) { return Equals(opt.Value, Value); } else { return opt.HasValue == HasValue; } } + public static Optional None { get { return new Optional(); } } } }