| 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 #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/builtin.h" | 10 #include "bin/builtin.h" |
| 11 #include "bin/eventhandler.h" | 11 #include "bin/eventhandler.h" |
| 12 #include "bin/file.h" | 12 #include "bin/file.h" |
| 13 #include "bin/lockers.h" | 13 #include "bin/lockers.h" |
| 14 #include "bin/log.h" | 14 #include "bin/log.h" |
| 15 #include "bin/socket.h" | 15 #include "bin/socket.h" |
| 16 #include "bin/socket_base_win.h" | 16 #include "bin/socket_base_win.h" |
| 17 #include "bin/thread.h" | 17 #include "bin/thread.h" |
| 18 #include "bin/utils.h" | 18 #include "bin/utils.h" |
| 19 #include "bin/utils_win.h" | 19 #include "bin/utils_win.h" |
| 20 | 20 |
| 21 namespace dart { | 21 namespace dart { |
| 22 namespace bin { | 22 namespace bin { |
| 23 | 23 |
| 24 Socket::Socket(intptr_t fd) : ReferenceCounted(), fd_(fd), port_(ILLEGAL_PORT) { | 24 Socket::Socket(intptr_t fd) : ReferenceCounted(), fd_(fd), port_(ILLEGAL_PORT) { |
| 25 ASSERT(fd_ != kClosedFd); | 25 ASSERT(fd_ != kClosedFd); |
| 26 Handle* handle = reinterpret_cast<Handle*>(fd_); | 26 Handle* handle = reinterpret_cast<Handle*>(fd_); |
| 27 ASSERT(handle != NULL); | 27 ASSERT(handle != NULL); |
| 28 } | 28 } |
| 29 | 29 |
| 30 | |
| 31 void Socket::SetClosedFd() { | 30 void Socket::SetClosedFd() { |
| 32 ASSERT(fd_ != kClosedFd); | 31 ASSERT(fd_ != kClosedFd); |
| 33 Handle* handle = reinterpret_cast<Handle*>(fd_); | 32 Handle* handle = reinterpret_cast<Handle*>(fd_); |
| 34 ASSERT(handle != NULL); | 33 ASSERT(handle != NULL); |
| 35 handle->Release(); | 34 handle->Release(); |
| 36 fd_ = kClosedFd; | 35 fd_ = kClosedFd; |
| 37 } | 36 } |
| 38 | 37 |
| 39 | |
| 40 static intptr_t Create(const RawAddr& addr) { | 38 static intptr_t Create(const RawAddr& addr) { |
| 41 SOCKET s = socket(addr.ss.ss_family, SOCK_STREAM, 0); | 39 SOCKET s = socket(addr.ss.ss_family, SOCK_STREAM, 0); |
| 42 if (s == INVALID_SOCKET) { | 40 if (s == INVALID_SOCKET) { |
| 43 return -1; | 41 return -1; |
| 44 } | 42 } |
| 45 | 43 |
| 46 linger l; | 44 linger l; |
| 47 l.l_onoff = 1; | 45 l.l_onoff = 1; |
| 48 l.l_linger = 10; | 46 l.l_linger = 10; |
| 49 int status = setsockopt(s, SOL_SOCKET, SO_LINGER, reinterpret_cast<char*>(&l), | 47 int status = setsockopt(s, SOL_SOCKET, SO_LINGER, reinterpret_cast<char*>(&l), |
| 50 sizeof(l)); | 48 sizeof(l)); |
| 51 if (status != NO_ERROR) { | 49 if (status != NO_ERROR) { |
| 52 FATAL("Failed setting SO_LINGER on socket"); | 50 FATAL("Failed setting SO_LINGER on socket"); |
| 53 } | 51 } |
| 54 | 52 |
| 55 ClientSocket* client_socket = new ClientSocket(s); | 53 ClientSocket* client_socket = new ClientSocket(s); |
| 56 return reinterpret_cast<intptr_t>(client_socket); | 54 return reinterpret_cast<intptr_t>(client_socket); |
| 57 } | 55 } |
| 58 | 56 |
| 59 | |
| 60 static intptr_t Connect(intptr_t fd, | 57 static intptr_t Connect(intptr_t fd, |
| 61 const RawAddr& addr, | 58 const RawAddr& addr, |
| 62 const RawAddr& bind_addr) { | 59 const RawAddr& bind_addr) { |
| 63 ASSERT(reinterpret_cast<Handle*>(fd)->is_client_socket()); | 60 ASSERT(reinterpret_cast<Handle*>(fd)->is_client_socket()); |
| 64 ClientSocket* handle = reinterpret_cast<ClientSocket*>(fd); | 61 ClientSocket* handle = reinterpret_cast<ClientSocket*>(fd); |
| 65 SOCKET s = handle->socket(); | 62 SOCKET s = handle->socket(); |
| 66 | 63 |
| 67 int status = | 64 int status = |
| 68 bind(s, &bind_addr.addr, SocketAddress::GetAddrLength(bind_addr)); | 65 bind(s, &bind_addr.addr, SocketAddress::GetAddrLength(bind_addr)); |
| 69 if (status != NO_ERROR) { | 66 if (status != NO_ERROR) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 83 &bytes, NULL, NULL); | 80 &bytes, NULL, NULL); |
| 84 DWORD rc; | 81 DWORD rc; |
| 85 if (status != SOCKET_ERROR) { | 82 if (status != SOCKET_ERROR) { |
| 86 handle->EnsureInitialized(EventHandler::delegate()); | 83 handle->EnsureInitialized(EventHandler::delegate()); |
| 87 | 84 |
| 88 OverlappedBuffer* overlapped = OverlappedBuffer::AllocateConnectBuffer(); | 85 OverlappedBuffer* overlapped = OverlappedBuffer::AllocateConnectBuffer(); |
| 89 | 86 |
| 90 status = connectEx(s, &addr.addr, SocketAddress::GetAddrLength(addr), NULL, | 87 status = connectEx(s, &addr.addr, SocketAddress::GetAddrLength(addr), NULL, |
| 91 0, NULL, overlapped->GetCleanOverlapped()); | 88 0, NULL, overlapped->GetCleanOverlapped()); |
| 92 | 89 |
| 93 | |
| 94 if (status == TRUE) { | 90 if (status == TRUE) { |
| 95 handle->ConnectComplete(overlapped); | 91 handle->ConnectComplete(overlapped); |
| 96 return fd; | 92 return fd; |
| 97 } else if (WSAGetLastError() == ERROR_IO_PENDING) { | 93 } else if (WSAGetLastError() == ERROR_IO_PENDING) { |
| 98 return fd; | 94 return fd; |
| 99 } | 95 } |
| 100 rc = WSAGetLastError(); | 96 rc = WSAGetLastError(); |
| 101 // Cleanup in case of error. | 97 // Cleanup in case of error. |
| 102 OverlappedBuffer::DisposeBuffer(overlapped); | 98 OverlappedBuffer::DisposeBuffer(overlapped); |
| 103 handle->Release(); | 99 handle->Release(); |
| 104 } else { | 100 } else { |
| 105 rc = WSAGetLastError(); | 101 rc = WSAGetLastError(); |
| 106 } | 102 } |
| 107 handle->Close(); | 103 handle->Close(); |
| 108 handle->Release(); | 104 handle->Release(); |
| 109 SetLastError(rc); | 105 SetLastError(rc); |
| 110 return -1; | 106 return -1; |
| 111 } | 107 } |
| 112 | 108 |
| 113 | |
| 114 intptr_t Socket::CreateConnect(const RawAddr& addr) { | 109 intptr_t Socket::CreateConnect(const RawAddr& addr) { |
| 115 intptr_t fd = Create(addr); | 110 intptr_t fd = Create(addr); |
| 116 if (fd < 0) { | 111 if (fd < 0) { |
| 117 return fd; | 112 return fd; |
| 118 } | 113 } |
| 119 | 114 |
| 120 RawAddr bind_addr; | 115 RawAddr bind_addr; |
| 121 memset(&bind_addr, 0, sizeof(bind_addr)); | 116 memset(&bind_addr, 0, sizeof(bind_addr)); |
| 122 bind_addr.ss.ss_family = addr.ss.ss_family; | 117 bind_addr.ss.ss_family = addr.ss.ss_family; |
| 123 if (addr.ss.ss_family == AF_INET) { | 118 if (addr.ss.ss_family == AF_INET) { |
| 124 bind_addr.in.sin_addr.s_addr = INADDR_ANY; | 119 bind_addr.in.sin_addr.s_addr = INADDR_ANY; |
| 125 } else { | 120 } else { |
| 126 bind_addr.in6.sin6_addr = in6addr_any; | 121 bind_addr.in6.sin6_addr = in6addr_any; |
| 127 } | 122 } |
| 128 | 123 |
| 129 return Connect(fd, addr, bind_addr); | 124 return Connect(fd, addr, bind_addr); |
| 130 } | 125 } |
| 131 | 126 |
| 132 | |
| 133 intptr_t Socket::CreateBindConnect(const RawAddr& addr, | 127 intptr_t Socket::CreateBindConnect(const RawAddr& addr, |
| 134 const RawAddr& source_addr) { | 128 const RawAddr& source_addr) { |
| 135 intptr_t fd = Create(addr); | 129 intptr_t fd = Create(addr); |
| 136 if (fd < 0) { | 130 if (fd < 0) { |
| 137 return fd; | 131 return fd; |
| 138 } | 132 } |
| 139 | 133 |
| 140 return Connect(fd, addr, source_addr); | 134 return Connect(fd, addr, source_addr); |
| 141 } | 135 } |
| 142 | 136 |
| 143 | |
| 144 intptr_t ServerSocket::Accept(intptr_t fd) { | 137 intptr_t ServerSocket::Accept(intptr_t fd) { |
| 145 ListenSocket* listen_socket = reinterpret_cast<ListenSocket*>(fd); | 138 ListenSocket* listen_socket = reinterpret_cast<ListenSocket*>(fd); |
| 146 ClientSocket* client_socket = listen_socket->Accept(); | 139 ClientSocket* client_socket = listen_socket->Accept(); |
| 147 if (client_socket != NULL) { | 140 if (client_socket != NULL) { |
| 148 return reinterpret_cast<intptr_t>(client_socket); | 141 return reinterpret_cast<intptr_t>(client_socket); |
| 149 } else { | 142 } else { |
| 150 return -1; | 143 return -1; |
| 151 } | 144 } |
| 152 } | 145 } |
| 153 | 146 |
| 154 | |
| 155 intptr_t Socket::CreateBindDatagram(const RawAddr& addr, bool reuseAddress) { | 147 intptr_t Socket::CreateBindDatagram(const RawAddr& addr, bool reuseAddress) { |
| 156 SOCKET s = socket(addr.ss.ss_family, SOCK_DGRAM, IPPROTO_UDP); | 148 SOCKET s = socket(addr.ss.ss_family, SOCK_DGRAM, IPPROTO_UDP); |
| 157 if (s == INVALID_SOCKET) { | 149 if (s == INVALID_SOCKET) { |
| 158 return -1; | 150 return -1; |
| 159 } | 151 } |
| 160 | 152 |
| 161 int status; | 153 int status; |
| 162 if (reuseAddress) { | 154 if (reuseAddress) { |
| 163 BOOL optval = true; | 155 BOOL optval = true; |
| 164 status = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, | 156 status = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 177 closesocket(s); | 169 closesocket(s); |
| 178 SetLastError(rc); | 170 SetLastError(rc); |
| 179 return -1; | 171 return -1; |
| 180 } | 172 } |
| 181 | 173 |
| 182 DatagramSocket* datagram_socket = new DatagramSocket(s); | 174 DatagramSocket* datagram_socket = new DatagramSocket(s); |
| 183 datagram_socket->EnsureInitialized(EventHandler::delegate()); | 175 datagram_socket->EnsureInitialized(EventHandler::delegate()); |
| 184 return reinterpret_cast<intptr_t>(datagram_socket); | 176 return reinterpret_cast<intptr_t>(datagram_socket); |
| 185 } | 177 } |
| 186 | 178 |
| 187 | |
| 188 intptr_t ServerSocket::CreateBindListen(const RawAddr& addr, | 179 intptr_t ServerSocket::CreateBindListen(const RawAddr& addr, |
| 189 intptr_t backlog, | 180 intptr_t backlog, |
| 190 bool v6_only) { | 181 bool v6_only) { |
| 191 SOCKET s = socket(addr.ss.ss_family, SOCK_STREAM, IPPROTO_TCP); | 182 SOCKET s = socket(addr.ss.ss_family, SOCK_STREAM, IPPROTO_TCP); |
| 192 if (s == INVALID_SOCKET) { | 183 if (s == INVALID_SOCKET) { |
| 193 return -1; | 184 return -1; |
| 194 } | 185 } |
| 195 | 186 |
| 196 BOOL optval = true; | 187 BOOL optval = true; |
| 197 int status = | 188 int status = |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 DWORD rc = WSAGetLastError(); | 230 DWORD rc = WSAGetLastError(); |
| 240 closesocket(s); | 231 closesocket(s); |
| 241 listen_socket->Release(); | 232 listen_socket->Release(); |
| 242 SetLastError(rc); | 233 SetLastError(rc); |
| 243 return -1; | 234 return -1; |
| 244 } | 235 } |
| 245 | 236 |
| 246 return reinterpret_cast<intptr_t>(listen_socket); | 237 return reinterpret_cast<intptr_t>(listen_socket); |
| 247 } | 238 } |
| 248 | 239 |
| 249 | |
| 250 bool ServerSocket::StartAccept(intptr_t fd) { | 240 bool ServerSocket::StartAccept(intptr_t fd) { |
| 251 ListenSocket* listen_socket = reinterpret_cast<ListenSocket*>(fd); | 241 ListenSocket* listen_socket = reinterpret_cast<ListenSocket*>(fd); |
| 252 listen_socket->EnsureInitialized(EventHandler::delegate()); | 242 listen_socket->EnsureInitialized(EventHandler::delegate()); |
| 253 // Always keep 5 outstanding accepts going, to enhance performance. | 243 // Always keep 5 outstanding accepts going, to enhance performance. |
| 254 for (int i = 0; i < 5; i++) { | 244 for (int i = 0; i < 5; i++) { |
| 255 if (!listen_socket->IssueAccept()) { | 245 if (!listen_socket->IssueAccept()) { |
| 256 DWORD rc = WSAGetLastError(); | 246 DWORD rc = WSAGetLastError(); |
| 257 listen_socket->Close(); | 247 listen_socket->Close(); |
| 258 if (!listen_socket->HasPendingAccept()) { | 248 if (!listen_socket->HasPendingAccept()) { |
| 259 // Delete socket now, if there are no pending accepts. Otherwise, | 249 // Delete socket now, if there are no pending accepts. Otherwise, |
| 260 // the event-handler will take care of deleting it. | 250 // the event-handler will take care of deleting it. |
| 261 listen_socket->Release(); | 251 listen_socket->Release(); |
| 262 } | 252 } |
| 263 SetLastError(rc); | 253 SetLastError(rc); |
| 264 return false; | 254 return false; |
| 265 } | 255 } |
| 266 } | 256 } |
| 267 return true; | 257 return true; |
| 268 } | 258 } |
| 269 | 259 |
| 270 } // namespace bin | 260 } // namespace bin |
| 271 } // namespace dart | 261 } // namespace dart |
| 272 | 262 |
| 273 #endif // defined(HOST_OS_WINDOWS) | 263 #endif // defined(HOST_OS_WINDOWS) |
| 274 | 264 |
| 275 #endif // !defined(DART_IO_DISABLED) | 265 #endif // !defined(DART_IO_DISABLED) |
| OLD | NEW |