| 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/eventhandler.h" |
| (...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 346 if (DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 1), &length)) { | 346 if (DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 1), &length)) { |
| 347 if (short_socket_read) { | 347 if (short_socket_read) { |
| 348 length = (length + 1) / 2; | 348 length = (length + 1) / 2; |
| 349 } | 349 } |
| 350 uint8_t* buffer = NULL; | 350 uint8_t* buffer = NULL; |
| 351 Dart_Handle result = IOBuffer::Allocate(length, &buffer); | 351 Dart_Handle result = IOBuffer::Allocate(length, &buffer); |
| 352 if (Dart_IsError(result)) { | 352 if (Dart_IsError(result)) { |
| 353 Dart_PropagateError(result); | 353 Dart_PropagateError(result); |
| 354 } | 354 } |
| 355 ASSERT(buffer != NULL); | 355 ASSERT(buffer != NULL); |
| 356 intptr_t bytes_read = SocketBase::Read(socket->fd(), buffer, length); | 356 intptr_t bytes_read = |
| 357 SocketBase::Read(socket->fd(), buffer, length, SocketBase::kAsync); |
| 357 if (bytes_read == length) { | 358 if (bytes_read == length) { |
| 358 Dart_SetReturnValue(args, result); | 359 Dart_SetReturnValue(args, result); |
| 359 } else if (bytes_read > 0) { | 360 } else if (bytes_read > 0) { |
| 360 uint8_t* new_buffer = NULL; | 361 uint8_t* new_buffer = NULL; |
| 361 Dart_Handle new_result = IOBuffer::Allocate(bytes_read, &new_buffer); | 362 Dart_Handle new_result = IOBuffer::Allocate(bytes_read, &new_buffer); |
| 362 if (Dart_IsError(new_result)) { | 363 if (Dart_IsError(new_result)) { |
| 363 Dart_PropagateError(new_result); | 364 Dart_PropagateError(new_result); |
| 364 } | 365 } |
| 365 ASSERT(new_buffer != NULL); | 366 ASSERT(new_buffer != NULL); |
| 366 memmove(new_buffer, buffer, bytes_read); | 367 memmove(new_buffer, buffer, bytes_read); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 386 | 387 |
| 387 // TODO(sgjesse): Use a MTU value here. Only the loopback adapter can | 388 // TODO(sgjesse): Use a MTU value here. Only the loopback adapter can |
| 388 // handle 64k datagrams. | 389 // handle 64k datagrams. |
| 389 IsolateData* isolate_data = | 390 IsolateData* isolate_data = |
| 390 reinterpret_cast<IsolateData*>(Dart_CurrentIsolateData()); | 391 reinterpret_cast<IsolateData*>(Dart_CurrentIsolateData()); |
| 391 if (isolate_data->udp_receive_buffer == NULL) { | 392 if (isolate_data->udp_receive_buffer == NULL) { |
| 392 isolate_data->udp_receive_buffer = | 393 isolate_data->udp_receive_buffer = |
| 393 reinterpret_cast<uint8_t*>(malloc(65536)); | 394 reinterpret_cast<uint8_t*>(malloc(65536)); |
| 394 } | 395 } |
| 395 RawAddr addr; | 396 RawAddr addr; |
| 396 intptr_t bytes_read = SocketBase::RecvFrom( | 397 intptr_t bytes_read = |
| 397 socket->fd(), isolate_data->udp_receive_buffer, 65536, &addr); | 398 SocketBase::RecvFrom(socket->fd(), isolate_data->udp_receive_buffer, |
| 399 65536, &addr, SocketBase::kAsync); |
| 398 if (bytes_read == 0) { | 400 if (bytes_read == 0) { |
| 399 Dart_SetReturnValue(args, Dart_Null()); | 401 Dart_SetReturnValue(args, Dart_Null()); |
| 400 return; | 402 return; |
| 401 } | 403 } |
| 402 if (bytes_read < 0) { | 404 if (bytes_read < 0) { |
| 403 ASSERT(bytes_read == -1); | 405 ASSERT(bytes_read == -1); |
| 404 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 406 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
| 405 return; | 407 return; |
| 406 } | 408 } |
| 407 // Datagram data read. Copy into buffer of the exact size, | 409 // Datagram data read. Copy into buffer of the exact size, |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 467 Dart_TypedData_Type type; | 469 Dart_TypedData_Type type; |
| 468 uint8_t* buffer = NULL; | 470 uint8_t* buffer = NULL; |
| 469 intptr_t len; | 471 intptr_t len; |
| 470 Dart_Handle result = Dart_TypedDataAcquireData( | 472 Dart_Handle result = Dart_TypedDataAcquireData( |
| 471 buffer_obj, &type, reinterpret_cast<void**>(&buffer), &len); | 473 buffer_obj, &type, reinterpret_cast<void**>(&buffer), &len); |
| 472 if (Dart_IsError(result)) { | 474 if (Dart_IsError(result)) { |
| 473 Dart_PropagateError(result); | 475 Dart_PropagateError(result); |
| 474 } | 476 } |
| 475 ASSERT((offset + length) <= len); | 477 ASSERT((offset + length) <= len); |
| 476 buffer += offset; | 478 buffer += offset; |
| 477 intptr_t bytes_written = SocketBase::Write(socket->fd(), buffer, length); | 479 intptr_t bytes_written = |
| 480 SocketBase::Write(socket->fd(), buffer, length, SocketBase::kAsync); |
| 478 if (bytes_written >= 0) { | 481 if (bytes_written >= 0) { |
| 479 Dart_TypedDataReleaseData(buffer_obj); | 482 Dart_TypedDataReleaseData(buffer_obj); |
| 480 if (short_write) { | 483 if (short_write) { |
| 481 // If the write was forced 'short', indicate by returning the negative | 484 // If the write was forced 'short', indicate by returning the negative |
| 482 // number of bytes. A forced short write may not trigger a write event. | 485 // number of bytes. A forced short write may not trigger a write event. |
| 483 Dart_SetReturnValue(args, Dart_NewInteger(-bytes_written)); | 486 Dart_SetReturnValue(args, Dart_NewInteger(-bytes_written)); |
| 484 } else { | 487 } else { |
| 485 Dart_SetReturnValue(args, Dart_NewInteger(bytes_written)); | 488 Dart_SetReturnValue(args, Dart_NewInteger(bytes_written)); |
| 486 } | 489 } |
| 487 } else { | 490 } else { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 509 Dart_TypedData_Type type; | 512 Dart_TypedData_Type type; |
| 510 uint8_t* buffer = NULL; | 513 uint8_t* buffer = NULL; |
| 511 intptr_t len; | 514 intptr_t len; |
| 512 Dart_Handle result = Dart_TypedDataAcquireData( | 515 Dart_Handle result = Dart_TypedDataAcquireData( |
| 513 buffer_obj, &type, reinterpret_cast<void**>(&buffer), &len); | 516 buffer_obj, &type, reinterpret_cast<void**>(&buffer), &len); |
| 514 if (Dart_IsError(result)) { | 517 if (Dart_IsError(result)) { |
| 515 Dart_PropagateError(result); | 518 Dart_PropagateError(result); |
| 516 } | 519 } |
| 517 ASSERT((offset + length) <= len); | 520 ASSERT((offset + length) <= len); |
| 518 buffer += offset; | 521 buffer += offset; |
| 519 intptr_t bytes_written = | 522 intptr_t bytes_written = SocketBase::SendTo(socket->fd(), buffer, length, |
| 520 SocketBase::SendTo(socket->fd(), buffer, length, addr); | 523 addr, SocketBase::kAsync); |
| 521 if (bytes_written >= 0) { | 524 if (bytes_written >= 0) { |
| 522 Dart_TypedDataReleaseData(buffer_obj); | 525 Dart_TypedDataReleaseData(buffer_obj); |
| 523 Dart_SetReturnValue(args, Dart_NewInteger(bytes_written)); | 526 Dart_SetReturnValue(args, Dart_NewInteger(bytes_written)); |
| 524 } else { | 527 } else { |
| 525 // Extract OSError before we release data, as it may override the error. | 528 // Extract OSError before we release data, as it may override the error. |
| 526 OSError os_error; | 529 OSError os_error; |
| 527 Dart_TypedDataReleaseData(buffer_obj); | 530 Dart_TypedDataReleaseData(buffer_obj); |
| 528 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); | 531 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); |
| 529 } | 532 } |
| 530 } | 533 } |
| (...skipping 470 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1001 Dart_PropagateError(err); | 1004 Dart_PropagateError(err); |
| 1002 } | 1005 } |
| 1003 Socket* socket = reinterpret_cast<Socket*>(id); | 1006 Socket* socket = reinterpret_cast<Socket*>(id); |
| 1004 return socket; | 1007 return socket; |
| 1005 } | 1008 } |
| 1006 | 1009 |
| 1007 } // namespace bin | 1010 } // namespace bin |
| 1008 } // namespace dart | 1011 } // namespace dart |
| 1009 | 1012 |
| 1010 #endif // !defined(DART_IO_DISABLED) | 1013 #endif // !defined(DART_IO_DISABLED) |
| OLD | NEW |