Ejemplo n.º 1
1
        public static int Execute(List <string> args)
        {
            var loggerFactory = new LoggerFactory();//.AddConsole().AddDebug();
            var logger        = new LoggerFactory().CreateLogger("Test");

            try
            {
                var param = new ServerParam();

                try
                {
                    param.Parse(args);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("*** FAILED ***");
                    Console.WriteLine("Error while  parsing arguments");
                    Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
                    return(1);
                }


                TTransportFactory transFactory = null;

                // Transport
                TServerTransport trans;

                switch (param.transport)
                {
                case TransportChoice.NamedPipe:
                    Debug.Assert(param.pipe != null);
                    trans = new TNamedPipeServerTransport(param.pipe);
                    break;


                case TransportChoice.TlsSocket:
                    var cert = GetServerCert();
                    if (cert == null || !cert.HasPrivateKey)
                    {
                        throw new InvalidOperationException("Certificate doesn't contain private key");
                    }

                    transFactory = new TTransportFactory();     // framed/buffered is built into socket transports
                    trans        = new TTlsServerSocketTransport(param.port, cert, param.buffering,
                                                                 (sender, certificate, chain, errors) => true,
                                                                 null, SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12);
                    break;

                case TransportChoice.Socket:
                default:
                    transFactory = new TTransportFactory();     // framed/buffered is built into socket transports
                    trans        = new TServerSocketTransport(param.port, 0, param.buffering);
                    break;
                }

                // add layered transport, if not already set above
                if (transFactory == null)
                {
                    switch (param.buffering)
                    {
                    case Buffering.FramedTransport:
                        transFactory = new TFramedTransport.Factory();
                        break;

                    case Buffering.BufferedTransport:
                        transFactory = new TBufferedTransport.Factory();
                        break;
                    }
                }

                // Protocol
                ITProtocolFactory proto;
                switch (param.protocol)
                {
                case ProtocolChoice.Compact:
                    proto = new TCompactProtocol.Factory();
                    break;

                case ProtocolChoice.Json:
                    proto = new TJsonProtocol.Factory();
                    break;

                case ProtocolChoice.Binary:
                default:
                    proto = new TBinaryProtocol.Factory();
                    break;
                }

                // Processor
                var testHandler      = new TestHandlerAsync();
                var testProcessor    = new ThriftTest.AsyncProcessor(testHandler);
                var processorFactory = new TSingletonProcessorFactory(testProcessor);

                TServer serverEngine = new TSimpleAsyncServer(processorFactory, trans, transFactory, transFactory, proto, proto, logger);

                //Server event handler
                var serverEvents = new MyServerEventHandler();
                serverEngine.SetEventHandler(serverEvents);

                // Run it
                var where = (!string.IsNullOrEmpty(param.pipe)) ? "on pipe " + param.pipe : "on port " + param.port;
                Console.WriteLine("Starting the AsyncBaseServer " + where +
                                  " with processor TPrototypeProcessorFactory prototype factory " +
                                  (param.buffering == Buffering.BufferedTransport ? " with buffered transport" : "") +
                                  (param.buffering == Buffering.FramedTransport ? " with framed transport" : "") +
                                  (param.transport == TransportChoice.TlsSocket ? " with encryption" : "") +
                                  (param.protocol == ProtocolChoice.Compact ? " with compact protocol" : "") +
                                  (param.protocol == ProtocolChoice.Json ? " with json protocol" : "") +
                                  "...");
                serverEngine.ServeAsync(CancellationToken.None).GetAwaiter().GetResult();
                Console.ReadLine();
            }
            catch (Exception x)
            {
                Console.Error.Write(x);
                return(1);
            }
            Console.WriteLine("done.");
            return(0);
        }
