| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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/io_buffer.h" |
| 5 #include "bin/socket.h" | 6 #include "bin/socket.h" |
| 6 #include "bin/dartutils.h" | 7 #include "bin/dartutils.h" |
| 7 #include "bin/thread.h" | 8 #include "bin/thread.h" |
| 8 #include "bin/utils.h" | 9 #include "bin/utils.h" |
| 9 | 10 |
| 10 #include "platform/globals.h" | 11 #include "platform/globals.h" |
| 11 #include "platform/thread.h" | 12 #include "platform/thread.h" |
| 12 #include "platform/utils.h" | 13 #include "platform/utils.h" |
| 13 | 14 |
| 14 #include "include/dart_api.h" | 15 #include "include/dart_api.h" |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 intptr_t available = Socket::Available(socket); | 52 intptr_t available = Socket::Available(socket); |
| 52 if (available >= 0) { | 53 if (available >= 0) { |
| 53 Dart_SetReturnValue(args, Dart_NewInteger(available)); | 54 Dart_SetReturnValue(args, Dart_NewInteger(available)); |
| 54 } else { | 55 } else { |
| 55 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 56 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
| 56 } | 57 } |
| 57 Dart_ExitScope(); | 58 Dart_ExitScope(); |
| 58 } | 59 } |
| 59 | 60 |
| 60 | 61 |
| 62 void FUNCTION_NAME(Socket_Read)(Dart_NativeArguments args) { |
| 63 Dart_EnterScope(); |
| 64 Dart_Handle socket_obj = Dart_GetNativeArgument(args, 0); |
| 65 intptr_t socket = 0; |
| 66 Socket::GetSocketIdNativeField(socket_obj, &socket); |
| 67 intptr_t available = Socket::Available(socket); |
| 68 if (available > 0) { |
| 69 int64_t length = 0; |
| 70 Dart_Handle length_obj = Dart_GetNativeArgument(args, 1); |
| 71 if (DartUtils::GetInt64Value(length_obj, &length)) { |
| 72 if (length == -1 || available < length) { |
| 73 length = available; |
| 74 } |
| 75 uint8_t* buffer = NULL; |
| 76 Dart_Handle result = IOBuffer::Allocate(length, &buffer); |
| 77 if (Dart_IsError(result)) Dart_PropagateError(result); |
| 78 ASSERT(buffer != NULL); |
| 79 intptr_t bytes_read = Socket::Read(socket, buffer, length); |
| 80 if (bytes_read == length) { |
| 81 Dart_SetReturnValue(args, result); |
| 82 } else if (bytes_read == 0) { |
| 83 // On MacOS when reading from a tty Ctrl-D will result in one |
| 84 // byte reported as available. Attempting to read it out will |
| 85 // result in zero bytes read. When that happens there is no |
| 86 // data which is indicated by a null return value. |
| 87 Dart_SetReturnValue(args, Dart_Null()); |
| 88 } else { |
| 89 ASSERT(bytes_read == -1); |
| 90 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
| 91 } |
| 92 } else { |
| 93 OSError os_error(-1, "Invalid argument", OSError::kUnknown); |
| 94 Dart_Handle err = DartUtils::NewDartOSError(&os_error); |
| 95 if (Dart_IsError(err)) Dart_PropagateError(err); |
| 96 Dart_SetReturnValue(args, err); |
| 97 } |
| 98 } else if (available == 0) { |
| 99 Dart_SetReturnValue(args, Dart_Null()); |
| 100 } else { |
| 101 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
| 102 } |
| 103 |
| 104 Dart_ExitScope(); |
| 105 } |
| 106 |
| 107 |
| 61 void FUNCTION_NAME(Socket_ReadList)(Dart_NativeArguments args) { | 108 void FUNCTION_NAME(Socket_ReadList)(Dart_NativeArguments args) { |
| 62 Dart_EnterScope(); | 109 Dart_EnterScope(); |
| 63 Dart_Handle socket_obj = Dart_GetNativeArgument(args, 0); | 110 Dart_Handle socket_obj = Dart_GetNativeArgument(args, 0); |
| 64 intptr_t socket = 0; | 111 intptr_t socket = 0; |
| 65 Socket::GetSocketIdNativeField(socket_obj, &socket); | 112 Socket::GetSocketIdNativeField(socket_obj, &socket); |
| 66 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); | 113 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); |
| 67 int64_t offset = 0; | 114 int64_t offset = 0; |
| 68 int64_t length = 0; | 115 int64_t length = 0; |
| 69 Dart_Handle offset_obj = Dart_GetNativeArgument(args, 2); | 116 Dart_Handle offset_obj = Dart_GetNativeArgument(args, 2); |
| 70 Dart_Handle length_obj = Dart_GetNativeArgument(args, 3); | 117 Dart_Handle length_obj = Dart_GetNativeArgument(args, 3); |
| (...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 356 | 403 |
| 357 | 404 |
| 358 Dart_Handle Socket::SetSocketIdNativeField(Dart_Handle socket, intptr_t id) { | 405 Dart_Handle Socket::SetSocketIdNativeField(Dart_Handle socket, intptr_t id) { |
| 359 return Dart_SetNativeInstanceField(socket, kSocketIdNativeField, id); | 406 return Dart_SetNativeInstanceField(socket, kSocketIdNativeField, id); |
| 360 } | 407 } |
| 361 | 408 |
| 362 | 409 |
| 363 Dart_Handle Socket::GetSocketIdNativeField(Dart_Handle socket, intptr_t* id) { | 410 Dart_Handle Socket::GetSocketIdNativeField(Dart_Handle socket, intptr_t* id) { |
| 364 return Dart_GetNativeInstanceField(socket, kSocketIdNativeField, id); | 411 return Dart_GetNativeInstanceField(socket, kSocketIdNativeField, id); |
| 365 } | 412 } |
| OLD | NEW |