THRIFT-2034: Give developers' C++ code direct access to socket FDs on
server side
Client: cpp
Patch: Ben Craig
diff --git a/lib/cpp/src/thrift/transport/TServerSocket.cpp b/lib/cpp/src/thrift/transport/TServerSocket.cpp
index 108be27..b686c6c 100755
--- a/lib/cpp/src/thrift/transport/TServerSocket.cpp
+++ b/lib/cpp/src/thrift/transport/TServerSocket.cpp
@@ -383,6 +383,8 @@
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 @@
}
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 c30d3e3..56ec2b5 100644
--- a/lib/cpp/src/thrift/transport/TServerSocket.h
+++ b/lib/cpp/src/thrift/transport/TServerSocket.h
@@ -22,6 +22,7 @@
#include <thrift/transport/TServerTransport.h>
#include <thrift/transport/PlatformSocket.h>
+#include <thrift/cxxfunctional.h>
#include <boost/shared_ptr.hpp>
namespace apache { namespace thrift { namespace transport {
@@ -35,6 +36,8 @@
*/
class TServerSocket : public TServerTransport {
public:
+ typedef apache::thrift::stdcxx::function<void(THRIFT_SOCKET fd)> socket_func_t;
+
const static int DEFAULT_BACKLOG = 1024;
TServerSocket(int port);
@@ -57,6 +60,17 @@
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 @@
THRIFT_SOCKET intSock1_;
THRIFT_SOCKET intSock2_;
+
+ socket_func_t listenCallback_;
+ socket_func_t acceptCallback_;
};
}}} // apache::thrift::transport