Index: src/platform-posix.cc |
diff --git a/src/platform-posix.cc b/src/platform-posix.cc |
index aaf0ca7e0dc81fbf8e7940ff80e6bcda1dab8240..504d140138c178fe50ff19480e20dcefd3db3a99 100644 |
--- a/src/platform-posix.cc |
+++ b/src/platform-posix.cc |
@@ -739,202 +739,4 @@ void Thread::SetThreadLocal(LocalStorageKey key, void* value) { |
} |
-// ---------------------------------------------------------------------------- |
-// POSIX socket support. |
-// |
- |
-class POSIXSocket : public Socket { |
- public: |
- explicit POSIXSocket() { |
- // Create the socket. |
- socket_ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
- if (IsValid()) { |
- // Allow rapid reuse. |
- static const int kOn = 1; |
- int ret = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, |
- &kOn, sizeof(kOn)); |
- ASSERT(ret == 0); |
- USE(ret); |
- } |
- } |
- explicit POSIXSocket(int socket): socket_(socket) { } |
- virtual ~POSIXSocket() { Shutdown(); } |
- |
- // Server initialization. |
- bool Bind(const int port); |
- bool Listen(int backlog) const; |
- Socket* Accept() const; |
- |
- // Client initialization. |
- bool Connect(const char* host, const char* port); |
- |
- // Shutdown socket for both read and write. |
- bool Shutdown(); |
- |
- // Data Transimission |
- int Send(const char* data, int len) const; |
- int Receive(char* data, int len) const; |
- |
- bool SetReuseAddress(bool reuse_address); |
- |
- bool IsValid() const { return socket_ != -1; } |
- |
- private: |
- int socket_; |
-}; |
- |
- |
-bool POSIXSocket::Bind(const int port) { |
- if (!IsValid()) { |
- return false; |
- } |
- |
- sockaddr_in addr; |
- memset(&addr, 0, sizeof(addr)); |
- addr.sin_family = AF_INET; |
- addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
- addr.sin_port = htons(port); |
- int status = bind(socket_, |
- BitCast<struct sockaddr *>(&addr), |
- sizeof(addr)); |
- return status == 0; |
-} |
- |
- |
-bool POSIXSocket::Listen(int backlog) const { |
- if (!IsValid()) { |
- return false; |
- } |
- |
- int status = listen(socket_, backlog); |
- return status == 0; |
-} |
- |
- |
-Socket* POSIXSocket::Accept() const { |
- if (!IsValid()) { |
- return NULL; |
- } |
- |
- int socket; |
- do { |
- socket = accept(socket_, NULL, NULL); |
- } while (socket == -1 && errno == EINTR); |
- |
- if (socket == -1) { |
- return NULL; |
- } else { |
- return new POSIXSocket(socket); |
- } |
-} |
- |
- |
-bool POSIXSocket::Connect(const char* host, const char* port) { |
- if (!IsValid()) { |
- return false; |
- } |
- |
- // Lookup host and port. |
- struct addrinfo *result = NULL; |
- struct addrinfo hints; |
- memset(&hints, 0, sizeof(addrinfo)); |
- hints.ai_family = AF_INET; |
- hints.ai_socktype = SOCK_STREAM; |
- hints.ai_protocol = IPPROTO_TCP; |
- int status = getaddrinfo(host, port, &hints, &result); |
- if (status != 0) { |
- return false; |
- } |
- |
- // Connect. |
- do { |
- status = connect(socket_, result->ai_addr, result->ai_addrlen); |
- } while (status == -1 && errno == EINTR); |
- freeaddrinfo(result); |
- return status == 0; |
-} |
- |
- |
-bool POSIXSocket::Shutdown() { |
- if (IsValid()) { |
- // Shutdown socket for both read and write. |
- int status = shutdown(socket_, SHUT_RDWR); |
- close(socket_); |
- socket_ = -1; |
- return status == 0; |
- } |
- return true; |
-} |
- |
- |
-int POSIXSocket::Send(const char* data, int len) const { |
- if (len <= 0) return 0; |
- int written = 0; |
- while (written < len) { |
- int status = send(socket_, data + written, len - written, 0); |
- if (status == 0) { |
- break; |
- } else if (status > 0) { |
- written += status; |
- } else if (errno != EINTR) { |
- return 0; |
- } |
- } |
- return written; |
-} |
- |
- |
-int POSIXSocket::Receive(char* data, int len) const { |
- if (len <= 0) return 0; |
- int status; |
- do { |
- status = recv(socket_, data, len, 0); |
- } while (status == -1 && errno == EINTR); |
- return (status < 0) ? 0 : status; |
-} |
- |
- |
-bool POSIXSocket::SetReuseAddress(bool reuse_address) { |
- int on = reuse_address ? 1 : 0; |
- int status = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
- return status == 0; |
-} |
- |
- |
-bool Socket::SetUp() { |
- // Nothing to do on POSIX. |
- return true; |
-} |
- |
- |
-int Socket::LastError() { |
- return errno; |
-} |
- |
- |
-uint16_t Socket::HToN(uint16_t value) { |
- return htons(value); |
-} |
- |
- |
-uint16_t Socket::NToH(uint16_t value) { |
- return ntohs(value); |
-} |
- |
- |
-uint32_t Socket::HToN(uint32_t value) { |
- return htonl(value); |
-} |
- |
- |
-uint32_t Socket::NToH(uint32_t value) { |
- return ntohl(value); |
-} |
- |
- |
-Socket* OS::CreateSocket() { |
- return new POSIXSocket(); |
-} |
- |
- |
} } // namespace v8::internal |