Exemple #1
0
        static void Init()
        {
            if (config != null)
            {
                return;
            }

            // Load config
            config = new Configuration("PluginData/settings.cfg");
            config.Load();

            // Set up core
            core = Core.Instance;
            core.OneRPCPerUpdate     = config.OneRPCPerUpdate;
            core.MaxTimePerUpdate    = config.MaxTimePerUpdate;
            core.AdaptiveRateControl = config.AdaptiveRateControl;
            core.BlockingRecv        = config.BlockingRecv;
            core.RecvTimeout         = config.RecvTimeout;

            // Set up server
            rpcTcpServer    = new TCPServer("RPCServer", config.Address, config.RPCPort);
            streamTcpServer = new TCPServer("StreamServer", config.Address, config.StreamPort);
            rpcServer       = new KRPC.Server.ProtocolBuffers.RPCServer(rpcTcpServer);
            streamServer    = new KRPC.Server.ProtocolBuffers.StreamServer(streamTcpServer);
            server          = new Server.Server(rpcServer, streamServer);
        }
Exemple #2
0
            /// <summary>
            /// Create a server instance from this configuration
            /// </summary>
            public KRPC.Server.Server Create()
            {
                KRPC.Server.Message.RPCServer    rpcServer    = null;
                KRPC.Server.Message.StreamServer streamServer = null;

                var serverProtocol = Protocol;

                if (serverProtocol == Protocol.ProtocolBuffersOverTCP ||
                    serverProtocol == Protocol.ProtocolBuffersOverWebsockets)
                {
                    var serverAddress = IPAddress.Loopback;
                    IPAddress.TryParse(Settings.GetValueOrDefault("address", IPAddress.Loopback.ToString()), out serverAddress);
                    ushort rpcPort    = 0;
                    ushort streamPort = 0;
                    ushort.TryParse(Settings.GetValueOrDefault("rpc_port", "0"), out rpcPort);
                    ushort.TryParse(Settings.GetValueOrDefault("stream_port", "0"), out streamPort);
                    var rpcTcpServer    = new TCPServer(serverAddress, rpcPort);
                    var streamTcpServer = new TCPServer(serverAddress, streamPort);
                    if (serverProtocol == Protocol.ProtocolBuffersOverTCP)
                    {
                        rpcServer    = new KRPC.Server.ProtocolBuffers.RPCServer(rpcTcpServer);
                        streamServer = new KRPC.Server.ProtocolBuffers.StreamServer(streamTcpServer);
                    }
                    else
                    {
                        rpcServer    = new KRPC.Server.WebSockets.RPCServer(rpcTcpServer);
                        streamServer = new KRPC.Server.WebSockets.StreamServer(streamTcpServer);
                    }
                }
                return(new KRPC.Server.Server(Id, serverProtocol, Name, rpcServer, streamServer));
            }
