Marc Slemko | e03da18 | 2006-07-21 21:32:36 +0000 | [diff] [blame] | 1 | #include <config.h> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 2 | #include <sys/socket.h> |
| 3 | #include <arpa/inet.h> |
| 4 | #include <netinet/in.h> |
| 5 | #include <netinet/tcp.h> |
| 6 | #include <netdb.h> |
| 7 | #include <unistd.h> |
| 8 | #include <errno.h> |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 9 | #include <fcntl.h> |
| 10 | #include <sys/select.h> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 11 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 12 | #include "concurrency/Monitor.h" |
Marc Slemko | d42a2c2 | 2006-08-10 03:30:18 +0000 | [diff] [blame] | 13 | #include "TSocket.h" |
| 14 | #include "TTransportException.h" |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 15 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 16 | namespace facebook { namespace thrift { namespace transport { |
| 17 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 18 | using namespace std; |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 19 | using namespace facebook::thrift::concurrency; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 20 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 21 | // Global var to track total socket sys calls |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 22 | uint32_t g_socket_syscalls = 0; |
| 23 | |
| 24 | /** |
| 25 | * TSocket implementation. |
| 26 | * |
| 27 | * @author Mark Slee <mcslee@facebook.com> |
| 28 | */ |
| 29 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 30 | // Mutex to protect syscalls to netdb |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 31 | static Monitor s_netdb_monitor; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 32 | |
| 33 | // TODO(mcslee): Make this an option to the socket class |
| 34 | #define MAX_RECV_RETRIES 20 |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 35 | |
| 36 | TSocket::TSocket(string host, int port) : |
| 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), |
| 45 | noDelay_(1) { |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 46 | recvTimeval_.tv_sec = (int)(recvTimeout_/1000); |
| 47 | recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 50 | TSocket::TSocket() : |
| 51 | host_(""), |
| 52 | port_(0), |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 53 | socket_(-1), |
Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 54 | connTimeout_(0), |
| 55 | sendTimeout_(0), |
| 56 | recvTimeout_(0), |
| 57 | lingerOn_(1), |
| 58 | lingerVal_(0), |
| 59 | noDelay_(1) { |
| 60 | recvTimeval_.tv_sec = (int)(recvTimeout_/1000); |
| 61 | recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000); |
| 62 | } |
| 63 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 64 | TSocket::TSocket(int socket) : |
| 65 | host_(""), |
| 66 | port_(0), |
| 67 | socket_(socket), |
| 68 | connTimeout_(0), |
| 69 | sendTimeout_(0), |
| 70 | recvTimeout_(0), |
| 71 | lingerOn_(1), |
| 72 | lingerVal_(0), |
| 73 | noDelay_(1) { |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 74 | recvTimeval_.tv_sec = (int)(recvTimeout_/1000); |
| 75 | recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 78 | TSocket::~TSocket() { |
| 79 | close(); |
| 80 | } |
| 81 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 82 | bool TSocket::isOpen() { |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 83 | return (socket_ >= 0); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 86 | bool TSocket::peek() { |
| 87 | if (!isOpen()) { |
| 88 | return false; |
| 89 | } |
| 90 | uint8_t buf; |
| 91 | int r = recv(socket_, &buf, 1, MSG_PEEK); |
| 92 | if (r == -1) { |
| 93 | perror("TSocket::peek()"); |
| 94 | close(); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 95 | throw TTransportException(TTransportException::UNKNOWN, "recv() ERROR:" + errno); |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 96 | } |
| 97 | return (r > 0); |
| 98 | } |
| 99 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 100 | void TSocket::open() { |
Mark Slee | a9848d7 | 2007-02-21 04:54:05 +0000 | [diff] [blame^] | 101 | if (isOpen()) { |
| 102 | throw TTransportException(TTransportException::ALREADY_OPEN); |
| 103 | } |
| 104 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 105 | // Create socket |
| 106 | socket_ = socket(AF_INET, SOCK_STREAM, 0); |
| 107 | if (socket_ == -1) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 108 | perror("TSocket::open() socket"); |
| 109 | close(); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 110 | throw TTransportException(TTransportException::NOT_OPEN, "socket() ERROR:" + errno); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 111 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 112 | |
| 113 | // Send timeout |
| 114 | if (sendTimeout_ > 0) { |
| 115 | setSendTimeout(sendTimeout_); |
| 116 | } |
| 117 | |
| 118 | // Recv timeout |
| 119 | if (recvTimeout_ > 0) { |
| 120 | setRecvTimeout(recvTimeout_); |
| 121 | } |
| 122 | |
| 123 | // Linger |
| 124 | setLinger(lingerOn_, lingerVal_); |
| 125 | |
| 126 | // No delay |
| 127 | setNoDelay(noDelay_); |
| 128 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 129 | // Lookup the hostname |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 130 | struct sockaddr_in addr; |
| 131 | addr.sin_family = AF_INET; |
| 132 | addr.sin_port = htons(port_); |
| 133 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 134 | { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 135 | // Scope lock on host entry lookup |
| 136 | Synchronized s(s_netdb_monitor); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 137 | struct hostent *host_entry = gethostbyname(host_.c_str()); |
| 138 | |
| 139 | if (host_entry == NULL) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 140 | perror("TSocket: dns error: failed call to gethostbyname."); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 141 | close(); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 142 | throw TTransportException(TTransportException::NOT_OPEN, "gethostbyname() failed"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | addr.sin_port = htons(port_); |
| 146 | memcpy(&addr.sin_addr.s_addr, |
| 147 | host_entry->h_addr_list[0], |
| 148 | host_entry->h_length); |
| 149 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 150 | |
| 151 | // Set the socket to be non blocking for connect if a timeout exists |
| 152 | int flags = fcntl(socket_, F_GETFL, 0); |
| 153 | if (connTimeout_ > 0) { |
| 154 | fcntl(socket_, F_SETFL, flags | O_NONBLOCK); |
| 155 | } else { |
| 156 | fcntl(socket_, F_SETFL, flags | ~O_NONBLOCK); |
| 157 | } |
| 158 | |
| 159 | // Conn timeout |
| 160 | struct timeval c = {(int)(connTimeout_/1000), |
| 161 | (int)((connTimeout_%1000)*1000)}; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 162 | |
| 163 | // Connect the socket |
| 164 | int ret = connect(socket_, (struct sockaddr *)&addr, sizeof(addr)); |
| 165 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 166 | if (ret == 0) { |
| 167 | goto done; |
| 168 | } |
| 169 | |
| 170 | if (errno != EINPROGRESS) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 171 | close(); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 172 | char buff[1024]; |
| 173 | sprintf(buff, "TSocket::open() connect %s %d", host_.c_str(), port_); |
| 174 | perror(buff); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 175 | throw TTransportException(TTransportException::NOT_OPEN, "open() ERROR: " + errno); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 178 | fd_set fds; |
| 179 | FD_ZERO(&fds); |
| 180 | FD_SET(socket_, &fds); |
| 181 | ret = select(socket_+1, NULL, &fds, NULL, &c); |
| 182 | |
| 183 | if (ret > 0) { |
| 184 | // Ensure connected |
| 185 | int val; |
| 186 | socklen_t lon; |
| 187 | lon = sizeof(int); |
| 188 | int ret2 = getsockopt(socket_, SOL_SOCKET, SO_ERROR, (void *)&val, &lon); |
| 189 | if (ret2 == -1) { |
| 190 | close(); |
| 191 | perror("TSocket::open() getsockopt SO_ERROR"); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 192 | throw TTransportException(TTransportException::NOT_OPEN, "open() ERROR: " + errno); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 193 | } |
| 194 | if (val == 0) { |
| 195 | goto done; |
| 196 | } |
| 197 | close(); |
| 198 | perror("TSocket::open() SO_ERROR was set"); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 199 | throw TTransportException(TTransportException::NOT_OPEN, "open() ERROR: " + errno); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 200 | } else if (ret == 0) { |
| 201 | close(); |
| 202 | perror("TSocket::open() timeed out"); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 203 | throw TTransportException(TTransportException::NOT_OPEN, "open() ERROR: " + errno); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 204 | } else { |
| 205 | close(); |
| 206 | perror("TSocket::open() select error"); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 207 | throw TTransportException(TTransportException::NOT_OPEN, "open() ERROR: " + errno); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | done: |
| 211 | // Set socket back to normal mode (blocking) |
| 212 | fcntl(socket_, F_SETFL, flags); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | void TSocket::close() { |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 216 | if (socket_ >= 0) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 217 | shutdown(socket_, SHUT_RDWR); |
| 218 | ::close(socket_); |
| 219 | } |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 220 | socket_ = -1; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 223 | uint32_t TSocket::read(uint8_t* buf, uint32_t len) { |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 224 | if (socket_ < 0) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 225 | throw TTransportException(TTransportException::NOT_OPEN, "Called read on non-open socket"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 226 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 227 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 228 | uint32_t retries = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 229 | |
| 230 | try_again: |
| 231 | // Read from the socket |
| 232 | int got = recv(socket_, buf, len, 0); |
| 233 | ++g_socket_syscalls; |
| 234 | |
| 235 | // Check for error on read |
Mark Slee | c425780 | 2007-01-24 23:14:30 +0000 | [diff] [blame] | 236 | if (got < 0) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 237 | // If temporarily out of resources, sleep a bit and try again |
| 238 | if (errno == EAGAIN && retries++ < MAX_RECV_RETRIES) { |
| 239 | usleep(50); |
| 240 | goto try_again; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 243 | // If interrupted, try again |
| 244 | if (errno == EINTR && retries++ < MAX_RECV_RETRIES) { |
| 245 | goto try_again; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Mark Slee | c425780 | 2007-01-24 23:14:30 +0000 | [diff] [blame] | 248 | // Now it's not a try again case, but a real probblez |
| 249 | perror("TSocket::read()"); |
| 250 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 251 | // If we disconnect with no linger time |
| 252 | if (errno == ECONNRESET) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 253 | throw TTransportException(TTransportException::NOT_OPEN, "ECONNRESET"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | // This ish isn't open |
| 257 | if (errno == ENOTCONN) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 258 | throw TTransportException(TTransportException::NOT_OPEN, "ENOTCONN"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | // Timed out! |
| 262 | if (errno == ETIMEDOUT) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 263 | throw TTransportException(TTransportException::TIMED_OUT, "ETIMEDOUT"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | // Some other error, whatevz |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 267 | throw TTransportException(TTransportException::UNKNOWN, "ERROR:" + errno); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | // The remote host has closed the socket |
| 271 | if (got == 0) { |
| 272 | close(); |
| 273 | return 0; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | // Pack data into string |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 277 | return got; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 280 | void TSocket::write(const uint8_t* buf, uint32_t len) { |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 281 | if (socket_ < 0) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 282 | throw TTransportException(TTransportException::NOT_OPEN, "Called write on non-open socket"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 285 | uint32_t sent = 0; |
| 286 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 287 | while (sent < len) { |
Marc Slemko | 9d4a3e2 | 2006-07-21 19:53:48 +0000 | [diff] [blame] | 288 | |
| 289 | int flags = 0; |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 290 | #ifdef MSG_NOSIGNAL |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 291 | // Note the use of MSG_NOSIGNAL to suppress SIGPIPE errors, instead we |
| 292 | // 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] | 293 | flags |= MSG_NOSIGNAL; |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 294 | #endif // ifdef MSG_NOSIGNAL |
Marc Slemko | 9d4a3e2 | 2006-07-21 19:53:48 +0000 | [diff] [blame] | 295 | |
| 296 | int b = send(socket_, buf + sent, len - sent, flags); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 297 | ++g_socket_syscalls; |
| 298 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 299 | // Fail on a send error |
| 300 | if (b < 0) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 301 | if (errno == EPIPE) { |
| 302 | close(); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 303 | throw TTransportException(TTransportException::NOT_OPEN, "EPIPE"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | if (errno == ECONNRESET) { |
| 307 | close(); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 308 | throw TTransportException(TTransportException::NOT_OPEN, "ECONNRESET"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | if (errno == ENOTCONN) { |
| 312 | close(); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 313 | throw TTransportException(TTransportException::NOT_OPEN, "ENOTCONN"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | perror("TSocket::write() send < 0"); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 317 | throw TTransportException(TTransportException::UNKNOWN, "ERROR:" + errno); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | // Fail on blocked send |
| 321 | if (b == 0) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 322 | throw TTransportException(TTransportException::NOT_OPEN, "Socket send returned 0."); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 323 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 324 | sent += b; |
| 325 | } |
| 326 | } |
| 327 | |
Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 328 | void TSocket::setHost(string host) { |
| 329 | host_ = host; |
| 330 | } |
| 331 | |
| 332 | void TSocket::setPort(int port) { |
| 333 | port_ = port; |
| 334 | } |
| 335 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 336 | void TSocket::setLinger(bool on, int linger) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 337 | lingerOn_ = on; |
| 338 | lingerVal_ = linger; |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 339 | if (socket_ < 0) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 340 | return; |
| 341 | } |
| 342 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 343 | struct linger l = {(lingerOn_ ? 1 : 0), lingerVal_}; |
| 344 | int ret = setsockopt(socket_, SOL_SOCKET, SO_LINGER, &l, sizeof(l)); |
| 345 | if (ret == -1) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 346 | perror("TSocket::setLinger()"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 347 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 350 | void TSocket::setNoDelay(bool noDelay) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 351 | noDelay_ = noDelay; |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 352 | if (socket_ < 0) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 353 | return; |
| 354 | } |
| 355 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 356 | // Set socket to NODELAY |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 357 | int v = noDelay_ ? 1 : 0; |
| 358 | int ret = setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v)); |
| 359 | if (ret == -1) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 360 | perror("TSocket::setNoDelay()"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 361 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 362 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 363 | |
| 364 | void TSocket::setConnTimeout(int ms) { |
| 365 | connTimeout_ = ms; |
| 366 | } |
| 367 | |
| 368 | void TSocket::setRecvTimeout(int ms) { |
| 369 | recvTimeout_ = ms; |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 370 | recvTimeval_.tv_sec = (int)(recvTimeout_/1000); |
| 371 | recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000); |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 372 | if (socket_ < 0) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 373 | return; |
| 374 | } |
| 375 | |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 376 | // Copy because select may modify |
| 377 | struct timeval r = recvTimeval_; |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 378 | int ret = setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO, &r, sizeof(r)); |
| 379 | if (ret == -1) { |
| 380 | perror("TSocket::setRecvTimeout()"); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | void TSocket::setSendTimeout(int ms) { |
| 385 | sendTimeout_ = ms; |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 386 | if (socket_ < 0) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 387 | return; |
| 388 | } |
| 389 | |
| 390 | struct timeval s = {(int)(sendTimeout_/1000), |
| 391 | (int)((sendTimeout_%1000)*1000)}; |
| 392 | int ret = setsockopt(socket_, SOL_SOCKET, SO_SNDTIMEO, &s, sizeof(s)); |
| 393 | if (ret == -1) { |
| 394 | perror("TSocket::setSendTimeout()"); |
| 395 | } |
| 396 | } |
| 397 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 398 | }}} // facebook::thrift::transport |