Пример #1
0
        public HTTPServer(int port) : base(port)
        {
            CallFuncThriftEx <T> handler = new CallFuncThriftEx <T>();

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


            string serviceUrl = "http://localhost:99/";

            TProtocolFactory protocolFactory = new TJSONProtocol.Factory();
            THttpHandler     httpServer      = new THttpHandler(processor, protocolFactory);

            HttpListener httpListener = new HttpListener();

            httpListener.Prefixes.Add(serviceUrl);
            httpListener.Start();

            //Logger.log("创建ThriftServer", $"端口号:{port}");
        }
Пример #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);
        }
Пример #3
0
        } // class TestHandler

        public static bool Execute(string[] args)
        {
            try
            {
                bool   useBufferedSockets = false, useFramed = false, useEncryption = false, compact = false, json = false;
                int    port = 9090;
                string pipe = null;
                string certPath = "../../../../../keys/server.pem";
                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] == "--ssl")
                    {
                        useEncryption = true;
                    }
                    else if (args[i].StartsWith("--cert="))
                    {
                        certPath = args[i].Substring("--cert=".Length);
                    }
                }

                // Processor
                TestHandler          testHandler   = new TestHandler();
                ThriftTest.Processor testProcessor = new ThriftTest.Processor(testHandler);

                // Transport
                TServerTransport trans;
                if (pipe != null)
                {
                    trans = new TNamedPipeServerTransport(pipe);
                }
                else
                {
                    if (useEncryption)
                    {
                        trans = new TTLSServerSocket(port, 0, useBufferedSockets, new X509Certificate2(certPath));
                    }
                    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();
                }

                // Simple Server
                TServer serverEngine;
                if (useFramed)
                {
                    serverEngine = new TSimpleServer(testProcessor, trans, new TFramedTransport.Factory(), proto);
                }
                else
                {
                    serverEngine = new TSimpleServer(testProcessor, trans, new TTransportFactory(), proto);
                }

                // ThreadPool Server
                // serverEngine = new TThreadPoolServer(testProcessor, tServerSocket);

                // Threaded Server
                // serverEngine = new TThreadedServer(testProcessor, tServerSocket);

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

                testHandler.server = serverEngine;

                // Run it
                string where = (pipe != null ? "on pipe " + pipe : "on port " + port);
                Console.WriteLine("Starting the server " + where +
                                  (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);
        }
Пример #4
0
        }         // class TestHandler

        public static void Execute(string[] args)
        {
            try
            {
                bool   useBufferedSockets = false, useFramed = false, useEncryption = false, compact = false, json = false;
                int    port = 9090, i = 0;
                string pipe = null;
                if (args.Length > 0)
                {
                    i = 0;
                    if (args[i] == "-pipe")                      // -pipe name
                    {
                        pipe = args[++i];
                    }
                    else                      // default to port number (compatibility)
                    {
                        port = int.Parse(args[i]);
                    }

                    ++i;
                    if (args.Length > i)
                    {
                        if (args[i] == "raw")
                        {
                            // as default
                        }
                        else if (args[i] == "buffered")
                        {
                            useBufferedSockets = true;
                        }
                        else if (args[i] == "framed")
                        {
                            useFramed = true;
                        }
                        else if (args[i] == "ssl")
                        {
                            useEncryption = true;
                        }
                        else if (args[i] == "compact")
                        {
                            compact = true;
                        }
                        else if (args[i] == "json")
                        {
                            json = true;
                        }
                        else
                        {
                            // Fall back to the older boolean syntax
                            bool.TryParse(args[i], out useBufferedSockets);
                        }
                    }
                }

                // Processor
                TestHandler          testHandler   = new TestHandler();
                ThriftTest.Processor testProcessor = new ThriftTest.Processor(testHandler);

                // Transport
                TServerTransport trans;
                if (pipe != null)
                {
                    trans = new TNamedPipeServerTransport(pipe);
                }
                else
                {
                    if (useEncryption)
                    {
                        trans = new TTLSServerSocket(port, 0, useBufferedSockets, new X509Certificate2("../../../../../keys/server.pem"));
                    }
                    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();
                }

                // Simple Server
                TServer serverEngine;
                if (useFramed)
                {
                    serverEngine = new TSimpleServer(testProcessor, trans, new TFramedTransport.Factory(), proto);
                }
                else
                {
                    serverEngine = new TSimpleServer(testProcessor, trans, new TTransportFactory(), proto);
                }

                // ThreadPool Server
                // serverEngine = new TThreadPoolServer(testProcessor, tServerSocket);

                // Threaded Server
                // serverEngine = new TThreadedServer(testProcessor, tServerSocket);

                testHandler.server = serverEngine;

                // Run it
                string where = (pipe != null ? "on pipe " + pipe : "on port " + port);
                Console.WriteLine("Starting the server " + where +
                                  (useBufferedSockets ? " with buffered socket" : "") +
                                  (useFramed ? " with framed transport" : "") +
                                  (useEncryption ? " with encryption" : "") +
                                  (compact ? " with compact protocol" : "") +
                                  "...");
                serverEngine.Serve();
            }
            catch (Exception x)
            {
                Console.Error.Write(x);
            }
            Console.WriteLine("done.");
        }
