| 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 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 if (status != NO_ERROR) { | 148 if (status != NO_ERROR) { |
| 149 FATAL("Failed setting SO_LINGER on socket"); | 149 FATAL("Failed setting SO_LINGER on socket"); |
| 150 } | 150 } |
| 151 | 151 |
| 152 ClientSocket* client_socket = new ClientSocket(s); | 152 ClientSocket* client_socket = new ClientSocket(s); |
| 153 return reinterpret_cast<intptr_t>(client_socket); | 153 return reinterpret_cast<intptr_t>(client_socket); |
| 154 } | 154 } |
| 155 | 155 |
| 156 | 156 |
| 157 intptr_t Socket::Connect(intptr_t fd, RawAddr addr, const intptr_t port) { | 157 intptr_t Socket::Connect(intptr_t fd, RawAddr addr, const intptr_t port) { |
| 158 ASSERT(reinterpret_cast<Handle*>(fd)->is_socket()); | 158 ASSERT(reinterpret_cast<Handle*>(fd)->is_client_socket()); |
| 159 SocketHandle* handle = reinterpret_cast<SocketHandle*>(fd); | 159 ClientSocket* handle = reinterpret_cast<ClientSocket*>(fd); |
| 160 SOCKET s = handle->socket(); | 160 SOCKET s = handle->socket(); |
| 161 SocketAddress::SetAddrPort(&addr, port); | 161 |
| 162 int status = connect(s, &addr.addr, SocketAddress::GetAddrLength(&addr)); | 162 RawAddr bind_addr; |
| 163 if (status == SOCKET_ERROR) { | 163 memset(&bind_addr, 0, sizeof(bind_addr)); |
| 164 DWORD rc = WSAGetLastError(); | 164 bind_addr.ss.ss_family = addr.ss.ss_family; |
| 165 ClientSocket* client_socket = reinterpret_cast<ClientSocket*>(fd); | 165 if (addr.ss.ss_family == AF_INET) { |
| 166 client_socket->Close(); | 166 bind_addr.in.sin_addr.s_addr = INADDR_ANY; |
| 167 } else { |
| 168 bind_addr.in6.sin6_addr = in6addr_any; |
| 169 } |
| 170 int status = bind( |
| 171 s, &bind_addr.addr, SocketAddress::GetAddrLength(&bind_addr)); |
| 172 if (status != NO_ERROR) { |
| 173 int rc = WSAGetLastError(); |
| 174 delete handle; |
| 175 closesocket(s); |
| 167 SetLastError(rc); | 176 SetLastError(rc); |
| 168 return -1; | 177 return -1; |
| 169 } | 178 } |
| 170 return fd; | 179 |
| 180 SocketAddress::SetAddrPort(&addr, port); |
| 181 |
| 182 LPFN_CONNECTEX connectEx = NULL; |
| 183 GUID guid_connect_ex = WSAID_CONNECTEX; |
| 184 DWORD bytes; |
| 185 status = WSAIoctl(s, |
| 186 SIO_GET_EXTENSION_FUNCTION_POINTER, |
| 187 &guid_connect_ex, |
| 188 sizeof(guid_connect_ex), |
| 189 &connectEx, |
| 190 sizeof(connectEx), |
| 191 &bytes, |
| 192 NULL, |
| 193 NULL); |
| 194 DWORD rc; |
| 195 if (status != SOCKET_ERROR) { |
| 196 handle->EnsureInitialized(EventHandler::delegate()); |
| 197 |
| 198 OverlappedBuffer* overlapped = OverlappedBuffer::AllocateConnectBuffer(); |
| 199 |
| 200 status = connectEx(s, |
| 201 &addr.addr, |
| 202 SocketAddress::GetAddrLength(&addr), |
| 203 NULL, |
| 204 0, |
| 205 NULL, |
| 206 overlapped->GetCleanOverlapped()); |
| 207 |
| 208 |
| 209 if (status == TRUE) { |
| 210 handle->ConnectComplete(overlapped); |
| 211 return fd; |
| 212 } else if (WSAGetLastError() == ERROR_IO_PENDING) { |
| 213 return fd; |
| 214 } |
| 215 rc = WSAGetLastError(); |
| 216 // Cleanup in case of error. |
| 217 OverlappedBuffer::DisposeBuffer(overlapped); |
| 218 } else { |
| 219 rc = WSAGetLastError(); |
| 220 } |
| 221 handle->Close(); |
| 222 delete handle; |
| 223 SetLastError(rc); |
| 224 return -1; |
| 171 } | 225 } |
| 172 | 226 |
| 173 | 227 |
| 174 intptr_t Socket::CreateConnect(RawAddr addr, const intptr_t port) { | 228 intptr_t Socket::CreateConnect(RawAddr addr, const intptr_t port) { |
| 175 intptr_t fd = Socket::Create(addr); | 229 intptr_t fd = Socket::Create(addr); |
| 176 if (fd < 0) { | 230 if (fd < 0) { |
| 177 return fd; | 231 return fd; |
| 178 } | 232 } |
| 179 | 233 |
| 180 return Socket::Connect(fd, addr, port); | 234 return Socket::Connect(fd, addr, port); |
| (...skipping 465 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 646 proto, | 700 proto, |
| 647 MCAST_LEAVE_GROUP, | 701 MCAST_LEAVE_GROUP, |
| 648 reinterpret_cast<char *>(&mreq), | 702 reinterpret_cast<char *>(&mreq), |
| 649 sizeof(mreq)) == 0; | 703 sizeof(mreq)) == 0; |
| 650 } | 704 } |
| 651 | 705 |
| 652 } // namespace bin | 706 } // namespace bin |
| 653 } // namespace dart | 707 } // namespace dart |
| 654 | 708 |
| 655 #endif // defined(TARGET_OS_WINDOWS) | 709 #endif // defined(TARGET_OS_WINDOWS) |
| OLD | NEW |