You've already forked lidgren-network-gen3
mirror of
https://github.com/lidgren/lidgren-network-gen3.git
synced 2026-05-17 23:56:30 +09:00
Added NetPeer.ThrowOrLog
Changed a bunch of exceptions for consistency and safety Enabled AutoExpandMTU for SpeedSample
This commit is contained in:
@@ -500,8 +500,7 @@ namespace Lidgren.Network
|
|||||||
int num2 = 0;
|
int num2 = 0;
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
if (num2 == 0x23)
|
NetException.Assert(num2 != 0x23, "Bad 7-bit encoded integer");
|
||||||
throw new FormatException("Bad 7-bit encoded integer");
|
|
||||||
|
|
||||||
byte num3 = buffer[offset++];
|
byte num3 = buffer[offset++];
|
||||||
num1 |= (num3 & 0x7f) << (num2 & 0x1f);
|
num1 |= (num3 & 0x7f) << (num2 & 0x1f);
|
||||||
|
|||||||
@@ -43,7 +43,8 @@ namespace Lidgren.Network
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case NetConnectionStatus.Disconnected:
|
case NetConnectionStatus.Disconnected:
|
||||||
throw new NetException("This connection is Disconnected; spent. A new one should have been created");
|
m_peer.ThrowOrLog("This connection is Disconnected; spent. A new one should have been created");
|
||||||
|
break;
|
||||||
|
|
||||||
case NetConnectionStatus.Disconnecting:
|
case NetConnectionStatus.Disconnecting:
|
||||||
// let disconnect finish first
|
// let disconnect finish first
|
||||||
@@ -192,7 +193,7 @@ namespace Lidgren.Network
|
|||||||
if (hi != null && hi.Length >= m_localHailMessage.LengthBytes)
|
if (hi != null && hi.Length >= m_localHailMessage.LengthBytes)
|
||||||
{
|
{
|
||||||
if (om.LengthBytes + m_localHailMessage.LengthBytes > m_peerConfiguration.m_maximumTransmissionUnit - 10)
|
if (om.LengthBytes + m_localHailMessage.LengthBytes > m_peerConfiguration.m_maximumTransmissionUnit - 10)
|
||||||
throw new NetException("Hail message too large; can maximally be " + (m_peerConfiguration.m_maximumTransmissionUnit - 10 - om.LengthBytes));
|
m_peer.ThrowOrLog("Hail message too large; can maximally be " + (m_peerConfiguration.m_maximumTransmissionUnit - 10 - om.LengthBytes));
|
||||||
om.Write(m_localHailMessage.Data, 0, m_localHailMessage.LengthBytes);
|
om.Write(m_localHailMessage.Data, 0, m_localHailMessage.LengthBytes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -323,7 +323,7 @@ namespace Lidgren.Network
|
|||||||
chan = CreateSenderChannel(tp);
|
chan = CreateSenderChannel(tp);
|
||||||
|
|
||||||
if (msg.GetEncodedSize() > m_currentMTU)
|
if (msg.GetEncodedSize() > m_currentMTU)
|
||||||
throw new NetException("Message too large! Fragmentation failure?");
|
m_peer.ThrowOrLog("Message too large! Fragmentation failure?");
|
||||||
|
|
||||||
var retval = chan.Enqueue(msg);
|
var retval = chan.Enqueue(msg);
|
||||||
if (retval == NetSendResult.Sent && m_peerConfiguration.m_autoFlushSendQueue == false)
|
if (retval == NetSendResult.Sent && m_peerConfiguration.m_autoFlushSendQueue == false)
|
||||||
@@ -394,11 +394,8 @@ namespace Lidgren.Network
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case NetMessageType.LibraryError:
|
case NetMessageType.LibraryError:
|
||||||
#if DEBUG
|
m_peer.ThrowOrLog("LibraryError received by ReceivedLibraryMessage; this usually indicates a malformed message");
|
||||||
throw new NetException("LibraryError received by ReceivedLibraryMessage; this usually indicates a malformed message");
|
|
||||||
#else
|
|
||||||
break;
|
break;
|
||||||
#endif
|
|
||||||
|
|
||||||
case NetMessageType.Disconnect:
|
case NetMessageType.Disconnect:
|
||||||
NetIncomingMessage msg = m_peer.SetupReadHelperMessage(ptr, payloadLength);
|
NetIncomingMessage msg = m_peer.SetupReadHelperMessage(ptr, payloadLength);
|
||||||
|
|||||||
@@ -270,6 +270,10 @@ namespace Lidgren.Network
|
|||||||
m_socket = null;
|
m_socket = null;
|
||||||
m_status = NetPeerStatus.NotRunning;
|
m_status = NetPeerStatus.NotRunning;
|
||||||
LogDebug("Shutdown complete");
|
LogDebug("Shutdown complete");
|
||||||
|
|
||||||
|
// wake up any threads waiting for server shutdown
|
||||||
|
if (m_messageReceivedEvent != null)
|
||||||
|
m_messageReceivedEvent.Set();
|
||||||
}
|
}
|
||||||
|
|
||||||
m_receiveBuffer = null;
|
m_receiveBuffer = null;
|
||||||
@@ -467,10 +471,14 @@ namespace Lidgren.Network
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (tp >= NetMessageType.Unused1 && tp <= NetMessageType.Unused29)
|
||||||
|
{
|
||||||
|
ThrowOrLog("Unexpected NetMessageType: " + tp);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
NetException.Assert(tp < NetMessageType.Unused1 || tp > NetMessageType.Unused29);
|
|
||||||
|
|
||||||
if (tp >= NetMessageType.LibraryError)
|
if (tp >= NetMessageType.LibraryError)
|
||||||
{
|
{
|
||||||
if (sender != null)
|
if (sender != null)
|
||||||
|
|||||||
@@ -136,10 +136,9 @@ namespace Lidgren.Network
|
|||||||
{
|
{
|
||||||
if (m_incomingMessagesPool == null)
|
if (m_incomingMessagesPool == null)
|
||||||
return;
|
return;
|
||||||
#if DEBUG
|
|
||||||
if (m_incomingMessagesPool.Contains(msg))
|
NetException.Assert(m_incomingMessagesPool.Contains(msg) == false, "Recyling already recycled message! Thread race?");
|
||||||
throw new NetException("Recyling already recycled message! Thread race?");
|
|
||||||
#endif
|
|
||||||
byte[] storage = msg.m_data;
|
byte[] storage = msg.m_data;
|
||||||
msg.m_data = null;
|
msg.m_data = null;
|
||||||
Recycle(storage);
|
Recycle(storage);
|
||||||
@@ -188,11 +187,9 @@ namespace Lidgren.Network
|
|||||||
{
|
{
|
||||||
if (m_outgoingMessagesPool == null)
|
if (m_outgoingMessagesPool == null)
|
||||||
return;
|
return;
|
||||||
#if DEBUG
|
|
||||||
if (m_outgoingMessagesPool.Contains(msg))
|
|
||||||
throw new NetException("Recyling already recycled message! Thread race?");
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
NetException.Assert(m_outgoingMessagesPool.Contains(msg) == false, "Recyling already recycled message! Thread race?");
|
||||||
|
|
||||||
byte[] storage = msg.m_data;
|
byte[] storage = msg.m_data;
|
||||||
msg.m_data = null;
|
msg.m_data = null;
|
||||||
|
|
||||||
|
|||||||
@@ -65,9 +65,18 @@ namespace Lidgren.Network
|
|||||||
internal static int GetMTU(IList<NetConnection> recipients)
|
internal static int GetMTU(IList<NetConnection> recipients)
|
||||||
{
|
{
|
||||||
int count = recipients.Count;
|
int count = recipients.Count;
|
||||||
NetException.Assert(count > 0);
|
|
||||||
|
|
||||||
int mtu = int.MaxValue;
|
int mtu = int.MaxValue;
|
||||||
|
if (count < 1)
|
||||||
|
{
|
||||||
|
#if DEBUG
|
||||||
|
throw new NetException("GetMTU called with no recipients");
|
||||||
|
#else
|
||||||
|
// we don't have access to the particular peer, so just use default MTU
|
||||||
|
return NetPeerConfiguration.kDefaultMTU;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
for(int i=0;i<count;i++)
|
for(int i=0;i<count;i++)
|
||||||
{
|
{
|
||||||
var conn = recipients[i];
|
var conn = recipients[i];
|
||||||
|
|||||||
@@ -327,6 +327,19 @@ namespace Lidgren.Network
|
|||||||
SendPacket(length, destination, 1, out unused);
|
SendPacket(length, destination, 1, out unused);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// In DEBUG, throws an exception, in RELEASE logs an error message
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="message"></param>
|
||||||
|
internal void ThrowOrLog(string message)
|
||||||
|
{
|
||||||
|
#if DEBUG
|
||||||
|
throw new NetException(message);
|
||||||
|
#else
|
||||||
|
LogError(message);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Disconnects all active connections and closes the socket
|
/// Disconnects all active connections and closes the socket
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -27,6 +27,16 @@ namespace Lidgren.Network
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public sealed class NetPeerConfiguration
|
public sealed class NetPeerConfiguration
|
||||||
{
|
{
|
||||||
|
// Maximum transmission unit
|
||||||
|
// Ethernet can take 1500 bytes of payload, so lets stay below that.
|
||||||
|
// The aim is for a max full packet to be 1440 bytes (30 x 48 bytes, lower than 1468)
|
||||||
|
// -20 bytes IP header
|
||||||
|
// -8 bytes UDP header
|
||||||
|
// -4 bytes to be on the safe side and align to 8-byte boundary
|
||||||
|
// Total 1408 bytes
|
||||||
|
// Note that lidgren headers (5 bytes) are not included here; since it's part of the "mtu payload"
|
||||||
|
public const int kDefaultMTU = 1408;
|
||||||
|
|
||||||
private const string c_isLockedMessage = "You may not modify the NetPeerConfiguration after it has been used to initialize a NetPeer";
|
private const string c_isLockedMessage = "You may not modify the NetPeerConfiguration after it has been used to initialize a NetPeer";
|
||||||
|
|
||||||
private bool m_isLocked;
|
private bool m_isLocked;
|
||||||
@@ -96,15 +106,7 @@ namespace Lidgren.Network
|
|||||||
m_maximumHandshakeAttempts = 5;
|
m_maximumHandshakeAttempts = 5;
|
||||||
m_autoFlushSendQueue = true;
|
m_autoFlushSendQueue = true;
|
||||||
|
|
||||||
// Maximum transmission unit
|
m_maximumTransmissionUnit = kDefaultMTU;
|
||||||
// Ethernet can take 1500 bytes of payload, so lets stay below that.
|
|
||||||
// The aim is for a max full packet to be 1440 bytes (30 x 48 bytes, lower than 1468)
|
|
||||||
// -20 bytes IP header
|
|
||||||
// -8 bytes UDP header
|
|
||||||
// -4 bytes to be on the safe side and align to 8-byte boundary
|
|
||||||
// Total 1408 bytes
|
|
||||||
// Note that lidgren headers (5 bytes) are not included here; since it's part of the "mtu payload"
|
|
||||||
m_maximumTransmissionUnit = 1408;
|
|
||||||
m_autoExpandMTU = false;
|
m_autoExpandMTU = false;
|
||||||
m_expandMTUFrequency = 2.0f;
|
m_expandMTUFrequency = 2.0f;
|
||||||
m_expandMTUFailAttempts = 5;
|
m_expandMTUFailAttempts = 5;
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ namespace SpeedTestClient
|
|||||||
s_form = new Form1();
|
s_form = new Form1();
|
||||||
|
|
||||||
NetPeerConfiguration config = new NetPeerConfiguration("speedtest");
|
NetPeerConfiguration config = new NetPeerConfiguration("speedtest");
|
||||||
|
config.AutoExpandMTU = true;
|
||||||
s_client = new NetClient(config);
|
s_client = new NetClient(config);
|
||||||
|
|
||||||
Application.Idle += new EventHandler(Application_Idle);
|
Application.Idle += new EventHandler(Application_Idle);
|
||||||
|
|||||||
@@ -43,18 +43,23 @@ namespace UnitTests
|
|||||||
|
|
||||||
// read all message
|
// read all message
|
||||||
NetIncomingMessage inc = peer.WaitMessage(5000);
|
NetIncomingMessage inc = peer.WaitMessage(5000);
|
||||||
switch(inc.MessageType)
|
while (inc != null)
|
||||||
{
|
{
|
||||||
case NetIncomingMessageType.DebugMessage:
|
switch (inc.MessageType)
|
||||||
case NetIncomingMessageType.VerboseDebugMessage:
|
{
|
||||||
case NetIncomingMessageType.WarningMessage:
|
case NetIncomingMessageType.DebugMessage:
|
||||||
case NetIncomingMessageType.ErrorMessage:
|
case NetIncomingMessageType.VerboseDebugMessage:
|
||||||
Console.WriteLine("Peer message: " + inc.ReadString());
|
case NetIncomingMessageType.WarningMessage:
|
||||||
break;
|
case NetIncomingMessageType.ErrorMessage:
|
||||||
case NetIncomingMessageType.Error:
|
Console.WriteLine("Peer message: " + inc.ReadString());
|
||||||
throw new Exception("Received error message!");
|
break;
|
||||||
|
case NetIncomingMessageType.Error:
|
||||||
|
throw new Exception("Received error message!");
|
||||||
|
}
|
||||||
|
|
||||||
|
inc = peer.ReadMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
Console.WriteLine("Done");
|
Console.WriteLine("Done");
|
||||||
Console.ReadKey();
|
Console.ReadKey();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user