Пример #5
0
        public static bool Execute(string[] args)
        {
            try
            {
                bool useBufferedSockets = false, useFramed = false, useEncryption = false, compact = false, json = false;
                int  port = 9090;

                string pipe     = null;
                string certPath = "";
                var    dic = Common.GetArgs(args);
                if (dic.ContainsKey("pipe"))
                {
                    pipe = dic["pipe"];
                }

                if (dic.ContainsKey("port"))
                {
                    port = int.Parse(dic["port"]);
                }

                if (dic.ContainsKey("b") || dic.ContainsKey("buffered"))
                {
                    useBufferedSockets = true;
                }

                if (dic.ContainsKey("f") || dic.ContainsKey("framed"))
                {
                    useFramed = true;
                }

                if (dic.ContainsKey("protocol"))
                {
                    compact = dic["protocol"] == "compact";
                    json    = dic["protocol"] == "json";
                }

                if (dic.ContainsKey("ssl"))
                {
                    useEncryption = true;
                }

                if (dic.ContainsKey("cert"))
                {
                    certPath = dic["cert"];
                }



                // Processor
                DemoServiceImpl          demoService   = new DemoServiceImpl();
                RPCDemoService.Processor demoProcessor = new RPCDemoService.Processor(demoService);

                // Transport
                TServerTransport trans;
                if (pipe != null)
                {
                    trans = new TNamedPipeServerTransport(pipe);
                }
                else
                {
                    if (useEncryption)
                    {
                        trans = new TTLSServerSocket(port, 0, useBufferedSockets, new X509Certificate2(certPath));
                    }
                    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();
                }

                // Simple Server
                TServer serverEngine;
                if (useFramed)
                {
                    serverEngine = new ThriftServer(demoProcessor, trans, new TFramedTransport.Factory(), proto);
                }
                else
                {
                    serverEngine = new ThriftServer(demoProcessor, trans, new TTransportFactory(), proto);
                }

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


                // Run it
                string where = (pipe != null ? "on pipe " + pipe : "on port " + port);
                Console.WriteLine("Starting the server " + where +
                                  (useBufferedSockets ? " with buffered socket" : "") +
                                  (useFramed ? " with framed transport" : "") +
                                  (useEncryption ? " with encryption" : "") +
                                  (compact ? " with compact protocol" : "") +
                                  (json ? " with json protocol" : "") +
                                  "...");

                Console.WriteLine("Thrift Service started. Press Ctrl+C to shut down.");
                (serverEngine as IServer).Start();
            }
            catch (Exception x)
            {
                Console.Error.Write(x);
                return(false);
            }
            Console.WriteLine("done.");
            return(true);
        }