| OLD | NEW | 
|---|
| 1 // Copyright (c) 2017, the Dart project authors.  Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 #if !defined(DART_IO_DISABLED) | 5 #if !defined(DART_IO_DISABLED) | 
| 6 | 6 | 
| 7 #include "platform/globals.h" | 7 #include "platform/globals.h" | 
| 8 #if defined(HOST_OS_WINDOWS) | 8 #if defined(HOST_OS_WINDOWS) | 
| 9 | 9 | 
| 10 #include "bin/socket_base.h" | 10 #include "bin/socket_base.h" | 
| 11 #include "bin/sync_socket.h" | 11 #include "bin/sync_socket.h" | 
| 12 | 12 | 
| 13 namespace dart { | 13 namespace dart { | 
| 14 namespace bin { | 14 namespace bin { | 
| 15 | 15 | 
| 16 bool SynchronousSocket::Initialize() { | 16 bool SynchronousSocket::Initialize() { | 
| 17   return SocketBase::Initialize(); | 17   return SocketBase::Initialize(); | 
| 18 } | 18 } | 
| 19 | 19 | 
| 20 |  | 
| 21 static intptr_t Create(const RawAddr& addr) { | 20 static intptr_t Create(const RawAddr& addr) { | 
| 22   const intptr_t type = SOCK_STREAM; | 21   const intptr_t type = SOCK_STREAM; | 
| 23   SOCKET s = WSASocket(addr.ss.ss_family, type, 0, NULL, 0, 0); | 22   SOCKET s = WSASocket(addr.ss.ss_family, type, 0, NULL, 0, 0); | 
| 24   return (s == INVALID_SOCKET) ? -1 : s; | 23   return (s == INVALID_SOCKET) ? -1 : s; | 
| 25 } | 24 } | 
| 26 | 25 | 
| 27 |  | 
| 28 static intptr_t Connect(intptr_t fd, const RawAddr& addr) { | 26 static intptr_t Connect(intptr_t fd, const RawAddr& addr) { | 
| 29   SOCKET socket = static_cast<SOCKET>(fd); | 27   SOCKET socket = static_cast<SOCKET>(fd); | 
| 30   intptr_t result = | 28   intptr_t result = | 
| 31       connect(socket, &addr.addr, SocketAddress::GetAddrLength(addr)); | 29       connect(socket, &addr.addr, SocketAddress::GetAddrLength(addr)); | 
| 32   return (result == SOCKET_ERROR) ? -1 : socket; | 30   return (result == SOCKET_ERROR) ? -1 : socket; | 
| 33 } | 31 } | 
| 34 | 32 | 
| 35 |  | 
| 36 intptr_t SynchronousSocket::CreateConnect(const RawAddr& addr) { | 33 intptr_t SynchronousSocket::CreateConnect(const RawAddr& addr) { | 
| 37   intptr_t fd = Create(addr); | 34   intptr_t fd = Create(addr); | 
| 38   return (fd < 0) ? fd : Connect(fd, addr); | 35   return (fd < 0) ? fd : Connect(fd, addr); | 
| 39 } | 36 } | 
| 40 | 37 | 
| 41 |  | 
| 42 intptr_t SynchronousSocket::Available(intptr_t fd) { | 38 intptr_t SynchronousSocket::Available(intptr_t fd) { | 
| 43   SOCKET socket = static_cast<SOCKET>(fd); | 39   SOCKET socket = static_cast<SOCKET>(fd); | 
| 44   DWORD available; | 40   DWORD available; | 
| 45   intptr_t result = ioctlsocket(socket, FIONREAD, &available); | 41   intptr_t result = ioctlsocket(socket, FIONREAD, &available); | 
| 46   return (result == SOCKET_ERROR) ? -1 : static_cast<intptr_t>(available); | 42   return (result == SOCKET_ERROR) ? -1 : static_cast<intptr_t>(available); | 
| 47 } | 43 } | 
| 48 | 44 | 
| 49 |  | 
| 50 intptr_t SynchronousSocket::GetPort(intptr_t fd) { | 45 intptr_t SynchronousSocket::GetPort(intptr_t fd) { | 
| 51   SOCKET socket = static_cast<SOCKET>(fd); | 46   SOCKET socket = static_cast<SOCKET>(fd); | 
| 52   RawAddr raw; | 47   RawAddr raw; | 
| 53   socklen_t size = sizeof(raw); | 48   socklen_t size = sizeof(raw); | 
| 54   if (getsockname(socket, &raw.addr, &size) == SOCKET_ERROR) { | 49   if (getsockname(socket, &raw.addr, &size) == SOCKET_ERROR) { | 
| 55     return 0; | 50     return 0; | 
| 56   } | 51   } | 
| 57   return SocketAddress::GetAddrPort(raw); | 52   return SocketAddress::GetAddrPort(raw); | 
| 58 } | 53 } | 
| 59 | 54 | 
| 60 |  | 
| 61 SocketAddress* SynchronousSocket::GetRemotePeer(intptr_t fd, intptr_t* port) { | 55 SocketAddress* SynchronousSocket::GetRemotePeer(intptr_t fd, intptr_t* port) { | 
| 62   SOCKET socket = static_cast<SOCKET>(fd); | 56   SOCKET socket = static_cast<SOCKET>(fd); | 
| 63   RawAddr raw; | 57   RawAddr raw; | 
| 64   socklen_t size = sizeof(raw); | 58   socklen_t size = sizeof(raw); | 
| 65   if (getpeername(socket, &raw.addr, &size)) { | 59   if (getpeername(socket, &raw.addr, &size)) { | 
| 66     return NULL; | 60     return NULL; | 
| 67   } | 61   } | 
| 68   *port = SocketAddress::GetAddrPort(raw); | 62   *port = SocketAddress::GetAddrPort(raw); | 
| 69   // Clear the port before calling WSAAddressToString as WSAAddressToString | 63   // Clear the port before calling WSAAddressToString as WSAAddressToString | 
| 70   // includes the port in the formatted string. | 64   // includes the port in the formatted string. | 
| 71   SocketAddress::SetAddrPort(&raw, 0); | 65   SocketAddress::SetAddrPort(&raw, 0); | 
| 72   return new SocketAddress(&raw.addr); | 66   return new SocketAddress(&raw.addr); | 
| 73 } | 67 } | 
| 74 | 68 | 
| 75 |  | 
| 76 intptr_t SynchronousSocket::Read(intptr_t fd, | 69 intptr_t SynchronousSocket::Read(intptr_t fd, | 
| 77                                  void* buffer, | 70                                  void* buffer, | 
| 78                                  intptr_t num_bytes) { | 71                                  intptr_t num_bytes) { | 
| 79   SOCKET socket = static_cast<SOCKET>(fd); | 72   SOCKET socket = static_cast<SOCKET>(fd); | 
| 80   return recv(socket, reinterpret_cast<char*>(buffer), num_bytes, 0); | 73   return recv(socket, reinterpret_cast<char*>(buffer), num_bytes, 0); | 
| 81 } | 74 } | 
| 82 | 75 | 
| 83 |  | 
| 84 intptr_t SynchronousSocket::Write(intptr_t fd, | 76 intptr_t SynchronousSocket::Write(intptr_t fd, | 
| 85                                   const void* buffer, | 77                                   const void* buffer, | 
| 86                                   intptr_t num_bytes) { | 78                                   intptr_t num_bytes) { | 
| 87   SOCKET socket = static_cast<SOCKET>(fd); | 79   SOCKET socket = static_cast<SOCKET>(fd); | 
| 88   return send(socket, reinterpret_cast<const char*>(buffer), num_bytes, 0); | 80   return send(socket, reinterpret_cast<const char*>(buffer), num_bytes, 0); | 
| 89 } | 81 } | 
| 90 | 82 | 
| 91 |  | 
| 92 void SynchronousSocket::ShutdownRead(intptr_t fd) { | 83 void SynchronousSocket::ShutdownRead(intptr_t fd) { | 
| 93   SOCKET socket = static_cast<SOCKET>(fd); | 84   SOCKET socket = static_cast<SOCKET>(fd); | 
| 94   shutdown(socket, SD_RECEIVE); | 85   shutdown(socket, SD_RECEIVE); | 
| 95 } | 86 } | 
| 96 | 87 | 
| 97 |  | 
| 98 void SynchronousSocket::ShutdownWrite(intptr_t fd) { | 88 void SynchronousSocket::ShutdownWrite(intptr_t fd) { | 
| 99   SOCKET socket = static_cast<SOCKET>(fd); | 89   SOCKET socket = static_cast<SOCKET>(fd); | 
| 100   shutdown(socket, SD_SEND); | 90   shutdown(socket, SD_SEND); | 
| 101 } | 91 } | 
| 102 | 92 | 
| 103 |  | 
| 104 void SynchronousSocket::Close(intptr_t fd) { | 93 void SynchronousSocket::Close(intptr_t fd) { | 
| 105   SOCKET socket = static_cast<SOCKET>(fd); | 94   SOCKET socket = static_cast<SOCKET>(fd); | 
| 106   closesocket(socket); | 95   closesocket(socket); | 
| 107 } | 96 } | 
| 108 | 97 | 
| 109 }  // namespace bin | 98 }  // namespace bin | 
| 110 }  // namespace dart | 99 }  // namespace dart | 
| 111 | 100 | 
| 112 #endif  // defined(HOST_OS_WINDOWS) | 101 #endif  // defined(HOST_OS_WINDOWS) | 
| 113 | 102 | 
| 114 #endif  // !defined(DART_IO_DISABLED) | 103 #endif  // !defined(DART_IO_DISABLED) | 
| OLD | NEW | 
|---|