small behavior fix #213 update

This commit is contained in:
d3agle 2015-05-26 14:55:23 -05:00
parent a2fb891e97
commit 7e13fa080e
3 changed files with 29 additions and 4 deletions

View File

@ -7,8 +7,28 @@ namespace xServer.Core.Helper
{
internal static class UPnP
{
private static bool _isPortForwarded = false;
public static bool IsPortForwarded
{
get
{
return _isPortForwarded;
}
}
private static ushort _port;
public static ushort Port
{
get
{
return _port;
}
}
public static void ForwardPort(ushort port)
{
_port = port;
new Thread(() =>
{
EndPoint endPoint;
@ -63,20 +83,23 @@ public static void ForwardPort(ushort port)
IStaticPortMappingCollection portMap = new UPnPNAT().StaticPortMappingCollection;
if (portMap != null)
portMap.Add(port, "TCP", port, ipAddr, true, "xRAT 2.0 UPnP");
_isPortForwarded = true;
}
catch
{
}
}).Start();
}
public static void RemovePort(ushort port)
public static void RemovePort()
{
try
{
IStaticPortMappingCollection portMap = new UPnPNAT().StaticPortMappingCollection;
if (portMap != null)
portMap.Remove(port, "TCP");
portMap.Remove(_port, "TCP");
_isPortForwarded = false;
}
catch
{

View File

@ -179,7 +179,7 @@ private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
if (ListenServer.Listening)
ListenServer.Disconnect();
if (XMLSettings.UseUPnP && ListenServer.Listening)
if (UPnP.IsPortForwarded)
UPnP.RemovePort(ushort.Parse(XMLSettings.ListenPort.ToString()));
nIcon.Visible = false;

View File

@ -45,7 +45,7 @@ private void btnListen_Click(object sender, EventArgs e)
{
try
{
if (chkUseUpnp.Checked)
if (chkUseUpnp.Checked && !Core.Helper.UPnP.IsPortForwarded)
Core.Helper.UPnP.ForwardPort(ushort.Parse(ncPort.Value.ToString(CultureInfo.InvariantCulture)));
if(chkNoIPIntegration.Checked)
NoIpUpdater.Start();
@ -63,6 +63,8 @@ private void btnListen_Click(object sender, EventArgs e)
try
{
_listenServer.Disconnect();
if (Core.Helper.UPnP.IsPortForwarded)
Core.Helper.UPnP.RemovePort();
}
finally
{