Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 1 | // Copyright (c) 2006- Facebook |
| 2 | // Distributed under the Thrift Software License |
| 3 | // |
| 4 | // See accompanying file LICENSE or visit the Thrift site at: |
| 5 | // http://developers.facebook.com/thrift/ |
| 6 | |
Marc Slemko | e03da18 | 2006-07-21 21:32:36 +0000 | [diff] [blame] | 7 | #include <config.h> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 8 | #include <sys/socket.h> |
David Reiss | 22b1886 | 2008-04-08 06:25:45 +0000 | [diff] [blame] | 9 | #include <sys/poll.h> |
Mark Slee | dd56497 | 2007-08-21 02:39:57 +0000 | [diff] [blame] | 10 | #include <sys/types.h> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 11 | #include <arpa/inet.h> |
| 12 | #include <netinet/in.h> |
| 13 | #include <netinet/tcp.h> |
| 14 | #include <netdb.h> |
| 15 | #include <unistd.h> |
| 16 | #include <errno.h> |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 17 | #include <fcntl.h> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 18 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 19 | #include "concurrency/Monitor.h" |
Marc Slemko | d42a2c2 | 2006-08-10 03:30:18 +0000 | [diff] [blame] | 20 | #include "TSocket.h" |
| 21 | #include "TTransportException.h" |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 22 | |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 23 | namespace facebook { namespace thrift { namespace transport { |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 24 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 25 | using namespace std; |
| 26 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 27 | // Global var to track total socket sys calls |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 28 | uint32_t g_socket_syscalls = 0; |
| 29 | |
| 30 | /** |
| 31 | * TSocket implementation. |
| 32 | * |
| 33 | * @author Mark Slee <mcslee@facebook.com> |
| 34 | */ |
| 35 | |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 36 | TSocket::TSocket(string host, int port) : |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 37 | host_(host), |
| 38 | port_(port), |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 39 | socket_(-1), |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 40 | connTimeout_(0), |
| 41 | sendTimeout_(0), |
| 42 | recvTimeout_(0), |
| 43 | lingerOn_(1), |
| 44 | lingerVal_(0), |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 45 | noDelay_(1), |
| 46 | maxRecvRetries_(5) { |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 47 | recvTimeval_.tv_sec = (int)(recvTimeout_/1000); |
| 48 | recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 51 | TSocket::TSocket() : |
Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 52 | host_(""), |
| 53 | port_(0), |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 54 | socket_(-1), |
Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 55 | connTimeout_(0), |
| 56 | sendTimeout_(0), |
| 57 | recvTimeout_(0), |
| 58 | lingerOn_(1), |
| 59 | lingerVal_(0), |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 60 | noDelay_(1), |
| 61 | maxRecvRetries_(5) { |
Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 62 | recvTimeval_.tv_sec = (int)(recvTimeout_/1000); |
| 63 | recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000); |
| 64 | } |
| 65 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 66 | TSocket::TSocket(int socket) : |
| 67 | host_(""), |
| 68 | port_(0), |
| 69 | socket_(socket), |
| 70 | connTimeout_(0), |
| 71 | sendTimeout_(0), |
| 72 | recvTimeout_(0), |
| 73 | lingerOn_(1), |
| 74 | lingerVal_(0), |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 75 | noDelay_(1), |
| 76 | maxRecvRetries_(5) { |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 77 | recvTimeval_.tv_sec = (int)(recvTimeout_/1000); |
| 78 | recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 79 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 80 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 81 | TSocket::~TSocket() { |
| 82 | close(); |
| 83 | } |
| 84 | |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 85 | bool TSocket::isOpen() { |
| 86 | return (socket_ >= 0); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 89 | bool TSocket::peek() { |
| 90 | if (!isOpen()) { |
| 91 | return false; |
| 92 | } |
| 93 | uint8_t buf; |
| 94 | int r = recv(socket_, &buf, 1, MSG_PEEK); |
| 95 | if (r == -1) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 96 | int errno_copy = errno; |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame^] | 97 | string errStr = "TSocket::peek() recv() " + getSocketInfo() + TOutput::strerror_s(errno_copy); |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 98 | GlobalOutput(errStr.c_str()); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 99 | throw TTransportException(TTransportException::UNKNOWN, "recv()", errno_copy); |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 100 | } |
| 101 | return (r > 0); |
| 102 | } |
| 103 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 104 | void TSocket::openConnection(struct addrinfo *res) { |
Mark Slee | a9848d7 | 2007-02-21 04:54:05 +0000 | [diff] [blame] | 105 | if (isOpen()) { |
| 106 | throw TTransportException(TTransportException::ALREADY_OPEN); |
| 107 | } |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 108 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 109 | socket_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 110 | if (socket_ == -1) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 111 | int errno_copy = errno; |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame^] | 112 | string errStr = "TSocket::open() socket() " + getSocketInfo() + TOutput::strerror_s(errno_copy); |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 113 | GlobalOutput(errStr.c_str()); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 114 | throw TTransportException(TTransportException::NOT_OPEN, "socket()", errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 115 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 116 | |
| 117 | // Send timeout |
| 118 | if (sendTimeout_ > 0) { |
| 119 | setSendTimeout(sendTimeout_); |
| 120 | } |
| 121 | |
| 122 | // Recv timeout |
| 123 | if (recvTimeout_ > 0) { |
| 124 | setRecvTimeout(recvTimeout_); |
| 125 | } |
| 126 | |
| 127 | // Linger |
| 128 | setLinger(lingerOn_, lingerVal_); |
| 129 | |
| 130 | // No delay |
| 131 | setNoDelay(noDelay_); |
| 132 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 133 | // 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] | 134 | int flags = fcntl(socket_, F_GETFL, 0); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 135 | if (connTimeout_ > 0) { |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 136 | if (-1 == fcntl(socket_, F_SETFL, flags | O_NONBLOCK)) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame^] | 137 | int errno_copy = errno; |
| 138 | string errStr = "TSocket::open() fcntl() " + getSocketInfo() + TOutput::strerror_s(errno_copy); |
| 139 | GlobalOutput(errStr.c_str()); |
| 140 | throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 141 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 142 | } else { |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 143 | if (-1 == fcntl(socket_, F_SETFL, flags & ~O_NONBLOCK)) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame^] | 144 | int errno_copy = errno; |
| 145 | string errStr = "TSocket::open() fcntl " + getSocketInfo() + TOutput::strerror_s(errno_copy); |
| 146 | GlobalOutput(errStr.c_str()); |
| 147 | throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 148 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 151 | // Connect the socket |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 152 | int ret = connect(socket_, res->ai_addr, res->ai_addrlen); |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 153 | |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame^] | 154 | // success case |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 155 | if (ret == 0) { |
| 156 | goto done; |
| 157 | } |
| 158 | |
| 159 | if (errno != EINPROGRESS) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 160 | int errno_copy = errno; |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame^] | 161 | string errStr = "TSocket::open() connect() " + getSocketInfo() + TOutput::strerror_s(errno_copy); |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 162 | GlobalOutput(errStr.c_str()); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame^] | 163 | throw TTransportException(TTransportException::NOT_OPEN, "connect() failed", errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 164 | } |
| 165 | |
David Reiss | 22b1886 | 2008-04-08 06:25:45 +0000 | [diff] [blame] | 166 | |
| 167 | struct pollfd fds[1]; |
| 168 | memset(fds, 0 , sizeof(fds)); |
| 169 | fds[0].fd = socket_; |
| 170 | fds[0].events = POLLOUT; |
| 171 | ret = poll(fds, 1, connTimeout_); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 172 | |
| 173 | if (ret > 0) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame^] | 174 | // Ensure the socket is connected and that there are no errors set |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 175 | int val; |
| 176 | socklen_t lon; |
| 177 | lon = sizeof(int); |
| 178 | int ret2 = getsockopt(socket_, SOL_SOCKET, SO_ERROR, (void *)&val, &lon); |
| 179 | if (ret2 == -1) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 180 | int errno_copy = errno; |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame^] | 181 | string errStr = "TSocket::open() getsockopt() " + getSocketInfo() + TOutput::strerror_s(errno_copy); |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 182 | GlobalOutput(errStr.c_str()); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame^] | 183 | throw TTransportException(TTransportException::NOT_OPEN, "getsockopt()", errno_copy); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 184 | } |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame^] | 185 | // no errors on socket, go to town |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 186 | if (val == 0) { |
| 187 | goto done; |
| 188 | } |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame^] | 189 | string errStr = "TSocket::open() error on socket (after poll) " + getSocketInfo() + TOutput::strerror_s(val); |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 190 | GlobalOutput(errStr.c_str()); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame^] | 191 | throw TTransportException(TTransportException::NOT_OPEN, "socket open() error", val); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 192 | } else if (ret == 0) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame^] | 193 | // socket timed out |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 194 | string errStr = "TSocket::open() timed out " + getSocketInfo(); |
| 195 | GlobalOutput(errStr.c_str()); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame^] | 196 | throw TTransportException(TTransportException::NOT_OPEN, "open() timed out"); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 197 | } else { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame^] | 198 | // error on poll() |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 199 | int errno_copy = errno; |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame^] | 200 | string errStr = "TSocket::open() poll() " + getSocketInfo() + TOutput::strerror_s(errno_copy); |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 201 | GlobalOutput(errStr.c_str()); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame^] | 202 | throw TTransportException(TTransportException::NOT_OPEN, "poll() failed", errno_copy); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | done: |
| 206 | // Set socket back to normal mode (blocking) |
| 207 | fcntl(socket_, F_SETFL, flags); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 210 | void TSocket::open() { |
| 211 | if (isOpen()) { |
| 212 | throw TTransportException(TTransportException::ALREADY_OPEN); |
| 213 | } |
| 214 | |
| 215 | // Validate port number |
| 216 | if (port_ < 0 || port_ > 65536) { |
| 217 | throw TTransportException(TTransportException::NOT_OPEN, "Specified port is invalid"); |
| 218 | } |
| 219 | |
| 220 | struct addrinfo hints, *res, *res0; |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame^] | 221 | res = NULL; |
| 222 | res0 = NULL; |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 223 | int error; |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame^] | 224 | char port[sizeof("65536")]; |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 225 | memset(&hints, 0, sizeof(hints)); |
| 226 | hints.ai_family = PF_UNSPEC; |
| 227 | hints.ai_socktype = SOCK_STREAM; |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 228 | hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 229 | sprintf(port, "%d", port_); |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 230 | |
Mark Slee | c37b4c5 | 2007-12-05 23:03:37 +0000 | [diff] [blame] | 231 | error = getaddrinfo(host_.c_str(), port, &hints, &res0); |
| 232 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 233 | if (error) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame^] | 234 | string errStr = "TSocket::open() getaddrinfo() " + getSocketInfo() + string(gai_strerror(error)); |
| 235 | GlobalOutput(errStr.c_str()); |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 236 | close(); |
| 237 | throw TTransportException(TTransportException::NOT_OPEN, "Could not resolve host for client socket."); |
| 238 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 239 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 240 | // Cycle through all the returned addresses until one |
| 241 | // connects or push the exception up. |
| 242 | for (res = res0; res; res = res->ai_next) { |
| 243 | try { |
| 244 | openConnection(res); |
| 245 | break; |
| 246 | } catch (TTransportException& ttx) { |
| 247 | if (res->ai_next) { |
| 248 | close(); |
| 249 | } else { |
| 250 | close(); |
Mark Slee | 85287d3 | 2007-07-09 19:50:30 +0000 | [diff] [blame] | 251 | freeaddrinfo(res0); // cleanup on failure |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 252 | throw; |
| 253 | } |
| 254 | } |
| 255 | } |
Mark Slee | 85287d3 | 2007-07-09 19:50:30 +0000 | [diff] [blame] | 256 | |
| 257 | // Free address structure memory |
| 258 | freeaddrinfo(res0); |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 261 | void TSocket::close() { |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 262 | if (socket_ >= 0) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 263 | shutdown(socket_, SHUT_RDWR); |
| 264 | ::close(socket_); |
| 265 | } |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 266 | socket_ = -1; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 269 | uint32_t TSocket::read(uint8_t* buf, uint32_t len) { |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 270 | if (socket_ < 0) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 271 | throw TTransportException(TTransportException::NOT_OPEN, "Called read on non-open socket"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 272 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 273 | |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 274 | int32_t retries = 0; |
| 275 | |
| 276 | // EAGAIN can be signalled both when a timeout has occurred and when |
| 277 | // the system is out of resources (an awesome undocumented feature). |
| 278 | // The following is an approximation of the time interval under which |
| 279 | // EAGAIN is taken to indicate an out of resources error. |
| 280 | uint32_t eagainThresholdMicros = 0; |
| 281 | if (recvTimeout_) { |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 282 | // 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] | 283 | // the threshold will ensure that the read timeout is not exceeded even in the |
| 284 | // case of resource errors |
| 285 | eagainThresholdMicros = (recvTimeout_*1000)/ ((maxRecvRetries_>0) ? maxRecvRetries_ : 2); |
| 286 | } |
| 287 | |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 288 | try_again: |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 289 | // Read from the socket |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 290 | struct timeval begin; |
| 291 | gettimeofday(&begin, NULL); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 292 | int got = recv(socket_, buf, len, 0); |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 293 | struct timeval end; |
| 294 | gettimeofday(&end, NULL); |
| 295 | uint32_t readElapsedMicros = (((end.tv_sec - begin.tv_sec) * 1000 * 1000) |
| 296 | + (((uint64_t)(end.tv_usec - begin.tv_usec)))); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 297 | ++g_socket_syscalls; |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 298 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 299 | // Check for error on read |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 300 | if (got < 0) { |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 301 | if (errno == EAGAIN) { |
| 302 | // check if this is the lack of resources or timeout case |
| 303 | if (!eagainThresholdMicros || (readElapsedMicros < eagainThresholdMicros)) { |
| 304 | if (retries++ < maxRecvRetries_) { |
| 305 | usleep(50); |
| 306 | goto try_again; |
| 307 | } else { |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 308 | throw TTransportException(TTransportException::TIMED_OUT, |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 309 | "EAGAIN (unavailable resources)"); |
| 310 | } |
| 311 | } else { |
| 312 | // infer that timeout has been hit |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 313 | throw TTransportException(TTransportException::TIMED_OUT, |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 314 | "EAGAIN (timed out)"); |
| 315 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 316 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 317 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 318 | // If interrupted, try again |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 319 | if (errno == EINTR && retries++ < maxRecvRetries_) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 320 | goto try_again; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 321 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 322 | |
Mark Slee | c425780 | 2007-01-24 23:14:30 +0000 | [diff] [blame] | 323 | // Now it's not a try again case, but a real probblez |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame^] | 324 | string errStr = "TSocket::read() recv() " + getSocketInfo() + TOutput::strerror_s(errno); |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 325 | GlobalOutput(errStr.c_str()); |
Mark Slee | c425780 | 2007-01-24 23:14:30 +0000 | [diff] [blame] | 326 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 327 | // If we disconnect with no linger time |
| 328 | if (errno == ECONNRESET) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 329 | throw TTransportException(TTransportException::NOT_OPEN, "ECONNRESET"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 330 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 331 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 332 | // This ish isn't open |
| 333 | if (errno == ENOTCONN) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 334 | throw TTransportException(TTransportException::NOT_OPEN, "ENOTCONN"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 335 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 336 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 337 | // Timed out! |
| 338 | if (errno == ETIMEDOUT) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 339 | throw TTransportException(TTransportException::TIMED_OUT, "ETIMEDOUT"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 340 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 341 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 342 | // Some other error, whatevz |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame^] | 343 | throw TTransportException(TTransportException::UNKNOWN, "Unknown", errno); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 344 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 345 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 346 | // The remote host has closed the socket |
| 347 | if (got == 0) { |
| 348 | close(); |
| 349 | return 0; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 350 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 351 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 352 | // Pack data into string |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 353 | return got; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 356 | void TSocket::write(const uint8_t* buf, uint32_t len) { |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 357 | if (socket_ < 0) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 358 | throw TTransportException(TTransportException::NOT_OPEN, "Called write on non-open socket"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 361 | uint32_t sent = 0; |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 362 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 363 | while (sent < len) { |
Marc Slemko | 9d4a3e2 | 2006-07-21 19:53:48 +0000 | [diff] [blame] | 364 | |
| 365 | int flags = 0; |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 366 | #ifdef MSG_NOSIGNAL |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 367 | // Note the use of MSG_NOSIGNAL to suppress SIGPIPE errors, instead we |
| 368 | // 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] | 369 | flags |= MSG_NOSIGNAL; |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 370 | #endif // ifdef MSG_NOSIGNAL |
Marc Slemko | 9d4a3e2 | 2006-07-21 19:53:48 +0000 | [diff] [blame] | 371 | |
| 372 | int b = send(socket_, buf + sent, len - sent, flags); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 373 | ++g_socket_syscalls; |
| 374 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 375 | // Fail on a send error |
| 376 | if (b < 0) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame^] | 377 | int errno_copy = errno; |
| 378 | string errStr = "TSocket::write() send() " + getSocketInfo() + TOutput::strerror_s(errno_copy); |
| 379 | GlobalOutput(errStr.c_str()); |
| 380 | |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 381 | if (errno == EPIPE || errno == ECONNRESET || errno == ENOTCONN) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 382 | close(); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame^] | 383 | throw TTransportException(TTransportException::NOT_OPEN, "write() send()", errno_copy); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 384 | } |
| 385 | |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame^] | 386 | throw TTransportException(TTransportException::UNKNOWN, "write() send()", errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 387 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 388 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 389 | // Fail on blocked send |
| 390 | if (b == 0) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 391 | throw TTransportException(TTransportException::NOT_OPEN, "Socket send returned 0."); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 392 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 393 | sent += b; |
| 394 | } |
| 395 | } |
| 396 | |
dweatherford | 14b0ed6 | 2007-10-19 01:03:32 +0000 | [diff] [blame] | 397 | std::string TSocket::getHost() { |
| 398 | return host_; |
| 399 | } |
| 400 | |
| 401 | int TSocket::getPort() { |
| 402 | return port_; |
| 403 | } |
| 404 | |
Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 405 | void TSocket::setHost(string host) { |
| 406 | host_ = host; |
| 407 | } |
| 408 | |
| 409 | void TSocket::setPort(int port) { |
| 410 | port_ = port; |
| 411 | } |
| 412 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 413 | void TSocket::setLinger(bool on, int linger) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 414 | lingerOn_ = on; |
| 415 | lingerVal_ = linger; |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 416 | if (socket_ < 0) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 417 | return; |
| 418 | } |
| 419 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 420 | struct linger l = {(lingerOn_ ? 1 : 0), lingerVal_}; |
| 421 | int ret = setsockopt(socket_, SOL_SOCKET, SO_LINGER, &l, sizeof(l)); |
| 422 | if (ret == -1) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame^] | 423 | int errno_copy = errno; |
| 424 | string errStr = "TSocket::setLinger() setsockopt() " + getSocketInfo() + TOutput::strerror_s(errno_copy); |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 425 | GlobalOutput(errStr.c_str()); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 426 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 429 | void TSocket::setNoDelay(bool noDelay) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 430 | noDelay_ = noDelay; |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 431 | if (socket_ < 0) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 432 | return; |
| 433 | } |
| 434 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 435 | // Set socket to NODELAY |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 436 | int v = noDelay_ ? 1 : 0; |
| 437 | int ret = setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v)); |
| 438 | if (ret == -1) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame^] | 439 | int errno_copy = errno; |
| 440 | string errStr = "TSocket::setNoDelay() setsockopt() " + getSocketInfo() + TOutput::strerror_s(errno_copy); |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 441 | GlobalOutput(errStr.c_str()); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 442 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 443 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 444 | |
| 445 | void TSocket::setConnTimeout(int ms) { |
| 446 | connTimeout_ = ms; |
| 447 | } |
| 448 | |
| 449 | void TSocket::setRecvTimeout(int ms) { |
Aditya Agarwal | c31769c | 2007-12-11 22:23:51 +0000 | [diff] [blame] | 450 | if (ms < 0) { |
| 451 | char errBuf[512]; |
| 452 | sprintf(errBuf, "TSocket::setRecvTimeout with negative input: %d\n", ms); |
| 453 | GlobalOutput(errBuf); |
| 454 | return; |
| 455 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 456 | recvTimeout_ = ms; |
Aditya Agarwal | c31769c | 2007-12-11 22:23:51 +0000 | [diff] [blame] | 457 | |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 458 | if (socket_ < 0) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 459 | return; |
| 460 | } |
| 461 | |
Aditya Agarwal | c31769c | 2007-12-11 22:23:51 +0000 | [diff] [blame] | 462 | recvTimeval_.tv_sec = (int)(recvTimeout_/1000); |
| 463 | recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000); |
| 464 | |
David Reiss | 22b1886 | 2008-04-08 06:25:45 +0000 | [diff] [blame] | 465 | // Copy because poll may modify |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 466 | struct timeval r = recvTimeval_; |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 467 | int ret = setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO, &r, sizeof(r)); |
| 468 | if (ret == -1) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame^] | 469 | int errno_copy = errno; |
| 470 | string errStr = "TSocket::setRecvTimeout() setsockopt() " + getSocketInfo() + TOutput::strerror_s(errno_copy); |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 471 | GlobalOutput(errStr.c_str()); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | |
| 475 | void TSocket::setSendTimeout(int ms) { |
Aditya Agarwal | c31769c | 2007-12-11 22:23:51 +0000 | [diff] [blame] | 476 | if (ms < 0) { |
| 477 | char errBuf[512]; |
| 478 | sprintf(errBuf, "TSocket::setSendTimeout with negative input: %d\n", ms); |
| 479 | GlobalOutput(errBuf); |
| 480 | return; |
| 481 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 482 | sendTimeout_ = ms; |
Aditya Agarwal | c31769c | 2007-12-11 22:23:51 +0000 | [diff] [blame] | 483 | |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 484 | if (socket_ < 0) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 485 | return; |
| 486 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 487 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 488 | struct timeval s = {(int)(sendTimeout_/1000), |
| 489 | (int)((sendTimeout_%1000)*1000)}; |
| 490 | int ret = setsockopt(socket_, SOL_SOCKET, SO_SNDTIMEO, &s, sizeof(s)); |
| 491 | if (ret == -1) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame^] | 492 | int errno_copy = errno; |
| 493 | string errStr = "TSocket::setSendTimeout() setsockopt() " + getSocketInfo() + TOutput::strerror_s(errno_copy); |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 494 | GlobalOutput(errStr.c_str()); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 498 | void TSocket::setMaxRecvRetries(int maxRecvRetries) { |
| 499 | maxRecvRetries_ = maxRecvRetries; |
| 500 | } |
| 501 | |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 502 | string TSocket::getSocketInfo() { |
| 503 | std::ostringstream oss; |
| 504 | oss << "<Host: " << host_ << " Port: " << port_ << ">"; |
| 505 | return oss.str(); |
| 506 | } |
| 507 | |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 508 | std::string TSocket::getPeerHost() { |
| 509 | if (peerHost_.empty()) { |
| 510 | struct sockaddr_storage addr; |
| 511 | socklen_t addrLen = sizeof(addr); |
| 512 | |
| 513 | if (socket_ < 0) { |
| 514 | return host_; |
| 515 | } |
| 516 | |
| 517 | int rv = getpeername(socket_, (sockaddr*) &addr, &addrLen); |
| 518 | |
| 519 | if (rv != 0) { |
| 520 | return peerHost_; |
| 521 | } |
| 522 | |
| 523 | char clienthost[NI_MAXHOST]; |
| 524 | char clientservice[NI_MAXSERV]; |
| 525 | |
| 526 | getnameinfo((sockaddr*) &addr, addrLen, |
| 527 | clienthost, sizeof(clienthost), |
| 528 | clientservice, sizeof(clientservice), 0); |
| 529 | |
| 530 | peerHost_ = clienthost; |
| 531 | } |
| 532 | return peerHost_; |
| 533 | } |
| 534 | |
| 535 | std::string TSocket::getPeerAddress() { |
| 536 | if (peerAddress_.empty()) { |
| 537 | struct sockaddr_storage addr; |
| 538 | socklen_t addrLen = sizeof(addr); |
| 539 | |
| 540 | if (socket_ < 0) { |
| 541 | return peerAddress_; |
| 542 | } |
| 543 | |
| 544 | int rv = getpeername(socket_, (sockaddr*) &addr, &addrLen); |
| 545 | |
| 546 | if (rv != 0) { |
| 547 | return peerAddress_; |
| 548 | } |
| 549 | |
| 550 | char clienthost[NI_MAXHOST]; |
| 551 | char clientservice[NI_MAXSERV]; |
| 552 | |
| 553 | getnameinfo((sockaddr*) &addr, addrLen, |
| 554 | clienthost, sizeof(clienthost), |
| 555 | clientservice, sizeof(clientservice), |
| 556 | NI_NUMERICHOST|NI_NUMERICSERV); |
| 557 | |
| 558 | peerAddress_ = clienthost; |
| 559 | peerPort_ = std::atoi(clientservice); |
| 560 | } |
| 561 | return peerAddress_; |
| 562 | } |
| 563 | |
| 564 | int TSocket::getPeerPort() { |
| 565 | getPeerAddress(); |
| 566 | return peerPort_; |
| 567 | } |
| 568 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 569 | }}} // facebook::thrift::transport |