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
SampleEventHandler());
server->setServerEventHandler(EventHandler);
server->serve();
}
//============================================================================
//Sample event callbacks triggered by the server when something
interesting
//happens with the client.
//Create an overload of TServerEventHandler specific to your needs and
//implement the necessary methods.
//
class SampleEventHandler : public server::TServerEventHandler {
public:
SampleEventHandler() :
NumClients_(0)
{}
//Called before the server begins -
//virtual void preServe() {}
//Called when a new client has connected and is about to being
processing.
//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>
input,
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
error.
virtual void deleteContext(void* serverContext,
boost::shared_ptr<protocol::TProtocol>input,
boost::shared_ptr<protocol::TProtocol>output)
{
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) {}
protected:
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.
-Peace
------------------------------
*From:* "Christian, Daniel" <[email protected]>
*To:* "[email protected]" <[email protected]>
*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?
Thanks,
-Dan