You've already forked lidgren-network-gen3
mirror of
https://github.com/lidgren/lidgren-network-gen3.git
synced 2026-05-17 15:46:33 +09:00
Capped max connection heartbeats per second
This commit is contained in:
@@ -21,6 +21,7 @@ namespace Lidgren.Network
|
|||||||
private EndPoint m_senderRemote;
|
private EndPoint m_senderRemote;
|
||||||
private object m_initializeLock = new object();
|
private object m_initializeLock = new object();
|
||||||
private uint m_frameCounter;
|
private uint m_frameCounter;
|
||||||
|
private double m_lastHeartbeat;
|
||||||
|
|
||||||
internal readonly NetPeerConfiguration m_configuration;
|
internal readonly NetPeerConfiguration m_configuration;
|
||||||
private readonly NetQueue<NetIncomingMessage> m_releasedIncomingMessages;
|
private readonly NetQueue<NetIncomingMessage> m_releasedIncomingMessages;
|
||||||
@@ -196,56 +197,66 @@ namespace Lidgren.Network
|
|||||||
{
|
{
|
||||||
VerifyNetworkThread();
|
VerifyNetworkThread();
|
||||||
|
|
||||||
float now = (float)NetTime.Now;
|
double dnow = NetTime.Now;
|
||||||
|
float now = (float)dnow;
|
||||||
|
|
||||||
m_frameCounter++;
|
double delta = dnow - m_lastHeartbeat;
|
||||||
|
|
||||||
// do handshake heartbeats
|
int maxCHBpS = 1250 - m_connections.Count;
|
||||||
if ((m_frameCounter % 3) == 0)
|
if (maxCHBpS < 250)
|
||||||
|
maxCHBpS = 250;
|
||||||
|
if (delta > (1.0 / (double)maxCHBpS)) // max connection heartbeats/second max
|
||||||
{
|
{
|
||||||
foreach (NetConnection conn in m_handshakes.Values)
|
m_frameCounter++;
|
||||||
|
m_lastHeartbeat = dnow;
|
||||||
|
|
||||||
|
// do handshake heartbeats
|
||||||
|
if ((m_frameCounter % 3) == 0)
|
||||||
{
|
{
|
||||||
conn.UnconnectedHeartbeat(now);
|
foreach (NetConnection conn in m_handshakes.Values)
|
||||||
if (conn.m_status == NetConnectionStatus.Connected || conn.m_status == NetConnectionStatus.Disconnected)
|
{
|
||||||
break; // collection has been modified
|
conn.UnconnectedHeartbeat(now);
|
||||||
|
if (conn.m_status == NetConnectionStatus.Connected || conn.m_status == NetConnectionStatus.Disconnected)
|
||||||
|
break; // collection has been modified
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
SendDelayedPackets();
|
SendDelayedPackets();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// do connection heartbeats
|
// do connection heartbeats
|
||||||
lock (m_connections)
|
lock (m_connections)
|
||||||
{
|
|
||||||
foreach (NetConnection conn in m_connections)
|
|
||||||
{
|
{
|
||||||
conn.Heartbeat(now, m_frameCounter);
|
foreach (NetConnection conn in m_connections)
|
||||||
if (conn.m_status == NetConnectionStatus.Disconnected)
|
|
||||||
{
|
{
|
||||||
//
|
conn.Heartbeat(now, m_frameCounter);
|
||||||
// remove connection
|
if (conn.m_status == NetConnectionStatus.Disconnected)
|
||||||
//
|
{
|
||||||
m_connections.Remove(conn);
|
//
|
||||||
m_connectionLookup.Remove(conn.RemoteEndpoint);
|
// remove connection
|
||||||
break; // can't continue iteration here
|
//
|
||||||
|
m_connections.Remove(conn);
|
||||||
|
m_connectionLookup.Remove(conn.RemoteEndpoint);
|
||||||
|
break; // can't continue iteration here
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// send unsent unconnected messages
|
// send unsent unconnected messages
|
||||||
NetTuple<IPEndPoint, NetOutgoingMessage> unsent;
|
NetTuple<IPEndPoint, NetOutgoingMessage> unsent;
|
||||||
while (m_unsentUnconnectedMessages.TryDequeue(out unsent))
|
while (m_unsentUnconnectedMessages.TryDequeue(out unsent))
|
||||||
{
|
{
|
||||||
NetOutgoingMessage om = unsent.Item2;
|
NetOutgoingMessage om = unsent.Item2;
|
||||||
|
|
||||||
bool connReset;
|
bool connReset;
|
||||||
int len = om.Encode(m_sendBuffer, 0, 0);
|
int len = om.Encode(m_sendBuffer, 0, 0);
|
||||||
SendPacket(len, unsent.Item1, 1, out connReset);
|
SendPacket(len, unsent.Item1, 1, out connReset);
|
||||||
|
|
||||||
Interlocked.Decrement(ref om.m_recyclingCount);
|
Interlocked.Decrement(ref om.m_recyclingCount);
|
||||||
if (om.m_recyclingCount <= 0)
|
if (om.m_recyclingCount <= 0)
|
||||||
Recycle(om);
|
Recycle(om);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ namespace ManyServer
|
|||||||
#else
|
#else
|
||||||
// throw new Exception("Sample not relevant in RELEASE; statistics needed to make sense!");
|
// throw new Exception("Sample not relevant in RELEASE; statistics needed to make sense!");
|
||||||
#endif
|
#endif
|
||||||
config.MaximumConnections = 64;
|
config.MaximumConnections = 256;
|
||||||
|
|
||||||
Server = new NetServer(config);
|
Server = new NetServer(config);
|
||||||
Server.Start();
|
Server.Start();
|
||||||
|
|||||||
Reference in New Issue
Block a user