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" |
11 #include "bin/log.h" | 11 #include "bin/log.h" |
12 #include "bin/socket.h" | 12 #include "bin/socket.h" |
13 | 13 |
14 bool Socket::Initialize() { | 14 bool Socket::Initialize() { |
| 15 static bool socket_initialized = false; |
| 16 if (socket_initialized) return true; |
15 int err; | 17 int err; |
16 WSADATA winsock_data; | 18 WSADATA winsock_data; |
17 WORD version_requested = MAKEWORD(1, 0); | 19 WORD version_requested = MAKEWORD(2, 2); |
18 err = WSAStartup(version_requested, &winsock_data); | 20 err = WSAStartup(version_requested, &winsock_data); |
19 if (err != 0) { | 21 if (err == 0) { |
| 22 socket_initialized = true; |
| 23 } else { |
20 Log::PrintErr("Unable to initialize Winsock: %d\n", WSAGetLastError()); | 24 Log::PrintErr("Unable to initialize Winsock: %d\n", WSAGetLastError()); |
21 } | 25 } |
22 return err == 0; | 26 return err == 0; |
23 } | 27 } |
24 | 28 |
25 intptr_t Socket::Available(intptr_t fd) { | 29 intptr_t Socket::Available(intptr_t fd) { |
26 ClientSocket* client_socket = reinterpret_cast<ClientSocket*>(fd); | 30 ClientSocket* client_socket = reinterpret_cast<ClientSocket*>(fd); |
27 return client_socket->Available(); | 31 return client_socket->Available(); |
28 } | 32 } |
29 | 33 |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 if (client_socket != NULL) { | 187 if (client_socket != NULL) { |
184 return reinterpret_cast<intptr_t>(client_socket); | 188 return reinterpret_cast<intptr_t>(client_socket); |
185 } else { | 189 } else { |
186 return -1; | 190 return -1; |
187 } | 191 } |
188 } | 192 } |
189 | 193 |
190 | 194 |
191 const char* Socket::LookupIPv4Address(char* host, OSError** os_error) { | 195 const char* Socket::LookupIPv4Address(char* host, OSError** os_error) { |
192 // Perform a name lookup for an IPv4 address. | 196 // Perform a name lookup for an IPv4 address. |
| 197 Initialize(); |
193 struct addrinfo hints; | 198 struct addrinfo hints; |
194 memset(&hints, 0, sizeof(hints)); | 199 memset(&hints, 0, sizeof(hints)); |
195 hints.ai_family = AF_INET; | 200 hints.ai_family = AF_INET; |
196 hints.ai_socktype = SOCK_STREAM; | 201 hints.ai_socktype = SOCK_STREAM; |
197 hints.ai_protocol = IPPROTO_TCP; | 202 hints.ai_protocol = IPPROTO_TCP; |
198 struct addrinfo* info = NULL; | 203 struct addrinfo* info = NULL; |
199 int status = getaddrinfo(host, 0, &hints, &info); | 204 int status = getaddrinfo(host, 0, &hints, &info); |
200 if (status != 0) { | 205 if (status != 0) { |
201 ASSERT(*os_error == NULL); | 206 ASSERT(*os_error == NULL); |
202 DWORD error_code = WSAGetLastError(); | 207 DWORD error_code = WSAGetLastError(); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 status = bind(s, | 263 status = bind(s, |
259 reinterpret_cast<struct sockaddr *>(&addr), | 264 reinterpret_cast<struct sockaddr *>(&addr), |
260 sizeof(addr)); | 265 sizeof(addr)); |
261 if (status == SOCKET_ERROR) { | 266 if (status == SOCKET_ERROR) { |
262 DWORD rc = WSAGetLastError(); | 267 DWORD rc = WSAGetLastError(); |
263 closesocket(s); | 268 closesocket(s); |
264 SetLastError(rc); | 269 SetLastError(rc); |
265 return -1; | 270 return -1; |
266 } | 271 } |
267 | 272 |
268 status = listen(s, backlog); | 273 status = listen(s, backlog > 0 ? backlog : SOMAXCONN); |
269 if (status == SOCKET_ERROR) { | 274 if (status == SOCKET_ERROR) { |
270 DWORD rc = WSAGetLastError(); | 275 DWORD rc = WSAGetLastError(); |
271 closesocket(s); | 276 closesocket(s); |
272 SetLastError(rc); | 277 SetLastError(rc); |
273 return -1; | 278 return -1; |
274 } | 279 } |
275 | 280 |
276 ListenSocket* listen_socket = new ListenSocket(s); | 281 ListenSocket* listen_socket = new ListenSocket(s); |
277 return reinterpret_cast<intptr_t>(listen_socket); | 282 return reinterpret_cast<intptr_t>(listen_socket); |
278 } | 283 } |
279 | 284 |
280 | 285 |
281 void Socket::Close(intptr_t fd) { | 286 void Socket::Close(intptr_t fd) { |
282 ClientSocket* client_socket = reinterpret_cast<ClientSocket*>(fd); | 287 ClientSocket* client_socket = reinterpret_cast<ClientSocket*>(fd); |
283 client_socket->close(); | 288 client_socket->Close(); |
284 } | 289 } |
285 | 290 |
286 #endif // defined(TARGET_OS_WINDOWS) | 291 #endif // defined(TARGET_OS_WINDOWS) |
OLD | NEW |