| 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_MACOS) | 8 #if defined(HOST_OS_MACOS) |
| 9 | 9 |
| 10 #include "bin/socket.h" | 10 #include "bin/socket.h" |
| 11 | 11 |
| 12 #include <errno.h> // NOLINT | 12 #include <errno.h> // NOLINT |
| 13 | 13 |
| 14 #include "bin/fdutils.h" | 14 #include "bin/fdutils.h" |
| 15 #include "platform/signal_blocker.h" | 15 #include "platform/signal_blocker.h" |
| 16 | 16 |
| 17 namespace dart { | 17 namespace dart { |
| 18 namespace bin { | 18 namespace bin { |
| 19 | 19 |
| 20 Socket::Socket(intptr_t fd) | 20 Socket::Socket(intptr_t fd) |
| 21 : ReferenceCounted(), fd_(fd), port_(ILLEGAL_PORT) {} | 21 : ReferenceCounted(), fd_(fd), port_(ILLEGAL_PORT) {} |
| 22 | 22 |
| 23 | |
| 24 void Socket::SetClosedFd() { | 23 void Socket::SetClosedFd() { |
| 25 fd_ = kClosedFd; | 24 fd_ = kClosedFd; |
| 26 } | 25 } |
| 27 | 26 |
| 28 | |
| 29 static intptr_t Create(const RawAddr& addr) { | 27 static intptr_t Create(const RawAddr& addr) { |
| 30 intptr_t fd; | 28 intptr_t fd; |
| 31 fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, SOCK_STREAM, 0)); | 29 fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, SOCK_STREAM, 0)); |
| 32 if (fd < 0) { | 30 if (fd < 0) { |
| 33 return -1; | 31 return -1; |
| 34 } | 32 } |
| 35 if (!FDUtils::SetCloseOnExec(fd)) { | 33 if (!FDUtils::SetCloseOnExec(fd)) { |
| 36 FDUtils::SaveErrorAndClose(fd); | 34 FDUtils::SaveErrorAndClose(fd); |
| 37 return -1; | 35 return -1; |
| 38 } | 36 } |
| 39 if (!FDUtils::SetNonBlocking(fd)) { | 37 if (!FDUtils::SetNonBlocking(fd)) { |
| 40 FDUtils::SaveErrorAndClose(fd); | 38 FDUtils::SaveErrorAndClose(fd); |
| 41 return -1; | 39 return -1; |
| 42 } | 40 } |
| 43 return fd; | 41 return fd; |
| 44 } | 42 } |
| 45 | 43 |
| 46 | |
| 47 static intptr_t Connect(intptr_t fd, const RawAddr& addr) { | 44 static intptr_t Connect(intptr_t fd, const RawAddr& addr) { |
| 48 intptr_t result = TEMP_FAILURE_RETRY( | 45 intptr_t result = TEMP_FAILURE_RETRY( |
| 49 connect(fd, &addr.addr, SocketAddress::GetAddrLength(addr))); | 46 connect(fd, &addr.addr, SocketAddress::GetAddrLength(addr))); |
| 50 if ((result == 0) || (errno == EINPROGRESS)) { | 47 if ((result == 0) || (errno == EINPROGRESS)) { |
| 51 return fd; | 48 return fd; |
| 52 } | 49 } |
| 53 FDUtils::SaveErrorAndClose(fd); | 50 FDUtils::SaveErrorAndClose(fd); |
| 54 return -1; | 51 return -1; |
| 55 } | 52 } |
| 56 | 53 |
| 57 | |
| 58 intptr_t Socket::CreateConnect(const RawAddr& addr) { | 54 intptr_t Socket::CreateConnect(const RawAddr& addr) { |
| 59 intptr_t fd = Create(addr); | 55 intptr_t fd = Create(addr); |
| 60 if (fd < 0) { | 56 if (fd < 0) { |
| 61 return fd; | 57 return fd; |
| 62 } | 58 } |
| 63 | 59 |
| 64 return Connect(fd, addr); | 60 return Connect(fd, addr); |
| 65 } | 61 } |
| 66 | 62 |
| 67 | |
| 68 intptr_t Socket::CreateBindConnect(const RawAddr& addr, | 63 intptr_t Socket::CreateBindConnect(const RawAddr& addr, |
| 69 const RawAddr& source_addr) { | 64 const RawAddr& source_addr) { |
| 70 intptr_t fd = Create(addr); | 65 intptr_t fd = Create(addr); |
| 71 if (fd < 0) { | 66 if (fd < 0) { |
| 72 return fd; | 67 return fd; |
| 73 } | 68 } |
| 74 | 69 |
| 75 intptr_t result = TEMP_FAILURE_RETRY( | 70 intptr_t result = TEMP_FAILURE_RETRY( |
| 76 bind(fd, &source_addr.addr, SocketAddress::GetAddrLength(source_addr))); | 71 bind(fd, &source_addr.addr, SocketAddress::GetAddrLength(source_addr))); |
| 77 if ((result != 0) && (errno != EINPROGRESS)) { | 72 if ((result != 0) && (errno != EINPROGRESS)) { |
| 78 FDUtils::SaveErrorAndClose(fd); | 73 FDUtils::SaveErrorAndClose(fd); |
| 79 return -1; | 74 return -1; |
| 80 } | 75 } |
| 81 | 76 |
| 82 return Connect(fd, addr); | 77 return Connect(fd, addr); |
| 83 } | 78 } |
| 84 | 79 |
| 85 | |
| 86 intptr_t Socket::CreateBindDatagram(const RawAddr& addr, bool reuseAddress) { | 80 intptr_t Socket::CreateBindDatagram(const RawAddr& addr, bool reuseAddress) { |
| 87 intptr_t fd; | 81 intptr_t fd; |
| 88 | 82 |
| 89 fd = NO_RETRY_EXPECTED(socket(addr.addr.sa_family, SOCK_DGRAM, IPPROTO_UDP)); | 83 fd = NO_RETRY_EXPECTED(socket(addr.addr.sa_family, SOCK_DGRAM, IPPROTO_UDP)); |
| 90 if (fd < 0) { | 84 if (fd < 0) { |
| 91 return -1; | 85 return -1; |
| 92 } | 86 } |
| 93 | 87 |
| 94 if (!FDUtils::SetCloseOnExec(fd)) { | 88 if (!FDUtils::SetCloseOnExec(fd)) { |
| 95 FDUtils::SaveErrorAndClose(fd); | 89 FDUtils::SaveErrorAndClose(fd); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 108 return -1; | 102 return -1; |
| 109 } | 103 } |
| 110 | 104 |
| 111 if (!FDUtils::SetNonBlocking(fd)) { | 105 if (!FDUtils::SetNonBlocking(fd)) { |
| 112 FDUtils::SaveErrorAndClose(fd); | 106 FDUtils::SaveErrorAndClose(fd); |
| 113 return -1; | 107 return -1; |
| 114 } | 108 } |
| 115 return fd; | 109 return fd; |
| 116 } | 110 } |
| 117 | 111 |
| 118 | |
| 119 intptr_t ServerSocket::CreateBindListen(const RawAddr& addr, | 112 intptr_t ServerSocket::CreateBindListen(const RawAddr& addr, |
| 120 intptr_t backlog, | 113 intptr_t backlog, |
| 121 bool v6_only) { | 114 bool v6_only) { |
| 122 intptr_t fd; | 115 intptr_t fd; |
| 123 | 116 |
| 124 fd = TEMP_FAILURE_RETRY(socket(addr.ss.ss_family, SOCK_STREAM, 0)); | 117 fd = TEMP_FAILURE_RETRY(socket(addr.ss.ss_family, SOCK_STREAM, 0)); |
| 125 if (fd < 0) { | 118 if (fd < 0) { |
| 126 return -1; | 119 return -1; |
| 127 } | 120 } |
| 128 | 121 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 return -1; | 155 return -1; |
| 163 } | 156 } |
| 164 | 157 |
| 165 if (!FDUtils::SetNonBlocking(fd)) { | 158 if (!FDUtils::SetNonBlocking(fd)) { |
| 166 FDUtils::SaveErrorAndClose(fd); | 159 FDUtils::SaveErrorAndClose(fd); |
| 167 return -1; | 160 return -1; |
| 168 } | 161 } |
| 169 return fd; | 162 return fd; |
| 170 } | 163 } |
| 171 | 164 |
| 172 | |
| 173 bool ServerSocket::StartAccept(intptr_t fd) { | 165 bool ServerSocket::StartAccept(intptr_t fd) { |
| 174 USE(fd); | 166 USE(fd); |
| 175 return true; | 167 return true; |
| 176 } | 168 } |
| 177 | 169 |
| 178 | |
| 179 intptr_t ServerSocket::Accept(intptr_t fd) { | 170 intptr_t ServerSocket::Accept(intptr_t fd) { |
| 180 intptr_t socket; | 171 intptr_t socket; |
| 181 struct sockaddr clientaddr; | 172 struct sockaddr clientaddr; |
| 182 socklen_t addrlen = sizeof(clientaddr); | 173 socklen_t addrlen = sizeof(clientaddr); |
| 183 socket = TEMP_FAILURE_RETRY(accept(fd, &clientaddr, &addrlen)); | 174 socket = TEMP_FAILURE_RETRY(accept(fd, &clientaddr, &addrlen)); |
| 184 if (socket == -1) { | 175 if (socket == -1) { |
| 185 if (errno == EAGAIN) { | 176 if (errno == EAGAIN) { |
| 186 // We need to signal to the caller that this is actually not an | 177 // We need to signal to the caller that this is actually not an |
| 187 // error. We got woken up from the poll on the listening socket, | 178 // error. We got woken up from the poll on the listening socket, |
| 188 // but there is no connection ready to be accepted. | 179 // but there is no connection ready to be accepted. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 201 } | 192 } |
| 202 return socket; | 193 return socket; |
| 203 } | 194 } |
| 204 | 195 |
| 205 } // namespace bin | 196 } // namespace bin |
| 206 } // namespace dart | 197 } // namespace dart |
| 207 | 198 |
| 208 #endif // defined(HOST_OS_MACOS) | 199 #endif // defined(HOST_OS_MACOS) |
| 209 | 200 |
| 210 #endif // !defined(DART_IO_DISABLED) | 201 #endif // !defined(DART_IO_DISABLED) |
| OLD | NEW |