| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "platform/globals.h" | 5 #include "platform/globals.h" |
| 6 #if defined(TARGET_OS_WINDOWS) | 6 #if defined(TARGET_OS_WINDOWS) |
| 7 | 7 |
| 8 #include "bin/builtin.h" | 8 #include "bin/builtin.h" |
| 9 #include "bin/eventhandler.h" | 9 #include "bin/eventhandler.h" |
| 10 #include "bin/file.h" | 10 #include "bin/file.h" |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 socklen_t salen = SocketAddress::GetAddrLength(addr); | 39 socklen_t salen = SocketAddress::GetAddrLength(addr); |
| 40 DWORD l = len; | 40 DWORD l = len; |
| 41 return WSAAddressToStringA(&addr->addr, | 41 return WSAAddressToStringA(&addr->addr, |
| 42 salen, | 42 salen, |
| 43 NULL, | 43 NULL, |
| 44 address, | 44 address, |
| 45 &l) != 0; | 45 &l) != 0; |
| 46 } | 46 } |
| 47 | 47 |
| 48 | 48 |
| 49 static Mutex* init_mutex = new Mutex(); |
| 50 static bool socket_initialized = false; |
| 51 |
| 49 bool Socket::Initialize() { | 52 bool Socket::Initialize() { |
| 50 static bool socket_initialized = false; | 53 MutexLocker lock(init_mutex); |
| 51 if (socket_initialized) return true; | 54 if (socket_initialized) return true; |
| 52 int err; | 55 int err; |
| 53 WSADATA winsock_data; | 56 WSADATA winsock_data; |
| 54 WORD version_requested = MAKEWORD(2, 2); | 57 WORD version_requested = MAKEWORD(2, 2); |
| 55 err = WSAStartup(version_requested, &winsock_data); | 58 err = WSAStartup(version_requested, &winsock_data); |
| 56 if (err == 0) { | 59 if (err == 0) { |
| 57 socket_initialized = true; | 60 socket_initialized = true; |
| 58 } else { | 61 } else { |
| 59 Log::PrintErr("Unable to initialize Winsock: %d\n", WSAGetLastError()); | 62 Log::PrintErr("Unable to initialize Winsock: %d\n", WSAGetLastError()); |
| 60 } | 63 } |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 ListenSocket* listen_socket = reinterpret_cast<ListenSocket*>(fd); | 215 ListenSocket* listen_socket = reinterpret_cast<ListenSocket*>(fd); |
| 213 ClientSocket* client_socket = listen_socket->Accept(); | 216 ClientSocket* client_socket = listen_socket->Accept(); |
| 214 if (client_socket != NULL) { | 217 if (client_socket != NULL) { |
| 215 return reinterpret_cast<intptr_t>(client_socket); | 218 return reinterpret_cast<intptr_t>(client_socket); |
| 216 } else { | 219 } else { |
| 217 return -1; | 220 return -1; |
| 218 } | 221 } |
| 219 } | 222 } |
| 220 | 223 |
| 221 | 224 |
| 222 static Mutex* getaddrinfo_mutex = new Mutex(); | |
| 223 AddressList<SocketAddress>* Socket::LookupAddress(const char* host, | 225 AddressList<SocketAddress>* Socket::LookupAddress(const char* host, |
| 224 int type, | 226 int type, |
| 225 OSError** os_error) { | 227 OSError** os_error) { |
| 226 Initialize(); | 228 Initialize(); |
| 227 | 229 |
| 228 // getaddrinfo is not thread-safe on Windows. Use a mutex to get around it. | |
| 229 MutexLocker locker(getaddrinfo_mutex); | |
| 230 | |
| 231 // Perform a name lookup for a host name. | 230 // Perform a name lookup for a host name. |
| 232 struct addrinfo hints; | 231 struct addrinfo hints; |
| 233 memset(&hints, 0, sizeof(hints)); | 232 memset(&hints, 0, sizeof(hints)); |
| 234 hints.ai_family = SocketAddress::FromType(type); | 233 hints.ai_family = SocketAddress::FromType(type); |
| 235 hints.ai_socktype = SOCK_STREAM; | 234 hints.ai_socktype = SOCK_STREAM; |
| 236 hints.ai_flags = AI_ADDRCONFIG; | 235 hints.ai_flags = AI_ADDRCONFIG; |
| 237 hints.ai_protocol = IPPROTO_TCP; | 236 hints.ai_protocol = IPPROTO_TCP; |
| 238 struct addrinfo* info = NULL; | 237 struct addrinfo* info = NULL; |
| 239 int status = getaddrinfo(host, 0, &hints, &info); | 238 int status = getaddrinfo(host, 0, &hints, &info); |
| 240 if (status != 0) { | 239 if (status != 0) { |
| (...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 647 proto, | 646 proto, |
| 648 MCAST_LEAVE_GROUP, | 647 MCAST_LEAVE_GROUP, |
| 649 reinterpret_cast<char *>(&mreq), | 648 reinterpret_cast<char *>(&mreq), |
| 650 sizeof(mreq)) == 0; | 649 sizeof(mreq)) == 0; |
| 651 } | 650 } |
| 652 | 651 |
| 653 } // namespace bin | 652 } // namespace bin |
| 654 } // namespace dart | 653 } // namespace dart |
| 655 | 654 |
| 656 #endif // defined(TARGET_OS_WINDOWS) | 655 #endif // defined(TARGET_OS_WINDOWS) |
| OLD | NEW |