| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 "bin/builtin.h" | 5 #include "bin/builtin.h" |
| 6 #include "bin/eventhandler.h" | 6 #include "bin/eventhandler.h" |
| 7 #include "bin/log.h" |
| 7 #include "bin/socket.h" | 8 #include "bin/socket.h" |
| 8 | 9 |
| 9 bool Socket::Initialize() { | 10 bool Socket::Initialize() { |
| 10 int err; | 11 int err; |
| 11 WSADATA winsock_data; | 12 WSADATA winsock_data; |
| 12 WORD version_requested = MAKEWORD(1, 0); | 13 WORD version_requested = MAKEWORD(1, 0); |
| 13 err = WSAStartup(version_requested, &winsock_data); | 14 err = WSAStartup(version_requested, &winsock_data); |
| 14 if (err != 0) { | 15 if (err != 0) { |
| 15 fprintf(stderr, "Unable to initialize Winsock: %d\n", WSAGetLastError()); | 16 Log::PrintErr("Unable to initialize Winsock: %d\n", WSAGetLastError()); |
| 16 } | 17 } |
| 17 return err == 0; | 18 return err == 0; |
| 18 } | 19 } |
| 19 | 20 |
| 20 intptr_t Socket::Available(intptr_t fd) { | 21 intptr_t Socket::Available(intptr_t fd) { |
| 21 ClientSocket* client_socket = reinterpret_cast<ClientSocket*>(fd); | 22 ClientSocket* client_socket = reinterpret_cast<ClientSocket*>(fd); |
| 22 return client_socket->Available(); | 23 return client_socket->Available(); |
| 23 } | 24 } |
| 24 | 25 |
| 25 | 26 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 36 | 37 |
| 37 | 38 |
| 38 intptr_t Socket::GetPort(intptr_t fd) { | 39 intptr_t Socket::GetPort(intptr_t fd) { |
| 39 ASSERT(reinterpret_cast<Handle*>(fd)->is_socket()); | 40 ASSERT(reinterpret_cast<Handle*>(fd)->is_socket()); |
| 40 SocketHandle* socket_handle = reinterpret_cast<SocketHandle*>(fd); | 41 SocketHandle* socket_handle = reinterpret_cast<SocketHandle*>(fd); |
| 41 struct sockaddr_in socket_address; | 42 struct sockaddr_in socket_address; |
| 42 socklen_t size = sizeof(socket_address); | 43 socklen_t size = sizeof(socket_address); |
| 43 if (getsockname(socket_handle->socket(), | 44 if (getsockname(socket_handle->socket(), |
| 44 reinterpret_cast<struct sockaddr *>(&socket_address), | 45 reinterpret_cast<struct sockaddr *>(&socket_address), |
| 45 &size)) { | 46 &size)) { |
| 46 fprintf(stderr, "Error getsockname: %s\n", strerror(errno)); | 47 Log::PrintErr("Error getsockname: %s\n", strerror(errno)); |
| 47 return 0; | 48 return 0; |
| 48 } | 49 } |
| 49 return ntohs(socket_address.sin_port); | 50 return ntohs(socket_address.sin_port); |
| 50 } | 51 } |
| 51 | 52 |
| 52 | 53 |
| 53 bool Socket::GetRemotePeer(intptr_t fd, char *host, intptr_t *port) { | 54 bool Socket::GetRemotePeer(intptr_t fd, char *host, intptr_t *port) { |
| 54 ASSERT(reinterpret_cast<Handle*>(fd)->is_socket()); | 55 ASSERT(reinterpret_cast<Handle*>(fd)->is_socket()); |
| 55 SocketHandle* socket_handle = reinterpret_cast<SocketHandle*>(fd); | 56 SocketHandle* socket_handle = reinterpret_cast<SocketHandle*>(fd); |
| 56 struct sockaddr_in socket_address; | 57 struct sockaddr_in socket_address; |
| 57 socklen_t size = sizeof(socket_address); | 58 socklen_t size = sizeof(socket_address); |
| 58 if (getpeername(socket_handle->socket(), | 59 if (getpeername(socket_handle->socket(), |
| 59 reinterpret_cast<struct sockaddr *>(&socket_address), | 60 reinterpret_cast<struct sockaddr *>(&socket_address), |
| 60 &size)) { | 61 &size)) { |
| 61 fprintf(stderr, "Error getpeername: %s\n", strerror(errno)); | 62 Log::PrintErr("Error getpeername: %s\n", strerror(errno)); |
| 62 return false; | 63 return false; |
| 63 } | 64 } |
| 64 *port = ntohs(socket_address.sin_port); | 65 *port = ntohs(socket_address.sin_port); |
| 65 // Clear the port before calling WSAAddressToString as WSAAddressToString | 66 // Clear the port before calling WSAAddressToString as WSAAddressToString |
| 66 // includes the port in the formatted string. | 67 // includes the port in the formatted string. |
| 67 socket_address.sin_port = 0; | 68 socket_address.sin_port = 0; |
| 68 DWORD len = INET_ADDRSTRLEN; | 69 DWORD len = INET_ADDRSTRLEN; |
| 69 int err = WSAAddressToString(reinterpret_cast<LPSOCKADDR>(&socket_address), | 70 int err = WSAAddressToString(reinterpret_cast<LPSOCKADDR>(&socket_address), |
| 70 sizeof(socket_address), | 71 sizeof(socket_address), |
| 71 NULL, | 72 NULL, |
| 72 host, | 73 host, |
| 73 &len); | 74 &len); |
| 74 if (err != 0) { | 75 if (err != 0) { |
| 75 fprintf(stderr, "Error WSAAddressToString: %d\n", WSAGetLastError()); | 76 Log::PrintErr("Error WSAAddressToString: %d\n", WSAGetLastError()); |
| 76 return false; | 77 return false; |
| 77 } | 78 } |
| 78 return true; | 79 return true; |
| 79 } | 80 } |
| 80 | 81 |
| 81 intptr_t Socket::CreateConnect(const char* host, const intptr_t port) { | 82 intptr_t Socket::CreateConnect(const char* host, const intptr_t port) { |
| 82 SOCKET s = socket(AF_INET, SOCK_STREAM, 0); | 83 SOCKET s = socket(AF_INET, SOCK_STREAM, 0); |
| 83 if (s == INVALID_SOCKET) { | 84 if (s == INVALID_SOCKET) { |
| 84 return -1; | 85 return -1; |
| 85 } | 86 } |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 if (status == SOCKET_ERROR) { | 254 if (status == SOCKET_ERROR) { |
| 254 DWORD rc = WSAGetLastError(); | 255 DWORD rc = WSAGetLastError(); |
| 255 closesocket(s); | 256 closesocket(s); |
| 256 SetLastError(rc); | 257 SetLastError(rc); |
| 257 return -1; | 258 return -1; |
| 258 } | 259 } |
| 259 | 260 |
| 260 ListenSocket* listen_socket = new ListenSocket(s); | 261 ListenSocket* listen_socket = new ListenSocket(s); |
| 261 return reinterpret_cast<intptr_t>(listen_socket); | 262 return reinterpret_cast<intptr_t>(listen_socket); |
| 262 } | 263 } |
| OLD | NEW |