You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by be...@apache.org on 2013/10/15 03:34:51 UTC

git commit: THRIFT-2034: Give developers' C++ code direct access to socket FDs on server side Client: cpp Patch: Ben Craig

Updated Branches:
  refs/heads/master 7f10de7ee -> 0d671c091


THRIFT-2034: Give developers' C++ code direct access to socket FDs on
server side
Client: cpp
Patch: Ben Craig


Project: http://git-wip-us.apache.org/repos/asf/thrift/repo
Commit: http://git-wip-us.apache.org/repos/asf/thrift/commit/0d671c09
Tree: http://git-wip-us.apache.org/repos/asf/thrift/tree/0d671c09
Diff: http://git-wip-us.apache.org/repos/asf/thrift/diff/0d671c09

Branch: refs/heads/master
Commit: 0d671c091cf425d7001d3eafef558e5f39e8194d
Parents: 7f10de7
Author: Ben Craig <be...@apache.org>
Authored: Mon Oct 14 20:32:29 2013 -0500
Committer: Ben Craig <be...@apache.org>
Committed: Mon Oct 14 20:32:29 2013 -0500

----------------------------------------------------------------------
 lib/cpp/src/thrift/transport/TServerSocket.cpp |  4 ++++
 lib/cpp/src/thrift/transport/TServerSocket.h   | 17 +++++++++++++++++
 2 files changed, 21 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/thrift/blob/0d671c09/lib/cpp/src/thrift/transport/TServerSocket.cpp
----------------------------------------------------------------------
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 @@ 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<TTransport> TServerSocket::acceptImpl() {
   }
   client->setCachedAddress((sockaddr*) &clientAddress, size);
 
+  if(acceptCallback_) acceptCallback_(clientSocket);
+
   return client;
 }
 

http://git-wip-us.apache.org/repos/asf/thrift/blob/0d671c09/lib/cpp/src/thrift/transport/TServerSocket.h
----------------------------------------------------------------------
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 TSocket;
  */
 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 @@ 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