Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 1 | #include <sys/socket.h> |
| 2 | #include <arpa/inet.h> |
| 3 | #include <netinet/in.h> |
| 4 | #include <netinet/tcp.h> |
| 5 | #include <netdb.h> |
| 6 | #include <unistd.h> |
| 7 | #include <errno.h> |
| 8 | |
| 9 | #include "transport/TSocket.h" |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 10 | #include "transport/TTransportException.h" |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 11 | |
| 12 | using namespace std; |
| 13 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 14 | uint32_t g_socket_syscalls = 0; |
| 15 | |
| 16 | /** |
| 17 | * TSocket implementation. |
| 18 | * |
| 19 | * @author Mark Slee <mcslee@facebook.com> |
| 20 | */ |
| 21 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 22 | // Mutex to protect syscalls to netdb |
| 23 | pthread_mutex_t g_netdb_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 24 | |
| 25 | // TODO(mcslee): Make this an option to the socket class |
| 26 | #define MAX_RECV_RETRIES 20 |
| 27 | |
| 28 | TSocket::TSocket(string host, int port) : |
| 29 | host_(host), port_(port), socket_(0) {} |
| 30 | |
| 31 | TSocket::TSocket(int socket) { |
| 32 | socket_ = socket; |
| 33 | } |
| 34 | |
| 35 | TSocket::~TSocket() { |
| 36 | close(); |
| 37 | } |
| 38 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 39 | bool TSocket::isOpen() { |
| 40 | return (socket_ > 0); |
| 41 | } |
| 42 | |
| 43 | void TSocket::open() { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 44 | // Create socket |
| 45 | socket_ = socket(AF_INET, SOCK_STREAM, 0); |
| 46 | if (socket_ == -1) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 47 | perror("TSocket::open() socket"); |
| 48 | close(); |
| 49 | throw TTransportException(TTX_NOT_OPEN, "socket() ERROR:" + errno); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 52 | // Lookup the hostname |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 53 | struct sockaddr_in addr; |
| 54 | addr.sin_family = AF_INET; |
| 55 | addr.sin_port = htons(port_); |
| 56 | |
| 57 | /* |
| 58 | if (inet_pton(AF_INET, host_.c_str(), &addr.sin_addr) < 0) { |
| 59 | perror("TSocket::open() inet_pton"); |
| 60 | } |
| 61 | */ |
| 62 | |
| 63 | { |
| 64 | // TODO(mcslee): Fix scope-locking here to protect hostname lookups |
| 65 | // scopelock sl(&netdb_mutex); |
| 66 | struct hostent *host_entry = gethostbyname(host_.c_str()); |
| 67 | |
| 68 | if (host_entry == NULL) { |
| 69 | // perror("dns error: failed call to gethostbyname.\n"); |
| 70 | close(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 71 | throw TTransportException(TTX_NOT_OPEN, "gethostbyname() failed"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | addr.sin_port = htons(port_); |
| 75 | memcpy(&addr.sin_addr.s_addr, |
| 76 | host_entry->h_addr_list[0], |
| 77 | host_entry->h_length); |
| 78 | } |
| 79 | |
| 80 | // Connect the socket |
| 81 | int ret = connect(socket_, (struct sockaddr *)&addr, sizeof(addr)); |
| 82 | |
| 83 | // Connect failed |
| 84 | if (ret < 0) { |
| 85 | perror("TSocket::open() connect"); |
| 86 | close(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 87 | throw TTransportException(TTX_NOT_OPEN, "open() ERROR: " + errno); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 90 | // Connection was successful |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | void TSocket::close() { |
| 94 | if (socket_ > 0) { |
| 95 | shutdown(socket_, SHUT_RDWR); |
| 96 | ::close(socket_); |
| 97 | } |
| 98 | socket_ = 0; |
| 99 | } |
| 100 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 101 | uint32_t TSocket::read(uint8_t* buf, uint32_t len) { |
| 102 | if (socket_ <= 0) { |
| 103 | throw TTransportException(TTX_NOT_OPEN, "Called read on non-open socket"); |
| 104 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 105 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 106 | uint32_t retries = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 107 | |
| 108 | try_again: |
| 109 | // Read from the socket |
| 110 | int got = recv(socket_, buf, len, 0); |
| 111 | ++g_socket_syscalls; |
| 112 | |
| 113 | // Check for error on read |
| 114 | if (got < 0) { |
| 115 | perror("TSocket::read()"); |
| 116 | |
| 117 | // If temporarily out of resources, sleep a bit and try again |
| 118 | if (errno == EAGAIN && retries++ < MAX_RECV_RETRIES) { |
| 119 | usleep(50); |
| 120 | goto try_again; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 123 | // If interrupted, try again |
| 124 | if (errno == EINTR && retries++ < MAX_RECV_RETRIES) { |
| 125 | goto try_again; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 128 | // If we disconnect with no linger time |
| 129 | if (errno == ECONNRESET) { |
| 130 | throw TTransportException(TTX_NOT_OPEN, "ECONNRESET"); |
| 131 | } |
| 132 | |
| 133 | // This ish isn't open |
| 134 | if (errno == ENOTCONN) { |
| 135 | throw TTransportException(TTX_NOT_OPEN, "ENOTCONN"); |
| 136 | } |
| 137 | |
| 138 | // Timed out! |
| 139 | if (errno == ETIMEDOUT) { |
| 140 | throw TTransportException(TTX_TIMED_OUT, "ETIMEDOUT"); |
| 141 | } |
| 142 | |
| 143 | // Some other error, whatevz |
| 144 | throw TTransportException(TTX_UNKNOWN, "ERROR:" + errno); |
| 145 | } |
| 146 | |
| 147 | // The remote host has closed the socket |
| 148 | if (got == 0) { |
| 149 | close(); |
| 150 | return 0; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | // Pack data into string |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 154 | return got; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 157 | void TSocket::write(const uint8_t* buf, uint32_t len) { |
| 158 | if (socket_ <= 0) { |
| 159 | throw TTransportException(TTX_NOT_OPEN, "Called write on non-open socket"); |
| 160 | } |
| 161 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 162 | uint32_t sent = 0; |
| 163 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 164 | while (sent < len) { |
Marc Slemko | 9d4a3e2 | 2006-07-21 19:53:48 +0000 | [diff] [blame^] | 165 | |
| 166 | int flags = 0; |
| 167 | |
| 168 | #if HAVE_BITS_SOCKET_H |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 169 | // Note the use of MSG_NOSIGNAL to suppress SIGPIPE errors, instead we |
| 170 | // 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^] | 171 | flags |= MSG_NOSIGNAL; |
| 172 | #endif // HAVE_BITS_SOCKET_H |
| 173 | |
| 174 | int b = send(socket_, buf + sent, len - sent, flags); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 175 | ++g_socket_syscalls; |
| 176 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 177 | // Fail on a send error |
| 178 | if (b < 0) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 179 | if (errno == EPIPE) { |
| 180 | close(); |
| 181 | throw TTransportException(TTX_NOT_OPEN, "EPIPE"); |
| 182 | } |
| 183 | |
| 184 | if (errno == ECONNRESET) { |
| 185 | close(); |
| 186 | throw TTransportException(TTX_NOT_OPEN, "ECONNRESET"); |
| 187 | } |
| 188 | |
| 189 | if (errno == ENOTCONN) { |
| 190 | close(); |
| 191 | throw TTransportException(TTX_NOT_OPEN, "ENOTCONN"); |
| 192 | } |
| 193 | |
| 194 | perror("TSocket::write() send < 0"); |
| 195 | throw TTransportException(TTX_UNKNOWN, "ERROR:" + errno); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | // Fail on blocked send |
| 199 | if (b == 0) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 200 | throw TTransportException(TTX_NOT_OPEN, "Socket send returned 0."); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 201 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 202 | sent += b; |
| 203 | } |
| 204 | } |
| 205 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 206 | void TSocket::setLinger(bool on, int linger) { |
| 207 | // TODO(mcslee): Store these options so they can be set pre-connect |
| 208 | if (socket_ <= 0) { |
| 209 | return; |
| 210 | } |
| 211 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 212 | struct linger ling = {(on ? 1 : 0), linger}; |
| 213 | if (-1 == setsockopt(socket_, SOL_SOCKET, SO_LINGER, &ling, sizeof(ling))) { |
| 214 | close(); |
| 215 | perror("TSocket::setLinger()"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 216 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 219 | void TSocket::setNoDelay(bool noDelay) { |
| 220 | // TODO(mcslee): Store these options so they can be set pre-connect |
| 221 | if (socket_ <= 0) { |
| 222 | return; |
| 223 | } |
| 224 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 225 | // Set socket to NODELAY |
| 226 | int val = (noDelay ? 1 : 0); |
| 227 | if (-1 == setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val))) { |
| 228 | close(); |
| 229 | perror("TSocket::setNoDelay()"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 230 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 231 | } |