Example #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);
        }
Example #2
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);
        }
Example #3
0
        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);
                }


                // Transport
                TServerTransport trans;
                if (param.pipe != null)
                {
                    trans = new TNamedPipeServerTransport(param.pipe);
                }
//                else if (param.useFramed)
//                {
//                    trans = new TServerFramedTransport(param.port);
//                }
                else
                {
                    if (param.useEncryption)
                    {
                        var cert = GetServerCert();

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

                        trans = new TTlsServerSocketTransport(param.port, param.useBufferedSockets, param.useFramed, cert,
                                                              (sender, certificate, chain, errors) => true,
                                                              null, SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12);
                    }
                    else
                    {
                        trans = new TServerSocketTransport(param.port, 0, param.useBufferedSockets, param.useFramed);
                    }
                }

                ITProtocolFactory proto;
                if (param.compact)
                {
                    proto = new TCompactProtocol.Factory();
                }
                else if (param.json)
                {
                    proto = new TJsonProtocol.Factory();
                }
                else
                {
                    proto = new TBinaryProtocol.Factory();
                }

                ITProcessorFactory processorFactory;

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

                TTransportFactory transFactory = new TTransportFactory();

                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.useBufferedSockets ? " with buffered socket" : "") +
                                  (param.useFramed ? " with framed transport" : "") +
                                  (param.useEncryption ? " with encryption" : "") +
                                  (param.compact ? " with compact protocol" : "") +
                                  (param.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);
        }