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

Added NetPeer.ThrowOrLog

Changed a bunch of exceptions for consistency and safety
Enabled AutoExpandMTU for SpeedSample
This commit is contained in:
lidgren
2014-07-31 13:16:09 +00:00
parent 85a3b28bbf
commit 04593ef00f
10 changed files with 71 additions and 39 deletions

View File

@@ -500,8 +500,7 @@ namespace Lidgren.Network
int num2 = 0;
while (true)
{
if (num2 == 0x23)
throw new FormatException("Bad 7-bit encoded integer");
NetException.Assert(num2 != 0x23, "Bad 7-bit encoded integer");
byte num3 = buffer[offset++];
num1 |= (num3 & 0x7f) << (num2 & 0x1f);

View File

@@ -43,7 +43,8 @@ namespace Lidgren.Network
break;
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:
// let disconnect finish first
@@ -192,7 +193,7 @@ namespace Lidgren.Network
if (hi != null && hi.Length >= m_localHailMessage.LengthBytes)
{
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);
}
}

View File

@@ -323,7 +323,7 @@ namespace Lidgren.Network
chan = CreateSenderChannel(tp);
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);
if (retval == NetSendResult.Sent && m_peerConfiguration.m_autoFlushSendQueue == false)
@@ -394,11 +394,8 @@ namespace Lidgren.Network
break;
case NetMessageType.LibraryError:
#if DEBUG
throw new NetException("LibraryError received by ReceivedLibraryMessage; this usually indicates a malformed message");
#else
m_peer.ThrowOrLog("LibraryError received by ReceivedLibraryMessage; this usually indicates a malformed message");
break;
#endif
case NetMessageType.Disconnect:
NetIncomingMessage msg = m_peer.SetupReadHelperMessage(ptr, payloadLength);

View File

@@ -270,6 +270,10 @@ namespace Lidgren.Network
m_socket = null;
m_status = NetPeerStatus.NotRunning;
LogDebug("Shutdown complete");
// wake up any threads waiting for server shutdown
if (m_messageReceivedEvent != null)
m_messageReceivedEvent.Set();
}
m_receiveBuffer = null;
@@ -467,10 +471,14 @@ namespace Lidgren.Network
return;
}
if (tp >= NetMessageType.Unused1 && tp <= NetMessageType.Unused29)
{
ThrowOrLog("Unexpected NetMessageType: " + tp);
return;
}
try
{
NetException.Assert(tp < NetMessageType.Unused1 || tp > NetMessageType.Unused29);
if (tp >= NetMessageType.LibraryError)
{
if (sender != null)

View File

@@ -136,10 +136,9 @@ namespace Lidgren.Network
{
if (m_incomingMessagesPool == null)
return;
#if DEBUG
if (m_incomingMessagesPool.Contains(msg))
throw new NetException("Recyling already recycled message! Thread race?");
#endif
NetException.Assert(m_incomingMessagesPool.Contains(msg) == false, "Recyling already recycled message! Thread race?");
byte[] storage = msg.m_data;
msg.m_data = null;
Recycle(storage);
@@ -188,11 +187,9 @@ namespace Lidgren.Network
{
if (m_outgoingMessagesPool == null)
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;
msg.m_data = null;

View File

@@ -65,9 +65,18 @@ namespace Lidgren.Network
internal static int GetMTU(IList<NetConnection> recipients)
{
int count = recipients.Count;
NetException.Assert(count > 0);
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++)
{
var conn = recipients[i];

View File

@@ -327,6 +327,19 @@ namespace Lidgren.Network
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>
/// Disconnects all active connections and closes the socket
/// </summary>

View File

@@ -27,6 +27,16 @@ namespace Lidgren.Network
/// </summary>
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 bool m_isLocked;
@@ -96,15 +106,7 @@ namespace Lidgren.Network
m_maximumHandshakeAttempts = 5;
m_autoFlushSendQueue = true;
// 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"
m_maximumTransmissionUnit = 1408;
m_maximumTransmissionUnit = kDefaultMTU;
m_autoExpandMTU = false;
m_expandMTUFrequency = 2.0f;
m_expandMTUFailAttempts = 5;

View File

@@ -31,6 +31,7 @@ namespace SpeedTestClient
s_form = new Form1();
NetPeerConfiguration config = new NetPeerConfiguration("speedtest");
config.AutoExpandMTU = true;
s_client = new NetClient(config);
Application.Idle += new EventHandler(Application_Idle);

View File

@@ -43,18 +43,23 @@ namespace UnitTests
// read all message
NetIncomingMessage inc = peer.WaitMessage(5000);
switch(inc.MessageType)
while (inc != null)
{
case NetIncomingMessageType.DebugMessage:
case NetIncomingMessageType.VerboseDebugMessage:
case NetIncomingMessageType.WarningMessage:
case NetIncomingMessageType.ErrorMessage:
Console.WriteLine("Peer message: " + inc.ReadString());
break;
case NetIncomingMessageType.Error:
throw new Exception("Received error message!");
switch (inc.MessageType)
{
case NetIncomingMessageType.DebugMessage:
case NetIncomingMessageType.VerboseDebugMessage:
case NetIncomingMessageType.WarningMessage:
case NetIncomingMessageType.ErrorMessage:
Console.WriteLine("Peer message: " + inc.ReadString());
break;
case NetIncomingMessageType.Error:
throw new Exception("Received error message!");
}
inc = peer.ReadMessage();
}
Console.WriteLine("Done");
Console.ReadKey();
}