Skip to content

Commit

Permalink
fix(enet): Enet timeout interval and peer lookup clearance
Browse files Browse the repository at this point in the history
  • Loading branch information
TwoTenPvP committed May 27, 2019
1 parent a9f7b4f commit 5dc8f7b
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions EnetTransport/EnetTransport.cs
Expand Up @@ -19,7 +19,13 @@ public struct EnetChannel
public string Address = "127.0.0.1";
public int MaxClients = 100;
public List<EnetChannel> Channels = new List<EnetChannel>();
public int MessageBufferSize = 1024 * 5;
public int MessageBufferSize = 1024 * 5;

[UnityEngine.Header("ENET Settings")]
public uint PingInterval = 500;
public uint TimeoutLimit = 32;
public uint TimeoutMinimum = 5000;
public uint TimeoutMaximum = 30000;


// Runtime / state
Expand Down Expand Up @@ -86,11 +92,11 @@ public override NetEventType PollEvent(out ulong clientId, out string channelNam
{
channelName = null;
payload = new ArraySegment<byte>();

if (!connectedEnetPeers.ContainsKey(@event.Peer.ID))
{
connectedEnetPeers.Add(@event.Peer.ID, @event.Peer);
}

connectedEnetPeers.Add(@event.Peer.ID, @event.Peer);

@event.Peer.PingInterval(PingInterval);
@event.Peer.Timeout(TimeoutLimit, TimeoutMinimum, TimeoutMaximum);

return NetEventType.Connect;
}
Expand All @@ -99,7 +105,7 @@ public override NetEventType PollEvent(out ulong clientId, out string channelNam
channelName = null;
payload = new ArraySegment<byte>();

connectedEnetPeers.Remove(@event.Peer.ID);
connectedEnetPeers.Remove(@event.Peer.ID);

return NetEventType.Disconnect;
}
Expand Down Expand Up @@ -164,11 +170,12 @@ public override void StartClient()
address.Port = Port;
address.SetHost(Address);

Peer serverPeer = host.Connect(address, MLAPI_CHANNELS.Length + Channels.Count);
Peer serverPeer = host.Connect(address, MLAPI_CHANNELS.Length + Channels.Count);

serverPeer.PingInterval(PingInterval);
serverPeer.Timeout(TimeoutLimit, TimeoutMinimum, TimeoutMaximum);

serverPeerId = serverPeer.ID;

connectedEnetPeers.Add(serverPeerId, serverPeer);
}

public override void StartServer()
Expand All @@ -194,12 +201,15 @@ public override void DisconnectLocalClient()

GetEnetConnectionDetails(serverPeerId, out uint peerId);

connectedEnetPeers[peerId].DisconnectNow(0);
if (connectedEnetPeers.ContainsKey(peerId))
{
connectedEnetPeers[peerId].DisconnectNow(0);
}
}

public override ulong GetCurrentRtt(ulong clientId)
{
GetEnetConnectionDetails(serverPeerId, out uint peerId);
GetEnetConnectionDetails(clientId, out uint peerId);

return connectedEnetPeers[peerId].RoundTripTime;
}
Expand Down

0 comments on commit 5dc8f7b

Please sign in to comment.