6#if defined(DART_HOST_OS_WINDOWS)
27 udp_receive_buffer_(nullptr) {
29 Handle* handle =
reinterpret_cast<Handle*
>(fd_);
33void Socket::CloseFd() {
35 Handle* handle =
reinterpret_cast<Handle*
>(fd_);
41void Socket::SetClosedFd() {
46 SOCKET
s = socket(
addr.ss.ss_family, SOCK_STREAM, 0);
47 if (
s == INVALID_SOCKET) {
54 int status = setsockopt(
s, SOL_SOCKET, SO_LINGER,
reinterpret_cast<char*
>(&l),
56 if (status != NO_ERROR) {
57 FATAL(
"Failed setting SO_LINGER on socket");
60 ClientSocket* client_socket =
new ClientSocket(
s);
61 return reinterpret_cast<intptr_t
>(client_socket);
64static intptr_t Connect(intptr_t fd,
66 const RawAddr& bind_addr) {
67 ASSERT(
reinterpret_cast<Handle*
>(fd)->is_client_socket());
68 ClientSocket* handle =
reinterpret_cast<ClientSocket*
>(fd);
69 SOCKET
s = handle->socket();
72 bind(
s, &bind_addr.addr, SocketAddress::GetAddrLength(bind_addr));
73 if (status != NO_ERROR) {
74 int rc = WSAGetLastError();
75 handle->mark_closed();
82 LPFN_CONNECTEX connectEx =
nullptr;
83 GUID guid_connect_ex = WSAID_CONNECTEX;
85 status = WSAIoctl(
s, SIO_GET_EXTENSION_FUNCTION_POINTER, &guid_connect_ex,
86 sizeof(guid_connect_ex), &connectEx,
sizeof(connectEx),
87 &bytes,
nullptr,
nullptr);
89 if (status != SOCKET_ERROR) {
90 handle->EnsureInitialized(EventHandler::delegate());
92 OverlappedBuffer* overlapped = OverlappedBuffer::AllocateConnectBuffer();
94 status = connectEx(
s, &
addr.addr, SocketAddress::GetAddrLength(
addr),
95 nullptr, 0,
nullptr, overlapped->GetCleanOverlapped());
98 handle->ConnectComplete(overlapped);
100 }
else if (WSAGetLastError() == ERROR_IO_PENDING) {
103 rc = WSAGetLastError();
105 OverlappedBuffer::DisposeBuffer(overlapped);
108 rc = WSAGetLastError();
116intptr_t Socket::CreateConnect(
const RawAddr&
addr) {
123 memset(&bind_addr, 0,
sizeof(bind_addr));
124 bind_addr.ss.ss_family =
addr.ss.ss_family;
125 if (
addr.ss.ss_family == AF_INET) {
126 bind_addr.in.sin_addr.s_addr = INADDR_ANY;
128 bind_addr.in6.sin6_addr = in6addr_any;
131 return Connect(fd,
addr, bind_addr);
134intptr_t Socket::CreateUnixDomainConnect(
const RawAddr&
addr) {
141intptr_t Socket::CreateBindConnect(
const RawAddr&
addr,
142 const RawAddr& source_addr) {
148 return Connect(fd,
addr, source_addr);
151intptr_t Socket::CreateUnixDomainBindConnect(
const RawAddr&
addr,
152 const RawAddr& source_addr) {
157intptr_t ServerSocket::Accept(intptr_t fd) {
158 ListenSocket* listen_socket =
reinterpret_cast<ListenSocket*
>(fd);
159 ClientSocket* client_socket = listen_socket->Accept();
160 if (client_socket !=
nullptr) {
161 return reinterpret_cast<intptr_t
>(client_socket);
167intptr_t Socket::CreateBindDatagram(
const RawAddr&
addr,
171 SOCKET
s = socket(
addr.ss.ss_family, SOCK_DGRAM, IPPROTO_UDP);
172 if (
s == INVALID_SOCKET) {
179 status = setsockopt(
s, SOL_SOCKET, SO_REUSEADDR,
180 reinterpret_cast<const char*
>(&optval),
sizeof(optval));
181 if (status == SOCKET_ERROR) {
192 "Dart Socket ERROR: %s:%d: `reusePort` not supported for "
201 int ttlLevel =
addr.addr.sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6;
203 addr.addr.sa_family == AF_INET ? IP_MULTICAST_TTL : IPV6_MULTICAST_HOPS;
204 if (setsockopt(
s, ttlLevel, ttlOptname,
reinterpret_cast<char*
>(&ttlValue),
205 sizeof(ttlValue)) != 0) {
212 status = bind(
s, &
addr.addr, SocketAddress::GetAddrLength(
addr));
213 if (status == SOCKET_ERROR) {
220 DatagramSocket* datagram_socket =
new DatagramSocket(
s);
221 datagram_socket->EnsureInitialized(EventHandler::delegate());
223 return reinterpret_cast<intptr_t
>(datagram_socket);
226intptr_t ServerSocket::CreateBindListen(
const RawAddr&
addr,
229 SOCKET
s = socket(
addr.ss.ss_family, SOCK_STREAM, IPPROTO_TCP);
230 if (
s == INVALID_SOCKET) {
236 setsockopt(
s, SOL_SOCKET, SO_EXCLUSIVEADDRUSE,
237 reinterpret_cast<const char*
>(&optval),
sizeof(optval));
238 if (status == SOCKET_ERROR) {
245 if (
addr.ss.ss_family == AF_INET6) {
247 setsockopt(
s, IPPROTO_IPV6, IPV6_V6ONLY,
248 reinterpret_cast<const char*
>(&optval),
sizeof(optval));
251 status = bind(
s, &
addr.addr, SocketAddress::GetAddrLength(
addr));
252 if (status == SOCKET_ERROR) {
259 ListenSocket* listen_socket =
new ListenSocket(
s);
262 if ((SocketAddress::GetAddrPort(
addr) == 0) &&
263 (SocketBase::GetPort(
reinterpret_cast<intptr_t
>(listen_socket)) ==
267 intptr_t new_s = CreateBindListen(
addr, backlog, v6_only);
270 listen_socket->Release();
275 status = listen(
s, backlog > 0 ? backlog : SOMAXCONN);
276 if (status == SOCKET_ERROR) {
279 listen_socket->Release();
284 return reinterpret_cast<intptr_t
>(listen_socket);
287intptr_t ServerSocket::CreateUnixDomainBindListen(
const RawAddr&
addr,
295bool ServerSocket::StartAccept(intptr_t fd) {
296 ListenSocket* listen_socket =
reinterpret_cast<ListenSocket*
>(fd);
297 listen_socket->EnsureInitialized(EventHandler::delegate());
299 for (
int i = 0;
i < 5;
i++) {
300 if (!listen_socket->IssueAccept()) {
302 listen_socket->Close();
303 if (!listen_socket->HasPendingAccept()) {
306 listen_socket->Release();
static sk_sp< Effect > Create()
DART_EXPORT Dart_Port Dart_GetMainPortId()
WINBASEAPI VOID WINAPI SetLastError(_In_ DWORD dwErrCode)