You've already forked lidgren-network-gen3
mirror of
https://github.com/lidgren/lidgren-network-gen3.git
synced 2026-05-16 15:16:33 +09:00
UPnP port forwarding improved to tolerate up to 1000 ms of router response time; UPnP added to DurableSample
This commit is contained in:
@@ -3,6 +3,7 @@ using System.IO;
|
|||||||
using System.Xml;
|
using System.Xml;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
namespace Lidgren.Network
|
namespace Lidgren.Network
|
||||||
{
|
{
|
||||||
@@ -11,8 +12,11 @@ namespace Lidgren.Network
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class NetUPnP
|
public class NetUPnP
|
||||||
{
|
{
|
||||||
|
private const int c_discoveryTimeOutMillis = 1000;
|
||||||
|
|
||||||
private string m_serviceUrl;
|
private string m_serviceUrl;
|
||||||
private NetPeer m_peer;
|
private NetPeer m_peer;
|
||||||
|
private ManualResetEvent m_discoveryComplete = new ManualResetEvent(false);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// NetUPnP constructor
|
/// NetUPnP constructor
|
||||||
@@ -38,7 +42,7 @@ namespace Lidgren.Network
|
|||||||
peer.Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, false);
|
peer.Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, false);
|
||||||
|
|
||||||
// allow some extra time for router to respond
|
// allow some extra time for router to respond
|
||||||
System.Threading.Thread.Sleep(50);
|
// System.Threading.Thread.Sleep(50);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void ExtractServiceUrl(string resp)
|
internal void ExtractServiceUrl(string resp)
|
||||||
@@ -59,7 +63,7 @@ namespace Lidgren.Network
|
|||||||
return;
|
return;
|
||||||
m_serviceUrl = CombineUrls(resp, node.Value);
|
m_serviceUrl = CombineUrls(resp, node.Value);
|
||||||
m_peer.LogDebug("UPnP service ready");
|
m_peer.LogDebug("UPnP service ready");
|
||||||
System.Threading.Thread.Sleep(50);
|
m_discoveryComplete.Set();
|
||||||
#if !DEBUG
|
#if !DEBUG
|
||||||
}
|
}
|
||||||
catch { return; }
|
catch { return; }
|
||||||
@@ -84,7 +88,7 @@ namespace Lidgren.Network
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public bool ForwardPort(int port, string description)
|
public bool ForwardPort(int port, string description)
|
||||||
{
|
{
|
||||||
if (m_serviceUrl == null)
|
if (m_serviceUrl == null && !m_discoveryComplete.WaitOne(c_discoveryTimeOutMillis))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
IPAddress mask;
|
IPAddress mask;
|
||||||
@@ -120,7 +124,7 @@ namespace Lidgren.Network
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public bool DeleteForwardingRule(int port)
|
public bool DeleteForwardingRule(int port)
|
||||||
{
|
{
|
||||||
if (m_serviceUrl == null)
|
if (m_serviceUrl == null && !m_discoveryComplete.WaitOne(c_discoveryTimeOutMillis))
|
||||||
return false;
|
return false;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -145,9 +149,8 @@ namespace Lidgren.Network
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public IPAddress GetExternalIP()
|
public IPAddress GetExternalIP()
|
||||||
{
|
{
|
||||||
if (m_serviceUrl == null)
|
if (m_serviceUrl == null && !m_discoveryComplete.WaitOne(c_discoveryTimeOutMillis))
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
XmlDocument xdoc = SOAPRequest(m_serviceUrl, "<u:GetExternalIPAddress xmlns:u=\"urn:schemas-upnp-org:service:WANIPConnection:1\">" +
|
XmlDocument xdoc = SOAPRequest(m_serviceUrl, "<u:GetExternalIPAddress xmlns:u=\"urn:schemas-upnp-org:service:WANIPConnection:1\">" +
|
||||||
|
|||||||
@@ -25,9 +25,13 @@ namespace DurableServer
|
|||||||
config.Port = 14242;
|
config.Port = 14242;
|
||||||
config.EnableMessageType(NetIncomingMessageType.ConnectionApproval);
|
config.EnableMessageType(NetIncomingMessageType.ConnectionApproval);
|
||||||
config.EnableMessageType(NetIncomingMessageType.DiscoveryRequest);
|
config.EnableMessageType(NetIncomingMessageType.DiscoveryRequest);
|
||||||
|
config.EnableUPnP = true;
|
||||||
Server = new NetServer(config);
|
Server = new NetServer(config);
|
||||||
Server.Start();
|
Server.Start();
|
||||||
|
|
||||||
|
// attempt upnp port forwarding
|
||||||
|
Server.UPnP.ForwardPort(14242, "Durable sample test");
|
||||||
|
|
||||||
m_expectedReliableOrdered = new uint[3];
|
m_expectedReliableOrdered = new uint[3];
|
||||||
m_reliableOrderedCorrect = new int[3];
|
m_reliableOrderedCorrect = new int[3];
|
||||||
m_reliableOrderedErrors = new int[3];
|
m_reliableOrderedErrors = new int[3];
|
||||||
|
|||||||
Reference in New Issue
Block a user