thrift-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Peace <>
Subject Re: Detecting socket close
Date Fri, 10 Feb 2012 00:12:50 GMT
Yes Thrift C++ has a transport-agnostic way of detecting client
connect/disconnect events.  I recently needed to do this and found the
TServerEventHandler class in TServer.h.  Documentation on it is thin so
here's sample code that will hopefully help.

    //Code snippet where the server is instantiated and started. This may
or may not be syntactically correct.
    //The event handler class is derived from TServerEventHandler (see the
following section).
        //... setup omitted...
        boost::shared_ptr<TServer> server(new TSimpleServer(processor,
transport, tfactory, pfactory));
        boost::shared_ptr<SampleEventHandler> EventHandler(new


    //Sample event callbacks triggered by the server when something
    //happens with the client.
    //Create an overload of TServerEventHandler specific to your needs and
    //implement the necessary methods.
    class SampleEventHandler : public server::TServerEventHandler {
        SampleEventHandler() :

        //Called before the server begins -
        //virtual void preServe() {}

        //Called when a new client has connected and is about to being
        //createContext may return a user-defined context to aid in cleaning
        //up client connections upon disconnection.  This example dispenses
        //with contextual information and returns NULL.
        virtual void* createContext(boost::shared_ptr<protocol::TProtocol>
            boost::shared_ptr<protocol::TProtocol> output)
            printf("SampleEventHandler callback: Client connected (total
%d)\n", ++NumClients_);
            return NULL;

        //Called when a client has disconnected, either naturally or by
        virtual void deleteContext(void* serverContext,
            printf("SampleEventHandler callback: Client disconnected (total
%d)\n", --NumClients_);
            return (void)NULL;

        //Called when a client is about to call the processor -
        //virtual void processContext(void* serverContext,
boost::shared_ptr<TTransport> transport) {}

        uint32_t NumClients_; //Example member


I believe this feature was also added to Java. I haven't used Thrift in
that language so can't say for certain.


*From:* "Christian, Daniel" <>
*To:* "" <>
*Sent:* Thursday, February 9, 2012 4:41 PM
*Subject:* Detecting socket close

I have a thrift (0.8) threaded server in python.  It needs to clean up some
state when a connection closes.  Is there a clean way to do this?


  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message