Thrift: C++ peek() method and TException not Exception


git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664876 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/cpp/src/transport/TSocket.cpp b/lib/cpp/src/transport/TSocket.cpp
index de3bea7..8b9048c 100644
--- a/lib/cpp/src/transport/TSocket.cpp
+++ b/lib/cpp/src/transport/TSocket.cpp
@@ -43,6 +43,8 @@
   lingerOn_(1),
   lingerVal_(0),
   noDelay_(1) {
+  recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
+  recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
 }
 
 TSocket::TSocket(int socket) :
@@ -55,6 +57,8 @@
   lingerOn_(1),
   lingerVal_(0),
   noDelay_(1) {
+  recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
+  recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
 }
   
 TSocket::~TSocket() {
@@ -65,6 +69,20 @@
   return (socket_ > 0); 
 }
 
+bool TSocket::peek() {
+  if (!isOpen()) {
+    return false;
+  }
+  uint8_t buf;
+  int r = recv(socket_, &buf, 1, MSG_PEEK);
+  if (r == -1) {
+    perror("TSocket::peek()");
+    close();
+    throw TTransportException(TTX_UNKNOWN, "recv() ERROR:" + errno);
+  }
+  return (r > 0);
+}
+
 void TSocket::open() {
   // Create socket
   socket_ = socket(AF_INET, SOCK_STREAM, 0);
@@ -322,12 +340,14 @@
 
 void TSocket::setRecvTimeout(int ms) {
   recvTimeout_ = ms;
+  recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
+  recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
   if (socket_ <= 0) {
     return;
   }
 
-  struct timeval r = {(int)(recvTimeout_/1000),
-                      (int)((recvTimeout_%1000)*1000)};
+  // Copy because select may modify
+  struct timeval r = recvTimeval_;
   int ret = setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO, &r, sizeof(r));
   if (ret == -1) {
     perror("TSocket::setRecvTimeout()");