Index: src/platform-win32.cc |
diff --git a/src/platform-win32.cc b/src/platform-win32.cc |
index 199ed2da4442ae48a11adf52f8077dd8f795085b..87387e7d12246aa2065c8bf35108bcee3dea8633 100644 |
--- a/src/platform-win32.cc |
+++ b/src/platform-win32.cc |
@@ -1615,198 +1615,6 @@ void Thread::YieldCPU() { |
} |
-// ---------------------------------------------------------------------------- |
-// Win32 socket support. |
-// |
- |
-class Win32Socket : public Socket { |
- public: |
- explicit Win32Socket() { |
- // Create the socket. |
- socket_ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
- } |
- explicit Win32Socket(SOCKET socket): socket_(socket) { } |
- virtual ~Win32Socket() { 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_ != INVALID_SOCKET; } |
- |
- private: |
- SOCKET socket_; |
-}; |
- |
- |
-bool Win32Socket::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_, |
- reinterpret_cast<struct sockaddr *>(&addr), |
- sizeof(addr)); |
- return status == 0; |
-} |
- |
- |
-bool Win32Socket::Listen(int backlog) const { |
- if (!IsValid()) { |
- return false; |
- } |
- |
- int status = listen(socket_, backlog); |
- return status == 0; |
-} |
- |
- |
-Socket* Win32Socket::Accept() const { |
- if (!IsValid()) { |
- return NULL; |
- } |
- |
- SOCKET socket = accept(socket_, NULL, NULL); |
- if (socket == INVALID_SOCKET) { |
- return NULL; |
- } else { |
- return new Win32Socket(socket); |
- } |
-} |
- |
- |
-bool Win32Socket::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. |
- status = connect(socket_, |
- result->ai_addr, |
- static_cast<int>(result->ai_addrlen)); |
- freeaddrinfo(result); |
- return status == 0; |
-} |
- |
- |
-bool Win32Socket::Shutdown() { |
- if (IsValid()) { |
- // Shutdown socket for both read and write. |
- int status = shutdown(socket_, SD_BOTH); |
- closesocket(socket_); |
- socket_ = INVALID_SOCKET; |
- return status == SOCKET_ERROR; |
- } |
- return true; |
-} |
- |
- |
-int Win32Socket::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 { |
- return 0; |
- } |
- } |
- return written; |
-} |
- |
- |
-int Win32Socket::Receive(char* data, int len) const { |
- if (len <= 0) return 0; |
- int status = recv(socket_, data, len, 0); |
- return (status == SOCKET_ERROR) ? 0 : status; |
-} |
- |
- |
-bool Win32Socket::SetReuseAddress(bool reuse_address) { |
- BOOL on = reuse_address ? true : false; |
- int status = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, |
- reinterpret_cast<char*>(&on), sizeof(on)); |
- return status == SOCKET_ERROR; |
-} |
- |
- |
-bool Socket::SetUp() { |
- // Initialize Winsock32 |
- int err; |
- WSADATA winsock_data; |
- WORD version_requested = MAKEWORD(1, 0); |
- err = WSAStartup(version_requested, &winsock_data); |
- if (err != 0) { |
- PrintF("Unable to initialize Winsock, err = %d\n", Socket::LastError()); |
- } |
- |
- return err == 0; |
-} |
- |
- |
-int Socket::LastError() { |
- return WSAGetLastError(); |
-} |
- |
- |
-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 Win32Socket(); |
-} |
- |
- |
void OS::SetUp() { |
// Seed the random number generator. |
// Convert the current time to a 64-bit integer first, before converting it |