Example #1
0
        public void ProcessRequest(Stream input, Stream output)
        {
            TTransport transport = new TStreamTransport(input, output);

            TProtocol inputProtocol  = null;
            TProtocol outputProtocol = null;

            try
            {
                inputProtocol  = inputProtocolFactory.GetProtocol(transport);
                outputProtocol = outputProtocolFactory.GetProtocol(transport);

                while (processor.Process(inputProtocol, outputProtocol))
                {
                }
            }
            catch (TTransportException)
            {
                // Client died, just move on
            }
            catch (TApplicationException tx)
            {
                Console.Error.Write(tx);
            }
            catch (Exception x)
            {
                Console.Error.Write(x);
            }

            transport.Close();
        }
Example #2
0
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType     = "application/x-thrift";
            context.Response.ContentEncoding = System.Text.Encoding.UTF8;

            TTransport transport = new TStreamTransport(context.Request.InputStream, context.Response.OutputStream);

            TProtocol inputProtocol  = null;
            TProtocol outputProtocol = null;

            try
            {
                inputProtocol  = inputProtocolFactory.GetProtocol(transport);
                outputProtocol = outputProtocolFactory.GetProtocol(transport);

                while (processor.Process(inputProtocol, outputProtocol))
                {
                }
            }
            catch (TTransportException)
            {
                // Client died, just move on
            }
            catch (TApplicationException tx)
            {
                Console.Error.Write(tx);
            }
            catch (Exception x)
            {
                Console.Error.Write(x);
            }

            transport.Close();
        }
Example #3
0
        /**
         * 该方法,客户端每调用一次,就会触发一次
         */
        public bool Process(TProtocol iprot, TProtocol oprot)
        {
            TSocket socket = (TSocket)iprot.Transport;

            IPEndPoint ip = (IPEndPoint)socket.TcpClient.Client.RemoteEndPoint;

            if (!IpLimit.AllowConfig(ip.Address.ToString()))
            {
                ThriftLog.Info($"{ip.Address.ToString()} 连接被限制!");
                return(false);
            }

            return(_processor.Process(iprot, oprot));
        }
Example #4
0
        public void ServeOne()
        {
            debug("Server_ServeOne");
            Byte[]       msg       = _socket.Recv();
            MemoryStream istream   = new MemoryStream(msg);
            MemoryStream ostream   = new MemoryStream();
            TProtocol    tProtocol = new TBinaryProtocol(new TStreamTransport(istream, ostream));

            _processor.Process(tProtocol, tProtocol);

            if (ostream.Length != 0)
            {
                byte[] newBuf = new byte[ostream.Length];
                Array.Copy(ostream.GetBuffer(), newBuf, ostream.Length);
                debug(string.Format("Server_ServeOne sending {0}b", ostream.Length));
                _socket.Send(newBuf);
            }
        }
Example #5
0
        public void ProcessRequest(Stream input, Stream output)
        {
            TTransport transport = new TStreamTransport(input, output);

            try
            {
                var inputProtocol  = inputProtocolFactory.GetProtocol(transport);
                var outputProtocol = outputProtocolFactory.GetProtocol(transport);

                while (processor.Process(inputProtocol, outputProtocol))
                {
                }
            }
            catch (TTransportException)
            {
                // Client died, just move on
            }
            finally
            {
                transport.Close();
            }
        }
