如何在thrift中使用C ++获取客户端IP地址

时间:2014-12-30 07:32:51

标签: c++ thrift

我目前正在用C ++实现基于thrift的(0.4.0)服务并遇到一个问题:

有没有办法从服务方法实现中获取客户端的IP地址?我正在使用TNonblockingServer。

提前致谢!

3 个答案:

答案 0 :(得分:1)

在TNonblockingServer中,当调用TProcessor :: process()时,TProtocol.transport是TMemoryBuffer,因此无法获取客户端IP地址。

但我们可以扩展类TServerEventHandler,当客户端要调用处理器时,会调用方法TServerEventHandler :: processContext()。

static boost::thread_specific_ptr<std::string> thrift_client_ip; // thread specific
class MyServerEventHandler : public TServerEventHandler
{
    virtual void processContext(void* serverContext, boost::shared_ptr<TTransport> transport)
    {
        TSocket *sock = static_cast<TSocket *>(transport.get());

        if (sock)
        {
            //thrift_client_ip.reset(new string(sock->getPeerAddress())); // 0.9.2, reused TNonblockingServer::TConnection return dirty address, see https://issues.apache.org/jira/browse/THRIFT-3270
            sock->getCachedAddress(); // use this api instead
        }
    }
};

// create nonblocking server
TNonblockingServer server(processor, protocolFactory, port, threadManager);
boost::shared_ptr<MyServerEventHandler> eventHandler(new MyServerEventHandler());
server.setServerEventHandler(eventHandler);

答案 1 :(得分:0)

Ticket THRIFT-1053描述了类似的Java请求。解决方案基本上是允许访问内部(端点)传输并从中检索数据。如果没有经过实际测试,为C ++构建类似的解决方案应该很容易。由于您使用的是Thrift 0.4.0,我强烈建议您先查看当前的主干(0.9.3)。 TBufferedTransportTFramedTransportTShortReadTransport已实施

boost::shared_ptr<TTransport> getUnderlyingTransport();

所以上面提到的补丁可能根本没有必要。

TProcessor - 派生类在调用process()时获取两个传输。如果您覆盖该方法,您应该能够管理对您感兴趣的数据的访问权限:

/**
 * A processor is a generic object that acts upon two streams of data, one
 * an input and the other an output. The definition of this object is loose,
 * though the typical case is for some sort of server that either generates
 * responses to an input stream or forwards data from one pipe onto another.
 *
 */
class TProcessor {
public:
  // more code

  virtual bool process(boost::shared_ptr<protocol::TProtocol> in,
                       boost::shared_ptr<protocol::TProtocol> out,
                       void* connectionContext) = 0;

  // more code

答案 2 :(得分:0)

#ifndef NONBLOCK_SERVER_EVENT_HANDLER_H
#define NONBLOCK_SERVER_EVENT_HANDLER_H

#include <thrift/transport/TSocket.h>
#include <thrift/server/TServer.h>

namespace apache{
namespace thrift{
namespace server{

class ServerEventHandler:public TServerEventHandler{
        void* createContext(boost::shared_ptr<TProtocol> input, boost::shared_ptr<TProtocol> output){
    (void)input;
    (void)output;
    return (void*)(new char[32]);//TODO
  }

  virtual void deleteContext(void* serverContext, 
                                boost::shared_ptr<TProtocol>input,
                                boost::shared_ptr<TProtocol>output) {
                delete [](char*)serverContext;
  }

  virtual void processContext(void *serverContext, boost::shared_ptr<TTransport> transport){
    TSocket *tsocket = static_cast<TSocket*>(transport.get());
    if(socket){
                        struct sockaddr* addrPtr;
                        socklen_t addrLen;
      addrPtr = tsocket->getCachedAddress(&addrLen);
                  if (addrPtr){
                                        getnameinfo((sockaddr*)addrPtr,addrLen,(char*)serverContext,32,NULL,0,0) ;
                        }
    }
  }
};

}
}
}

#endif

boost::shared_ptr<ServerEventHandler> serverEventHandler(new ServerEventHandler()
server.setServerEventHandler(serverEventHandler);