From 0d671c091cf425d7001d3eafef558e5f39e8194d Mon Sep 17 00:00:00 2001 From: Ben Craig Date: Mon, 14 Oct 2013 20:32:29 -0500 Subject: [PATCH] THRIFT-2034: Give developers' C++ code direct access to socket FDs on server side Client: cpp Patch: Ben Craig --- lib/cpp/src/thrift/transport/TServerSocket.cpp | 4 ++++ lib/cpp/src/thrift/transport/TServerSocket.h | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/cpp/src/thrift/transport/TServerSocket.cpp b/lib/cpp/src/thrift/transport/TServerSocket.cpp index 108be27f..b686c6ca 100755 --- a/lib/cpp/src/thrift/transport/TServerSocket.cpp +++ b/lib/cpp/src/thrift/transport/TServerSocket.cpp @@ -383,6 +383,8 @@ void TServerSocket::listen() { THRIFT_GET_SOCKET_ERROR); } + if(listenCallback_) listenCallback_(serverSocket_); + // Call listen if (-1 == ::listen(serverSocket_, acceptBacklog_)) { int errno_copy = THRIFT_GET_SOCKET_ERROR; @@ -491,6 +493,8 @@ shared_ptr TServerSocket::acceptImpl() { } client->setCachedAddress((sockaddr*) &clientAddress, size); + if(acceptCallback_) acceptCallback_(clientSocket); + return client; } diff --git a/lib/cpp/src/thrift/transport/TServerSocket.h b/lib/cpp/src/thrift/transport/TServerSocket.h index c30d3e31..56ec2b56 100644 --- a/lib/cpp/src/thrift/transport/TServerSocket.h +++ b/lib/cpp/src/thrift/transport/TServerSocket.h @@ -22,6 +22,7 @@ #include #include +#include #include namespace apache { namespace thrift { namespace transport { @@ -35,6 +36,8 @@ class TSocket; */ class TServerSocket : public TServerTransport { public: + typedef apache::thrift::stdcxx::function socket_func_t; + const static int DEFAULT_BACKLOG = 1024; TServerSocket(int port); @@ -57,6 +60,17 @@ class TServerSocket : public TServerTransport { void setTcpSendBuffer(int tcpSendBuffer); void setTcpRecvBuffer(int tcpRecvBuffer); + // listenCallback gets called just before listen, and after all Thrift + // setsockopt calls have been made. If you have custom setsockopt + // things that need to happen on the listening socket, this is the place to do it. + void setListenCallback(const socket_func_t &listenCallback) { listenCallback_ = listenCallback; } + + // acceptCallback gets called after each accept call, on the newly created socket. + // It is called after all Thrift setsockopt calls have been made. If you have + // custom setsockopt things that need to happen on the accepted + // socket, this is the place to do it. + void setAcceptCallback(const socket_func_t &acceptCallback) { acceptCallback_ = acceptCallback; } + void listen(); void close(); @@ -83,6 +97,9 @@ class TServerSocket : public TServerTransport { THRIFT_SOCKET intSock1_; THRIFT_SOCKET intSock2_; + + socket_func_t listenCallback_; + socket_func_t acceptCallback_; }; }}} // apache::thrift::transport -- 2.17.1