v0.2.2: updated Lidgren, railgun shells can be bought, autorestart server, netstats, tutorial moloch spawning in a wall fix, misc error checks
This commit is contained in:
+71
-30
@@ -3,6 +3,10 @@ using System.Threading;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
|
||||
#if !__NOIPENDPOINT__
|
||||
using NetEndPoint = System.Net.IPEndPoint;
|
||||
#endif
|
||||
|
||||
namespace Lidgren.Network
|
||||
{
|
||||
/// <summary>
|
||||
@@ -14,9 +18,10 @@ namespace Lidgren.Network
|
||||
|
||||
private int m_listenPort;
|
||||
private object m_tag;
|
||||
private object m_messageReceivedEventCreationLock = new object();
|
||||
|
||||
internal readonly List<NetConnection> m_connections;
|
||||
private readonly Dictionary<IPEndPoint, NetConnection> m_connectionLookup;
|
||||
private readonly Dictionary<NetEndPoint, NetConnection> m_connectionLookup;
|
||||
|
||||
private string m_shutdownReason;
|
||||
|
||||
@@ -36,7 +41,13 @@ namespace Lidgren.Network
|
||||
get
|
||||
{
|
||||
if (m_messageReceivedEvent == null)
|
||||
m_messageReceivedEvent = new AutoResetEvent(false);
|
||||
{
|
||||
lock (m_messageReceivedEventCreationLock) // make sure we don't create more than one event object
|
||||
{
|
||||
if (m_messageReceivedEvent == null)
|
||||
m_messageReceivedEvent = new AutoResetEvent(false);
|
||||
}
|
||||
}
|
||||
return m_messageReceivedEvent;
|
||||
}
|
||||
}
|
||||
@@ -106,11 +117,11 @@ namespace Lidgren.Network
|
||||
m_configuration = config;
|
||||
m_statistics = new NetPeerStatistics(this);
|
||||
m_releasedIncomingMessages = new NetQueue<NetIncomingMessage>(4);
|
||||
m_unsentUnconnectedMessages = new NetQueue<NetTuple<IPEndPoint, NetOutgoingMessage>>(2);
|
||||
m_unsentUnconnectedMessages = new NetQueue<NetTuple<NetEndPoint, NetOutgoingMessage>>(2);
|
||||
m_connections = new List<NetConnection>();
|
||||
m_connectionLookup = new Dictionary<IPEndPoint, NetConnection>();
|
||||
m_handshakes = new Dictionary<IPEndPoint, NetConnection>();
|
||||
m_senderRemote = (EndPoint)new IPEndPoint(IPAddress.Any, 0);
|
||||
m_connectionLookup = new Dictionary<NetEndPoint, NetConnection>();
|
||||
m_handshakes = new Dictionary<NetEndPoint, NetConnection>();
|
||||
m_senderRemote = (EndPoint)new NetEndPoint(IPAddress.Any, 0);
|
||||
m_status = NetPeerStatus.NotRunning;
|
||||
m_receivedFragmentGroups = new Dictionary<NetConnection, Dictionary<int, ReceivedFragmentGroup>>();
|
||||
}
|
||||
@@ -149,13 +160,13 @@ namespace Lidgren.Network
|
||||
m_upnp.Discover(this);
|
||||
|
||||
// allow some time for network thread to start up in case they call Connect() or UPnP calls immediately
|
||||
Thread.Sleep(50);
|
||||
NetUtility.Sleep(50);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the connection, if any, for a certain remote endpoint
|
||||
/// </summary>
|
||||
public NetConnection GetConnection(IPEndPoint ep)
|
||||
public NetConnection GetConnection(NetEndPoint ep)
|
||||
{
|
||||
NetConnection retval;
|
||||
|
||||
@@ -169,15 +180,24 @@ namespace Lidgren.Network
|
||||
/// <summary>
|
||||
/// Read a pending message from any connection, blocking up to maxMillis if needed
|
||||
/// </summary>
|
||||
public NetIncomingMessage WaitMessage(int maxMillis)
|
||||
{
|
||||
var msg = ReadMessage();
|
||||
if (msg != null)
|
||||
return msg; // no need to wait; we already have a message to deliver
|
||||
if (m_messageReceivedEvent != null)
|
||||
m_messageReceivedEvent.WaitOne(maxMillis);
|
||||
return ReadMessage();
|
||||
}
|
||||
public NetIncomingMessage WaitMessage(int maxMillis)
|
||||
{
|
||||
NetIncomingMessage msg = ReadMessage();
|
||||
|
||||
while (msg == null)
|
||||
{
|
||||
// This could return true...
|
||||
if (!MessageReceivedEvent.WaitOne(maxMillis))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// ... while this will still returns null. That's why we need to cycle.
|
||||
msg = ReadMessage();
|
||||
}
|
||||
|
||||
return msg;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read a pending message from any connection, if any
|
||||
@@ -218,8 +238,8 @@ namespace Lidgren.Network
|
||||
return added;
|
||||
}
|
||||
|
||||
// send message immediately
|
||||
internal void SendLibrary(NetOutgoingMessage msg, IPEndPoint recipient)
|
||||
// send message immediately and recycle it
|
||||
internal void SendLibrary(NetOutgoingMessage msg, NetEndPoint recipient)
|
||||
{
|
||||
VerifyNetworkThread();
|
||||
NetException.Assert(msg.m_isSent == false);
|
||||
@@ -227,6 +247,18 @@ namespace Lidgren.Network
|
||||
bool connReset;
|
||||
int len = msg.Encode(m_sendBuffer, 0, 0);
|
||||
SendPacket(len, recipient, 1, out connReset);
|
||||
|
||||
// no reliability, no multiple recipients - we can just recycle this message immediately
|
||||
msg.m_recyclingCount = 0;
|
||||
Recycle(msg);
|
||||
}
|
||||
|
||||
static NetEndPoint GetNetEndPoint(string host, int port)
|
||||
{
|
||||
IPAddress address = NetUtility.Resolve(host);
|
||||
if (address == null)
|
||||
throw new NetException("Could not resolve host");
|
||||
return new NetEndPoint(address, port);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -234,7 +266,7 @@ namespace Lidgren.Network
|
||||
/// </summary>
|
||||
public NetConnection Connect(string host, int port)
|
||||
{
|
||||
return Connect(new IPEndPoint(NetUtility.Resolve(host), port), null);
|
||||
return Connect(GetNetEndPoint(host, port), null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -242,13 +274,13 @@ namespace Lidgren.Network
|
||||
/// </summary>
|
||||
public NetConnection Connect(string host, int port, NetOutgoingMessage hailMessage)
|
||||
{
|
||||
return Connect(new IPEndPoint(NetUtility.Resolve(host), port), hailMessage);
|
||||
return Connect(GetNetEndPoint(host, port), hailMessage);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a connection to a remote endpoint
|
||||
/// </summary>
|
||||
public NetConnection Connect(IPEndPoint remoteEndPoint)
|
||||
public NetConnection Connect(NetEndPoint remoteEndPoint)
|
||||
{
|
||||
return Connect(remoteEndPoint, null);
|
||||
}
|
||||
@@ -256,7 +288,7 @@ namespace Lidgren.Network
|
||||
/// <summary>
|
||||
/// Create a connection to a remote endpoint
|
||||
/// </summary>
|
||||
public virtual NetConnection Connect(IPEndPoint remoteEndPoint, NetOutgoingMessage hailMessage)
|
||||
public virtual NetConnection Connect(NetEndPoint remoteEndPoint, NetOutgoingMessage hailMessage)
|
||||
{
|
||||
if (remoteEndPoint == null)
|
||||
throw new ArgumentNullException("remoteEndPoint");
|
||||
@@ -281,7 +313,7 @@ namespace Lidgren.Network
|
||||
break;
|
||||
case NetConnectionStatus.RespondedConnect:
|
||||
// send another response
|
||||
hs.SendConnectResponse((float)NetTime.Now, false);
|
||||
hs.SendConnectResponse(NetTime.Now, false);
|
||||
break;
|
||||
default:
|
||||
// weird
|
||||
@@ -308,18 +340,27 @@ namespace Lidgren.Network
|
||||
/// <summary>
|
||||
/// Send raw bytes; only used for debugging
|
||||
/// </summary>
|
||||
#if DEBUG
|
||||
public void RawSend(byte[] arr, int offset, int length, IPEndPoint destination)
|
||||
#else
|
||||
internal void RawSend(byte[] arr, int offset, int length, IPEndPoint destination)
|
||||
#endif
|
||||
{
|
||||
public void RawSend(byte[] arr, int offset, int length, NetEndPoint destination)
|
||||
{
|
||||
// wrong thread - this miiiight crash with network thread... but what's a boy to do.
|
||||
Array.Copy(arr, offset, m_sendBuffer, 0, length);
|
||||
bool 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>
|
||||
/// Disconnects all active connections and closes the socket
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user