1
0
mirror of https://github.com/lidgren/lidgren-network-gen3.git synced 2026-05-15 22:56:30 +09:00

Added some optimizations

This commit is contained in:
lidgren
2010-11-03 10:38:13 +00:00
parent 9b24fa7f38
commit 6a30a00172
6 changed files with 87 additions and 62 deletions

View File

@@ -228,7 +228,7 @@ namespace Lidgren.Network
SendConnectResponse(true);
return;
}
m_peer.LogDebug("Unhandled handshake: " + tp + ", status is " + m_status + " length: " + payloadLength);
m_peer.LogDebug("Unhandled Connect: " + tp + ", status is " + m_status + " length: " + payloadLength);
break;
case NetMessageType.ConnectResponse:
switch (m_status)
@@ -303,7 +303,7 @@ namespace Lidgren.Network
SetStatus(NetConnectionStatus.Disconnected, reason);
break;
default:
m_peer.LogDebug("Unhandled handshake: " + tp + " length: " + payloadLength);
m_peer.LogDebug("Unhandled type during handshake: " + tp + " length: " + payloadLength);
break;
}
}

View File

@@ -68,6 +68,15 @@ namespace Lidgren.Network
m_peer.LogVerbose("Updated average roundtrip time to " + NetTime.ToReadable(m_averageRoundtripTime));
}
// update resend delay for all channels
float resendDelay = GetResendDelay();
foreach (var chan in m_sendChannels)
{
var rchan = chan as NetReliableSenderChannel;
if (rchan != null)
rchan.m_resendDelay = resendDelay;
}
m_peer.LogVerbose("Timeout deadline pushed to " + m_timeoutDeadline);
}
}

View File