Exemple #3
0
            /// <summary>
            /// Create a server instance from this configuration
            /// </summary>
            public KRPC.Server.Server Create()
            {
                KRPC.Server.Message.RPCServer    rpcServer    = null;
                KRPC.Server.Message.StreamServer streamServer = null;

                var serverProtocol = Protocol;

                if (serverProtocol == Protocol.ProtocolBuffersOverTCP ||
                    serverProtocol == Protocol.ProtocolBuffersOverWebsockets)
                {
                    var serverAddress = IPAddress.Loopback;
                    IPAddress.TryParse(Settings.GetValueOrDefault("address", IPAddress.Loopback.ToString()), out serverAddress);
                    ushort rpcPort    = 0;
                    ushort streamPort = 0;
                    ushort.TryParse(Settings.GetValueOrDefault("rpc_port", "0"), out rpcPort);
                    ushort.TryParse(Settings.GetValueOrDefault("stream_port", "0"), out streamPort);
                    var rpcTcpServer    = new TCPServer(serverAddress, rpcPort);
                    var streamTcpServer = new TCPServer(serverAddress, streamPort);
                    if (serverProtocol == Protocol.ProtocolBuffersOverTCP)
                    {
                        rpcServer    = new KRPC.Server.ProtocolBuffers.RPCServer(rpcTcpServer);
                        streamServer = new KRPC.Server.ProtocolBuffers.StreamServer(streamTcpServer);
                    }
                    else
                    {
                        rpcServer    = new KRPC.Server.WebSockets.RPCServer(rpcTcpServer);
                        streamServer = new KRPC.Server.WebSockets.StreamServer(streamTcpServer);
                    }
                }
                else
                {
                    uint   baudRate = 0;
                    ushort dataBits = 0;
                    KRPC.IO.Ports.Parity   parity;
                    KRPC.IO.Ports.StopBits stopBits;
                    uint.TryParse(Settings.GetValueOrDefault("baud_rate", "9600"), out baudRate);
                    ushort.TryParse(Settings.GetValueOrDefault("data_bits", "8"), out dataBits);
                    // TODO: add helper method for Enum.TryParse (only available in .NET 4)
                    try {
                        parity = (KRPC.IO.Ports.Parity)Enum.Parse(
                            typeof(KRPC.IO.Ports.Parity),
                            Settings.GetValueOrDefault("parity", "None"));
                    } catch (ArgumentException) {
                        parity = KRPC.IO.Ports.Parity.None;
                    }
                    try {
                        stopBits = (KRPC.IO.Ports.StopBits)Enum.Parse(
                            typeof(KRPC.IO.Ports.StopBits),
                            Settings.GetValueOrDefault("stop_bits", "One"));
                    } catch (ArgumentException) {
                        stopBits = KRPC.IO.Ports.StopBits.None;
                    }
                    var serialServer = new KRPC.Server.SerialIO.ByteServer(
                        Settings.GetValueOrDefault("port", string.Empty),
                        baudRate, dataBits, parity, stopBits);
                    rpcServer    = new KRPC.Server.SerialIO.RPCServer(serialServer);
                    streamServer = new KRPC.Server.SerialIO.StreamServer();
                }
                return(new KRPC.Server.Server(Id, serverProtocol, Name, rpcServer, streamServer));
            }
