Skip to content

Commit 5a3d065

Browse files
committed
Added hang/flood prevention to NetworkingManager
1 parent ae0f523 commit 5a3d065

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

MLAPI/Data/NetworkingConfiguration.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class NetworkingConfiguration
1616
public List<string> RegisteredScenes = new List<string>();
1717
public int MessageBufferSize = 65535;
1818
public int ReceiveTickrate = 64;
19+
public int MaxReceiveEventsPerTickRate = 500;
1920
public int SendTickrate = 64;
2021
public int EventTickrate = 64;
2122
public int MaxConnections = 100;

MLAPI/MonoBehaviours/Core/NetworkingManager.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,10 @@ private void Update()
312312
if((Time.time - lastReceiveTickTime >= (1f / NetworkConfig.ReceiveTickrate)) || NetworkConfig.ReceiveTickrate <= 0)
313313
{
314314
NetworkEventType eventType;
315+
int processedEvents = 0;
315316
do
316317
{
318+
processedEvents++;
317319
eventType = NetworkTransport.Receive(out hostId, out clientId, out channelId, messageBuffer, messageBuffer.Length, out receivedSize, out error);
318320
NetworkError networkError = (NetworkError)error;
319321
if (networkError == NetworkError.Timeout)
@@ -368,7 +370,8 @@ private void Update()
368370
OnClientDisconnect(clientId);
369371
break;
370372
}
371-
} while (eventType != NetworkEventType.Nothing);
373+
// Only do another iteration if: there are no more messages AND (there is no limit to max events or we have processed less than the maximum)
374+
} while (eventType != NetworkEventType.Nothing && (NetworkConfig.MaxReceiveEventsPerTickRate <= 0 || processedEvents < NetworkConfig.MaxReceiveEventsPerTickRate));
372375
lastReceiveTickTime = Time.time;
373376
}
374377
if (isServer && ((Time.time - lastEventTickTime >= (1f / NetworkConfig.EventTickrate)) || NetworkConfig.EventTickrate <= 0))

0 commit comments

Comments
 (0)