David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 19 | |
Marc Slemko | e03da18 | 2006-07-21 21:32:36 +0000 | [diff] [blame] | 20 | #include <config.h> |
David Reiss | c88eb8c | 2008-06-11 01:18:54 +0000 | [diff] [blame] | 21 | #include <cstring> |
| 22 | #include <sstream> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 23 | #include <sys/socket.h> |
Bryan Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 24 | #include <sys/un.h> |
David Reiss | 22b1886 | 2008-04-08 06:25:45 +0000 | [diff] [blame] | 25 | #include <sys/poll.h> |
Mark Slee | dd56497 | 2007-08-21 02:39:57 +0000 | [diff] [blame] | 26 | #include <sys/types.h> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 27 | #include <arpa/inet.h> |
| 28 | #include <netinet/in.h> |
| 29 | #include <netinet/tcp.h> |
| 30 | #include <netdb.h> |
| 31 | #include <unistd.h> |
| 32 | #include <errno.h> |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 33 | #include <fcntl.h> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 34 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 35 | #include "concurrency/Monitor.h" |
Marc Slemko | d42a2c2 | 2006-08-10 03:30:18 +0000 | [diff] [blame] | 36 | #include "TSocket.h" |
| 37 | #include "TTransportException.h" |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 38 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 39 | namespace apache { namespace thrift { namespace transport { |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 40 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 41 | using namespace std; |
| 42 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 43 | // Global var to track total socket sys calls |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 44 | uint32_t g_socket_syscalls = 0; |
| 45 | |
| 46 | /** |
| 47 | * TSocket implementation. |
| 48 | * |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 49 | */ |
| 50 | |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 51 | TSocket::TSocket(string host, int port) : |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 52 | host_(host), |
| 53 | port_(port), |
Bryan Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 54 | path_(""), |
| 55 | socket_(-1), |
| 56 | connTimeout_(0), |
| 57 | sendTimeout_(0), |
| 58 | recvTimeout_(0), |
| 59 | lingerOn_(1), |
| 60 | lingerVal_(0), |
| 61 | noDelay_(1), |
| 62 | maxRecvRetries_(5) { |
| 63 | recvTimeval_.tv_sec = (int)(recvTimeout_/1000); |
| 64 | recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000); |
| 65 | } |
| 66 | |
| 67 | TSocket::TSocket(string path) : |
| 68 | host_(""), |
| 69 | port_(0), |
| 70 | path_(path), |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 71 | socket_(-1), |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 72 | connTimeout_(0), |
| 73 | sendTimeout_(0), |
| 74 | recvTimeout_(0), |
| 75 | lingerOn_(1), |
| 76 | lingerVal_(0), |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 77 | noDelay_(1), |
| 78 | maxRecvRetries_(5) { |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 79 | recvTimeval_.tv_sec = (int)(recvTimeout_/1000); |
| 80 | recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 83 | TSocket::TSocket() : |
Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 84 | host_(""), |
| 85 | port_(0), |
Bryan Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 86 | path_(""), |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 87 | socket_(-1), |
Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 88 | connTimeout_(0), |
| 89 | sendTimeout_(0), |
| 90 | recvTimeout_(0), |
| 91 | lingerOn_(1), |
| 92 | lingerVal_(0), |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 93 | noDelay_(1), |
| 94 | maxRecvRetries_(5) { |
Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 95 | recvTimeval_.tv_sec = (int)(recvTimeout_/1000); |
| 96 | recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000); |
| 97 | } |
| 98 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 99 | TSocket::TSocket(int socket) : |
| 100 | host_(""), |
| 101 | port_(0), |
Bryan Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 102 | path_(""), |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 103 | socket_(socket), |
| 104 | connTimeout_(0), |
| 105 | sendTimeout_(0), |
| 106 | recvTimeout_(0), |
| 107 | lingerOn_(1), |
| 108 | lingerVal_(0), |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 109 | noDelay_(1), |
| 110 | maxRecvRetries_(5) { |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 111 | recvTimeval_.tv_sec = (int)(recvTimeout_/1000); |
| 112 | recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 113 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 114 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 115 | TSocket::~TSocket() { |
| 116 | close(); |
| 117 | } |
| 118 | |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 119 | bool TSocket::isOpen() { |
| 120 | return (socket_ >= 0); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 123 | bool TSocket::peek() { |
| 124 | if (!isOpen()) { |
| 125 | return false; |
| 126 | } |
| 127 | uint8_t buf; |
| 128 | int r = recv(socket_, &buf, 1, MSG_PEEK); |
| 129 | if (r == -1) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 130 | int errno_copy = errno; |
David Reiss | 840e752 | 2009-06-04 00:10:50 +0000 | [diff] [blame] | 131 | #if defined __FreeBSD__ || defined __MACH__ |
Kevin Clark | 022b224 | 2009-03-05 21:05:37 +0000 | [diff] [blame] | 132 | /* shigin: |
| 133 | * freebsd returns -1 and ECONNRESET if socket was closed by |
| 134 | * the other side |
| 135 | */ |
| 136 | if (errno_copy == ECONNRESET) |
| 137 | { |
| 138 | close(); |
| 139 | return false; |
| 140 | } |
| 141 | #endif |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 142 | GlobalOutput.perror("TSocket::peek() recv() " + getSocketInfo(), errno_copy); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 143 | throw TTransportException(TTransportException::UNKNOWN, "recv()", errno_copy); |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 144 | } |
| 145 | return (r > 0); |
| 146 | } |
| 147 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 148 | void TSocket::openConnection(struct addrinfo *res) { |
Mark Slee | a9848d7 | 2007-02-21 04:54:05 +0000 | [diff] [blame] | 149 | if (isOpen()) { |
Bryan Duxbury | 010f1e0 | 2010-09-02 00:56:53 +0000 | [diff] [blame] | 150 | return; |
Mark Slee | a9848d7 | 2007-02-21 04:54:05 +0000 | [diff] [blame] | 151 | } |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 152 | |
Bryan Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 153 | if (! path_.empty()) { |
| 154 | socket_ = socket(PF_UNIX, SOCK_STREAM, IPPROTO_IP); |
| 155 | } else { |
| 156 | socket_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol); |
| 157 | } |
| 158 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 159 | if (socket_ == -1) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 160 | int errno_copy = errno; |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 161 | GlobalOutput.perror("TSocket::open() socket() " + getSocketInfo(), errno_copy); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 162 | throw TTransportException(TTransportException::NOT_OPEN, "socket()", errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 163 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 164 | |
| 165 | // Send timeout |
| 166 | if (sendTimeout_ > 0) { |
| 167 | setSendTimeout(sendTimeout_); |
| 168 | } |
| 169 | |
| 170 | // Recv timeout |
| 171 | if (recvTimeout_ > 0) { |
| 172 | setRecvTimeout(recvTimeout_); |
| 173 | } |
| 174 | |
| 175 | // Linger |
| 176 | setLinger(lingerOn_, lingerVal_); |
| 177 | |
| 178 | // No delay |
| 179 | setNoDelay(noDelay_); |
| 180 | |
David Reiss | 1c20c87 | 2010-03-09 05:20:14 +0000 | [diff] [blame] | 181 | // Uses a low min RTO if asked to. |
| 182 | #ifdef TCP_LOW_MIN_RTO |
| 183 | if (getUseLowMinRto()) { |
| 184 | int one = 1; |
| 185 | setsockopt(socket_, IPPROTO_TCP, TCP_LOW_MIN_RTO, &one, sizeof(one)); |
| 186 | } |
| 187 | #endif |
| 188 | |
| 189 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 190 | // Set the socket to be non blocking for connect if a timeout exists |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 191 | int flags = fcntl(socket_, F_GETFL, 0); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 192 | if (connTimeout_ > 0) { |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 193 | if (-1 == fcntl(socket_, F_SETFL, flags | O_NONBLOCK)) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 194 | int errno_copy = errno; |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 195 | GlobalOutput.perror("TSocket::open() fcntl() " + getSocketInfo(), errno_copy); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 196 | throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 197 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 198 | } else { |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 199 | if (-1 == fcntl(socket_, F_SETFL, flags & ~O_NONBLOCK)) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 200 | int errno_copy = errno; |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 201 | GlobalOutput.perror("TSocket::open() fcntl " + getSocketInfo(), errno_copy); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 202 | throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 203 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 206 | // Connect the socket |
Bryan Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 207 | int ret; |
| 208 | if (! path_.empty()) { |
| 209 | struct sockaddr_un address; |
| 210 | socklen_t len; |
| 211 | |
| 212 | if (path_.length() > sizeof(address.sun_path)) { |
| 213 | int errno_copy = errno; |
| 214 | GlobalOutput.perror("TSocket::open() Unix Domain socket path too long", errno_copy); |
| 215 | throw TTransportException(TTransportException::NOT_OPEN, " Unix Domain socket path too long"); |
| 216 | } |
| 217 | |
| 218 | address.sun_family = AF_UNIX; |
| 219 | sprintf(address.sun_path, path_.c_str()); |
| 220 | len = sizeof(address); |
| 221 | ret = connect(socket_, (struct sockaddr *) &address, len); |
| 222 | } else { |
| 223 | ret = connect(socket_, res->ai_addr, res->ai_addrlen); |
| 224 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 225 | |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 226 | // success case |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 227 | if (ret == 0) { |
| 228 | goto done; |
| 229 | } |
| 230 | |
| 231 | if (errno != EINPROGRESS) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 232 | int errno_copy = errno; |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 233 | GlobalOutput.perror("TSocket::open() connect() " + getSocketInfo(), errno_copy); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 234 | throw TTransportException(TTransportException::NOT_OPEN, "connect() failed", errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 235 | } |
| 236 | |
David Reiss | 22b1886 | 2008-04-08 06:25:45 +0000 | [diff] [blame] | 237 | |
| 238 | struct pollfd fds[1]; |
David Reiss | c88eb8c | 2008-06-11 01:18:54 +0000 | [diff] [blame] | 239 | std::memset(fds, 0 , sizeof(fds)); |
David Reiss | 22b1886 | 2008-04-08 06:25:45 +0000 | [diff] [blame] | 240 | fds[0].fd = socket_; |
| 241 | fds[0].events = POLLOUT; |
| 242 | ret = poll(fds, 1, connTimeout_); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 243 | |
| 244 | if (ret > 0) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 245 | // Ensure the socket is connected and that there are no errors set |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 246 | int val; |
| 247 | socklen_t lon; |
| 248 | lon = sizeof(int); |
| 249 | int ret2 = getsockopt(socket_, SOL_SOCKET, SO_ERROR, (void *)&val, &lon); |
| 250 | if (ret2 == -1) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 251 | int errno_copy = errno; |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 252 | GlobalOutput.perror("TSocket::open() getsockopt() " + getSocketInfo(), errno_copy); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 253 | throw TTransportException(TTransportException::NOT_OPEN, "getsockopt()", errno_copy); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 254 | } |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 255 | // no errors on socket, go to town |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 256 | if (val == 0) { |
| 257 | goto done; |
| 258 | } |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 259 | GlobalOutput.perror("TSocket::open() error on socket (after poll) " + getSocketInfo(), val); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 260 | throw TTransportException(TTransportException::NOT_OPEN, "socket open() error", val); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 261 | } else if (ret == 0) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 262 | // socket timed out |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 263 | string errStr = "TSocket::open() timed out " + getSocketInfo(); |
| 264 | GlobalOutput(errStr.c_str()); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 265 | throw TTransportException(TTransportException::NOT_OPEN, "open() timed out"); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 266 | } else { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 267 | // error on poll() |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 268 | int errno_copy = errno; |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 269 | GlobalOutput.perror("TSocket::open() poll() " + getSocketInfo(), errno_copy); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 270 | throw TTransportException(TTransportException::NOT_OPEN, "poll() failed", errno_copy); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | done: |
| 274 | // Set socket back to normal mode (blocking) |
| 275 | fcntl(socket_, F_SETFL, flags); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 278 | void TSocket::open() { |
| 279 | if (isOpen()) { |
Bryan Duxbury | 010f1e0 | 2010-09-02 00:56:53 +0000 | [diff] [blame] | 280 | return; |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 281 | } |
Bryan Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 282 | if (! path_.empty()) { |
| 283 | unix_open(); |
| 284 | } else { |
| 285 | local_open(); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | void TSocket::unix_open(){ |
| 290 | if (! path_.empty()) { |
| 291 | // Unix Domain SOcket does not need addrinfo struct, so we pass NULL |
| 292 | openConnection(NULL); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | void TSocket::local_open(){ |
| 297 | if (isOpen()) { |
| 298 | return; |
| 299 | } |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 300 | |
| 301 | // Validate port number |
David Reiss | 450e35d | 2010-03-09 05:19:41 +0000 | [diff] [blame] | 302 | if (port_ < 0 || port_ > 0xFFFF) { |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 303 | throw TTransportException(TTransportException::NOT_OPEN, "Specified port is invalid"); |
| 304 | } |
| 305 | |
| 306 | struct addrinfo hints, *res, *res0; |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 307 | res = NULL; |
| 308 | res0 = NULL; |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 309 | int error; |
David Reiss | 450e35d | 2010-03-09 05:19:41 +0000 | [diff] [blame] | 310 | char port[sizeof("65535")]; |
David Reiss | c88eb8c | 2008-06-11 01:18:54 +0000 | [diff] [blame] | 311 | std::memset(&hints, 0, sizeof(hints)); |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 312 | hints.ai_family = PF_UNSPEC; |
| 313 | hints.ai_socktype = SOCK_STREAM; |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 314 | hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 315 | sprintf(port, "%d", port_); |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 316 | |
Mark Slee | c37b4c5 | 2007-12-05 23:03:37 +0000 | [diff] [blame] | 317 | error = getaddrinfo(host_.c_str(), port, &hints, &res0); |
| 318 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 319 | if (error) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 320 | string errStr = "TSocket::open() getaddrinfo() " + getSocketInfo() + string(gai_strerror(error)); |
| 321 | GlobalOutput(errStr.c_str()); |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 322 | close(); |
| 323 | throw TTransportException(TTransportException::NOT_OPEN, "Could not resolve host for client socket."); |
| 324 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 325 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 326 | // Cycle through all the returned addresses until one |
| 327 | // connects or push the exception up. |
| 328 | for (res = res0; res; res = res->ai_next) { |
| 329 | try { |
| 330 | openConnection(res); |
| 331 | break; |
| 332 | } catch (TTransportException& ttx) { |
| 333 | if (res->ai_next) { |
| 334 | close(); |
| 335 | } else { |
| 336 | close(); |
Mark Slee | 85287d3 | 2007-07-09 19:50:30 +0000 | [diff] [blame] | 337 | freeaddrinfo(res0); // cleanup on failure |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 338 | throw; |
| 339 | } |
| 340 | } |
| 341 | } |
Mark Slee | 85287d3 | 2007-07-09 19:50:30 +0000 | [diff] [blame] | 342 | |
| 343 | // Free address structure memory |
| 344 | freeaddrinfo(res0); |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 347 | void TSocket::close() { |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 348 | if (socket_ >= 0) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 349 | shutdown(socket_, SHUT_RDWR); |
| 350 | ::close(socket_); |
| 351 | } |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 352 | socket_ = -1; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 355 | uint32_t TSocket::read(uint8_t* buf, uint32_t len) { |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 356 | if (socket_ < 0) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 357 | throw TTransportException(TTransportException::NOT_OPEN, "Called read on non-open socket"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 358 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 359 | |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 360 | int32_t retries = 0; |
| 361 | |
| 362 | // EAGAIN can be signalled both when a timeout has occurred and when |
| 363 | // the system is out of resources (an awesome undocumented feature). |
| 364 | // The following is an approximation of the time interval under which |
| 365 | // EAGAIN is taken to indicate an out of resources error. |
| 366 | uint32_t eagainThresholdMicros = 0; |
| 367 | if (recvTimeout_) { |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 368 | // if a readTimeout is specified along with a max number of recv retries, then |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 369 | // the threshold will ensure that the read timeout is not exceeded even in the |
| 370 | // case of resource errors |
| 371 | eagainThresholdMicros = (recvTimeout_*1000)/ ((maxRecvRetries_>0) ? maxRecvRetries_ : 2); |
| 372 | } |
| 373 | |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 374 | try_again: |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 375 | // Read from the socket |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 376 | struct timeval begin; |
| 377 | gettimeofday(&begin, NULL); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 378 | int got = recv(socket_, buf, len, 0); |
Kevin Clark | 022b224 | 2009-03-05 21:05:37 +0000 | [diff] [blame] | 379 | int errno_copy = errno; //gettimeofday can change errno |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 380 | ++g_socket_syscalls; |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 381 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 382 | // Check for error on read |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 383 | if (got < 0) { |
Kevin Clark | 022b224 | 2009-03-05 21:05:37 +0000 | [diff] [blame] | 384 | if (errno_copy == EAGAIN) { |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 385 | // check if this is the lack of resources or timeout case |
David Reiss | a1a1511 | 2010-03-09 05:19:54 +0000 | [diff] [blame] | 386 | struct timeval end; |
| 387 | gettimeofday(&end, NULL); |
| 388 | uint32_t readElapsedMicros = (((end.tv_sec - begin.tv_sec) * 1000 * 1000) |
| 389 | + (((uint64_t)(end.tv_usec - begin.tv_usec)))); |
| 390 | |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 391 | if (!eagainThresholdMicros || (readElapsedMicros < eagainThresholdMicros)) { |
| 392 | if (retries++ < maxRecvRetries_) { |
| 393 | usleep(50); |
| 394 | goto try_again; |
| 395 | } else { |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 396 | throw TTransportException(TTransportException::TIMED_OUT, |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 397 | "EAGAIN (unavailable resources)"); |
| 398 | } |
| 399 | } else { |
| 400 | // infer that timeout has been hit |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 401 | throw TTransportException(TTransportException::TIMED_OUT, |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 402 | "EAGAIN (timed out)"); |
| 403 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 404 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 405 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 406 | // If interrupted, try again |
Kevin Clark | 022b224 | 2009-03-05 21:05:37 +0000 | [diff] [blame] | 407 | if (errno_copy == EINTR && retries++ < maxRecvRetries_) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 408 | goto try_again; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 409 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 410 | |
David Reiss | 840e752 | 2009-06-04 00:10:50 +0000 | [diff] [blame] | 411 | #if defined __FreeBSD__ || defined __MACH__ |
Kevin Clark | 022b224 | 2009-03-05 21:05:37 +0000 | [diff] [blame] | 412 | if (errno_copy == ECONNRESET) { |
Kevin Clark | 022b224 | 2009-03-05 21:05:37 +0000 | [diff] [blame] | 413 | /* shigin: freebsd doesn't follow POSIX semantic of recv and fails with |
| 414 | * ECONNRESET if peer performed shutdown |
| 415 | */ |
| 416 | close(); |
| 417 | return 0; |
David Reiss | 840e752 | 2009-06-04 00:10:50 +0000 | [diff] [blame] | 418 | } |
| 419 | #endif |
| 420 | |
| 421 | // Now it's not a try again case, but a real probblez |
| 422 | GlobalOutput.perror("TSocket::read() recv() " + getSocketInfo(), errno_copy); |
| 423 | |
| 424 | // If we disconnect with no linger time |
| 425 | if (errno_copy == ECONNRESET) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 426 | throw TTransportException(TTransportException::NOT_OPEN, "ECONNRESET"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 427 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 428 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 429 | // This ish isn't open |
Kevin Clark | 022b224 | 2009-03-05 21:05:37 +0000 | [diff] [blame] | 430 | if (errno_copy == ENOTCONN) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 431 | throw TTransportException(TTransportException::NOT_OPEN, "ENOTCONN"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 432 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 433 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 434 | // Timed out! |
Kevin Clark | 022b224 | 2009-03-05 21:05:37 +0000 | [diff] [blame] | 435 | if (errno_copy == ETIMEDOUT) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 436 | throw TTransportException(TTransportException::TIMED_OUT, "ETIMEDOUT"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 437 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 438 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 439 | // Some other error, whatevz |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 440 | throw TTransportException(TTransportException::UNKNOWN, "Unknown", errno_copy); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 441 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 442 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 443 | // The remote host has closed the socket |
| 444 | if (got == 0) { |
| 445 | close(); |
| 446 | return 0; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 447 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 448 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 449 | // Pack data into string |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 450 | return got; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 453 | void TSocket::write(const uint8_t* buf, uint32_t len) { |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 454 | if (socket_ < 0) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 455 | throw TTransportException(TTransportException::NOT_OPEN, "Called write on non-open socket"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 458 | uint32_t sent = 0; |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 459 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 460 | while (sent < len) { |
Marc Slemko | 9d4a3e2 | 2006-07-21 19:53:48 +0000 | [diff] [blame] | 461 | |
| 462 | int flags = 0; |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 463 | #ifdef MSG_NOSIGNAL |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 464 | // Note the use of MSG_NOSIGNAL to suppress SIGPIPE errors, instead we |
| 465 | // check for the EPIPE return condition and close the socket in that case |
Marc Slemko | 9d4a3e2 | 2006-07-21 19:53:48 +0000 | [diff] [blame] | 466 | flags |= MSG_NOSIGNAL; |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 467 | #endif // ifdef MSG_NOSIGNAL |
Marc Slemko | 9d4a3e2 | 2006-07-21 19:53:48 +0000 | [diff] [blame] | 468 | |
| 469 | int b = send(socket_, buf + sent, len - sent, flags); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 470 | ++g_socket_syscalls; |
| 471 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 472 | // Fail on a send error |
| 473 | if (b < 0) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 474 | int errno_copy = errno; |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 475 | GlobalOutput.perror("TSocket::write() send() " + getSocketInfo(), errno_copy); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 476 | |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 477 | if (errno == EPIPE || errno == ECONNRESET || errno == ENOTCONN) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 478 | close(); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 479 | throw TTransportException(TTransportException::NOT_OPEN, "write() send()", errno_copy); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 480 | } |
| 481 | |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 482 | throw TTransportException(TTransportException::UNKNOWN, "write() send()", errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 483 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 484 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 485 | // Fail on blocked send |
| 486 | if (b == 0) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 487 | throw TTransportException(TTransportException::NOT_OPEN, "Socket send returned 0."); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 488 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 489 | sent += b; |
| 490 | } |
| 491 | } |
| 492 | |
dweatherford | 14b0ed6 | 2007-10-19 01:03:32 +0000 | [diff] [blame] | 493 | std::string TSocket::getHost() { |
| 494 | return host_; |
| 495 | } |
| 496 | |
| 497 | int TSocket::getPort() { |
| 498 | return port_; |
| 499 | } |
| 500 | |
Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 501 | void TSocket::setHost(string host) { |
| 502 | host_ = host; |
| 503 | } |
| 504 | |
| 505 | void TSocket::setPort(int port) { |
| 506 | port_ = port; |
| 507 | } |
| 508 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 509 | void TSocket::setLinger(bool on, int linger) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 510 | lingerOn_ = on; |
| 511 | lingerVal_ = linger; |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 512 | if (socket_ < 0) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 513 | return; |
| 514 | } |
| 515 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 516 | struct linger l = {(lingerOn_ ? 1 : 0), lingerVal_}; |
| 517 | int ret = setsockopt(socket_, SOL_SOCKET, SO_LINGER, &l, sizeof(l)); |
| 518 | if (ret == -1) { |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 519 | int errno_copy = errno; // Copy errno because we're allocating memory. |
| 520 | GlobalOutput.perror("TSocket::setLinger() setsockopt() " + getSocketInfo(), errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 521 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 524 | void TSocket::setNoDelay(bool noDelay) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 525 | noDelay_ = noDelay; |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 526 | if (socket_ < 0) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 527 | return; |
| 528 | } |
| 529 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 530 | // Set socket to NODELAY |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 531 | int v = noDelay_ ? 1 : 0; |
| 532 | int ret = setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v)); |
| 533 | if (ret == -1) { |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 534 | int errno_copy = errno; // Copy errno because we're allocating memory. |
| 535 | GlobalOutput.perror("TSocket::setNoDelay() setsockopt() " + getSocketInfo(), errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 536 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 537 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 538 | |
| 539 | void TSocket::setConnTimeout(int ms) { |
| 540 | connTimeout_ = ms; |
| 541 | } |
| 542 | |
| 543 | void TSocket::setRecvTimeout(int ms) { |
Aditya Agarwal | c31769c | 2007-12-11 22:23:51 +0000 | [diff] [blame] | 544 | if (ms < 0) { |
| 545 | char errBuf[512]; |
| 546 | sprintf(errBuf, "TSocket::setRecvTimeout with negative input: %d\n", ms); |
| 547 | GlobalOutput(errBuf); |
| 548 | return; |
| 549 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 550 | recvTimeout_ = ms; |
Aditya Agarwal | c31769c | 2007-12-11 22:23:51 +0000 | [diff] [blame] | 551 | |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 552 | if (socket_ < 0) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 553 | return; |
| 554 | } |
| 555 | |
Aditya Agarwal | c31769c | 2007-12-11 22:23:51 +0000 | [diff] [blame] | 556 | recvTimeval_.tv_sec = (int)(recvTimeout_/1000); |
| 557 | recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000); |
| 558 | |
David Reiss | 22b1886 | 2008-04-08 06:25:45 +0000 | [diff] [blame] | 559 | // Copy because poll may modify |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 560 | struct timeval r = recvTimeval_; |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 561 | int ret = setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO, &r, sizeof(r)); |
| 562 | if (ret == -1) { |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 563 | int errno_copy = errno; // Copy errno because we're allocating memory. |
| 564 | GlobalOutput.perror("TSocket::setRecvTimeout() setsockopt() " + getSocketInfo(), errno_copy); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 565 | } |
| 566 | } |
| 567 | |
| 568 | void TSocket::setSendTimeout(int ms) { |
Aditya Agarwal | c31769c | 2007-12-11 22:23:51 +0000 | [diff] [blame] | 569 | if (ms < 0) { |
| 570 | char errBuf[512]; |
| 571 | sprintf(errBuf, "TSocket::setSendTimeout with negative input: %d\n", ms); |
| 572 | GlobalOutput(errBuf); |
| 573 | return; |
| 574 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 575 | sendTimeout_ = ms; |
Aditya Agarwal | c31769c | 2007-12-11 22:23:51 +0000 | [diff] [blame] | 576 | |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 577 | if (socket_ < 0) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 578 | return; |
| 579 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 580 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 581 | struct timeval s = {(int)(sendTimeout_/1000), |
| 582 | (int)((sendTimeout_%1000)*1000)}; |
| 583 | int ret = setsockopt(socket_, SOL_SOCKET, SO_SNDTIMEO, &s, sizeof(s)); |
| 584 | if (ret == -1) { |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 585 | int errno_copy = errno; // Copy errno because we're allocating memory. |
| 586 | GlobalOutput.perror("TSocket::setSendTimeout() setsockopt() " + getSocketInfo(), errno_copy); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 587 | } |
| 588 | } |
| 589 | |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 590 | void TSocket::setMaxRecvRetries(int maxRecvRetries) { |
| 591 | maxRecvRetries_ = maxRecvRetries; |
| 592 | } |
| 593 | |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 594 | string TSocket::getSocketInfo() { |
| 595 | std::ostringstream oss; |
| 596 | oss << "<Host: " << host_ << " Port: " << port_ << ">"; |
| 597 | return oss.str(); |
| 598 | } |
| 599 | |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 600 | std::string TSocket::getPeerHost() { |
| 601 | if (peerHost_.empty()) { |
| 602 | struct sockaddr_storage addr; |
| 603 | socklen_t addrLen = sizeof(addr); |
| 604 | |
| 605 | if (socket_ < 0) { |
| 606 | return host_; |
| 607 | } |
| 608 | |
| 609 | int rv = getpeername(socket_, (sockaddr*) &addr, &addrLen); |
| 610 | |
| 611 | if (rv != 0) { |
| 612 | return peerHost_; |
| 613 | } |
| 614 | |
| 615 | char clienthost[NI_MAXHOST]; |
| 616 | char clientservice[NI_MAXSERV]; |
| 617 | |
| 618 | getnameinfo((sockaddr*) &addr, addrLen, |
| 619 | clienthost, sizeof(clienthost), |
| 620 | clientservice, sizeof(clientservice), 0); |
| 621 | |
| 622 | peerHost_ = clienthost; |
| 623 | } |
| 624 | return peerHost_; |
| 625 | } |
| 626 | |
| 627 | std::string TSocket::getPeerAddress() { |
| 628 | if (peerAddress_.empty()) { |
| 629 | struct sockaddr_storage addr; |
| 630 | socklen_t addrLen = sizeof(addr); |
| 631 | |
| 632 | if (socket_ < 0) { |
| 633 | return peerAddress_; |
| 634 | } |
| 635 | |
| 636 | int rv = getpeername(socket_, (sockaddr*) &addr, &addrLen); |
| 637 | |
| 638 | if (rv != 0) { |
| 639 | return peerAddress_; |
| 640 | } |
| 641 | |
| 642 | char clienthost[NI_MAXHOST]; |
| 643 | char clientservice[NI_MAXSERV]; |
| 644 | |
| 645 | getnameinfo((sockaddr*) &addr, addrLen, |
| 646 | clienthost, sizeof(clienthost), |
| 647 | clientservice, sizeof(clientservice), |
| 648 | NI_NUMERICHOST|NI_NUMERICSERV); |
| 649 | |
| 650 | peerAddress_ = clienthost; |
| 651 | peerPort_ = std::atoi(clientservice); |
| 652 | } |
| 653 | return peerAddress_; |
| 654 | } |
| 655 | |
| 656 | int TSocket::getPeerPort() { |
| 657 | getPeerAddress(); |
| 658 | return peerPort_; |
| 659 | } |
| 660 | |
David Reiss | 1c20c87 | 2010-03-09 05:20:14 +0000 | [diff] [blame] | 661 | bool TSocket::useLowMinRto_ = false; |
| 662 | void TSocket::setUseLowMinRto(bool useLowMinRto) { |
| 663 | useLowMinRto_ = useLowMinRto; |
| 664 | } |
| 665 | bool TSocket::getUseLowMinRto() { |
| 666 | return useLowMinRto_; |
| 667 | } |
| 668 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 669 | }}} // apache::thrift::transport |