Exemple #4
0
        public static void Main(string[] args)
        {
            bool showHelp    = false;
            bool showVersion = false;

            Logger.Format  = "{0:G} - {1} - {2}";
            Logger.Enabled = true;
            Logger.Level   = Logger.Severity.Info;
            bool   serverDebug = false;
            string bind        = "127.0.0.1";
            ushort rpcPort     = 0;
            ushort streamPort  = 0;
            string type        = "protobuf";

            var options = new OptionSet {
                {
                    "h|help", "show this help message and exit",
                    v => showHelp = v != null
                }, {
                    "v|version", "show program's version number and exit",
                    v => showVersion = v != null
                }, {
                    "bind=", "Address to bind the server to. If unspecified, the loopback address is used (127.0.0.1).",
                    v => bind = v
                }, {
                    "rpc-port=", "Port number to use for the RPC server. If unspecified, use an ephemeral port.",
                    (ushort v) => rpcPort = v
                }, {
                    "stream-port=", "Port number to use for the stream server. If unspecified, use an ephemeral port.",
                    (ushort v) => streamPort = v
                }, {
                    "type=", "Type of server to run. Either protobuf, websockets or websockets-echo.",
                    v => type = v
                }, {
                    "debug", "Set log level to 'debug', defaults to 'info'",
                    v => {
                        if (v != null)
                        {
                            Logger.Enabled = true;
                            Logger.Level   = Logger.Severity.Debug;
                        }
                    }
                }, { "quiet", "Set log level to 'warning'",
                     v => {
                         if (v != null)
                         {
                             Logger.Enabled = true;
                             Logger.Level   = Logger.Severity.Warning;
                         }
                     } }, {
                    "server-debug", "Output debug information about the server",
                    v => serverDebug = v != null
                }
            };

            options.Parse(args);

            if (showHelp)
            {
                Help(options);
                return;
            }

            if (showVersion)
            {
                var assembly = Assembly.GetEntryAssembly();
                var info     = FileVersionInfo.GetVersionInfo(assembly.Location);
                var version  = string.Format("{0}.{1}.{2}", info.FileMajorPart, info.FileMinorPart, info.FileBuildPart);
                Console.WriteLine("TestServer.exe version " + version);
                return;
            }

            KRPC.Service.Scanner.Scanner.GetServices();

            var core = Core.Instance;

            CallContext.SetGameScene(GameScene.SpaceCenter);
            core.OnClientRequestingConnection += (s, e) => e.Request.Allow();

            IPAddress bindAddress;

            if (!IPAddress.TryParse(bind, out bindAddress))
            {
                Console.WriteLine("Failed to parse bind address.");
                return;
            }

            var rpcTcpServer    = new TCPServer(bindAddress, rpcPort);
            var streamTcpServer = new TCPServer(bindAddress, streamPort);

            Server server;

            if (type == "protobuf")
            {
                var rpcServer    = new KRPC.Server.ProtocolBuffers.RPCServer(rpcTcpServer);
                var streamServer = new KRPC.Server.ProtocolBuffers.StreamServer(streamTcpServer);
                server = new Server(Guid.NewGuid(), Protocol.ProtocolBuffersOverTCP, "TestServer", rpcServer, streamServer);
            }
            else if (type == "websockets")
            {
                var rpcServer    = new KRPC.Server.WebSockets.RPCServer(rpcTcpServer);
                var streamServer = new KRPC.Server.WebSockets.StreamServer(streamTcpServer);
                server = new Server(Guid.NewGuid(), Protocol.ProtocolBuffersOverWebsockets, "TestServer", rpcServer, streamServer);
            }
            else if (type == "websockets-echo")
            {
                var rpcServer    = new KRPC.Server.WebSockets.RPCServer(rpcTcpServer, true);
                var streamServer = new KRPC.Server.WebSockets.StreamServer(streamTcpServer);
                server = new Server(Guid.NewGuid(), Protocol.ProtocolBuffersOverWebsockets, "TestServer", rpcServer, streamServer);
            }
            else
            {
                Logger.WriteLine("Server type '" + type + "' not supported", Logger.Severity.Error);
                return;
            }
            core.Add(server);

            Logger.WriteLine("Starting server...");
            core.StartAll();
            Logger.WriteLine("type = " + type);
            Logger.WriteLine("bind = " + bindAddress);
            Logger.WriteLine("rpc_port = " + rpcTcpServer.ActualPort);
            Logger.WriteLine("stream_port = " + streamTcpServer.ActualPort);
            Logger.WriteLine("Server started successfully");

            const long targetFPS      = 60;
            long       update         = 0;
            long       ticksPerUpdate = Stopwatch.Frequency / targetFPS;
            var        timer          = new Stopwatch();

            while (server.Running)
            {
                timer.Reset();
                timer.Start();

                core.Update();

                if (serverDebug && update % targetFPS == 0)
                {
                    // Output details about whether server.Update() took too little or too long to execute
                    var elapsed   = timer.ElapsedTicks;
                    var diffTicks = Math.Abs(ticksPerUpdate - elapsed);
                    var diffTime  = Math.Round(diffTicks / (double)Stopwatch.Frequency * 1000d, 2);
                    if (elapsed > ticksPerUpdate)
                    {
                        Console.WriteLine("Slow by " + diffTime + " ms (" + diffTicks + " ticks)");
                    }
                    else
                    {
                        Console.WriteLine("Fast by " + diffTime + " ms (" + diffTicks + " ticks)");
                    }
                }

                // Wait, to force 60 FPS
                while (timer.ElapsedTicks < ticksPerUpdate)
                {
                }
                update++;
            }
        }
