Thrift obtains the Client IP address through tservereventhandler

Source: Internet
Author: User

After using thrift for so long, I have never known how to print the IP address of the sender at the receiving end. A stupid method is to add a field in struct and fill in the IP address (or host) of the sender. It is agreed that a valid IP address must be filled, but this not only increases the amount of code of the caller, the receiver also needs to determine whether a string field is a valid IP address.

I checked some information online some time ago, but it was fruitless. I reorganized these materials today and finally got it done.

General idea: Http://web.archiveorange.com/archive/v/fzey581cbguiblqh1gpv. I will repeat the following based on my ideas:

It mainly implements a class whose base class is tservereventhandler. I call it clientiphandler.

1. Define a Map <uint64, string> to store <pthread_self (), ip>.

2. Insert Map in createcontext () in clientiphandler.

3. define a global function to obtain the MAP value. This function is used by your xservicehandler (automatically generated by Thrift.

4. Before calling serve (), define shared_ptr <clientiphandler> variable A and call setservereventhandler to set eventhandler _ (server/tserver. h: 217 ).

The read and write operations of map must be locked.

Some code:

static map<uint64, std::string> thrift_client_ip;static base::lock::Mutex mutex;std::string GetThriftClientIp() {  lock::MutexLock g(&mutex);  return thrift_client_ip[pthread_self()];}。。。class ClientIPHandler : virtual public TServerEventHandler { public:  ClientIPHandler() {  }  virtual ~ClientIPHandler() {  }  virtual void preServe() {  }  virtual void* createContext(boost::shared_ptr<TProtocol> input,                              boost::shared_ptr<TProtocol> output) {    TBufferedTransport *tbuf = dynamic_cast<TBufferedTransport *>(input->getTransport().get());    TSocket *sock = dynamic_cast<TSocket *>(tbuf->getUnderlyingTransport().get());    lock::MutexLock g(&mutex);    thrift_client_ip[pthread_self()] = sock->getPeerAddress();    return NULL;  }  virtual void deleteContext(void* serverContext,                             boost::shared_ptr<TProtocol>input,                             boost::shared_ptr<TProtocol>output) {  }  virtual void processContext(void* serverContext,                              boost::shared_ptr<TTransport> transport) {  } private:};。。。  TThreadedServer server(processor,                         serverTransport,                         transportFactory,                         protocolFactory);  try {    boost::shared_ptr<ClientIPHandler> ip_handler(new ClientIPHandler());    server.setServerEventHandler(ip_handler);    LOG(INFO)<< "thrift server start, listen port:" << listen_port;    server.serve();  } catch (apache::thrift::transport::TTransportException &ex) {    LOG(FATAL) << ex.what();  }
The corresponding namespaces are required for tservereventhandler and tbufferedtransport.


So far, we can call getthrifle tclientip () in xservicehandler to obtain the client's IP address, and finally no longer worry about where the data is sent.

Thrift obtains the Client IP address through tservereventhandler

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.