| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 int status = listen(socket_, backlog); | 414 int status = listen(socket_, backlog); |
| 415 return status == 0; | 415 return status == 0; |
| 416 } | 416 } |
| 417 | 417 |
| 418 | 418 |
| 419 Socket* POSIXSocket::Accept() const { | 419 Socket* POSIXSocket::Accept() const { |
| 420 if (!IsValid()) { | 420 if (!IsValid()) { |
| 421 return NULL; | 421 return NULL; |
| 422 } | 422 } |
| 423 | 423 |
| 424 int socket = accept(socket_, NULL, NULL); | 424 int socket; |
| 425 do { |
| 426 socket = accept(socket_, NULL, NULL); |
| 427 } while (socket == -1 && errno == EINTR); |
| 428 |
| 425 if (socket == -1) { | 429 if (socket == -1) { |
| 426 return NULL; | 430 return NULL; |
| 427 } else { | 431 } else { |
| 428 return new POSIXSocket(socket); | 432 return new POSIXSocket(socket); |
| 429 } | 433 } |
| 430 } | 434 } |
| 431 | 435 |
| 432 | 436 |
| 433 bool POSIXSocket::Connect(const char* host, const char* port) { | 437 bool POSIXSocket::Connect(const char* host, const char* port) { |
| 434 if (!IsValid()) { | 438 if (!IsValid()) { |
| 435 return false; | 439 return false; |
| 436 } | 440 } |
| 437 | 441 |
| 438 // Lookup host and port. | 442 // Lookup host and port. |
| 439 struct addrinfo *result = NULL; | 443 struct addrinfo *result = NULL; |
| 440 struct addrinfo hints; | 444 struct addrinfo hints; |
| 441 memset(&hints, 0, sizeof(addrinfo)); | 445 memset(&hints, 0, sizeof(addrinfo)); |
| 442 hints.ai_family = AF_INET; | 446 hints.ai_family = AF_INET; |
| 443 hints.ai_socktype = SOCK_STREAM; | 447 hints.ai_socktype = SOCK_STREAM; |
| 444 hints.ai_protocol = IPPROTO_TCP; | 448 hints.ai_protocol = IPPROTO_TCP; |
| 445 int status = getaddrinfo(host, port, &hints, &result); | 449 int status = getaddrinfo(host, port, &hints, &result); |
| 446 if (status != 0) { | 450 if (status != 0) { |
| 447 return false; | 451 return false; |
| 448 } | 452 } |
| 449 | 453 |
| 450 // Connect. | 454 // Connect. |
| 451 status = connect(socket_, result->ai_addr, result->ai_addrlen); | 455 do { |
| 456 status = connect(socket_, result->ai_addr, result->ai_addrlen); |
| 457 } while (status == -1 && errno == EINTR); |
| 452 freeaddrinfo(result); | 458 freeaddrinfo(result); |
| 453 return status == 0; | 459 return status == 0; |
| 454 } | 460 } |
| 455 | 461 |
| 456 | 462 |
| 457 bool POSIXSocket::Shutdown() { | 463 bool POSIXSocket::Shutdown() { |
| 458 if (IsValid()) { | 464 if (IsValid()) { |
| 459 // Shutdown socket for both read and write. | 465 // Shutdown socket for both read and write. |
| 460 int status = shutdown(socket_, SHUT_RDWR); | 466 int status = shutdown(socket_, SHUT_RDWR); |
| 461 close(socket_); | 467 close(socket_); |
| 462 socket_ = -1; | 468 socket_ = -1; |
| 463 return status == 0; | 469 return status == 0; |
| 464 } | 470 } |
| 465 return true; | 471 return true; |
| 466 } | 472 } |
| 467 | 473 |
| 468 | 474 |
| 469 int POSIXSocket::Send(const char* data, int len) const { | 475 int POSIXSocket::Send(const char* data, int len) const { |
| 470 if (len <= 0) return 0; | 476 if (len <= 0) return 0; |
| 471 int status = send(socket_, data, len, 0); | 477 int written = 0; |
| 472 return (status < 0) ? 0 : status; | 478 while (written < len) { |
| 479 int status = send(socket_, data + written, len - written, 0); |
| 480 if (status == 0) { |
| 481 break; |
| 482 } else if (status > 0) { |
| 483 written += status; |
| 484 } else if (errno != EINTR) { |
| 485 return 0; |
| 486 } |
| 487 } |
| 488 return written; |
| 473 } | 489 } |
| 474 | 490 |
| 475 | 491 |
| 476 int POSIXSocket::Receive(char* data, int len) const { | 492 int POSIXSocket::Receive(char* data, int len) const { |
| 477 if (len <= 0) return 0; | 493 if (len <= 0) return 0; |
| 478 int status = recv(socket_, data, len, 0); | 494 int status; |
| 495 do { |
| 496 status = recv(socket_, data, len, 0); |
| 497 } while (status == -1 && errno == EINTR); |
| 479 return (status < 0) ? 0 : status; | 498 return (status < 0) ? 0 : status; |
| 480 } | 499 } |
| 481 | 500 |
| 482 | 501 |
| 483 bool POSIXSocket::SetReuseAddress(bool reuse_address) { | 502 bool POSIXSocket::SetReuseAddress(bool reuse_address) { |
| 484 int on = reuse_address ? 1 : 0; | 503 int on = reuse_address ? 1 : 0; |
| 485 int status = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); | 504 int status = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 486 return status == 0; | 505 return status == 0; |
| 487 } | 506 } |
| 488 | 507 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 517 return ntohl(value); | 536 return ntohl(value); |
| 518 } | 537 } |
| 519 | 538 |
| 520 | 539 |
| 521 Socket* OS::CreateSocket() { | 540 Socket* OS::CreateSocket() { |
| 522 return new POSIXSocket(); | 541 return new POSIXSocket(); |
| 523 } | 542 } |
| 524 | 543 |
| 525 | 544 |
| 526 } } // namespace v8::internal | 545 } } // namespace v8::internal |
| OLD | NEW |