@@ -115,26 +115,29 @@ namespace Lidgren.Network
}
}
internal void Heartbeat(float now)
internal void Heartbeat(float now, uint frameCounter)
{
m_peer.VerifyNetworkThread();
NetException.Assert(m_status != NetConnectionStatus.InitiatedConnect && m_status != NetConnectionStatus.RespondedConnect);
if (now > m_timeoutDeadline)
if ((frameCounter % 5) == 0)
{
//
// connection timed out
//
m_peer.LogVerbose("Connection timed out at " + now + " deadline was " + m_timeoutDeadline);
ExecuteDisconnect("Connection timed out", true);
}
if (now > m_timeoutDeadline)
{
//
// connection timed out
//
m_peer.LogVerbose("Connection timed out at " + now + " deadline was " + m_timeoutDeadline);
ExecuteDisconnect("Connection timed out", true);
}
// send ping?
if (m_status == NetConnectionStatus.Connected)
{
if (now > m_sentPingTime + m_peer.m_configuration.m_pingInterval)
SendPing();
// send ping?
if (m_status == NetConnectionStatus.Connected)
{
if (now > m_sentPingTime + m_peer.m_configuration.m_pingInterval)
SendPing();
}
}
bool connectionReset; // TODO: handle connection reset
@@ -146,48 +149,51 @@ namespace Lidgren.Network
byte[] sendBuffer = m_peer.m_sendBuffer;
int mtu = m_peerConfiguration.m_maximumTransmissionUnit;
//
// send ack messages
//
while (m_queuedAcks.Count > 0)
if ((frameCounter % 3) == 0) // coalesce a few frames
{
int acks = (mtu - (m_sendBufferWritePtr + 5)) / 3; // 3 bytes per actual ack
if (acks > m_queuedAcks.Count)
acks = m_queuedAcks.Count;
NetException.Assert(acks > 0);
m_sendBufferNumMessages++;
// write acks header
sendBuffer[m_sendBufferWritePtr++] = (byte)NetMessageType.Acknowledge;
sendBuffer[m_sendBufferWritePtr++] = 0; // no sequence number
sendBuffer[m_sendBufferWritePtr++] = 0; // no sequence number
int len = (acks * 3) * 8; // bits
sendBuffer[m_sendBufferWritePtr++] = (byte)len;
sendBuffer[m_sendBufferWritePtr++] = (byte)(len >> 8);
// write acks
for(int i=0;i<acks;i++)
//
// send ack messages
//
while (m_queuedAcks.Count > 0)
{
NetTuple<NetMessageType, int> tuple;
m_queuedAcks.TryDequeue(out tuple);
int acks = (mtu - (m_sendBufferWritePtr + 5)) / 3; // 3 bytes per actual ack
if (acks > m_queuedAcks.Count)
acks = m_queuedAcks.Count;
//m_peer.LogVerbose("Sending ack for " + tuple.Item1 + "#" + tuple.Item2);
NetException.Assert(acks > 0);
sendBuffer[m_sendBufferWritePtr++] = (byte)tuple.Item1;
sendBuffer[m_sendBufferWritePtr++] = (byte)tuple.Item2;
sendBuffer[m_sendBufferWritePtr++] = (byte)(tuple.Item2 >> 8);
}
m_sendBufferNumMessages++;
if (m_queuedAcks.Count > 0)
{
// send packet and go for another round of acks
NetException.Assert(m_sendBufferWritePtr > 0 && m_sendBufferNumMessages > 0);
m_peer.SendPacket(m_sendBufferWritePtr, m_remoteEndpoint, m_sendBufferNumMessages, out connectionReset);
m_statistics.PacketSent(m_sendBufferWritePtr, 1);
m_sendBufferWritePtr = 0;
m_sendBufferNumMessages = 0;
// write acks header
sendBuffer[m_sendBufferWritePtr++] = (byte)NetMessageType.Acknowledge;
sendBuffer[m_sendBufferWritePtr++] = 0; // no sequence number
sendBuffer[m_sendBufferWritePtr++] = 0; // no sequence number
int len = (acks * 3) * 8; // bits
sendBuffer[m_sendBufferWritePtr++] = (byte)len;
sendBuffer[m_sendBufferWritePtr++] = (byte)(len >> 8);
// write acks
for (int i = 0; i < acks; i++)
{
NetTuple<NetMessageType, int> tuple;
m_queuedAcks.TryDequeue(out tuple);
//m_peer.LogVerbose("Sending ack for " + tuple.Item1 + "#" + tuple.Item2);
sendBuffer[m_sendBufferWritePtr++] = (byte)tuple.Item1;
sendBuffer[m_sendBufferWritePtr++] = (byte)tuple.Item2;
sendBuffer[m_sendBufferWritePtr++] = (byte)(tuple.Item2 >> 8);
}
if (m_queuedAcks.Count > 0)
{
// send packet and go for another round of acks
NetException.Assert(m_sendBufferWritePtr > 0 && m_sendBufferNumMessages > 0);
m_peer.SendPacket(m_sendBufferWritePtr, m_remoteEndpoint, m_sendBufferNumMessages, out connectionReset);
m_statistics.PacketSent(m_sendBufferWritePtr, 1);
m_sendBufferWritePtr = 0;
m_sendBufferNumMessages = 0;
}
}
}

View File

@@ -20,6 +20,7 @@ namespace Lidgren.Network
internal NetIncomingMessage m_readHelperMessage;
private EndPoint m_senderRemote;
private object m_initializeLock = new object();
private uint m_frameCounter;
internal readonly NetPeerConfiguration m_configuration;
private readonly NetQueue<NetIncomingMessage> m_releasedIncomingMessages;
@@ -195,12 +196,17 @@ namespace Lidgren.Network
float now = (float)NetTime.Now;
m_frameCounter++;
// do handshake heartbeats
foreach (NetConnection conn in m_handshakes.Values)
if ((m_frameCounter % 3) == 0)
{
conn.UnconnectedHeartbeat(now);
if (conn.m_status == NetConnectionStatus.Connected || conn.m_status == NetConnectionStatus.Disconnected)
break; // collection is modified
foreach (NetConnection conn in m_handshakes.Values)
{
conn.UnconnectedHeartbeat(now);
if (conn.m_status == NetConnectionStatus.Connected || conn.m_status == NetConnectionStatus.Disconnected)
break; // collection has been modified
}
}
#if DEBUG
@@ -212,7 +218,7 @@ namespace Lidgren.Network
{
foreach (NetConnection conn in m_connections)
{
conn.Heartbeat(now);
conn.Heartbeat(now, m_frameCounter);
if (conn.m_status == NetConnectionStatus.Disconnected)
{
//
@@ -246,7 +252,7 @@ namespace Lidgren.Network
if (m_socket == null)
return;
if (!m_socket.Poll(500, SelectMode.SelectRead)) // wait up to 1/2 ms for data to arrive
if (!m_socket.Poll(1000, SelectMode.SelectRead)) // wait up to 1 ms for data to arrive
return;
//if (m_socket == null || m_socket.Available < 1)
@@ -359,7 +365,7 @@ namespace Lidgren.Network
}
catch (Exception ex)
{
LogError("Packet parsing error: " + ex.Message);
LogError("Packet parsing error: " + ex.Message + " from " + ipsender);
}
ptr += payloadByteLength;
}

View File

@@ -70,7 +70,7 @@ namespace Lidgren.Network
m_acceptIncomingConnections = false;
m_maximumConnections = 32;
m_defaultOutgoingMessageCapacity = 16;
m_pingInterval = 3.0f;
m_pingInterval = 4.0f;
m_connectionTimeout = 25.0f;
m_useMessageRecycling = true;

View File

@@ -16,6 +16,8 @@ namespace Lidgren.Network
private NetBitVector m_receivedAcks;
internal NetStoredReliableMessage[] m_storedMessages;
internal float m_resendDelay;
internal override int WindowSize { get { return m_windowSize; } }
internal NetReliableSenderChannel(NetConnection connection, int windowSize)
@@ -27,6 +29,7 @@ namespace Lidgren.Network
m_receivedAcks = new NetBitVector(NetConstants.NumSequenceNumbers);
m_storedMessages = new NetStoredReliableMessage[m_windowSize];
m_queuedSends = new NetQueue<NetOutgoingMessage>(8);
m_resendDelay = m_connection.GetResendDelay();
}
internal override int GetAllowedSends()
@@ -60,8 +63,9 @@ namespace Lidgren.Network
// call this regularely
internal override void SendQueuedMessages(float now)
{
//
// resends
float resendDelay = m_connection.GetResendDelay();
//
for (int i = 0; i < m_storedMessages.Length; i++)
{
NetOutgoingMessage om = m_storedMessages[i].Message;
@@ -69,7 +73,7 @@ namespace Lidgren.Network
continue;
float t = m_storedMessages[i].LastSent;
if (t > 0 && (now - t) > resendDelay)
if (t > 0 && (now - t) > m_resendDelay)
{
// deduce sequence number
int startSlot = m_windowStart % m_windowSize;