Exemple #5
0
        public static void Main(string[] args)
        {
            bool showHelp    = false;
            bool showVersion = false;

            Logger.Enabled             = true;
            Logger.Level               = Logger.Severity.Info;
            RPCException.VerboseErrors = true;
            bool   serverDebug = false;
            ushort rpcPort     = 0;
            ushort streamPort  = 0;
            string type        = "protobuf";

            var options = new OptionSet {
                {
                    "h|help", "show this help message and exit",
                    v => showHelp = v != null
                }, {
                    "v|version", "show program's version number and exit",
                    v => showVersion = v != null
                }, {
                    "rpc-port=", "Port number to use for the RPC server. If unspecified, use an ephemeral port.",
                    (ushort v) => rpcPort = v
                }, {
                    "stream-port=", "Port number to use for the stream server. If unspecified, use an ephemeral port.",
                    (ushort v) => streamPort = v
                }, {
                    "type=", "Type of server to run. Current just 'protobuf'.",
                    v => type = v
                }, {
                    "debug", "Set log level to 'debug', defaults to 'info'",
                    v => {
                        if (v != null)
                        {
                            Logger.Enabled = true;
                            Logger.Level   = Logger.Severity.Debug;
                        }
                    }
                }, { "quiet", "Set log level to 'warning'",
                     v => {
                         if (v != null)
                         {
                             Logger.Enabled = true;
                             Logger.Level   = Logger.Severity.Warning;
                         }
                     } }, {
                    "server-debug", "Output debug information about the server",
                    v => serverDebug = v != null
                },
            };

            options.Parse(args);

            if (showHelp)
            {
                Help(options);
                return;
            }

            if (showVersion)
            {
                var assembly = Assembly.GetEntryAssembly();
                var info     = FileVersionInfo.GetVersionInfo(assembly.Location);
                var version  = String.Format("{0}.{1}.{2}", info.FileMajorPart, info.FileMinorPart, info.FileBuildPart);
                Console.WriteLine("TestServer.exe version " + version);
                return;
            }

            KRPC.Service.Scanner.Scanner.GetServices();

            var core = Core.Instance;

            CallContext.SetGameScene(GameScene.SpaceCenter);
            var timeSpan = new TimeSpan();

            Core.Instance.GetUniversalTime = () => timeSpan.TotalSeconds;

            var    rpcTcpServer    = new TCPServer("RPCServer", IPAddress.Loopback, rpcPort);
            var    streamTcpServer = new TCPServer("StreamServer", IPAddress.Loopback, streamPort);
            Server server;

            if (type == "protobuf")
            {
                var rpcServer    = new KRPC.Server.ProtocolBuffers.RPCServer(rpcTcpServer);
                var streamServer = new KRPC.Server.ProtocolBuffers.StreamServer(streamTcpServer);
                server = new Server(rpcServer, streamServer);
            }
            else
            {
                Console.WriteLine("Server type '" + type + "' not supported");
                return;
            }
            server.OnClientRequestingConnection += (s, e) => e.Request.Allow();

            Console.WriteLine("Starting server...");
            server.Start();
            Console.WriteLine("type = " + type);
            Console.WriteLine("rpc_port = " + rpcTcpServer.Port);
            Console.WriteLine("stream_port = " + streamTcpServer.Port);
            Console.WriteLine("Server started successfully");

            const long targetFPS      = 60;
            long       update         = 0;
            long       ticksPerUpdate = Stopwatch.Frequency / targetFPS;
            var        timer          = new Stopwatch();

            while (server.Running)
            {
                timer.Reset();
                timer.Start();

                core.Update();

                if (serverDebug && update % targetFPS == 0)
                {
                    // Output details about whether server.Update() took too little or too long to execute
                    var elapsed   = timer.ElapsedTicks;
                    var diffTicks = Math.Abs(ticksPerUpdate - elapsed);
                    var diffTime  = Math.Round((double)diffTicks / (double)Stopwatch.Frequency * 1000d, 2);
                    if (elapsed > ticksPerUpdate)
                    {
                        Console.WriteLine("Slow by " + diffTime + " ms (" + diffTicks + " ticks)");
                    }
                    else
                    {
                        Console.WriteLine("Fast by " + diffTime + " ms (" + diffTicks + " ticks)");
                    }
                }

                // Wait, to force 60 FPS
                while (timer.ElapsedTicks < ticksPerUpdate)
                {
                }
                update++;
            }
        }