Example #6
0
        public override void Serve()
        {
            try
            {
                serverTransport.Listen();
            }
            catch (TTransportException ttx)
            {
                logDelegate(ttx.ToString());
                return;
            }

            //Fire the preServe server event when server is up but before any client connections
            if (serverEventHandler != null)
            {
                serverEventHandler.preServe();
            }

            while (!stop)
            {
                TProcessor processor         = null;
                TTransport client            = null;
                TTransport inputTransport    = null;
                TTransport outputTransport   = null;
                TProtocol  inputProtocol     = null;
                TProtocol  outputProtocol    = null;
                Object     connectionContext = null;
                try
                {
                    using (client = serverTransport.Accept())
                    {
                        processor = processorFactory.GetProcessor(client);
                        if (client != null)
                        {
                            using (inputTransport = inputTransportFactory.GetTransport(client))
                            {
                                using (outputTransport = outputTransportFactory.GetTransport(client))
                                {
                                    inputProtocol  = inputProtocolFactory.GetProtocol(inputTransport);
                                    outputProtocol = outputProtocolFactory.GetProtocol(outputTransport);

                                    //Recover event handler (if any) and fire createContext server event when a client connects
                                    if (serverEventHandler != null)
                                    {
                                        connectionContext = serverEventHandler.createContext(inputProtocol, outputProtocol);
                                    }

                                    //Process client requests until client disconnects
                                    while (!stop)
                                    {
                                        if (!inputTransport.Peek())
                                        {
                                            break;
                                        }

                                        //Fire processContext server event
                                        //N.B. This is the pattern implemented in C++ and the event fires provisionally.
                                        //That is to say it may be many minutes between the event firing and the client request
                                        //actually arriving or the client may hang up without ever makeing a request.
                                        if (serverEventHandler != null)
                                        {
                                            serverEventHandler.processContext(connectionContext, inputTransport);
                                        }
                                        //Process client request (blocks until transport is readable)
                                        if (!processor.Process(inputProtocol, outputProtocol))
                                        {
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                catch (TTransportException ttx)
                {
                    if (!stop || ttx.Type != TTransportException.ExceptionType.Interrupted)
                    {
                        logDelegate(ttx.ToString());
                    }
                }
                catch (Exception x)
                {
                    //Unexpected
                    logDelegate(x.ToString());
                }

                //Fire deleteContext server event after client disconnects
                if (serverEventHandler != null)
                {
                    serverEventHandler.deleteContext(connectionContext, inputProtocol, outputProtocol);
                }
            }
        }
Example #7
0
        /// <summary>
        /// Loops on processing a client forever
        /// threadContext will be a TTransport instance
        /// </summary>
        /// <param name="threadContext"></param>
        private void Execute(Object threadContext)
        {
            TTransport client            = (TTransport)threadContext;
            TProcessor processor         = processorFactory.GetProcessor(client, this);
            TTransport inputTransport    = null;
            TTransport outputTransport   = null;
            TProtocol  inputProtocol     = null;
            TProtocol  outputProtocol    = null;
            Object     connectionContext = null;

            try
            {
                inputTransport  = inputTransportFactory.GetTransport(client);
                outputTransport = outputTransportFactory.GetTransport(client);
                inputProtocol   = inputProtocolFactory.GetProtocol(inputTransport);
                outputProtocol  = outputProtocolFactory.GetProtocol(outputTransport);

                //Recover event handler (if any) and fire createContext server event when a client connects
                if (serverEventHandler != null)
                {
                    connectionContext = serverEventHandler.createContext(inputProtocol, outputProtocol);
                }

                //Process client requests until client disconnects
                while (!stop)
                {
                    if (!inputTransport.Peek())
                    {
                        break;
                    }

                    //Fire processContext server event
                    //N.B. This is the pattern implemented in C++ and the event fires provisionally.
                    //That is to say it may be many minutes between the event firing and the client request
                    //actually arriving or the client may hang up without ever makeing a request.
                    if (serverEventHandler != null)
                    {
                        serverEventHandler.processContext(connectionContext, inputTransport);
                    }
                    //Process client request (blocks until transport is readable)
                    if (!processor.Process(inputProtocol, outputProtocol))
                    {
                        break;
                    }
                }
            }
            catch (TTransportException)
            {
                //Usually a client disconnect, expected
            }
            catch (Exception x)
            {
                //Unexpected
                logDelegate("Error: " + x);
            }

            //Fire deleteContext server event after client disconnects
            if (serverEventHandler != null)
            {
                serverEventHandler.deleteContext(connectionContext, inputProtocol, outputProtocol);
            }

            //Close transports
            if (inputTransport != null)
            {
                inputTransport.Close();
            }
            if (outputTransport != null)
            {
                outputTransport.Close();
            }
        }
Example #8
0
 /**
  * Adapt a {@link TProcessor} to a standard Thrift {@link NiftyProcessor}. Nifty uses this
  * internally to adapt the processors generated by the standard Thrift code generator into
  * instances of {@link NiftyProcessor} usable by {@link com.facebook.nifty.core.NiftyDispatcher}
  */
 public static INiftyProcessor ProcessorFromTProcessor(TProcessor standardThriftProcessor)
 {
     CheckProcessMethodSignature();
     return(new DelegateNiftyProcessor((pIn, pOut, rContext) => standardThriftProcessor.Process(pIn, pOut)));
 }