Ejemplo n.º 2
0
        /**
         * 点击事件
         **/
        public static void InputEvent(Int32 port, MouseEventType type)
        {
            TSocket socket = new TSocket("127.0.0.1", port, 1000);

            TFramedTransport.Factory factory = new
                                               TFramedTransport.Factory();
            TTransport transport = factory.GetTransport(socket);

            TBinaryProtocol.Factory factory1 =
                new TBinaryProtocol.Factory();
            TProtocol            protocol = factory1.GetProtocol(transport);
            TMultiplexedProtocol impl1
                = new TMultiplexedProtocol(protocol, "ScreenService");

            ScreenService.Client client =
                new ScreenService.Client(impl1);
            try
            {
                socket.Open();
                bool r
                    = client.InputEvent(type);
            }
            catch (Exception x)
            {
                Console.WriteLine(x.StackTrace);
            }
            finally
            {
                transport.Close();
            }
        }
Ejemplo n.º 3
0
        static void Execute(int port)
        {
            try
            {
                // create protocol factory, default to BinaryProtocol
                TProtocolFactory  ProtocolFactory  = new TBinaryProtocol.Factory(true, true);
                TServerTransport  servertrans      = new TServerSocket(port, 0, false);
                TTransportFactory TransportFactory = new TFramedTransport.Factory();

                BenchmarkService.Iface benchHandler   = new BenchmarkServiceImpl();
                TProcessor             benchProcessor = new BenchmarkService.Processor(benchHandler);

                Aggr.Iface aggrHandler   = new AggrServiceImpl();
                TProcessor aggrProcessor = new Aggr.Processor(aggrHandler);

                TMultiplexedProcessor multiplex = new TMultiplexedProcessor();
                multiplex.RegisterProcessor(Constants.NAME_BENCHMARKSERVICE, benchProcessor);
                multiplex.RegisterProcessor(Constants.NAME_AGGR, aggrProcessor);

                TServer ServerEngine = new TSimpleServer(multiplex, servertrans, TransportFactory, ProtocolFactory);

                Console.WriteLine("Starting the server ...");
                ServerEngine.Serve();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
Ejemplo n.º 4
0
        /**
         * 获取手机屏幕分辨率
         **/
        public static String sendPoint(Int32 port, Int32 x, Int32 y)
        {
            TSocket socket = new TSocket("127.0.0.1", port, 1000);

            TFramedTransport.Factory factory = new
                                               TFramedTransport.Factory();
            TTransport transport = factory.GetTransport(socket);

            TBinaryProtocol.Factory factory1 =
                new TBinaryProtocol.Factory();
            TProtocol            protocol = factory1.GetProtocol(transport);
            TMultiplexedProtocol impl1
                = new TMultiplexedProtocol(protocol, "ScreenService");

            ScreenService.Client client =
                new ScreenService.Client(impl1);
            try
            {
                socket.Open();
                return(client.sendPoint(x, y));
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
            }
            finally
            {
                transport.Close();
            }
            return("");
        }
Ejemplo n.º 5
0
        public static string RemoveProcess(Int32 port, String lineNumber)
        {
            TSocket socket = new TSocket("127.0.0.1", port, 3000);

            TFramedTransport.Factory factory = new
                                               TFramedTransport.Factory();
            TTransport transport = factory.GetTransport(socket);

            TBinaryProtocol.Factory factory1 =
                new TBinaryProtocol.Factory();
            TProtocol            protocol = factory1.GetProtocol(transport);
            TMultiplexedProtocol impl1
                = new TMultiplexedProtocol(protocol, "NodeService");

            NodeService.Client client =
                new NodeService.Client(impl1);
            try
            {
                socket.Open();
                return(client.RemoveProcess(lineNumber));
            }
            catch (Exception x)
            {
                Console.WriteLine(x.StackTrace);
            }
            finally
            {
                transport.Close();
            }
            return("");
        }
Ejemplo n.º 6
0
        static internal void Run(int port)
        {
            // check whether the port is free
            TServerTransport serverTransport = new TServerSocket(port);

            serverTransport.Listen();
            serverTransport.Close();
            serverTransport = new TServerSocket(port);

            // one processor to rule them all
            var multiplexProcessor = new TMultiplexedProcessor();

            // create protocol factory, default to "framed binary"
            TProtocolFactory  protocolFactory  = new TBinaryProtocol.Factory(true, true);
            TTransportFactory transportFactory = new TFramedTransport.Factory();

            // create handler/processor for the public service
            Handler handler = new Handler();

            Pizzeria.Processor pizzeria = new Pizzeria.Processor(handler);
            multiplexProcessor.RegisterProcessor(typeof(Pizzeria).Name, pizzeria);

            // create handler/processor for the internal service
            // handler = same
            PizzeriaCallback.Processor callback = new PizzeriaCallback.Processor(handler);
            multiplexProcessor.RegisterProcessor(typeof(PizzeriaCallback).Name, callback);

            // create handler/processor for the diagnostics service
            // handler = same
            Diagnostics.Diagnostics.Processor diagnose = new Diagnostics.Diagnostics.Processor(handler);
            multiplexProcessor.RegisterProcessor(typeof(Diagnostics.Diagnostics).Name, diagnose);

            // more processors as needed ...

            // complete internal setup
            Console.Title = Environment.MachineName + "-" + port.ToString();

            ReadinessHttpServer.Start(PizzaConfig.ReadinessPorts.Pizzeria);
            try
            {
                // return the server instance
                //var server = new TThreadPoolServer(multiplexProcessor, serverTransport, transportFactory, protocolFactory);
                var server = new TThreadedServer(multiplexProcessor, serverTransport, transportFactory, protocolFactory);
                //var server = new TSimpleServer(multiplexProcessor, serverTransport, transportFactory, protocolFactory);

                ReadinessHttpServer.Status = Readiness.AliveAndReady;
                server.Serve();
            }
            finally
            {
                ReadinessHttpServer.Stop();
            }
        }
Ejemplo n.º 7
0
        public static void StartServer()
        {
            SearchServiceHandler handler = new SearchServiceHandler();

            SearchService.Processor processor = new SearchService.Processor(handler);

            TMultiplexedProcessor multiProcessor = new TMultiplexedProcessor();

            multiProcessor.RegisterProcessor("search_service", processor);

            TServerTransport serverTransport = new TServerSocket(9090);
            var     a      = new TFramedTransport.Factory();
            TServer server = new TThreadPoolServer(multiProcessor, serverTransport, new TFramedTransport.Factory(), new TBinaryProtocol.Factory());

            serverTask = Task.Factory.StartNew(server.Serve);
        }
Ejemplo n.º 8
0
        static void Main()
        {
            //TServerSocket serverTransport = new TServerSocket(new System.Net.Sockets.TcpListener(IPAddress.Parse("192.168.43.1"), 25000));
            TServerSocket serverTransport = new TServerSocket(25001, 0, false);
            //异步IO,需要使用TFramedTransport,它将分块缓存读取
            var tfactory = new TFramedTransport.Factory();
            //使用高密度二进制协议
            var pfactory = new TCompactProtocol.Factory();
            TMultiplexedProcessor processor = new TMultiplexedProcessor();

            com.msunsoft.service.calculator.Processor calcProcessor = new com.msunsoft.service.calculator.Processor(new Server());
            processor.RegisterProcessor("test-server-rpc$com.msunsoft.service.calculator$2.0", calcProcessor);
            TThreadedServer server = new TThreadedServer(processor, serverTransport, tfactory, pfactory);

            Console.WriteLine("Starting server on port 25001 ...");
            server.Serve();
        }
Ejemplo n.º 9
0
        private static async Task RunSelectedConfigurationAsync(Transport transport, Buffering buffering, Protocol protocol, CancellationToken cancellationToken)
        {
            var handler = new CalculatorAsyncHandler();

            TServerTransport serverTransport = null;

            switch (transport)
            {
            case Transport.Tcp:
                serverTransport = new TServerSocketTransport(9090);
                break;

            case Transport.NamedPipe:
                serverTransport = new TNamedPipeServerTransport(".test");
                break;

            case Transport.TcpTls:
                serverTransport = new TTlsServerSocketTransport(9090, GetCertificate(), ClientCertValidator, LocalCertificateSelectionCallback);
                break;
            }

            TTransportFactory inputTransportFactory  = null;
            TTransportFactory outputTransportFactory = null;

            switch (buffering)
            {
            case Buffering.Buffered:
                inputTransportFactory  = new TBufferedTransport.Factory();
                outputTransportFactory = new TBufferedTransport.Factory();
                break;

            case Buffering.Framed:
                inputTransportFactory  = new TFramedTransport.Factory();
                outputTransportFactory = new TFramedTransport.Factory();
                break;

            default:     // layered transport(s) are optional
                Debug.Assert(buffering == Buffering.None, "unhandled case");
                break;
            }

            TProtocolFactory inputProtocolFactory  = null;
            TProtocolFactory outputProtocolFactory = null;
            ITAsyncProcessor processor             = null;

            switch (protocol)
            {
            case Protocol.Binary:
                inputProtocolFactory  = new TBinaryProtocol.Factory();
                outputProtocolFactory = new TBinaryProtocol.Factory();
                processor             = new Calculator.AsyncProcessor(handler);
                break;

            case Protocol.Compact:
                inputProtocolFactory  = new TCompactProtocol.Factory();
                outputProtocolFactory = new TCompactProtocol.Factory();
                processor             = new Calculator.AsyncProcessor(handler);
                break;

            case Protocol.Json:
                inputProtocolFactory  = new TJsonProtocol.Factory();
                outputProtocolFactory = new TJsonProtocol.Factory();
                processor             = new Calculator.AsyncProcessor(handler);
                break;

            case Protocol.Multiplexed:
                inputProtocolFactory  = new TBinaryProtocol.Factory();
                outputProtocolFactory = new TBinaryProtocol.Factory();

                var calcHandler   = new CalculatorAsyncHandler();
                var calcProcessor = new Calculator.AsyncProcessor(calcHandler);

                var sharedServiceHandler   = new SharedServiceAsyncHandler();
                var sharedServiceProcessor = new SharedService.AsyncProcessor(sharedServiceHandler);

                var multiplexedProcessor = new TMultiplexedProcessor();
                multiplexedProcessor.RegisterProcessor(nameof(Calculator), calcProcessor);
                multiplexedProcessor.RegisterProcessor(nameof(SharedService), sharedServiceProcessor);

                processor = multiplexedProcessor;
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(protocol), protocol, null);
            }


            try
            {
                Logger.LogInformation(
                    $"Selected TAsyncServer with {serverTransport} transport, {processor} processor and {inputProtocolFactory} protocol factories");

                var loggerFactory = ServiceCollection.BuildServiceProvider().GetService <ILoggerFactory>();

                var server = new TSimpleAsyncServer(
                    itProcessorFactory: new TSingletonProcessorFactory(processor),
                    serverTransport: serverTransport,
                    inputTransportFactory: inputTransportFactory,
                    outputTransportFactory: outputTransportFactory,
                    inputProtocolFactory: inputProtocolFactory,
                    outputProtocolFactory: outputProtocolFactory,
                    logger: loggerFactory.CreateLogger <TSimpleAsyncServer>());

                Logger.LogInformation("Starting the server...");

                await server.ServeAsync(cancellationToken);
            }
            catch (Exception x)
            {
                Logger.LogInformation(x.ToString());
            }
        }
Ejemplo n.º 10
0
        public static bool Execute(string[] args)
        {
            try
            {
                bool                 useBufferedSockets = false, useFramed = false, useEncryption = false, compact = false, json = false;
                ServerType           serverType           = ServerType.TSimpleServer;
                ProcessorFactoryType processorFactoryType = ProcessorFactoryType.TSingletonProcessorFactory;
                int    port = 9090;
                string pipe = null;
                for (int i = 0; i < args.Length; i++)
                {
                    if (args[i] == "-pipe")  // -pipe name
                    {
                        pipe = args[++i];
                    }
                    else if (args[i].Contains("--port="))
                    {
                        port = int.Parse(args[i].Substring(args[i].IndexOf("=") + 1));
                    }
                    else if (args[i] == "-b" || args[i] == "--buffered" || args[i] == "--transport=buffered")
                    {
                        useBufferedSockets = true;
                    }
                    else if (args[i] == "-f" || args[i] == "--framed" || args[i] == "--transport=framed")
                    {
                        useFramed = true;
                    }
                    else if (args[i] == "--compact" || args[i] == "--protocol=compact")
                    {
                        compact = true;
                    }
                    else if (args[i] == "--json" || args[i] == "--protocol=json")
                    {
                        json = true;
                    }
                    else if (args[i] == "--threaded" || args[i] == "--server-type=threaded")
                    {
                        serverType = ServerType.TThreadedServer;
                    }
                    else if (args[i] == "--threadpool" || args[i] == "--server-type=threadpool")
                    {
                        serverType = ServerType.TThreadPoolServer;
                    }
                    else if (args[i] == "--prototype" || args[i] == "--processor=prototype")
                    {
                        processorFactoryType = ProcessorFactoryType.TPrototypeProcessorFactory;
                    }
                    else if (args[i] == "--ssl")
                    {
                        useEncryption = true;
                    }
                }

                // Transport
                TServerTransport trans;
                if (pipe != null)
                {
                    trans = new TNamedPipeServerTransport(pipe);
                }
                else
                {
                    if (useEncryption)
                    {
                        string certPath = "../keys/server.p12";
                        trans = new TTLSServerSocket(port, 0, useBufferedSockets, new X509Certificate2(certPath, "thrift"), null, null, SslProtocols.Tls);
                    }
                    else
                    {
                        trans = new TServerSocket(port, 0, useBufferedSockets);
                    }
                }

                TProtocolFactory proto;
                if (compact)
                {
                    proto = new TCompactProtocol.Factory();
                }
                else if (json)
                {
                    proto = new TJSONProtocol.Factory();
                }
                else
                {
                    proto = new TBinaryProtocol.Factory();
                }

                TProcessorFactory processorFactory;
                if (processorFactoryType == ProcessorFactoryType.TPrototypeProcessorFactory)
                {
                    processorFactory = new TPrototypeProcessorFactory <ThriftTest.Processor, TestHandler>();
                }
                else
                {
                    // Processor
                    TestHandler          testHandler   = new TestHandler();
                    ThriftTest.Processor testProcessor = new ThriftTest.Processor(testHandler);
                    processorFactory = new TSingletonProcessorFactory(testProcessor);
                }

                TTransportFactory transFactory;
                if (useFramed)
                {
                    transFactory = new TFramedTransport.Factory();
                }
                else
                {
                    transFactory = new TTransportFactory();
                }

                TServer serverEngine;
                switch (serverType)
                {
                case ServerType.TThreadPoolServer:
                    serverEngine = new TThreadPoolServer(processorFactory, trans, transFactory, proto);
                    break;

                case ServerType.TThreadedServer:
                    serverEngine = new TThreadedServer(processorFactory, trans, transFactory, proto);
                    break;

                default:
                    serverEngine = new TSimpleServer(processorFactory, trans, transFactory, proto);
                    break;
                }

                //Server event handler
                TradeServerEventHandler serverEvents = new TradeServerEventHandler();
                serverEngine.setEventHandler(serverEvents);

                // Run it
                string where = (pipe != null ? "on pipe " + pipe : "on port " + port);
                Console.WriteLine("Starting the " + serverType.ToString() + " " + where +
                                  (processorFactoryType == ProcessorFactoryType.TPrototypeProcessorFactory ? " with processor prototype factory " : "") +
                                  (useBufferedSockets ? " with buffered socket" : "") +
                                  (useFramed ? " with framed transport" : "") +
                                  (useEncryption ? " with encryption" : "") +
                                  (compact ? " with compact protocol" : "") +
                                  (json ? " with json protocol" : "") +
                                  "...");
                serverEngine.Serve();
            }
            catch (Exception x)
            {
                Console.Error.Write(x);
                return(false);
            }
            Console.WriteLine("done.");
            return(true);
        }