Exemple #6
0
        public static void Main (string[] args)
        {
            bool showHelp = false;
            bool showVersion = false;

            Logger.Enabled = true;
            Logger.Level = Logger.Severity.Info;
            RPCException.VerboseErrors = true;
            bool serverDebug = false;
            ushort rpcPort = 0;
            ushort streamPort = 0;
            string type = "protobuf";

            var options = new OptionSet { {
                    "h|help", "show this help message and exit",
                    v => showHelp = v != null
                }, {
                    "v|version", "show program's version number and exit",
                    v => showVersion = v != null
                }, {
                    "rpc-port=", "Port number to use for the RPC server. If unspecified, use an ephemeral port.",
                    (ushort v) => rpcPort = v
                }, {
                    "stream-port=", "Port number to use for the stream server. If unspecified, use an ephemeral port.",
                    (ushort v) => streamPort = v
                }, {
                    "type=", "Type of server to run. Current just 'protobuf'.",
                    v => type = v
                }, {
                    "debug", "Set log level to 'debug', defaults to 'info'",
                    v => {
                        if (v != null) {
                            Logger.Enabled = true;
                            Logger.Level = Logger.Severity.Debug;
                        }
                    }
                }, { "quiet", "Set log level to 'warning'",
                    v => {
                        if (v != null) {
                            Logger.Enabled = true;
                            Logger.Level = Logger.Severity.Warning;
                        }
                    }
                }, {
                    "server-debug", "Output debug information about the server",
                    v => serverDebug = v != null
                },
            };
            options.Parse (args);

            if (showHelp) {
                Help (options);
                return;
            }

            if (showVersion) {
                var assembly = Assembly.GetEntryAssembly ();
                var info = FileVersionInfo.GetVersionInfo (assembly.Location);
                var version = String.Format ("{0}.{1}.{2}", info.FileMajorPart, info.FileMinorPart, info.FileBuildPart);
                Console.WriteLine ("TestServer.exe version " + version);
                return;
            }

            KRPC.Service.Scanner.Scanner.GetServices ();

            var core = Core.Instance;
            CallContext.SetGameScene (GameScene.SpaceCenter);
            var timeSpan = new TimeSpan ();
            Core.Instance.GetUniversalTime = () => timeSpan.TotalSeconds;

            var rpcTcpServer = new TCPServer ("RPCServer", IPAddress.Loopback, rpcPort);
            var streamTcpServer = new TCPServer ("StreamServer", IPAddress.Loopback, streamPort);
            Server server;
            if (type == "protobuf") {
                var rpcServer = new KRPC.Server.ProtocolBuffers.RPCServer (rpcTcpServer);
                var streamServer = new KRPC.Server.ProtocolBuffers.StreamServer (streamTcpServer);
                server = new Server (rpcServer, streamServer);
            } else {
                Console.WriteLine ("Server type '" + type + "' not supported");
                return;
            }
            server.OnClientRequestingConnection += (s, e) => e.Request.Allow ();

            Console.WriteLine ("Starting server...");
            server.Start ();
            Console.WriteLine ("type = " + type);
            Console.WriteLine ("rpc_port = " + rpcTcpServer.Port);
            Console.WriteLine ("stream_port = " + streamTcpServer.Port);
            Console.WriteLine ("Server started successfully");

            const long targetFPS = 60;
            long update = 0;
            long ticksPerUpdate = Stopwatch.Frequency / targetFPS;
            var timer = new Stopwatch ();
            while (server.Running) {
                timer.Reset ();
                timer.Start ();

                core.Update ();

                if (serverDebug && update % targetFPS == 0) {
                    // Output details about whether server.Update() took too little or too long to execute
                    var elapsed = timer.ElapsedTicks;
                    var diffTicks = Math.Abs (ticksPerUpdate - elapsed);
                    var diffTime = Math.Round ((double)diffTicks / (double)Stopwatch.Frequency * 1000d, 2);
                    if (elapsed > ticksPerUpdate)
                        Console.WriteLine ("Slow by " + diffTime + " ms (" + diffTicks + " ticks)");
                    else
                        Console.WriteLine ("Fast by " + diffTime + " ms (" + diffTicks + " ticks)");
                }

                // Wait, to force 60 FPS
                while (timer.ElapsedTicks < ticksPerUpdate) {
                }
                update++;
            }
        }