예제 #1
0
 private static bool CheckPort()
 {
     if (Common.PortIsInUse(Lidgren.MasterServer.Port))
     {
         ConsoleLogger.Log(LogLevels.Error, $"Port {Lidgren.MasterServer.Port} is already in use!");
         return(false);
     }
     return(true);
 }
예제 #2
0
        private static bool ParsePortNumber(Arguments commandLineArguments, string parameter, out ushort portNum)
        {
            if (ushort.TryParse(commandLineArguments[parameter].Trim(), out portNum))
            {
                return(true);
            }

            ConsoleLogger.Log(LogLevels.Error, $"Invalid port specified: {commandLineArguments[parameter].Trim()}");
            return(false);
        }
예제 #3
0
        public static async void Start()
        {
            var config = new NetPeerConfiguration("masterserver")
            {
                Port = Port,
                SuppressUnreliableUnorderedAcks = true,
                PingInterval      = 500,
                ConnectionTimeout = ServerMsTimeout
            };

            config.EnableMessageType(NetIncomingMessageType.UnconnectedData);

            var peer = new NetServer(config);

            peer.Start();

            CheckMasterServerListed();

            ConsoleLogger.Log(LogLevels.Normal, $"Master server {LmpVersioning.CurrentVersion} started! Поехали!");
            RemoveExpiredServers();

            while (RunServer)
            {
                NetIncomingMessage msg;
                while ((msg = peer.ReadMessage()) != null)
                {
                    switch (msg.MessageType)
                    {
                    case NetIncomingMessageType.DebugMessage:
                    case NetIncomingMessageType.VerboseDebugMessage:
                        ConsoleLogger.Log(LogLevels.Debug, msg.ReadString());
                        break;

                    case NetIncomingMessageType.WarningMessage:
                        ConsoleLogger.Log(LogLevels.Warning, msg.ReadString());
                        break;

                    case NetIncomingMessageType.ErrorMessage:
                        ConsoleLogger.Log(LogLevels.Error, msg.ReadString());
                        break;

                    case NetIncomingMessageType.UnconnectedData:
                        var message = GetMessage(msg);
                        if (message != null && !message.VersionMismatch)
                        {
                            HandleMessage(message, msg, peer);
                        }
                        break;
                    }
                }
                await Task.Delay(ServerMsTick);
            }
            peer.Shutdown("So long and thanks for all the fish!");
        }
예제 #4
0
        public static void MainEntryPoint(string[] args)
        {
            MasterServerPortMapper.UseUpnp = !args.Any(a => a.Contains("noupnp"));
            IsNightly = args.Any(a => a.Contains("nightly"));
            if (Common.PlatformIsWindows())
            {
                ConsoleUtil.DisableConsoleQuickEdit();
            }

            Console.Title = $"LMP MasterServer {LmpVersioning.CurrentVersion}";

            if (IsNightly)
            {
                Console.Title += " NIGHTLY";
            }

            Console.OutputEncoding = Encoding.Unicode;

            var commandLineArguments = new Arguments(args);

            if (commandLineArguments["h"] != null)
            {
                ShowCommandLineHelp();
                return;
            }

            if (!ParseMasterServerPortNumber(commandLineArguments))
            {
                return;
            }
            if (!ParseHttpServerPort(commandLineArguments))
            {
                return;
            }
            MasterServerPortMapper.OpenPort().Wait();

            ConsoleLogger.Log(LogLevels.Normal, $"Starting MasterServer at port: {Lidgren.MasterServer.Port}");
            if (IsNightly)
            {
                ConsoleLogger.Log(LogLevels.Normal, "Will download NIGHTLY versions!");
            }
            ConsoleLogger.Log(LogLevels.Normal, $"Listening for GET requests at port: {LunaHttpServer.Port}");

            if (CheckPort())
            {
                Lidgren.MasterServer.RunServer = true;
                WebHandler.InitWebFiles();
                LunaHttpServer.Start();
                Task.Run(() => MasterServerPortMapper.RefreshUpnpPort());
                Task.Run(() => Lidgren.MasterServer.Start());
            }
        }
