Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(69)

Side by Side Diff: runtime/bin/socket.cc

Issue 3009643002: Fix for Flutter issue 11260 (https://github.com/flutter/flutter/issues/11260) (Closed)
Patch Set: Address review comments. Created 3 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « runtime/bin/isolate_data.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 // TODO(sgjesse): Use a MTU value here. Only the loopback adapter can 368 const intptr_t available = SocketBase::Available(socket->fd());
369 // handle 64k datagrams. 369 if (available < 0) {
370 IsolateData* isolate_data = 370 Dart_SetReturnValue(args, DartUtils::NewDartOSError());
371 reinterpret_cast<IsolateData*>(Dart_CurrentIsolateData()); 371 return;
372 if (isolate_data->udp_receive_buffer == NULL) {
373 isolate_data->udp_receive_buffer =
374 reinterpret_cast<uint8_t*>(malloc(65536));
375 } 372 }
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.
376 RawAddr addr; 387 RawAddr addr;
377 intptr_t bytes_read = 388 const intptr_t bytes_read = SocketBase::RecvFrom(
378 SocketBase::RecvFrom(socket->fd(), isolate_data->udp_receive_buffer, 389 socket->fd(), data_buffer, available, &addr, SocketBase::kAsync);
379 65536, &addr, SocketBase::kAsync);
380 if (bytes_read == 0) { 390 if (bytes_read == 0) {
381 Dart_SetReturnValue(args, Dart_Null()); 391 Dart_SetReturnValue(args, Dart_Null());
382 return; 392 return;
383 } 393 }
384 if (bytes_read < 0) { 394 if (bytes_read < 0) {
385 ASSERT(bytes_read == -1); 395 ASSERT(bytes_read == -1);
386 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); 396 Dart_SetReturnValue(args, DartUtils::NewDartOSError());
387 return; 397 return;
388 } 398 }
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);
398 399
399 // Get the port and clear it in the sockaddr structure. 400 // Get the port and clear it in the sockaddr structure.
400 int port = SocketAddress::GetAddrPort(addr); 401 int port = SocketAddress::GetAddrPort(addr);
401 if (addr.addr.sa_family == AF_INET) { 402 if (addr.addr.sa_family == AF_INET) {
402 addr.in.sin_port = 0; 403 addr.in.sin_port = 0;
403 } else { 404 } else {
404 ASSERT(addr.addr.sa_family == AF_INET6); 405 ASSERT(addr.addr.sa_family == AF_INET6);
405 addr.in6.sin6_port = 0; 406 addr.in6.sin6_port = 0;
406 } 407 }
407 // Format the address to a string using the numeric format. 408 // Format the address to a string using the numeric format.
(...skipping 550 matching lines...) Expand 10 before | Expand all | Expand 10 after
958 Dart_GetNativeInstanceField(socket_obj, kSocketIdNativeField, &id); 959 Dart_GetNativeInstanceField(socket_obj, kSocketIdNativeField, &id);
959 if (Dart_IsError(err)) { 960 if (Dart_IsError(err)) {
960 Dart_PropagateError(err); 961 Dart_PropagateError(err);
961 } 962 }
962 Socket* socket = reinterpret_cast<Socket*>(id); 963 Socket* socket = reinterpret_cast<Socket*>(id);
963 return socket; 964 return socket;
964 } 965 }
965 966
966 } // namespace bin 967 } // namespace bin
967 } // namespace dart 968 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/bin/isolate_data.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698