| 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 "bin/socket.h" | 5 #include "bin/socket.h" |
| 6 | 6 |
| 7 #include "bin/dartutils.h" | 7 #include "bin/dartutils.h" |
| 8 #include "bin/eventhandler.h" | 8 #include "bin/eventhandler.h" |
| 9 #include "bin/io_buffer.h" | 9 #include "bin/io_buffer.h" |
| 10 #include "bin/isolate_data.h" | 10 #include "bin/isolate_data.h" |
| (...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 358 } else { | 358 } else { |
| 359 OSError os_error(-1, "Invalid argument", OSError::kUnknown); | 359 OSError os_error(-1, "Invalid argument", OSError::kUnknown); |
| 360 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); | 360 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); |
| 361 } | 361 } |
| 362 } | 362 } |
| 363 | 363 |
| 364 void FUNCTION_NAME(Socket_RecvFrom)(Dart_NativeArguments args) { | 364 void FUNCTION_NAME(Socket_RecvFrom)(Dart_NativeArguments args) { |
| 365 Socket* socket = | 365 Socket* socket = |
| 366 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 366 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
| 367 | 367 |
| 368 const intptr_t available = SocketBase::Available(socket->fd()); | 368 // TODO(sgjesse): Use a MTU value here. Only the loopback adapter can |
| 369 if (available < 0) { | 369 // handle 64k datagrams. |
| 370 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 370 IsolateData* isolate_data = |
| 371 return; | 371 reinterpret_cast<IsolateData*>(Dart_CurrentIsolateData()); |
| 372 if (isolate_data->udp_receive_buffer == NULL) { |
| 373 isolate_data->udp_receive_buffer = |
| 374 reinterpret_cast<uint8_t*>(malloc(65536)); |
| 372 } | 375 } |
| 373 if (available == 0) { | |
| 374 Dart_SetReturnValue(args, Dart_Null()); | |
| 375 return; | |
| 376 } | |
| 377 | |
| 378 // Allocate a buffer of the exact size into which data can be read. | |
| 379 uint8_t* data_buffer = NULL; | |
| 380 Dart_Handle data = IOBuffer::Allocate(available, &data_buffer); | |
| 381 if (Dart_IsError(data)) { | |
| 382 Dart_PropagateError(data); | |
| 383 } | |
| 384 ASSERT(data_buffer != NULL); | |
| 385 | |
| 386 // Read data into the buffer. | |
| 387 RawAddr addr; | 376 RawAddr addr; |
| 388 const intptr_t bytes_read = SocketBase::RecvFrom( | 377 intptr_t bytes_read = |
| 389 socket->fd(), data_buffer, available, &addr, SocketBase::kAsync); | 378 SocketBase::RecvFrom(socket->fd(), isolate_data->udp_receive_buffer, |
| 379 65536, &addr, SocketBase::kAsync); |
| 390 if (bytes_read == 0) { | 380 if (bytes_read == 0) { |
| 391 Dart_SetReturnValue(args, Dart_Null()); | 381 Dart_SetReturnValue(args, Dart_Null()); |
| 392 return; | 382 return; |
| 393 } | 383 } |
| 394 if (bytes_read < 0) { | 384 if (bytes_read < 0) { |
| 395 ASSERT(bytes_read == -1); | 385 ASSERT(bytes_read == -1); |
| 396 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 386 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
| 397 return; | 387 return; |
| 398 } | 388 } |
| 389 // Datagram data read. Copy into buffer of the exact size, |
| 390 ASSERT(bytes_read > 0); |
| 391 uint8_t* data_buffer = NULL; |
| 392 Dart_Handle data = IOBuffer::Allocate(bytes_read, &data_buffer); |
| 393 if (Dart_IsError(data)) { |
| 394 Dart_PropagateError(data); |
| 395 } |
| 396 ASSERT(data_buffer != NULL); |
| 397 memmove(data_buffer, isolate_data->udp_receive_buffer, bytes_read); |
| 399 | 398 |
| 400 // Get the port and clear it in the sockaddr structure. | 399 // Get the port and clear it in the sockaddr structure. |
| 401 int port = SocketAddress::GetAddrPort(addr); | 400 int port = SocketAddress::GetAddrPort(addr); |
| 402 if (addr.addr.sa_family == AF_INET) { | 401 if (addr.addr.sa_family == AF_INET) { |
| 403 addr.in.sin_port = 0; | 402 addr.in.sin_port = 0; |
| 404 } else { | 403 } else { |
| 405 ASSERT(addr.addr.sa_family == AF_INET6); | 404 ASSERT(addr.addr.sa_family == AF_INET6); |
| 406 addr.in6.sin6_port = 0; | 405 addr.in6.sin6_port = 0; |
| 407 } | 406 } |
| 408 // Format the address to a string using the numeric format. | 407 // Format the address to a string using the numeric format. |
| (...skipping 550 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 959 Dart_GetNativeInstanceField(socket_obj, kSocketIdNativeField, &id); | 958 Dart_GetNativeInstanceField(socket_obj, kSocketIdNativeField, &id); |
| 960 if (Dart_IsError(err)) { | 959 if (Dart_IsError(err)) { |
| 961 Dart_PropagateError(err); | 960 Dart_PropagateError(err); |
| 962 } | 961 } |
| 963 Socket* socket = reinterpret_cast<Socket*>(id); | 962 Socket* socket = reinterpret_cast<Socket*>(id); |
| 964 return socket; | 963 return socket; |
| 965 } | 964 } |
| 966 | 965 |
| 967 } // namespace bin | 966 } // namespace bin |
| 968 } // namespace dart | 967 } // namespace dart |
| OLD | NEW |