Example #1
0
        /// <summary>
        /// Starts the server
        /// </summary>
        /// <returns>True if the server was successfully started</returns>
        public virtual bool Start()
        {
            if (Configuration.EnableUPnP)
            {
                try
                {
                    UPnPNat nat = new UPnPNat();
                    if (!nat.Discover())
                        throw new Exception("[UPNP] Unable to access the UPnP Internet Gateway Device");
                    if (Log.IsDebugEnabled)
                    {
                        Log.Debug("[UPNP] Current UPnP mappings:");
                        foreach (var info in nat.ListForwardedPort())
                            Log.DebugFormat("[UPNP] {0} - {1} -> {2}:{3}({4}) ({5})",
                                            info.description,
                                            info.externalPort,
                                            info.internalIP,
                                            info.internalPort,
                                            info.protocol,
                                            info.enabled ? "enabled" : "disabled");
                    }
                    IPAddress localAddr = Configuration.IP;
                    nat.ForwardPort(Configuration.UDPPort, Configuration.UDPPort, ProtocolType.Udp, "DOL UDP", localAddr);
                    nat.ForwardPort(Configuration.Port, Configuration.Port, ProtocolType.Tcp, "DOL TCP", localAddr);
                    if (Configuration.DetectRegionIP)
                    {
                        try
                        {
                            Configuration.RegionIP = nat.GetExternalIP();
                            if (Log.IsDebugEnabled)
                                Log.Debug("[UPNP] Found the RegionIP: " + Configuration.RegionIP);
                        }
                        catch (Exception e)
                        {
                            Log.Warn("[UPNP] Unable to detect the RegionIP, It is possible that no mappings exist yet", e);
                        }
                    }
                }
                catch (Exception e)
                {
                    Log.Warn(e.Message, e);
                }
            }
            //Test if we have a valid port yet
            //if not try  binding.
            if (_listen == null && !InitSocket())
                return false;

            try
            {
                _listen.Listen(100);
                _listen.BeginAccept(_asyncAcceptCallback, this);

                if (Log.IsDebugEnabled)
                    Log.Debug("Server is now listening to incoming connections!");
            }
            catch (Exception e)
            {
                if (Log.IsErrorEnabled)
                    Log.Error("Start", e);

                if (_listen != null)
                    _listen.Close();

                return false;
            }

            return true;
        }
Example #2
0
 public void init()
 {
     upnp = new UPnPNat();
 }
Example #3
0
        /// <summary>
        /// Starts the server
        /// </summary>
        /// <returns>True if the server was successfully started</returns>
        public virtual bool Start()
        {
            if (Configuration.EnableUPnP)
            {
                try
                {
                    UPnPNat nat = new UPnPNat();
                    if (!nat.Discover())
                    {
                        throw new Exception("[UPNP] Unable to access the UPnP Internet Gateway Device");
                    }

                    if (Log.IsDebugEnabled)
                    {
                        Log.Debug("[UPNP] Current UPnP mappings:");
                        foreach (var info in nat.ListForwardedPort())
                        {
                            Log.DebugFormat(
                                "[UPNP] {0} - {1} -> {2}:{3}({4}) ({5})",
                                info.description,
                                info.externalPort,
                                info.internalIP,
                                info.internalPort,
                                info.protocol,
                                info.enabled ? "enabled" : "disabled");
                        }
                    }

                    IPAddress localAddr = Configuration.IP;
                    nat.ForwardPort(Configuration.UDPPort, Configuration.UDPPort, ProtocolType.Udp, "DOL UDP", localAddr);
                    nat.ForwardPort(Configuration.Port, Configuration.Port, ProtocolType.Tcp, "DOL TCP", localAddr);
                    if (Configuration.DetectRegionIP)
                    {
                        try
                        {
                            Configuration.RegionIP = nat.GetExternalIP();
                            if (Log.IsDebugEnabled)
                            {
                                Log.Debug("[UPNP] Found the RegionIP: " + Configuration.RegionIP);
                            }
                        }
                        catch (Exception e)
                        {
                            Log.Warn("[UPNP] Unable to detect the RegionIP, It is possible that no mappings exist yet", e);
                        }
                    }
                }
                catch (Exception e)
                {
                    Log.Warn(e.Message, e);
                }
            }

            // Test if we have a valid port yet
            // if not try  binding.
            if (_listen == null && !InitSocket())
            {
                return(false);
            }

            try
            {
                _listen.Listen(100);
                _listen.BeginAccept(_asyncAcceptCallback, this);

                if (Log.IsDebugEnabled)
                {
                    Log.Debug("Server is now listening to incoming connections!");
                }
            }
            catch (Exception e)
            {
                if (Log.IsErrorEnabled)
                {
                    Log.Error("Start", e);
                }

                if (_listen != null)
                {
                    _listen.Close();
                }

                return(false);
            }

            return(true);
        }
Example #4
0
 public void init()
 {
     upnp = new UPnPNat();
 }
Example #5
0
 public UpnpFrm()
 {
     InitializeComponent();
     up = new UPnPNat();
 }