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
- UPnP changes - now tries to parse any incoming message, regardless of port
This commit is contained in:
@@ -398,9 +398,9 @@ namespace Lidgren.Network
|
|||||||
|
|
||||||
IPEndPoint ipsender = (IPEndPoint)m_senderRemote;
|
IPEndPoint ipsender = (IPEndPoint)m_senderRemote;
|
||||||
|
|
||||||
if (ipsender.Port == 1900)
|
if (m_upnp != null && now < m_upnp.m_discoveryResponseDeadline)
|
||||||
{
|
{
|
||||||
// UPnP response
|
// is this an UPnP response?
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
string resp = System.Text.Encoding.ASCII.GetString(m_receiveBuffer, 0, bytesReceived);
|
string resp = System.Text.Encoding.ASCII.GetString(m_receiveBuffer, 0, bytesReceived);
|
||||||
|
|||||||
@@ -7,6 +7,13 @@ using System.Threading;
|
|||||||
|
|
||||||
namespace Lidgren.Network
|
namespace Lidgren.Network
|
||||||
{
|
{
|
||||||
|
public enum UPnPStatus
|
||||||
|
{
|
||||||
|
Discovering,
|
||||||
|
NotAvailable,
|
||||||
|
Available
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// UPnP support class
|
/// UPnP support class
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -18,12 +25,19 @@ namespace Lidgren.Network
|
|||||||
private NetPeer m_peer;
|
private NetPeer m_peer;
|
||||||
private ManualResetEvent m_discoveryComplete = new ManualResetEvent(false);
|
private ManualResetEvent m_discoveryComplete = new ManualResetEvent(false);
|
||||||
|
|
||||||
|
internal float m_discoveryResponseDeadline;
|
||||||
|
|
||||||
|
private UPnPStatus m_status;
|
||||||
|
|
||||||
|
public UPnPStatus Status { get { return m_status; } }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// NetUPnP constructor
|
/// NetUPnP constructor
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public NetUPnP(NetPeer peer)
|
public NetUPnP(NetPeer peer)
|
||||||
{
|
{
|
||||||
m_peer = peer;
|
m_peer = peer;
|
||||||
|
m_discoveryResponseDeadline = float.MinValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void Discover(NetPeer peer)
|
internal void Discover(NetPeer peer)
|
||||||
@@ -35,14 +49,20 @@ namespace Lidgren.Network
|
|||||||
"MAN:\"ssdp:discover\"\r\n" +
|
"MAN:\"ssdp:discover\"\r\n" +
|
||||||
"MX:3\r\n\r\n";
|
"MX:3\r\n\r\n";
|
||||||
|
|
||||||
|
m_status = UPnPStatus.Discovering;
|
||||||
|
|
||||||
byte[] arr = System.Text.Encoding.UTF8.GetBytes(str);
|
byte[] arr = System.Text.Encoding.UTF8.GetBytes(str);
|
||||||
|
|
||||||
|
m_peer.LogDebug("Attempting UPnP discovery");
|
||||||
peer.Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
|
peer.Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
|
||||||
peer.RawSend(arr, 0, arr.Length, new IPEndPoint(IPAddress.Broadcast, 1900));
|
peer.RawSend(arr, 0, arr.Length, new IPEndPoint(IPAddress.Broadcast, 1900));
|
||||||
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);
|
||||||
|
|
||||||
|
m_discoveryResponseDeadline = (float)NetTime.Now + 6.0f; // arbitrarily chosen number, router gets 6 seconds to respond
|
||||||
|
m_status = UPnPStatus.Discovering;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void ExtractServiceUrl(string resp)
|
internal void ExtractServiceUrl(string resp)
|
||||||
@@ -63,6 +83,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");
|
||||||
|
m_status = UPnPStatus.Available;
|
||||||
m_discoveryComplete.Set();
|
m_discoveryComplete.Set();
|
||||||
#if !DEBUG
|
#if !DEBUG
|
||||||
}
|
}
|
||||||
@@ -83,12 +104,30 @@ namespace Lidgren.Network
|
|||||||
return "http://" + gatewayURL + subURL;
|
return "http://" + gatewayURL + subURL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private bool CheckAvailability()
|
||||||
|
{
|
||||||
|
switch (m_status)
|
||||||
|
{
|
||||||
|
case UPnPStatus.NotAvailable:
|
||||||
|
return false;
|
||||||
|
case UPnPStatus.Available:
|
||||||
|
return true;
|
||||||
|
case UPnPStatus.Discovering:
|
||||||
|
if (m_discoveryComplete.WaitOne(c_discoveryTimeOutMillis))
|
||||||
|
return true;
|
||||||
|
if (NetTime.Now > m_discoveryResponseDeadline)
|
||||||
|
m_status = UPnPStatus.NotAvailable;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Add a forwarding rule to the router using UPnP
|
/// Add a forwarding rule to the router using UPnP
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool ForwardPort(int port, string description)
|
public bool ForwardPort(int port, string description)
|
||||||
{
|
{
|
||||||
if (m_serviceUrl == null && !m_discoveryComplete.WaitOne(c_discoveryTimeOutMillis))
|
if (!CheckAvailability())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
IPAddress mask;
|
IPAddress mask;
|
||||||
@@ -126,8 +165,9 @@ namespace Lidgren.Network
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public bool DeleteForwardingRule(int port)
|
public bool DeleteForwardingRule(int port)
|
||||||
{
|
{
|
||||||
if (m_serviceUrl == null && !m_discoveryComplete.WaitOne(c_discoveryTimeOutMillis))
|
if (!CheckAvailability())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
XmlDocument xdoc = SOAPRequest(m_serviceUrl,
|
XmlDocument xdoc = SOAPRequest(m_serviceUrl,
|
||||||
@@ -151,7 +191,7 @@ namespace Lidgren.Network
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public IPAddress GetExternalIP()
|
public IPAddress GetExternalIP()
|
||||||
{
|
{
|
||||||
if (m_serviceUrl == null && !m_discoveryComplete.WaitOne(c_discoveryTimeOutMillis))
|
if (!CheckAvailability())
|
||||||
return null;
|
return null;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user