| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 7 | #include <netdb.h> |
| 8 | #include <unistd.h> |
| 9 | #include <errno.h> |
| 10 | #include <fcntl.h> |
| 11 | #include "SkSockets.h" |
| 12 | #include "SkData.h" |
| 13 | |
| 14 | SkSocket::SkSocket() { |
| 15 | fMaxfd = 0; |
| 16 | FD_ZERO(&fMasterSet); |
| 17 | fConnected = false; |
| 18 | fReady = false; |
| 19 | fReadSuspended = false; |
| 20 | fWriteSuspended = false; |
| 21 | fSockfd = this->createSocket(); |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | SkSocket::~SkSocket() { |
| 25 | this->closeSocket(fSockfd); |
| 26 | } |
| 27 | |
| 28 | int SkSocket::createSocket() { |
| 29 | int sockfd = socket(AF_INET, SOCK_STREAM, 0); |
| 30 | if (sockfd < 0) { |
| yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame^] | 31 | SkDebugf("ERROR opening socket\n"); |
| 32 | return -1; |
| 33 | } |
| 34 | int reuse = 1; |
| 35 | |
| 36 | if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(int)) < 0) { |
| 37 | SkDebugf("error: %s\n", strerror(errno)); |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 38 | return -1; |
| 39 | } |
| 40 | #ifdef NONBLOCKING_SOCKETS |
| 41 | this->setNonBlocking(sockfd); |
| 42 | #endif |
| 43 | //SkDebugf("Opened fd:%d\n", sockfd); |
| 44 | fReady = true; |
| 45 | return sockfd; |
| 46 | } |
| 47 | |
| 48 | void SkSocket::closeSocket(int sockfd) { |
| 49 | if (!fReady) |
| 50 | return; |
| 51 | |
| yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame^] | 52 | shutdown(sockfd, 2); //stop sending/receiving |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 53 | close(sockfd); |
| yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame^] | 54 | //SkDebugf("Closed fd:%d\n", sockfd); |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 55 | |
| 56 | if (FD_ISSET(sockfd, &fMasterSet)) { |
| 57 | FD_CLR(sockfd, &fMasterSet); |
| 58 | if (sockfd >= fMaxfd) { |
| 59 | while (FD_ISSET(fMaxfd, &fMasterSet) == false && fMaxfd > 0) |
| 60 | fMaxfd -= 1; |
| 61 | } |
| 62 | } |
| yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame^] | 63 | if (0 == fMaxfd) |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 64 | fConnected = false; |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void SkSocket::onFailedConnection(int sockfd) { |
| 68 | this->closeSocket(sockfd); |
| 69 | } |
| 70 | |
| 71 | void SkSocket::setNonBlocking(int sockfd) { |
| 72 | int flags = fcntl(sockfd, F_GETFL); |
| 73 | fcntl(sockfd, F_SETFL, flags | O_NONBLOCK); |
| 74 | } |
| 75 | |
| 76 | void SkSocket::addToMasterSet(int sockfd) { |
| 77 | FD_SET(sockfd, &fMasterSet); |
| 78 | if (sockfd > fMaxfd) |
| 79 | fMaxfd = sockfd; |
| 80 | } |
| 81 | |
| yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame^] | 82 | int SkSocket::readPacket(void (*onRead)(int, const void*, size_t, DataType, |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 83 | void*), void* context) { |
| yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame^] | 84 | if (!fConnected || !fReady || NULL == onRead || NULL == context |
| 85 | || fReadSuspended) |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 86 | return -1; |
| 87 | |
| 88 | int totalBytesRead = 0; |
| 89 | |
| 90 | char packet[PACKET_SIZE]; |
| 91 | for (int i = 0; i <= fMaxfd; ++i) { |
| 92 | if (!FD_ISSET (i, &fMasterSet)) |
| 93 | continue; |
| 94 | |
| 95 | memset(packet, 0, PACKET_SIZE); |
| 96 | SkDynamicMemoryWStream stream; |
| 97 | int attempts = 0; |
| 98 | bool failure = false; |
| 99 | int bytesReadInTransfer = 0; |
| 100 | int bytesReadInPacket = 0; |
| 101 | header h; |
| 102 | h.done = false; |
| 103 | h.bytes = 0; |
| 104 | while (!h.done && fConnected && !failure) { |
| 105 | int retval = read(i, packet + bytesReadInPacket, |
| 106 | PACKET_SIZE - bytesReadInPacket); |
| 107 | |
| 108 | ++attempts; |
| 109 | if (retval < 0) { |
| 110 | #ifdef NONBLOCKING_SOCKETS |
| 111 | if (errno == EWOULDBLOCK || errno == EAGAIN) { |
| 112 | if (bytesReadInPacket > 0 || bytesReadInTransfer > 0) |
| 113 | continue; //incomplete packet or frame, keep tring |
| 114 | else |
| 115 | break; //nothing to read |
| 116 | } |
| 117 | #endif |
| 118 | //SkDebugf("Read() failed with error: %s\n", strerror(errno)); |
| 119 | failure = true; |
| 120 | break; |
| 121 | } |
| 122 | |
| 123 | if (retval == 0) { |
| 124 | //SkDebugf("Peer closed connection or connection failed\n"); |
| 125 | failure = true; |
| 126 | break; |
| 127 | } |
| 128 | |
| 129 | SkASSERT(retval > 0); |
| 130 | bytesReadInPacket += retval; |
| 131 | if (bytesReadInPacket < PACKET_SIZE) { |
| 132 | //SkDebugf("Read %d/%d\n", bytesReadInPacket, PACKET_SIZE); |
| 133 | continue; //incomplete packet, keep trying |
| 134 | } |
| 135 | |
| 136 | SkASSERT((bytesReadInPacket == PACKET_SIZE) && !failure); |
| 137 | memcpy(&h.done, packet, sizeof(bool)); |
| 138 | memcpy(&h.bytes, packet + sizeof(bool), sizeof(int)); |
| 139 | memcpy(&h.type, packet + sizeof(bool) + sizeof(int), sizeof(DataType)); |
| 140 | if (h.bytes > CONTENT_SIZE || h.bytes <= 0) { |
| 141 | //SkDebugf("bad packet\n"); |
| 142 | failure = true; |
| 143 | break; |
| 144 | } |
| yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame^] | 145 | //SkDebugf("read packet(done:%d, bytes:%d) from fd:%d in %d tries\n", |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 146 | // h.done, h.bytes, fSockfd, attempts); |
| yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame^] | 147 | stream.write(packet + HEADER_SIZE, h.bytes); |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 148 | bytesReadInPacket = 0; |
| 149 | attempts = 0; |
| 150 | bytesReadInTransfer += h.bytes; |
| 151 | } |
| 152 | |
| 153 | if (failure) { |
| 154 | onRead(NULL, 0, i, h.type, context); |
| 155 | this->onFailedConnection(i); |
| 156 | continue; |
| 157 | } |
| 158 | |
| 159 | if (bytesReadInTransfer > 0) { |
| 160 | SkData* data = stream.copyToData(); |
| 161 | SkASSERT(data->size() == bytesReadInTransfer); |
| yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame^] | 162 | onRead(i, data->data(), data->size(), h.type, context); |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 163 | data->unref(); |
| 164 | |
| 165 | totalBytesRead += bytesReadInTransfer; |
| 166 | } |
| 167 | } |
| 168 | return totalBytesRead; |
| 169 | } |
| 170 | |
| 171 | int SkSocket::writePacket(void* data, size_t size, DataType type) { |
| 172 | if (size < 0|| NULL == data || !fConnected || !fReady || fWriteSuspended) |
| 173 | return -1; |
| 174 | |
| 175 | int totalBytesWritten = 0; |
| 176 | header h; |
| 177 | char packet[PACKET_SIZE]; |
| 178 | for (int i = 0; i <= fMaxfd; ++i) { |
| 179 | if (!FD_ISSET (i, &fMasterSet)) |
| 180 | continue; |
| 181 | |
| 182 | //Don't signal broken pipe |
| 183 | setsockopt(i, SOL_SOCKET, SO_NOSIGPIPE, (void*)1, sizeof(int)); |
| 184 | int bytesWrittenInTransfer = 0; |
| 185 | int bytesWrittenInPacket = 0; |
| 186 | int attempts = 0; |
| 187 | bool failure = false; |
| 188 | while (bytesWrittenInTransfer < size && fConnected && !failure) { |
| 189 | memset(packet, 0, PACKET_SIZE); |
| 190 | h.done = (size - bytesWrittenInTransfer <= CONTENT_SIZE); |
| 191 | h.bytes = (h.done) ? size - bytesWrittenInTransfer : CONTENT_SIZE; |
| 192 | h.type = type; |
| 193 | memcpy(packet, &h.done, sizeof(bool)); |
| 194 | memcpy(packet + sizeof(bool), &h.bytes, sizeof(int)); |
| 195 | memcpy(packet + sizeof(bool) + sizeof(int), &h.type, sizeof(DataType)); |
| 196 | memcpy(packet + HEADER_SIZE, (char*)data + bytesWrittenInTransfer, |
| 197 | h.bytes); |
| 198 | |
| 199 | int retval = write(i, packet + bytesWrittenInPacket, |
| 200 | PACKET_SIZE - bytesWrittenInPacket); |
| 201 | attempts++; |
| 202 | |
| 203 | if (retval < 0) { |
| 204 | if (errno == EPIPE) { |
| 205 | //SkDebugf("broken pipe, client closed connection"); |
| 206 | failure = true; |
| 207 | break; |
| 208 | } |
| 209 | #ifdef NONBLOCKING_SOCKETS |
| 210 | else if (errno == EWOULDBLOCK || errno == EAGAIN) { |
| 211 | if (bytesWrittenInPacket > 0 || bytesWrittenInTransfer > 0) |
| yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame^] | 212 | continue; //incomplete packet or frame, keep trying |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 213 | else |
| 214 | break; //client not available, skip current transfer |
| 215 | } |
| 216 | #endif |
| 217 | else { |
| 218 | //SkDebugf("write(%d) failed with error:%s\n", i, |
| 219 | // strerror(errno)); |
| 220 | failure = true; |
| 221 | break; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | bytesWrittenInPacket += retval; |
| yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame^] | 226 | if (bytesWrittenInPacket < PACKET_SIZE) |
| 227 | continue; //incomplete packet, keep trying |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 228 | |
| 229 | SkASSERT(bytesWrittenInPacket == PACKET_SIZE); |
| 230 | //SkDebugf("wrote to packet(done:%d, bytes:%d) to fd:%d in %d tries\n", |
| 231 | // h.done, h.bytes, i, attempts); |
| 232 | bytesWrittenInTransfer += h.bytes; |
| 233 | bytesWrittenInPacket = 0; |
| 234 | attempts = 0; |
| 235 | } |
| 236 | |
| yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame^] | 237 | if (failure) |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 238 | this->onFailedConnection(i); |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 239 | |
| 240 | totalBytesWritten += bytesWrittenInTransfer; |
| 241 | } |
| 242 | return totalBytesWritten; |
| 243 | } |
| yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame^] | 244 | |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 245 | //////////////////////////////////////////////////////////////////////////////// |
| 246 | SkTCPServer::SkTCPServer(int port) { |
| 247 | sockaddr_in serverAddr; |
| 248 | serverAddr.sin_family = AF_INET; |
| 249 | serverAddr.sin_addr.s_addr = INADDR_ANY; |
| 250 | serverAddr.sin_port = htons(port); |
| 251 | |
| 252 | if (bind(fSockfd, (sockaddr*)&serverAddr, sizeof(serverAddr)) < 0) { |
| yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame^] | 253 | SkDebugf("ERROR on binding: %s\n", strerror(errno)); |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 254 | fReady = false; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | SkTCPServer::~SkTCPServer() { |
| yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame^] | 259 | this->disconnectAll(); |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame^] | 262 | int SkTCPServer::acceptConnections() { |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 263 | if (!fReady) |
| 264 | return -1; |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 265 | |
| yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame^] | 266 | listen(fSockfd, MAX_WAITING_CLIENTS); |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 267 | int newfd; |
| yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame^] | 268 | for (int i = 0; i < MAX_WAITING_CLIENTS; ++i) { |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 269 | #ifdef NONBLOCKING_SOCKETS |
| 270 | fd_set workingSet; |
| 271 | FD_ZERO(&workingSet); |
| 272 | FD_SET(fSockfd, &workingSet); |
| yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame^] | 273 | timeval timeout; |
| 274 | timeout.tv_sec = 0; |
| 275 | timeout.tv_usec = 0; |
| 276 | int sel = select(fSockfd + 1, &workingSet, NULL, NULL, &timeout); |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 277 | if (sel < 0) { |
| yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame^] | 278 | SkDebugf("select() failed with error %s\n", strerror(errno)); |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 279 | continue; |
| 280 | } |
| 281 | if (sel == 0) //select() timed out |
| 282 | continue; |
| 283 | #endif |
| 284 | sockaddr_in clientAddr; |
| 285 | socklen_t clientLen = sizeof(clientAddr); |
| 286 | newfd = accept(fSockfd, (struct sockaddr*)&clientAddr, &clientLen); |
| 287 | if (newfd< 0) { |
| yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame^] | 288 | SkDebugf("accept() failed with error %s\n", strerror(errno)); |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 289 | continue; |
| 290 | } |
| yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame^] | 291 | SkDebugf("New incoming connection - %d\n", newfd); |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 292 | fConnected = true; |
| 293 | #ifdef NONBLOCKING_SOCKETS |
| 294 | this->setNonBlocking(newfd); |
| 295 | #endif |
| 296 | this->addToMasterSet(newfd); |
| 297 | } |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | |
| yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame^] | 302 | int SkTCPServer::disconnectAll() { |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 303 | if (!fConnected || !fReady) |
| 304 | return -1; |
| yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame^] | 305 | for (int i = 0; i <= fMaxfd; ++i) { |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 306 | if (FD_ISSET(i, &fMasterSet)) |
| 307 | this->closeSocket(i); |
| 308 | } |
| 309 | fConnected = false; |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | //////////////////////////////////////////////////////////////////////////////// |
| 314 | SkTCPClient::SkTCPClient(const char* hostname, int port) { |
| 315 | //Add fSockfd since the client will be using it to read/write |
| 316 | this->addToMasterSet(fSockfd); |
| 317 | |
| 318 | hostent* server = gethostbyname(hostname); |
| 319 | if (server) { |
| 320 | fServerAddr.sin_family = AF_INET; |
| 321 | memcpy((char*)&fServerAddr.sin_addr.s_addr, (char*)server->h_addr, |
| 322 | server->h_length); |
| 323 | fServerAddr.sin_port = htons(port); |
| 324 | } |
| 325 | else { |
| 326 | //SkDebugf("ERROR, no such host\n"); |
| 327 | fReady = false; |
| 328 | } |
| 329 | } |
| 330 | |
| yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame^] | 331 | void SkTCPClient::onFailedConnection(int sockfd) { //cleanup and recreate socket |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 332 | SkASSERT(sockfd == fSockfd); |
| 333 | this->closeSocket(fSockfd); |
| 334 | fSockfd = this->createSocket(); |
| 335 | //Add fSockfd since the client will be using it to read/write |
| 336 | this->addToMasterSet(fSockfd); |
| 337 | } |
| 338 | |
| 339 | int SkTCPClient::connectToServer() { |
| 340 | if (!fReady) |
| 341 | return -1; |
| 342 | if (fConnected) |
| 343 | return 0; |
| 344 | |
| 345 | int conn = connect(fSockfd, (sockaddr*)&fServerAddr, sizeof(fServerAddr)); |
| 346 | if (conn < 0) { |
| 347 | #ifdef NONBLOCKING_SOCKETS |
| 348 | if (errno == EINPROGRESS || errno == EALREADY) |
| 349 | return conn; |
| 350 | #endif |
| 351 | if (errno != EISCONN) { |
| 352 | //SkDebugf("error: %s\n", strerror(errno)); |
| 353 | this->onFailedConnection(fSockfd); |
| 354 | return conn; |
| 355 | } |
| 356 | } |
| 357 | fConnected = true; |
| yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame^] | 358 | SkDebugf("Succesfully reached server\n"); |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 359 | return 0; |
| 360 | } |