Terrarium.PeerToPeer.NetworkEngine.SetHostIPInformation C# (CSharp) Method

SetHostIPInformation() private method

private SetHostIPInformation ( ) : void
return void
        internal void SetHostIPInformation()
        {
            _networkStatusMessage = "";

            if (GameConfig.LocalIPAddress.Length != 0)
            {
                _hostIP = IPAddress.Parse(GameConfig.LocalIPAddress.Trim());
                if (_hostIP.ToString() == "0.0.0.0")
                {
                    Debug.Assert(_networkStatusMessage.Length == 0);
                    _networkStatusMessage =
                        "You have not entered a valid ipAddress in the localIPAddress tag of the Userconfig.xml file.\nUse the format: <localIPAddress>X.X.X.X</localIPAddress>.";
                }
                return;
            }

            var peerService = new PeerDiscoveryService
                                  {
                                      Url = (GameConfig.WebRoot + "/discovery/discoverydb.asmx"),
                                      Timeout = NetworkTimeoutMsec
                                  };

            string address;
            var isVisibleNetworkAddress = false;
            var contactedDiscoveryServer = false;

            try
            {
                address = peerService.ValidatePeer();
                contactedDiscoveryServer = true;
            }
            catch (Exception e)
            {
                Trace.WriteLine("HANDLED EXCEPTION: There was a problem accessing the Peer Discovery Web Service. " + e);
                Debug.Assert(_networkStatusMessage.Length == 0);
                _networkStatusMessage =
                    "The .NET Terrarium is unable to connect to the Terrarium server. \nThis means you won't receive any creatures from other peers.\nYou'll need to restart the Terrarium when it is accessible again to receive creatures.";
                address = "127.0.0.1";
            }

            if (contactedDiscoveryServer)
            {
                isVisibleNetworkAddress = ValidatePeer(address);
            }

            if (isVisibleNetworkAddress)
            {
                _hostIP = IPAddress.Parse(address);
            }
            else
            {
                if (contactedDiscoveryServer)
                {
                    Debug.Assert(_networkStatusMessage.Length == 0);
                    _networkStatusMessage = NetworkBehindNatMessage;
                    IPHostEntry he = null;
                    try
                    {
                        he = Dns.GetHostEntry(Dns.GetHostName());
                    }
                    catch (Exception ex)
                    {
                        Trace.WriteLine("There was a problem resolving the hostname for this peer: " + address + " : " +
                                        ex);
                    }

                    _hostIP = he != null ? he.AddressList[0] : IPAddress.Parse("127.0.0.1");
                }
            }

            Trace.WriteLine("The IP address used for listening is " + _hostIP);
        }