コード例 #1
0
        CreateTestServer <TEventHandler>(int port, bool start = false)
            where TEventHandler : IServerEventHandler
        {
            var            dataProcessor = SimpleMessageDataProcessor.CreateProcessor(_loggerFactory.CreateLogger <SimpleMessageDataProcessor>());
            var            eventHandler  = Activator.CreateInstance <TEventHandler>();
            IServerService server        = new TcpServerService(eventHandler, new ServerConfig("127.0.0.1", port),
                                                                _loggerFactory, EnumDataProcessorType.Hsm, (b) => b.ToList().Take(1).ToArray());

            if (start)
            {
                server.Start();
            }
            return(new Tuple <IServerService, TEventHandler, SimpleMessageDataProcessor>(server, eventHandler, dataProcessor));
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: jrozac/Layer4Stack
        /// <summary>
        /// Runs server
        /// </summary>
        static void RunServer()
        {
            // setup server
            var config              = new ServerConfig(_ipAddress, _port);
            var handler             = new ServerEventHandler();
            TcpServerService server = new TcpServerService(handler, config, _loggerFactory, EnumDataProcessorType.Hsm);

            // setup handler
            handler.EventRequestDisconnect += (obj, client) => server.DisconnectClient(client);
            handler.EventRequestStop       += (obj, client) => server.Stop();

            // UI commands
            Console.WriteLine("========================================================================");
            Console.WriteLine("Commands");
            Console.WriteLine("quit - Quits the program");
            Console.WriteLine("start - Starts the server");
            Console.WriteLine("stop - Stops the server");
            Console.WriteLine("status - Displays the server status");
            Console.WriteLine("Any other input is threated like a message which is sent to all clients.");
            Console.WriteLine("========================================================================");
            Console.WriteLine();

            // start server
            server.Start();

            // read for UI commands
            while (true)
            {
                string cmd = Console.ReadLine();
                if (string.IsNullOrWhiteSpace(cmd))
                {
                    continue;
                }

                if (cmd == "stop")
                {
                    server.Stop();
                }
                else if (cmd == "start")
                {
                    server.Start();
                }
                else if (cmd == "quit")
                {
                    break;
                }
                else if (cmd == "status")
                {
                    Console.WriteLine(server.Started ? "Server is running." : "Server is stopped.");
                }
                else
                {
                    server.SendToAll(Encoding.UTF8.GetBytes(cmd));
                }
            }

            // stop server if not stopped
            server.Stop();

            // wait for enter
            Console.WriteLine("Press Enter to quit the program.");
            Console.ReadLine();
        }