/// <summary>
        /// Send a request to the master server to introduce us and do the nat punchtrough to the selected server
        /// </summary>
        public static void IntroduceToServer(long serverId)
        {
            if (Servers.TryGetValue(serverId, out var serverInfo))
            {
                if (ServerIsInLocalLan(serverInfo.ExternalEndpoint))
                {
                    LunaLog.Log("Server is in LAN. Skipping NAT punch");
                    NetworkConnection.ConnectToServer(serverInfo.InternalEndpoint, Password);
                }
                else
                {
                    try
                    {
                        var msgData = NetworkMain.CliMsgFactory.CreateNewMessageData <MsIntroductionMsgData>();
                        msgData.Id               = serverId;
                        msgData.Token            = MainSystem.UniqueIdentifier;
                        msgData.InternalEndpoint = new IPEndPoint(LunaNetUtils.GetOwnInternalIpAddress(), NetworkMain.Config.Port);

                        var introduceMsg = NetworkMain.MstSrvMsgFactory.CreateNew <MainMstSrvMsg>(msgData);

                        MainSystem.Singleton.Status = string.Empty;
                        LunaLog.Log($"[LMP]: Sending NAT introduction to server. Token: {MainSystem.UniqueIdentifier}");
                        NetworkSender.QueueOutgoingMessage(introduceMsg);
                    }
                    catch (Exception e)
                    {
                        LunaLog.LogError($"[LMP]: Error connecting to server: {e}");
                    }
                }
            }
        }
        public static async void RegisterWithMasterServer()
        {
            if (!MasterServerSettings.SettingsStore.RegisterWithMasterServer)
            {
                return;
            }

            LunaLog.Normal("Registering with master servers...");

            var adr = LunaNetUtils.GetOwnInternalIpAddress();

            if (adr == null)
            {
                return;
            }

            var endpoint = new IPEndPoint(adr, ServerContext.Config.Port);

            while (ServerContext.ServerRunning)
            {
                var msgData = ServerContext.ServerMessageFactory.CreateNewMessageData <MsRegisterServerMsgData>();
                msgData.Id               = LidgrenServer.Server.UniqueIdentifier;
                msgData.Password         = !string.IsNullOrEmpty(GeneralSettings.SettingsStore.Password);
                msgData.Cheats           = GeneralSettings.SettingsStore.Cheats;
                msgData.Description      = GeneralSettings.SettingsStore.Description;
                msgData.CountryCode      = GeneralSettings.SettingsStore.CountryCode;
                msgData.Website          = GeneralSettings.SettingsStore.Website;
                msgData.WebsiteText      = GeneralSettings.SettingsStore.WebsiteText;
                msgData.RainbowEffect    = DedicatedServerSettings.SettingsStore.UseRainbowEffect;
                msgData.Color            = new[] { DedicatedServerSettings.SettingsStore.Red, DedicatedServerSettings.SettingsStore.Green, DedicatedServerSettings.SettingsStore.Blue };
                msgData.GameMode         = (int)GeneralSettings.SettingsStore.GameMode;
                msgData.InternalEndpoint = endpoint;
                msgData.MaxPlayers       = GeneralSettings.SettingsStore.MaxPlayers;
                msgData.ModControl       = GeneralSettings.SettingsStore.ModControl;
                msgData.PlayerCount      = ServerContext.Clients.Count;
                msgData.ServerName       = GeneralSettings.SettingsStore.ServerName;
                msgData.ServerVersion    = LmpVersioning.CurrentVersion.ToString(3);
                msgData.VesselPositionUpdatesMsInterval          = IntervalSettings.SettingsStore.VesselUpdatesMsInterval;
                msgData.SecondaryVesselPositionUpdatesMsInterval = IntervalSettings.SettingsStore.SecondaryVesselUpdatesMsInterval;
                msgData.WarpMode       = (int)WarpSettings.SettingsStore.WarpMode;
                msgData.TerrainQuality = (int)GeneralSettings.SettingsStore.TerrainQuality;

                msgData.Description = msgData.Description.Length > 200 ? msgData.Description.Substring(0, 200) : msgData.Description;
                msgData.CountryCode = msgData.CountryCode.Length > 2 ? msgData.CountryCode.Substring(0, 2) : msgData.CountryCode;
                msgData.Website     = msgData.Website.Length > 60 ? msgData.Website.Substring(0, 60) : msgData.Website;
                msgData.WebsiteText = msgData.WebsiteText.Length > 15 ? msgData.WebsiteText.Substring(0, 15) : msgData.WebsiteText;
                msgData.ServerName  = msgData.ServerName.Length > 30 ? msgData.ServerName.Substring(0, 30) : msgData.ServerName;

                foreach (var masterServer in MasterServerRetriever.MasterServers.GetValues)
                {
                    RegisterWithMasterServer(msgData, masterServer);
                }

                await Task.Delay(MasterServerRegistrationMsInterval);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Gets my local IPv4 address (not necessarily external) and subnet mask
        /// </summary>
        public static IPAddress GetMyAddress(out IPAddress mask)
        {
            mask = null;
#if UNITY_ANDROID || UNITY_STANDALONE_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_LINUX || UNITY_IOS || UNITY
            try
            {
                if (!(UnityEngine.Application.internetReachability == UnityEngine.NetworkReachability.NotReachable))
                {
                    return(null);
                }

                return(LunaNetUtils.GetOwnInternalIpAddress());
            }
            catch // Catch Access Denied errors
            {
                return(null);
            }
#endif
            return(null);
        }