예제 #5
0
        private static void RegisterServer(IMessageBase message, NetIncomingMessage netMsg)
        {
            var msgData = (MsRegisterServerMsgData)message.Data;

            if (!ServerDictionary.ContainsKey(msgData.Id))
            {
                ServerDictionary.TryAdd(msgData.Id, new Server(msgData, netMsg.SenderEndPoint));
                ConsoleLogger.Log(LogLevels.Normal, $"NEW SERVER: {netMsg.SenderEndPoint}");
            }
            else
            {
                //Just update
                ServerDictionary[msgData.Id].Update(msgData);
            }
        }
예제 #6
0
        private static void CheckMasterServerListed()
        {
            var servers     = MasterServerRetriever.RetrieveWorkingMasterServersEndpoints();
            var ownEndpoint = $"{LunaNetUtils.GetOwnExternalIpAddress()}:{Port}";

            if (!servers.Contains(ownEndpoint))
            {
                ConsoleLogger.Log(LogLevels.Error, $"You're not in the master-servers URL ({RepoConstants.MasterServersListShortUrl}) " +
                                  "Clients/Servers won't see you");
            }
            else
            {
                ConsoleLogger.Log(LogLevels.Normal, "Own ip correctly listed in master - servers URL");
            }
        }
예제 #7
0
        private static void RemoveExpiredServers()
        {
            Task.Run(async() =>
            {
                while (RunServer)
                {
                    var serversIdsToRemove = ServerDictionary
                                             .Where(s => LunaNetworkTime.UtcNow.Ticks - s.Value.LastRegisterTime >
                                                    TimeSpan.FromMilliseconds(ServerMsTimeout).Ticks)
                                             .ToArray();

                    foreach (var serverId in serversIdsToRemove)
                    {
                        ConsoleLogger.Log(LogLevels.Normal, $"REMOVING SERVER: {serverId.Value.ExternalEndpoint}");
                        ServerDictionary.TryRemove(serverId.Key, out var _);
                    }

                    await Task.Delay(ServerRemoveMsCheckInterval);
                }
            });
        }
예제 #8
0
        private static void HandleMessage(IMasterServerMessageBase message, NetIncomingMessage netMsg, NetPeer peer)
        {
            try
            {
                switch ((message?.Data as MsBaseMsgData)?.MasterServerMessageSubType)
                {
                case MasterServerMessageSubType.RegisterServer:
                    RegisterServer(message, netMsg);
                    break;

                case MasterServerMessageSubType.RequestServers:
                    ConsoleLogger.Log(LogLevels.Normal, $"LIST REQUEST from: {netMsg.SenderEndPoint}");
                    SendServerLists(netMsg, peer);
                    break;

                case MasterServerMessageSubType.Introduction:
                    var msgData = (MsIntroductionMsgData)message.Data;
                    if (ServerDictionary.TryGetValue(msgData.Id, out var server))
                    {
                        ConsoleLogger.Log(LogLevels.Normal, $"INTRODUCTION request from: {netMsg.SenderEndPoint} to server: {server.ExternalEndpoint}");
                        peer.Introduce(server.InternalEndpoint, server.ExternalEndpoint,
                                       Common.CreateEndpointFromString(msgData.InternalEndpoint), // client internal
                                       netMsg.SenderEndPoint,                                     // client external
                                       msgData.Token);                                            // request token
                    }
                    else
                    {
                        ConsoleLogger.Log(LogLevels.Warning, $"Client {netMsg.SenderEndPoint} requested introduction to nonlisted host!");
                    }
                    break;
                }
            }
            catch (Exception e)
            {
                ConsoleLogger.Log(LogLevels.Error, $"Error handling message. Details: {e}");
            }
        }