Chromium Code Reviews| 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 "bin/socket.h" | 7 #include "bin/socket.h" |
| 8 | 8 |
| 9 #include "bin/dartutils.h" | 9 #include "bin/dartutils.h" |
| 10 #include "bin/eventhandler.h" | |
| 10 #include "bin/io_buffer.h" | 11 #include "bin/io_buffer.h" |
| 11 #include "bin/isolate_data.h" | 12 #include "bin/isolate_data.h" |
| 12 #include "bin/lockers.h" | 13 #include "bin/lockers.h" |
| 13 #include "bin/thread.h" | 14 #include "bin/thread.h" |
| 14 #include "bin/utils.h" | 15 #include "bin/utils.h" |
| 15 | 16 |
| 16 #include "include/dart_api.h" | 17 #include "include/dart_api.h" |
| 17 | 18 |
| 18 #include "platform/globals.h" | 19 #include "platform/globals.h" |
| 19 #include "platform/utils.h" | 20 #include "platform/utils.h" |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 65 } | 66 } |
| 66 | 67 |
| 67 | 68 |
| 68 void ListeningSocketRegistry::RemoveByPort(intptr_t port) { | 69 void ListeningSocketRegistry::RemoveByPort(intptr_t port) { |
| 69 sockets_by_port_.Remove(GetHashmapKeyFromIntptr(port), | 70 sockets_by_port_.Remove(GetHashmapKeyFromIntptr(port), |
| 70 GetHashmapHashFromIntptr(port)); | 71 GetHashmapHashFromIntptr(port)); |
| 71 } | 72 } |
| 72 | 73 |
| 73 | 74 |
| 74 ListeningSocketRegistry::OSSocket* ListeningSocketRegistry::LookupByFd( | 75 ListeningSocketRegistry::OSSocket* ListeningSocketRegistry::LookupByFd( |
| 75 intptr_t fd) { | 76 Socket* fd) { |
| 76 HashMap::Entry* entry = sockets_by_fd_.Lookup( | 77 HashMap::Entry* entry = sockets_by_fd_.Lookup( |
| 77 GetHashmapKeyFromIntptr(fd), GetHashmapHashFromIntptr(fd), false); | 78 GetHashmapKeyFromIntptr(reinterpret_cast<intptr_t>(fd)), |
| 79 GetHashmapHashFromIntptr(reinterpret_cast<intptr_t>(fd)), false); | |
| 78 if (entry == NULL) { | 80 if (entry == NULL) { |
| 79 return NULL; | 81 return NULL; |
| 80 } | 82 } |
| 81 return reinterpret_cast<OSSocket*>(entry->value); | 83 return reinterpret_cast<OSSocket*>(entry->value); |
| 82 } | 84 } |
| 83 | 85 |
| 84 | 86 |
| 85 void ListeningSocketRegistry::InsertByFd(intptr_t fd, OSSocket* socket) { | 87 void ListeningSocketRegistry::InsertByFd(Socket* fd, OSSocket* socket) { |
| 86 HashMap::Entry* entry = sockets_by_fd_.Lookup( | 88 HashMap::Entry* entry = sockets_by_fd_.Lookup( |
| 87 GetHashmapKeyFromIntptr(fd), GetHashmapHashFromIntptr(fd), true); | 89 GetHashmapKeyFromIntptr(reinterpret_cast<intptr_t>(fd)), |
| 90 GetHashmapHashFromIntptr(reinterpret_cast<intptr_t>(fd)), true); | |
| 88 ASSERT(entry != NULL); | 91 ASSERT(entry != NULL); |
| 89 entry->value = reinterpret_cast<void*>(socket); | 92 entry->value = reinterpret_cast<void*>(socket); |
| 90 } | 93 } |
| 91 | 94 |
| 92 | 95 |
| 93 void ListeningSocketRegistry::RemoveByFd(intptr_t fd) { | 96 void ListeningSocketRegistry::RemoveByFd(Socket* fd) { |
| 94 sockets_by_fd_.Remove(GetHashmapKeyFromIntptr(fd), | 97 sockets_by_fd_.Remove( |
| 95 GetHashmapHashFromIntptr(fd)); | 98 GetHashmapKeyFromIntptr(reinterpret_cast<intptr_t>(fd)), |
| 99 GetHashmapHashFromIntptr(reinterpret_cast<intptr_t>(fd))); | |
| 96 } | 100 } |
| 97 | 101 |
| 98 | 102 |
| 99 Dart_Handle ListeningSocketRegistry::CreateBindListen(Dart_Handle socket_object, | 103 Dart_Handle ListeningSocketRegistry::CreateBindListen(Dart_Handle socket_object, |
| 100 RawAddr addr, | 104 RawAddr addr, |
| 101 intptr_t backlog, | 105 intptr_t backlog, |
| 102 bool v6_only, | 106 bool v6_only, |
| 103 bool shared) { | 107 bool shared) { |
| 104 MutexLocker ml(mutex_); | 108 MutexLocker ml(mutex_); |
| 105 | 109 |
| 106 OSSocket* first_os_socket = NULL; | 110 OSSocket* first_os_socket = NULL; |
| 107 intptr_t port = SocketAddress::GetAddrPort(addr); | 111 intptr_t port = SocketAddress::GetAddrPort(addr); |
| 108 if (port > 0) { | 112 if (port > 0) { |
| 109 first_os_socket = LookupByPort(port); | 113 first_os_socket = LookupByPort(port); |
| 110 if (first_os_socket != NULL) { | 114 if (first_os_socket != NULL) { |
| 111 // There is already a socket listening on this port. We need to ensure | 115 // There is already a socket listening on this port. We need to ensure |
| 112 // that if there is one also listening on the same address, it was created | 116 // that if there is one also listening on the same address, it was created |
| 113 // with `shared = true`, ... | 117 // with `shared = true`, ... |
| 114 OSSocket* os_socket = first_os_socket; | 118 OSSocket* os_socket = first_os_socket; |
| 115 OSSocket* os_socket_same_addr = findOSSocketWithAddress(os_socket, addr); | 119 OSSocket* os_socket_same_addr = FindOSSocketWithAddress(os_socket, addr); |
| 116 | 120 |
| 117 if (os_socket_same_addr != NULL) { | 121 if (os_socket_same_addr != NULL) { |
| 118 if (!os_socket_same_addr->shared || !shared) { | 122 if (!os_socket_same_addr->shared || !shared) { |
| 119 OSError os_error(-1, | 123 OSError os_error(-1, |
| 120 "The shared flag to bind() needs to be `true` if " | 124 "The shared flag to bind() needs to be `true` if " |
| 121 "binding multiple times on the same (address, port) " | 125 "binding multiple times on the same (address, port) " |
| 122 "combination.", | 126 "combination.", |
| 123 OSError::kUnknown); | 127 OSError::kUnknown); |
| 124 return DartUtils::NewDartOSError(&os_error); | 128 return DartUtils::NewDartOSError(&os_error); |
| 125 } | 129 } |
| 126 if (os_socket_same_addr->v6_only != v6_only) { | 130 if (os_socket_same_addr->v6_only != v6_only) { |
| 127 OSError os_error(-1, | 131 OSError os_error(-1, |
| 128 "The v6Only flag to bind() needs to be the same if " | 132 "The v6Only flag to bind() needs to be the same if " |
| 129 "binding multiple times on the same (address, port) " | 133 "binding multiple times on the same (address, port) " |
| 130 "combination.", | 134 "combination.", |
| 131 OSError::kUnknown); | 135 OSError::kUnknown); |
| 132 return DartUtils::NewDartOSError(&os_error); | 136 return DartUtils::NewDartOSError(&os_error); |
| 133 } | 137 } |
| 134 | 138 |
| 135 // This socket creation is the exact same as the one which originally | 139 // This socket creation is the exact same as the one which originally |
| 136 // created the socket. We therefore increment the refcount and reuse | 140 // created the socket. We therefore increment the refcount and reuse |
| 137 // the file descriptor. | 141 // the file descriptor. |
| 138 os_socket->ref_count++; | 142 os_socket->ref_count++; |
| 139 | 143 |
| 140 // We set as a side-effect the file descriptor on the dart | 144 // We set as a side-effect the file descriptor on the dart |
| 141 // socket_object. | 145 // socket_object. |
| 142 Socket::SetSocketIdNativeField(socket_object, os_socket->socketfd); | 146 os_socket->socketfd->Retain(); |
|
zra
2017/03/25 22:39:50
// The same Socket is used by a second Dart _Nativ
| |
| 147 Socket::ReuseSocketIdNativeField(socket_object, os_socket->socketfd, | |
| 148 true); | |
| 143 | 149 |
| 144 return Dart_True(); | 150 return Dart_True(); |
| 145 } | 151 } |
| 146 } | 152 } |
| 147 } | 153 } |
| 148 | 154 |
| 149 // There is no socket listening on that (address, port), so we create new one. | 155 // There is no socket listening on that (address, port), so we create new one. |
| 150 intptr_t socketfd = ServerSocket::CreateBindListen(addr, backlog, v6_only); | 156 intptr_t fd = ServerSocket::CreateBindListen(addr, backlog, v6_only); |
| 151 if (socketfd == -5) { | 157 if (fd == -5) { |
| 152 OSError os_error(-1, "Invalid host", OSError::kUnknown); | 158 OSError os_error(-1, "Invalid host", OSError::kUnknown); |
| 153 return DartUtils::NewDartOSError(&os_error); | 159 return DartUtils::NewDartOSError(&os_error); |
| 154 } | 160 } |
| 155 if (socketfd < 0) { | 161 if (fd < 0) { |
| 156 OSError error; | 162 OSError error; |
| 157 return DartUtils::NewDartOSError(&error); | 163 return DartUtils::NewDartOSError(&error); |
| 158 } | 164 } |
| 159 if (!ServerSocket::StartAccept(socketfd)) { | 165 if (!ServerSocket::StartAccept(fd)) { |
| 160 OSError os_error(-1, "Failed to start accept", OSError::kUnknown); | 166 OSError os_error(-1, "Failed to start accept", OSError::kUnknown); |
| 161 return DartUtils::NewDartOSError(&os_error); | 167 return DartUtils::NewDartOSError(&os_error); |
| 162 } | 168 } |
| 163 intptr_t allocated_port = Socket::GetPort(socketfd); | 169 intptr_t allocated_port = Socket::GetPort(fd); |
| 164 ASSERT(allocated_port > 0); | 170 ASSERT(allocated_port > 0); |
| 165 | 171 |
| 166 if (allocated_port != port) { | 172 if (allocated_port != port) { |
| 167 // There are two cases to consider: | 173 // There are two cases to consider: |
| 168 // | 174 // |
| 169 // a) The user requested (address, port) where port != 0 which means | 175 // a) The user requested (address, port) where port != 0 which means |
| 170 // we re-use an existing socket if available (and it is shared) or we | 176 // we re-use an existing socket if available (and it is shared) or we |
| 171 // create a new one. The new socket is guaranteed to have that | 177 // create a new one. The new socket is guaranteed to have that |
| 172 // selected port. | 178 // selected port. |
| 173 // | 179 // |
| 174 // b) The user requested (address, 0). This will make us *always* create a | 180 // b) The user requested (address, 0). This will make us *always* create a |
| 175 // new socket. The OS will assign it a new `allocated_port` and we will | 181 // new socket. The OS will assign it a new `allocated_port` and we will |
| 176 // insert into our data structures. *BUT* There might already be an | 182 // insert into our data structures. *BUT* There might already be an |
| 177 // existing (address2, `allocated_port`) where address != address2. So | 183 // existing (address2, `allocated_port`) where address != address2. So |
| 178 // we need to do another `LookupByPort(allocated_port)` and link them | 184 // we need to do another `LookupByPort(allocated_port)` and link them |
| 179 // via `OSSocket->next`. | 185 // via `OSSocket->next`. |
| 180 ASSERT(port == 0); | 186 ASSERT(port == 0); |
| 181 first_os_socket = LookupByPort(allocated_port); | 187 first_os_socket = LookupByPort(allocated_port); |
| 182 } | 188 } |
| 183 | 189 |
| 190 Socket* socketfd = new Socket(fd); | |
| 184 OSSocket* os_socket = | 191 OSSocket* os_socket = |
| 185 new OSSocket(addr, allocated_port, v6_only, shared, socketfd); | 192 new OSSocket(addr, allocated_port, v6_only, shared, socketfd); |
| 186 os_socket->ref_count = 1; | 193 os_socket->ref_count = 1; |
| 187 os_socket->next = first_os_socket; | 194 os_socket->next = first_os_socket; |
| 188 | 195 |
| 189 InsertByPort(allocated_port, os_socket); | 196 InsertByPort(allocated_port, os_socket); |
| 190 InsertByFd(socketfd, os_socket); | 197 InsertByFd(socketfd, os_socket); |
| 191 | 198 |
| 192 // We set as a side-effect the port on the dart socket_object. | 199 // We set as a side-effect the port on the dart socket_object. |
| 193 Socket::SetSocketIdNativeField(socket_object, socketfd); | 200 Socket::ReuseSocketIdNativeField(socket_object, socketfd, true); |
| 194 | 201 |
| 195 return Dart_True(); | 202 return Dart_True(); |
| 196 } | 203 } |
| 197 | 204 |
| 198 | 205 |
| 199 bool ListeningSocketRegistry::CloseOneSafe(OSSocket* os_socket, | 206 bool ListeningSocketRegistry::CloseOneSafe(OSSocket* os_socket, |
| 200 bool update_hash_maps) { | 207 bool update_hash_maps) { |
| 201 ASSERT(!mutex_->TryLock()); | 208 ASSERT(!mutex_->TryLock()); |
| 202 ASSERT(os_socket != NULL); | 209 ASSERT(os_socket != NULL); |
| 203 ASSERT(os_socket->ref_count > 0); | 210 ASSERT(os_socket->ref_count > 0); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 238 void ListeningSocketRegistry::CloseAllSafe() { | 245 void ListeningSocketRegistry::CloseAllSafe() { |
| 239 MutexLocker ml(mutex_); | 246 MutexLocker ml(mutex_); |
| 240 | 247 |
| 241 for (HashMap::Entry* cursor = sockets_by_fd_.Start(); cursor != NULL; | 248 for (HashMap::Entry* cursor = sockets_by_fd_.Start(); cursor != NULL; |
| 242 cursor = sockets_by_fd_.Next(cursor)) { | 249 cursor = sockets_by_fd_.Next(cursor)) { |
| 243 CloseOneSafe(reinterpret_cast<OSSocket*>(cursor->value), false); | 250 CloseOneSafe(reinterpret_cast<OSSocket*>(cursor->value), false); |
| 244 } | 251 } |
| 245 } | 252 } |
| 246 | 253 |
| 247 | 254 |
| 248 bool ListeningSocketRegistry::CloseSafe(intptr_t socketfd) { | 255 bool ListeningSocketRegistry::CloseSafe(Socket* socketfd) { |
| 249 ASSERT(!mutex_->TryLock()); | 256 ASSERT(!mutex_->TryLock()); |
| 250 OSSocket* os_socket = LookupByFd(socketfd); | 257 OSSocket* os_socket = LookupByFd(socketfd); |
| 251 if (os_socket != NULL) { | 258 if (os_socket != NULL) { |
| 252 return CloseOneSafe(os_socket, true); | 259 return CloseOneSafe(os_socket, true); |
| 253 } else { | 260 } else { |
| 254 // It should be impossible for the event handler to close something that | 261 // A finalizer may direct the event handler to close a listening socket |
| 255 // hasn't been created before. | 262 // that it has never seen before. In this case, we return true to direct |
| 256 UNREACHABLE(); | 263 // the eventhandler to clean up the socket. |
| 257 return false; | 264 return true; |
| 258 } | 265 } |
| 259 } | 266 } |
| 260 | 267 |
| 261 | 268 |
| 262 void FUNCTION_NAME(InternetAddress_Parse)(Dart_NativeArguments args) { | 269 void FUNCTION_NAME(InternetAddress_Parse)(Dart_NativeArguments args) { |
| 263 const char* address = | 270 const char* address = |
| 264 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | 271 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
| 265 ASSERT(address != NULL); | 272 ASSERT(address != NULL); |
| 266 RawAddr raw; | 273 RawAddr raw; |
| 267 memset(&raw, 0, sizeof(raw)); | 274 memset(&raw, 0, sizeof(raw)); |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 288 | 295 |
| 289 void FUNCTION_NAME(Socket_CreateConnect)(Dart_NativeArguments args) { | 296 void FUNCTION_NAME(Socket_CreateConnect)(Dart_NativeArguments args) { |
| 290 RawAddr addr; | 297 RawAddr addr; |
| 291 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); | 298 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); |
| 292 Dart_Handle port_arg = Dart_GetNativeArgument(args, 2); | 299 Dart_Handle port_arg = Dart_GetNativeArgument(args, 2); |
| 293 int64_t port = DartUtils::GetInt64ValueCheckRange(port_arg, 0, 65535); | 300 int64_t port = DartUtils::GetInt64ValueCheckRange(port_arg, 0, 65535); |
| 294 SocketAddress::SetAddrPort(&addr, static_cast<intptr_t>(port)); | 301 SocketAddress::SetAddrPort(&addr, static_cast<intptr_t>(port)); |
| 295 intptr_t socket = Socket::CreateConnect(addr); | 302 intptr_t socket = Socket::CreateConnect(addr); |
| 296 OSError error; | 303 OSError error; |
| 297 if (socket >= 0) { | 304 if (socket >= 0) { |
| 298 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket); | 305 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket, |
| 306 false); | |
| 299 Dart_SetReturnValue(args, Dart_True()); | 307 Dart_SetReturnValue(args, Dart_True()); |
| 300 } else { | 308 } else { |
| 301 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&error)); | 309 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&error)); |
| 302 } | 310 } |
| 303 } | 311 } |
| 304 | 312 |
| 305 | 313 |
| 306 void FUNCTION_NAME(Socket_CreateBindConnect)(Dart_NativeArguments args) { | 314 void FUNCTION_NAME(Socket_CreateBindConnect)(Dart_NativeArguments args) { |
| 307 RawAddr addr; | 315 RawAddr addr; |
| 308 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); | 316 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); |
| 309 Dart_Handle port_arg = Dart_GetNativeArgument(args, 2); | 317 Dart_Handle port_arg = Dart_GetNativeArgument(args, 2); |
| 310 int64_t port = DartUtils::GetInt64ValueCheckRange(port_arg, 0, 65535); | 318 int64_t port = DartUtils::GetInt64ValueCheckRange(port_arg, 0, 65535); |
| 311 SocketAddress::SetAddrPort(&addr, static_cast<intptr_t>(port)); | 319 SocketAddress::SetAddrPort(&addr, static_cast<intptr_t>(port)); |
| 312 RawAddr sourceAddr; | 320 RawAddr sourceAddr; |
| 313 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 3), &sourceAddr); | 321 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 3), &sourceAddr); |
| 314 intptr_t socket = Socket::CreateBindConnect(addr, sourceAddr); | 322 intptr_t socket = Socket::CreateBindConnect(addr, sourceAddr); |
| 315 OSError error; | 323 OSError error; |
| 316 if (socket >= 0) { | 324 if (socket >= 0) { |
| 317 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket); | 325 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket, |
| 326 false); | |
| 318 Dart_SetReturnValue(args, Dart_True()); | 327 Dart_SetReturnValue(args, Dart_True()); |
| 319 } else { | 328 } else { |
| 320 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&error)); | 329 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&error)); |
| 321 } | 330 } |
| 322 } | 331 } |
| 323 | 332 |
| 324 void FUNCTION_NAME(Socket_IsBindError)(Dart_NativeArguments args) { | 333 void FUNCTION_NAME(Socket_IsBindError)(Dart_NativeArguments args) { |
| 325 intptr_t error_number = | 334 intptr_t error_number = |
| 326 DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 1)); | 335 DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 1)); |
| 327 bool is_bind_error = Socket::IsBindError(error_number); | 336 bool is_bind_error = Socket::IsBindError(error_number); |
| 328 Dart_SetReturnValue(args, is_bind_error ? Dart_True() : Dart_False()); | 337 Dart_SetReturnValue(args, is_bind_error ? Dart_True() : Dart_False()); |
| 329 } | 338 } |
| 330 | 339 |
| 331 void FUNCTION_NAME(Socket_CreateBindDatagram)(Dart_NativeArguments args) { | 340 void FUNCTION_NAME(Socket_CreateBindDatagram)(Dart_NativeArguments args) { |
| 332 RawAddr addr; | 341 RawAddr addr; |
| 333 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); | 342 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); |
| 334 Dart_Handle port_arg = Dart_GetNativeArgument(args, 2); | 343 Dart_Handle port_arg = Dart_GetNativeArgument(args, 2); |
| 335 int64_t port = DartUtils::GetInt64ValueCheckRange(port_arg, 0, 65535); | 344 int64_t port = DartUtils::GetInt64ValueCheckRange(port_arg, 0, 65535); |
| 336 SocketAddress::SetAddrPort(&addr, port); | 345 SocketAddress::SetAddrPort(&addr, port); |
| 337 bool reuse_addr = DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 3)); | 346 bool reuse_addr = DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 3)); |
| 338 intptr_t socket = Socket::CreateBindDatagram(addr, reuse_addr); | 347 intptr_t socket = Socket::CreateBindDatagram(addr, reuse_addr); |
| 339 if (socket >= 0) { | 348 if (socket >= 0) { |
| 340 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket); | 349 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket, |
| 350 false); | |
| 341 Dart_SetReturnValue(args, Dart_True()); | 351 Dart_SetReturnValue(args, Dart_True()); |
| 342 } else { | 352 } else { |
| 343 OSError error; | 353 OSError error; |
| 344 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&error)); | 354 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&error)); |
| 345 } | 355 } |
| 346 } | 356 } |
| 347 | 357 |
| 348 | 358 |
| 349 void FUNCTION_NAME(Socket_Available)(Dart_NativeArguments args) { | 359 void FUNCTION_NAME(Socket_Available)(Dart_NativeArguments args) { |
| 350 intptr_t socket = | 360 Socket* socket = |
| 351 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 361 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
| 352 intptr_t available = Socket::Available(socket); | 362 intptr_t available = Socket::Available(socket->fd()); |
| 353 if (available >= 0) { | 363 if (available >= 0) { |
| 354 Dart_SetReturnValue(args, Dart_NewInteger(available)); | 364 Dart_SetReturnValue(args, Dart_NewInteger(available)); |
| 355 } else { | 365 } else { |
| 356 // Available failed. Mark socket as having data, to trigger a future read | 366 // Available failed. Mark socket as having data, to trigger a future read |
| 357 // event where the actual error can be reported. | 367 // event where the actual error can be reported. |
| 358 Dart_SetReturnValue(args, Dart_NewInteger(1)); | 368 Dart_SetReturnValue(args, Dart_NewInteger(1)); |
| 359 } | 369 } |
| 360 } | 370 } |
| 361 | 371 |
| 362 | 372 |
| 363 void FUNCTION_NAME(Socket_Read)(Dart_NativeArguments args) { | 373 void FUNCTION_NAME(Socket_Read)(Dart_NativeArguments args) { |
| 364 intptr_t socket = | 374 Socket* socket = |
| 365 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 375 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
| 366 int64_t length = 0; | 376 int64_t length = 0; |
| 367 if (DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 1), &length)) { | 377 if (DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 1), &length)) { |
| 368 if (short_socket_read) { | 378 if (short_socket_read) { |
| 369 length = (length + 1) / 2; | 379 length = (length + 1) / 2; |
| 370 } | 380 } |
| 371 uint8_t* buffer = NULL; | 381 uint8_t* buffer = NULL; |
| 372 Dart_Handle result = IOBuffer::Allocate(length, &buffer); | 382 Dart_Handle result = IOBuffer::Allocate(length, &buffer); |
| 373 if (Dart_IsError(result)) { | 383 if (Dart_IsError(result)) { |
| 374 Dart_PropagateError(result); | 384 Dart_PropagateError(result); |
| 375 } | 385 } |
| 376 ASSERT(buffer != NULL); | 386 ASSERT(buffer != NULL); |
| 377 intptr_t bytes_read = Socket::Read(socket, buffer, length); | 387 intptr_t bytes_read = Socket::Read(socket->fd(), buffer, length); |
| 378 if (bytes_read == length) { | 388 if (bytes_read == length) { |
| 379 Dart_SetReturnValue(args, result); | 389 Dart_SetReturnValue(args, result); |
| 380 } else if (bytes_read > 0) { | 390 } else if (bytes_read > 0) { |
| 381 uint8_t* new_buffer = NULL; | 391 uint8_t* new_buffer = NULL; |
| 382 Dart_Handle new_result = IOBuffer::Allocate(bytes_read, &new_buffer); | 392 Dart_Handle new_result = IOBuffer::Allocate(bytes_read, &new_buffer); |
| 383 if (Dart_IsError(new_result)) { | 393 if (Dart_IsError(new_result)) { |
| 384 Dart_PropagateError(new_result); | 394 Dart_PropagateError(new_result); |
| 385 } | 395 } |
| 386 ASSERT(new_buffer != NULL); | 396 ASSERT(new_buffer != NULL); |
| 387 memmove(new_buffer, buffer, bytes_read); | 397 memmove(new_buffer, buffer, bytes_read); |
| 388 Dart_SetReturnValue(args, new_result); | 398 Dart_SetReturnValue(args, new_result); |
| 389 } else if (bytes_read == 0) { | 399 } else if (bytes_read == 0) { |
| 390 // On MacOS when reading from a tty Ctrl-D will result in reading one | 400 // On MacOS when reading from a tty Ctrl-D will result in reading one |
| 391 // less byte then reported as available. | 401 // less byte then reported as available. |
| 392 Dart_SetReturnValue(args, Dart_Null()); | 402 Dart_SetReturnValue(args, Dart_Null()); |
| 393 } else { | 403 } else { |
| 394 ASSERT(bytes_read == -1); | 404 ASSERT(bytes_read == -1); |
| 395 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 405 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
| 396 } | 406 } |
| 397 } else { | 407 } else { |
| 398 OSError os_error(-1, "Invalid argument", OSError::kUnknown); | 408 OSError os_error(-1, "Invalid argument", OSError::kUnknown); |
| 399 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); | 409 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); |
| 400 } | 410 } |
| 401 } | 411 } |
| 402 | 412 |
| 403 | 413 |
| 404 void FUNCTION_NAME(Socket_RecvFrom)(Dart_NativeArguments args) { | 414 void FUNCTION_NAME(Socket_RecvFrom)(Dart_NativeArguments args) { |
| 405 intptr_t socket = | 415 Socket* socket = |
| 406 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 416 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
| 407 | 417 |
| 408 // TODO(sgjesse): Use a MTU value here. Only the loopback adapter can | 418 // TODO(sgjesse): Use a MTU value here. Only the loopback adapter can |
| 409 // handle 64k datagrams. | 419 // handle 64k datagrams. |
| 410 IsolateData* isolate_data = | 420 IsolateData* isolate_data = |
| 411 reinterpret_cast<IsolateData*>(Dart_CurrentIsolateData()); | 421 reinterpret_cast<IsolateData*>(Dart_CurrentIsolateData()); |
| 412 if (isolate_data->udp_receive_buffer == NULL) { | 422 if (isolate_data->udp_receive_buffer == NULL) { |
| 413 isolate_data->udp_receive_buffer = | 423 isolate_data->udp_receive_buffer = |
| 414 reinterpret_cast<uint8_t*>(malloc(65536)); | 424 reinterpret_cast<uint8_t*>(malloc(65536)); |
| 415 } | 425 } |
| 416 RawAddr addr; | 426 RawAddr addr; |
| 417 intptr_t bytes_read = | 427 intptr_t bytes_read = Socket::RecvFrom( |
| 418 Socket::RecvFrom(socket, isolate_data->udp_receive_buffer, 65536, &addr); | 428 socket->fd(), isolate_data->udp_receive_buffer, 65536, &addr); |
| 419 if (bytes_read == 0) { | 429 if (bytes_read == 0) { |
| 420 Dart_SetReturnValue(args, Dart_Null()); | 430 Dart_SetReturnValue(args, Dart_Null()); |
| 421 return; | 431 return; |
| 422 } | 432 } |
| 423 if (bytes_read < 0) { | 433 if (bytes_read < 0) { |
| 424 ASSERT(bytes_read == -1); | 434 ASSERT(bytes_read == -1); |
| 425 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 435 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
| 426 return; | 436 return; |
| 427 } | 437 } |
| 428 // Datagram data read. Copy into buffer of the exact size, | 438 // Datagram data read. Copy into buffer of the exact size, |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 465 if (Dart_IsError(io_lib)) { | 475 if (Dart_IsError(io_lib)) { |
| 466 Dart_PropagateError(io_lib); | 476 Dart_PropagateError(io_lib); |
| 467 } | 477 } |
| 468 Dart_Handle result = Dart_Invoke( | 478 Dart_Handle result = Dart_Invoke( |
| 469 io_lib, DartUtils::NewString("_makeDatagram"), kNumArgs, dart_args); | 479 io_lib, DartUtils::NewString("_makeDatagram"), kNumArgs, dart_args); |
| 470 Dart_SetReturnValue(args, result); | 480 Dart_SetReturnValue(args, result); |
| 471 } | 481 } |
| 472 | 482 |
| 473 | 483 |
| 474 void FUNCTION_NAME(Socket_WriteList)(Dart_NativeArguments args) { | 484 void FUNCTION_NAME(Socket_WriteList)(Dart_NativeArguments args) { |
| 475 intptr_t socket = | 485 Socket* socket = |
| 476 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 486 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
| 477 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); | 487 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); |
| 478 ASSERT(Dart_IsList(buffer_obj)); | 488 ASSERT(Dart_IsList(buffer_obj)); |
| 479 intptr_t offset = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 2)); | 489 intptr_t offset = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 2)); |
| 480 intptr_t length = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 3)); | 490 intptr_t length = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 3)); |
| 481 bool short_write = false; | 491 bool short_write = false; |
| 482 if (short_socket_write) { | 492 if (short_socket_write) { |
| 483 if (length > 1) { | 493 if (length > 1) { |
| 484 short_write = true; | 494 short_write = true; |
| 485 } | 495 } |
| 486 length = (length + 1) / 2; | 496 length = (length + 1) / 2; |
| 487 } | 497 } |
| 488 Dart_TypedData_Type type; | 498 Dart_TypedData_Type type; |
| 489 uint8_t* buffer = NULL; | 499 uint8_t* buffer = NULL; |
| 490 intptr_t len; | 500 intptr_t len; |
| 491 Dart_Handle result = Dart_TypedDataAcquireData( | 501 Dart_Handle result = Dart_TypedDataAcquireData( |
| 492 buffer_obj, &type, reinterpret_cast<void**>(&buffer), &len); | 502 buffer_obj, &type, reinterpret_cast<void**>(&buffer), &len); |
| 493 if (Dart_IsError(result)) { | 503 if (Dart_IsError(result)) { |
| 494 Dart_PropagateError(result); | 504 Dart_PropagateError(result); |
| 495 } | 505 } |
| 496 ASSERT((offset + length) <= len); | 506 ASSERT((offset + length) <= len); |
| 497 buffer += offset; | 507 buffer += offset; |
| 498 intptr_t bytes_written = Socket::Write(socket, buffer, length); | 508 intptr_t bytes_written = Socket::Write(socket->fd(), buffer, length); |
| 499 if (bytes_written >= 0) { | 509 if (bytes_written >= 0) { |
| 500 Dart_TypedDataReleaseData(buffer_obj); | 510 Dart_TypedDataReleaseData(buffer_obj); |
| 501 if (short_write) { | 511 if (short_write) { |
| 502 // If the write was forced 'short', indicate by returning the negative | 512 // If the write was forced 'short', indicate by returning the negative |
| 503 // number of bytes. A forced short write may not trigger a write event. | 513 // number of bytes. A forced short write may not trigger a write event. |
| 504 Dart_SetReturnValue(args, Dart_NewInteger(-bytes_written)); | 514 Dart_SetReturnValue(args, Dart_NewInteger(-bytes_written)); |
| 505 } else { | 515 } else { |
| 506 Dart_SetReturnValue(args, Dart_NewInteger(bytes_written)); | 516 Dart_SetReturnValue(args, Dart_NewInteger(bytes_written)); |
| 507 } | 517 } |
| 508 } else { | 518 } else { |
| 509 // Extract OSError before we release data, as it may override the error. | 519 // Extract OSError before we release data, as it may override the error. |
| 510 OSError os_error; | 520 OSError os_error; |
| 511 Dart_TypedDataReleaseData(buffer_obj); | 521 Dart_TypedDataReleaseData(buffer_obj); |
| 512 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); | 522 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); |
| 513 } | 523 } |
| 514 } | 524 } |
| 515 | 525 |
| 516 | 526 |
| 517 void FUNCTION_NAME(Socket_SendTo)(Dart_NativeArguments args) { | 527 void FUNCTION_NAME(Socket_SendTo)(Dart_NativeArguments args) { |
| 518 intptr_t socket = | 528 Socket* socket = |
| 519 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 529 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
| 520 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); | 530 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); |
| 521 intptr_t offset = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 2)); | 531 intptr_t offset = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 2)); |
| 522 intptr_t length = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 3)); | 532 intptr_t length = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 3)); |
| 523 Dart_Handle address_obj = Dart_GetNativeArgument(args, 4); | 533 Dart_Handle address_obj = Dart_GetNativeArgument(args, 4); |
| 524 ASSERT(Dart_IsList(address_obj)); | 534 ASSERT(Dart_IsList(address_obj)); |
| 525 RawAddr addr; | 535 RawAddr addr; |
| 526 SocketAddress::GetSockAddr(address_obj, &addr); | 536 SocketAddress::GetSockAddr(address_obj, &addr); |
| 527 int64_t port = DartUtils::GetInt64ValueCheckRange( | 537 int64_t port = DartUtils::GetInt64ValueCheckRange( |
| 528 Dart_GetNativeArgument(args, 5), 0, 65535); | 538 Dart_GetNativeArgument(args, 5), 0, 65535); |
| 529 SocketAddress::SetAddrPort(&addr, port); | 539 SocketAddress::SetAddrPort(&addr, port); |
| 530 Dart_TypedData_Type type; | 540 Dart_TypedData_Type type; |
| 531 uint8_t* buffer = NULL; | 541 uint8_t* buffer = NULL; |
| 532 intptr_t len; | 542 intptr_t len; |
| 533 Dart_Handle result = Dart_TypedDataAcquireData( | 543 Dart_Handle result = Dart_TypedDataAcquireData( |
| 534 buffer_obj, &type, reinterpret_cast<void**>(&buffer), &len); | 544 buffer_obj, &type, reinterpret_cast<void**>(&buffer), &len); |
| 535 if (Dart_IsError(result)) { | 545 if (Dart_IsError(result)) { |
| 536 Dart_PropagateError(result); | 546 Dart_PropagateError(result); |
| 537 } | 547 } |
| 538 ASSERT((offset + length) <= len); | 548 ASSERT((offset + length) <= len); |
| 539 buffer += offset; | 549 buffer += offset; |
| 540 intptr_t bytes_written = Socket::SendTo(socket, buffer, length, addr); | 550 intptr_t bytes_written = Socket::SendTo(socket->fd(), buffer, length, addr); |
| 541 if (bytes_written >= 0) { | 551 if (bytes_written >= 0) { |
| 542 Dart_TypedDataReleaseData(buffer_obj); | 552 Dart_TypedDataReleaseData(buffer_obj); |
| 543 Dart_SetReturnValue(args, Dart_NewInteger(bytes_written)); | 553 Dart_SetReturnValue(args, Dart_NewInteger(bytes_written)); |
| 544 } else { | 554 } else { |
| 545 // Extract OSError before we release data, as it may override the error. | 555 // Extract OSError before we release data, as it may override the error. |
| 546 OSError os_error; | 556 OSError os_error; |
| 547 Dart_TypedDataReleaseData(buffer_obj); | 557 Dart_TypedDataReleaseData(buffer_obj); |
| 548 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); | 558 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); |
| 549 } | 559 } |
| 550 } | 560 } |
| 551 | 561 |
| 552 | 562 |
| 553 void FUNCTION_NAME(Socket_GetPort)(Dart_NativeArguments args) { | 563 void FUNCTION_NAME(Socket_GetPort)(Dart_NativeArguments args) { |
| 554 intptr_t socket = | 564 Socket* socket = |
| 555 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 565 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
| 556 OSError os_error; | 566 OSError os_error; |
| 557 intptr_t port = Socket::GetPort(socket); | 567 intptr_t port = Socket::GetPort(socket->fd()); |
| 558 if (port > 0) { | 568 if (port > 0) { |
| 559 Dart_SetReturnValue(args, Dart_NewInteger(port)); | 569 Dart_SetReturnValue(args, Dart_NewInteger(port)); |
| 560 } else { | 570 } else { |
| 561 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 571 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
| 562 } | 572 } |
| 563 } | 573 } |
| 564 | 574 |
| 565 | 575 |
| 566 void FUNCTION_NAME(Socket_GetRemotePeer)(Dart_NativeArguments args) { | 576 void FUNCTION_NAME(Socket_GetRemotePeer)(Dart_NativeArguments args) { |
| 567 intptr_t socket = | 577 Socket* socket = |
| 568 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 578 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
| 569 OSError os_error; | 579 OSError os_error; |
| 570 intptr_t port = 0; | 580 intptr_t port = 0; |
| 571 SocketAddress* addr = Socket::GetRemotePeer(socket, &port); | 581 SocketAddress* addr = Socket::GetRemotePeer(socket->fd(), &port); |
| 572 if (addr != NULL) { | 582 if (addr != NULL) { |
| 573 Dart_Handle list = Dart_NewList(2); | 583 Dart_Handle list = Dart_NewList(2); |
| 574 | 584 |
| 575 Dart_Handle entry = Dart_NewList(3); | 585 Dart_Handle entry = Dart_NewList(3); |
| 576 Dart_ListSetAt(entry, 0, Dart_NewInteger(addr->GetType())); | 586 Dart_ListSetAt(entry, 0, Dart_NewInteger(addr->GetType())); |
| 577 Dart_ListSetAt(entry, 1, Dart_NewStringFromCString(addr->as_string())); | 587 Dart_ListSetAt(entry, 1, Dart_NewStringFromCString(addr->as_string())); |
| 578 | 588 |
| 579 RawAddr raw = addr->addr(); | 589 RawAddr raw = addr->addr(); |
| 580 Dart_ListSetAt(entry, 2, SocketAddress::ToTypedData(raw)); | 590 Dart_ListSetAt(entry, 2, SocketAddress::ToTypedData(raw)); |
| 581 | 591 |
| 582 Dart_ListSetAt(list, 0, entry); | 592 Dart_ListSetAt(list, 0, entry); |
| 583 Dart_ListSetAt(list, 1, Dart_NewInteger(port)); | 593 Dart_ListSetAt(list, 1, Dart_NewInteger(port)); |
| 584 Dart_SetReturnValue(args, list); | 594 Dart_SetReturnValue(args, list); |
| 585 delete addr; | 595 delete addr; |
| 586 } else { | 596 } else { |
| 587 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 597 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
| 588 } | 598 } |
| 589 } | 599 } |
| 590 | 600 |
| 591 | 601 |
| 592 void FUNCTION_NAME(Socket_GetError)(Dart_NativeArguments args) { | 602 void FUNCTION_NAME(Socket_GetError)(Dart_NativeArguments args) { |
| 593 intptr_t socket = | 603 Socket* socket = |
| 594 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 604 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
| 595 OSError os_error; | 605 OSError os_error; |
| 596 Socket::GetError(socket, &os_error); | 606 Socket::GetError(socket->fd(), &os_error); |
| 597 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); | 607 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); |
| 598 } | 608 } |
| 599 | 609 |
| 600 | 610 |
| 601 void FUNCTION_NAME(Socket_GetType)(Dart_NativeArguments args) { | 611 void FUNCTION_NAME(Socket_GetType)(Dart_NativeArguments args) { |
| 602 intptr_t socket = | 612 Socket* socket = |
| 603 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 613 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
| 604 OSError os_error; | 614 OSError os_error; |
| 605 intptr_t type = Socket::GetType(socket); | 615 intptr_t type = Socket::GetType(socket->fd()); |
| 606 if (type >= 0) { | 616 if (type >= 0) { |
| 607 Dart_SetReturnValue(args, Dart_NewInteger(type)); | 617 Dart_SetReturnValue(args, Dart_NewInteger(type)); |
| 608 } else { | 618 } else { |
| 609 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 619 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
| 610 } | 620 } |
| 611 } | 621 } |
| 612 | 622 |
| 613 | 623 |
| 614 void FUNCTION_NAME(Socket_GetStdioHandle)(Dart_NativeArguments args) { | 624 void FUNCTION_NAME(Socket_GetStdioHandle)(Dart_NativeArguments args) { |
| 615 int64_t num = | 625 int64_t num = |
| 616 DartUtils::GetInt64ValueCheckRange(Dart_GetNativeArgument(args, 1), 0, 2); | 626 DartUtils::GetInt64ValueCheckRange(Dart_GetNativeArgument(args, 1), 0, 2); |
| 617 intptr_t socket = Socket::GetStdioHandle(num); | 627 intptr_t socket = Socket::GetStdioHandle(num); |
| 618 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket); | 628 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket, |
| 629 false); | |
| 619 Dart_SetReturnValue(args, Dart_NewBoolean(socket >= 0)); | 630 Dart_SetReturnValue(args, Dart_NewBoolean(socket >= 0)); |
| 620 } | 631 } |
| 621 | 632 |
| 622 | 633 |
| 623 void FUNCTION_NAME(Socket_GetSocketId)(Dart_NativeArguments args) { | 634 void FUNCTION_NAME(Socket_GetSocketId)(Dart_NativeArguments args) { |
| 624 intptr_t id = Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 635 Socket* socket = |
| 636 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | |
| 637 intptr_t id = reinterpret_cast<intptr_t>(socket); | |
| 625 Dart_SetReturnValue(args, Dart_NewInteger(id)); | 638 Dart_SetReturnValue(args, Dart_NewInteger(id)); |
| 626 } | 639 } |
| 627 | 640 |
| 628 | 641 |
| 629 void FUNCTION_NAME(Socket_SetSocketId)(Dart_NativeArguments args) { | 642 void FUNCTION_NAME(Socket_SetSocketId)(Dart_NativeArguments args) { |
| 630 intptr_t id = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 1)); | 643 intptr_t id = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 1)); |
| 631 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), id); | 644 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), id, false); |
| 632 } | 645 } |
| 633 | 646 |
| 634 | 647 |
| 635 void FUNCTION_NAME(ServerSocket_CreateBindListen)(Dart_NativeArguments args) { | 648 void FUNCTION_NAME(ServerSocket_CreateBindListen)(Dart_NativeArguments args) { |
| 636 RawAddr addr; | 649 RawAddr addr; |
| 637 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); | 650 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); |
| 638 int64_t port = DartUtils::GetInt64ValueCheckRange( | 651 int64_t port = DartUtils::GetInt64ValueCheckRange( |
| 639 Dart_GetNativeArgument(args, 2), 0, 65535); | 652 Dart_GetNativeArgument(args, 2), 0, 65535); |
| 640 SocketAddress::SetAddrPort(&addr, port); | 653 SocketAddress::SetAddrPort(&addr, port); |
| 641 int64_t backlog = DartUtils::GetInt64ValueCheckRange( | 654 int64_t backlog = DartUtils::GetInt64ValueCheckRange( |
| 642 Dart_GetNativeArgument(args, 3), 0, 65535); | 655 Dart_GetNativeArgument(args, 3), 0, 65535); |
| 643 bool v6_only = DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 4)); | 656 bool v6_only = DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 4)); |
| 644 bool shared = DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 5)); | 657 bool shared = DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 5)); |
| 645 | 658 |
| 646 Dart_Handle socket_object = Dart_GetNativeArgument(args, 0); | 659 Dart_Handle socket_object = Dart_GetNativeArgument(args, 0); |
| 647 Dart_Handle result = ListeningSocketRegistry::Instance()->CreateBindListen( | 660 Dart_Handle result = ListeningSocketRegistry::Instance()->CreateBindListen( |
| 648 socket_object, addr, backlog, v6_only, shared); | 661 socket_object, addr, backlog, v6_only, shared); |
| 649 Dart_SetReturnValue(args, result); | 662 Dart_SetReturnValue(args, result); |
| 650 } | 663 } |
| 651 | 664 |
| 652 | 665 |
| 653 void FUNCTION_NAME(ServerSocket_Accept)(Dart_NativeArguments args) { | 666 void FUNCTION_NAME(ServerSocket_Accept)(Dart_NativeArguments args) { |
| 654 intptr_t socket = | 667 Socket* socket = |
| 655 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 668 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
| 656 intptr_t new_socket = ServerSocket::Accept(socket); | 669 intptr_t new_socket = ServerSocket::Accept(socket->fd()); |
| 657 if (new_socket >= 0) { | 670 if (new_socket >= 0) { |
| 658 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 1), new_socket); | 671 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 1), new_socket, |
| 672 false); | |
| 659 Dart_SetReturnValue(args, Dart_True()); | 673 Dart_SetReturnValue(args, Dart_True()); |
| 660 } else if (new_socket == ServerSocket::kTemporaryFailure) { | 674 } else if (new_socket == ServerSocket::kTemporaryFailure) { |
| 661 Dart_SetReturnValue(args, Dart_False()); | 675 Dart_SetReturnValue(args, Dart_False()); |
| 662 } else { | 676 } else { |
| 663 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 677 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
| 664 } | 678 } |
| 665 } | 679 } |
| 666 | 680 |
| 667 | 681 |
| 668 CObject* Socket::LookupRequest(const CObjectArray& request) { | 682 CObject* Socket::LookupRequest(const CObjectArray& request) { |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 784 result = CObject::NewOSError(os_error); | 798 result = CObject::NewOSError(os_error); |
| 785 delete os_error; | 799 delete os_error; |
| 786 } | 800 } |
| 787 return result; | 801 return result; |
| 788 } | 802 } |
| 789 return CObject::IllegalArgumentError(); | 803 return CObject::IllegalArgumentError(); |
| 790 } | 804 } |
| 791 | 805 |
| 792 | 806 |
| 793 void FUNCTION_NAME(Socket_GetOption)(Dart_NativeArguments args) { | 807 void FUNCTION_NAME(Socket_GetOption)(Dart_NativeArguments args) { |
| 794 intptr_t socket = | 808 Socket* socket = |
| 795 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 809 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
| 796 int64_t option = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); | 810 int64_t option = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); |
| 797 intptr_t protocol = static_cast<intptr_t>( | 811 intptr_t protocol = static_cast<intptr_t>( |
| 798 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2))); | 812 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2))); |
| 799 bool ok = false; | 813 bool ok = false; |
| 800 switch (option) { | 814 switch (option) { |
| 801 case 0: { // TCP_NODELAY. | 815 case 0: { // TCP_NODELAY. |
| 802 bool enabled; | 816 bool enabled; |
| 803 ok = Socket::GetNoDelay(socket, &enabled); | 817 ok = Socket::GetNoDelay(socket->fd(), &enabled); |
| 804 if (ok) { | 818 if (ok) { |
| 805 Dart_SetReturnValue(args, enabled ? Dart_True() : Dart_False()); | 819 Dart_SetReturnValue(args, enabled ? Dart_True() : Dart_False()); |
| 806 } | 820 } |
| 807 break; | 821 break; |
| 808 } | 822 } |
| 809 case 1: { // IP_MULTICAST_LOOP. | 823 case 1: { // IP_MULTICAST_LOOP. |
| 810 bool enabled; | 824 bool enabled; |
| 811 ok = Socket::GetMulticastLoop(socket, protocol, &enabled); | 825 ok = Socket::GetMulticastLoop(socket->fd(), protocol, &enabled); |
| 812 if (ok) { | 826 if (ok) { |
| 813 Dart_SetReturnValue(args, enabled ? Dart_True() : Dart_False()); | 827 Dart_SetReturnValue(args, enabled ? Dart_True() : Dart_False()); |
| 814 } | 828 } |
| 815 break; | 829 break; |
| 816 } | 830 } |
| 817 case 2: { // IP_MULTICAST_TTL. | 831 case 2: { // IP_MULTICAST_TTL. |
| 818 int value; | 832 int value; |
| 819 ok = Socket::GetMulticastHops(socket, protocol, &value); | 833 ok = Socket::GetMulticastHops(socket->fd(), protocol, &value); |
| 820 if (ok) { | 834 if (ok) { |
| 821 Dart_SetReturnValue(args, Dart_NewInteger(value)); | 835 Dart_SetReturnValue(args, Dart_NewInteger(value)); |
| 822 } | 836 } |
| 823 break; | 837 break; |
| 824 } | 838 } |
| 825 case 3: { // IP_MULTICAST_IF. | 839 case 3: { // IP_MULTICAST_IF. |
| 826 UNIMPLEMENTED(); | 840 UNIMPLEMENTED(); |
| 827 break; | 841 break; |
| 828 } | 842 } |
| 829 case 4: { // IP_BROADCAST. | 843 case 4: { // IP_BROADCAST. |
| 830 bool enabled; | 844 bool enabled; |
| 831 ok = Socket::GetBroadcast(socket, &enabled); | 845 ok = Socket::GetBroadcast(socket->fd(), &enabled); |
| 832 if (ok) { | 846 if (ok) { |
| 833 Dart_SetReturnValue(args, enabled ? Dart_True() : Dart_False()); | 847 Dart_SetReturnValue(args, enabled ? Dart_True() : Dart_False()); |
| 834 } | 848 } |
| 835 break; | 849 break; |
| 836 } | 850 } |
| 837 default: | 851 default: |
| 838 UNREACHABLE(); | 852 UNREACHABLE(); |
| 839 break; | 853 break; |
| 840 } | 854 } |
| 841 // In case of failure the return value is not set above. | 855 // In case of failure the return value is not set above. |
| 842 if (!ok) { | 856 if (!ok) { |
| 843 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 857 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
| 844 } | 858 } |
| 845 } | 859 } |
| 846 | 860 |
| 847 | 861 |
| 848 void FUNCTION_NAME(Socket_SetOption)(Dart_NativeArguments args) { | 862 void FUNCTION_NAME(Socket_SetOption)(Dart_NativeArguments args) { |
| 849 bool result = false; | 863 bool result = false; |
| 850 intptr_t socket = | 864 Socket* socket = |
| 851 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 865 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
| 852 int64_t option = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); | 866 int64_t option = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); |
| 853 int64_t protocol = DartUtils::GetInt64ValueCheckRange( | 867 int64_t protocol = DartUtils::GetInt64ValueCheckRange( |
| 854 Dart_GetNativeArgument(args, 2), SocketAddress::TYPE_IPV4, | 868 Dart_GetNativeArgument(args, 2), SocketAddress::TYPE_IPV4, |
| 855 SocketAddress::TYPE_IPV6); | 869 SocketAddress::TYPE_IPV6); |
| 856 switch (option) { | 870 switch (option) { |
| 857 case 0: // TCP_NODELAY. | 871 case 0: // TCP_NODELAY. |
| 858 result = Socket::SetNoDelay( | 872 result = Socket::SetNoDelay( |
| 859 socket, DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 3))); | 873 socket->fd(), |
| 874 DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 3))); | |
| 860 break; | 875 break; |
| 861 case 1: // IP_MULTICAST_LOOP. | 876 case 1: // IP_MULTICAST_LOOP. |
| 862 result = Socket::SetMulticastLoop( | 877 result = Socket::SetMulticastLoop( |
| 863 socket, protocol, | 878 socket->fd(), protocol, |
| 864 DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 3))); | 879 DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 3))); |
| 865 break; | 880 break; |
| 866 case 2: // IP_MULTICAST_TTL. | 881 case 2: // IP_MULTICAST_TTL. |
| 867 result = Socket::SetMulticastHops( | 882 result = Socket::SetMulticastHops( |
| 868 socket, protocol, | 883 socket->fd(), protocol, |
| 869 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3))); | 884 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3))); |
| 870 break; | 885 break; |
| 871 case 3: { // IP_MULTICAST_IF. | 886 case 3: { // IP_MULTICAST_IF. |
| 872 UNIMPLEMENTED(); | 887 UNIMPLEMENTED(); |
| 873 break; | 888 break; |
| 874 } | 889 } |
| 875 case 4: // IP_BROADCAST. | 890 case 4: // IP_BROADCAST. |
| 876 result = Socket::SetBroadcast( | 891 result = Socket::SetBroadcast( |
| 877 socket, DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 3))); | 892 socket->fd(), |
| 893 DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 3))); | |
| 878 break; | 894 break; |
| 879 default: | 895 default: |
| 880 Dart_PropagateError(Dart_NewApiError("Value outside expected range")); | 896 Dart_PropagateError(Dart_NewApiError("Value outside expected range")); |
| 881 break; | 897 break; |
| 882 } | 898 } |
| 883 if (result) { | 899 if (result) { |
| 884 Dart_SetReturnValue(args, Dart_Null()); | 900 Dart_SetReturnValue(args, Dart_Null()); |
| 885 } else { | 901 } else { |
| 886 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 902 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
| 887 } | 903 } |
| 888 } | 904 } |
| 889 | 905 |
| 890 | 906 |
| 891 void FUNCTION_NAME(Socket_JoinMulticast)(Dart_NativeArguments args) { | 907 void FUNCTION_NAME(Socket_JoinMulticast)(Dart_NativeArguments args) { |
| 892 intptr_t socket = | 908 Socket* socket = |
| 893 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 909 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
| 894 RawAddr addr; | 910 RawAddr addr; |
| 895 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); | 911 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); |
| 896 RawAddr interface; | 912 RawAddr interface; |
| 897 if (Dart_GetNativeArgument(args, 2) != Dart_Null()) { | 913 if (Dart_GetNativeArgument(args, 2) != Dart_Null()) { |
| 898 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 2), &interface); | 914 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 2), &interface); |
| 899 } | 915 } |
| 900 int interfaceIndex = | 916 int interfaceIndex = |
| 901 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); | 917 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); |
| 902 if (Socket::JoinMulticast(socket, addr, interface, interfaceIndex)) { | 918 if (Socket::JoinMulticast(socket->fd(), addr, interface, interfaceIndex)) { |
| 903 Dart_SetReturnValue(args, Dart_Null()); | 919 Dart_SetReturnValue(args, Dart_Null()); |
| 904 } else { | 920 } else { |
| 905 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 921 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
| 906 } | 922 } |
| 907 } | 923 } |
| 908 | 924 |
| 909 | 925 |
| 910 void FUNCTION_NAME(Socket_LeaveMulticast)(Dart_NativeArguments args) { | 926 void FUNCTION_NAME(Socket_LeaveMulticast)(Dart_NativeArguments args) { |
| 911 intptr_t socket = | 927 Socket* socket = |
| 912 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 928 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
| 913 RawAddr addr; | 929 RawAddr addr; |
| 914 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); | 930 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); |
| 915 RawAddr interface; | 931 RawAddr interface; |
| 916 if (Dart_GetNativeArgument(args, 2) != Dart_Null()) { | 932 if (Dart_GetNativeArgument(args, 2) != Dart_Null()) { |
| 917 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 2), &interface); | 933 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 2), &interface); |
| 918 } | 934 } |
| 919 int interfaceIndex = | 935 int interfaceIndex = |
| 920 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); | 936 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); |
| 921 if (Socket::LeaveMulticast(socket, addr, interface, interfaceIndex)) { | 937 if (Socket::LeaveMulticast(socket->fd(), addr, interface, interfaceIndex)) { |
| 922 Dart_SetReturnValue(args, Dart_Null()); | 938 Dart_SetReturnValue(args, Dart_Null()); |
| 923 } else { | 939 } else { |
| 924 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 940 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
| 925 } | 941 } |
| 926 } | 942 } |
| 927 | 943 |
| 928 | 944 |
| 929 void Socket::SetSocketIdNativeField(Dart_Handle socket, intptr_t id) { | 945 static void SocketFinalizer(void* isolate_data, |
| 930 Dart_Handle err = | 946 Dart_WeakPersistentHandle handle, |
| 931 Dart_SetNativeInstanceField(socket, kSocketIdNativeField, id); | 947 void* data) { |
| 948 Socket* socket = reinterpret_cast<Socket*>(data); | |
| 949 if (socket->fd() >= 0) { | |
| 950 const int64_t flags = 1 << kCloseCommand; | |
| 951 socket->Retain(); | |
| 952 EventHandler::SendFromNative(reinterpret_cast<intptr_t>(socket), | |
| 953 socket->port(), flags); | |
| 954 } | |
| 955 socket->Release(); | |
| 956 } | |
| 957 | |
| 958 | |
| 959 static void ListeningSocketFinalizer(void* isolate_data, | |
| 960 Dart_WeakPersistentHandle handle, | |
| 961 void* data) { | |
| 962 Socket* socket = reinterpret_cast<Socket*>(data); | |
| 963 if (socket->fd() >= 0) { | |
| 964 const int64_t flags = (1 << kListeningSocket) | (1 << kCloseCommand); | |
| 965 socket->Retain(); | |
| 966 EventHandler::SendFromNative(reinterpret_cast<intptr_t>(socket), | |
| 967 socket->port(), flags); | |
| 968 } | |
| 969 socket->Release(); | |
| 970 } | |
| 971 | |
| 972 | |
| 973 void Socket::ReuseSocketIdNativeField(Dart_Handle handle, | |
| 974 Socket* socket, | |
| 975 bool listening) { | |
| 976 Dart_Handle err = Dart_SetNativeInstanceField( | |
| 977 handle, kSocketIdNativeField, reinterpret_cast<intptr_t>(socket)); | |
| 932 if (Dart_IsError(err)) { | 978 if (Dart_IsError(err)) { |
| 933 Dart_PropagateError(err); | 979 Dart_PropagateError(err); |
| 934 } | 980 } |
| 981 if (listening) { | |
| 982 Dart_NewWeakPersistentHandle(handle, reinterpret_cast<void*>(socket), | |
| 983 sizeof(Socket), ListeningSocketFinalizer); | |
| 984 } else { | |
| 985 Dart_NewWeakPersistentHandle(handle, reinterpret_cast<void*>(socket), | |
| 986 sizeof(Socket), SocketFinalizer); | |
| 987 } | |
| 935 } | 988 } |
| 936 | 989 |
| 937 | 990 |
| 938 intptr_t Socket::GetSocketIdNativeField(Dart_Handle socket_obj) { | 991 void Socket::SetSocketIdNativeField(Dart_Handle handle, |
| 939 intptr_t socket = 0; | 992 intptr_t id, |
| 993 bool listening) { | |
| 994 Socket* socket = new Socket(id); | |
| 995 ReuseSocketIdNativeField(handle, socket, listening); | |
| 996 } | |
| 997 | |
| 998 | |
| 999 Socket* Socket::GetSocketIdNativeField(Dart_Handle socket_obj) { | |
| 1000 intptr_t id; | |
| 940 Dart_Handle err = | 1001 Dart_Handle err = |
| 941 Dart_GetNativeInstanceField(socket_obj, kSocketIdNativeField, &socket); | 1002 Dart_GetNativeInstanceField(socket_obj, kSocketIdNativeField, &id); |
| 942 if (Dart_IsError(err)) { | 1003 if (Dart_IsError(err)) { |
| 943 Dart_PropagateError(err); | 1004 Dart_PropagateError(err); |
| 944 } | 1005 } |
| 1006 Socket* socket = reinterpret_cast<Socket*>(id); | |
| 945 return socket; | 1007 return socket; |
| 946 } | 1008 } |
| 947 | 1009 |
| 948 } // namespace bin | 1010 } // namespace bin |
| 949 } // namespace dart | 1011 } // namespace dart |
| 950 | 1012 |
| 951 #endif // !defined(DART_IO_DISABLED) | 1013 #endif // !defined(DART_IO_DISABLED) |
| OLD | NEW |