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/io_buffer.h" | 5 #include "bin/io_buffer.h" |
6 #include "bin/isolate_data.h" | 6 #include "bin/isolate_data.h" |
7 #include "bin/socket.h" | 7 #include "bin/socket.h" |
8 #include "bin/dartutils.h" | 8 #include "bin/dartutils.h" |
9 #include "bin/thread.h" | 9 #include "bin/thread.h" |
10 #include "bin/utils.h" | 10 #include "bin/utils.h" |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 } else { | 92 } else { |
93 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 93 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
94 } | 94 } |
95 } | 95 } |
96 | 96 |
97 | 97 |
98 void FUNCTION_NAME(Socket_Read)(Dart_NativeArguments args) { | 98 void FUNCTION_NAME(Socket_Read)(Dart_NativeArguments args) { |
99 static bool short_socket_reads = Dart_IsVMFlagSet("short_socket_read"); | 99 static bool short_socket_reads = Dart_IsVMFlagSet("short_socket_read"); |
100 intptr_t socket = | 100 intptr_t socket = |
101 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 101 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
102 intptr_t available = Socket::Available(socket); | 102 int64_t length = 0; |
103 if (available > 0) { | 103 if (DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 1), &length)) { |
104 int64_t length = 0; | 104 if (short_socket_reads) { |
105 if (DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 1), &length)) { | 105 length = (length + 1) / 2; |
106 if (length == -1 || available < length) { | 106 } |
107 length = available; | 107 uint8_t* buffer = NULL; |
108 } | 108 Dart_Handle result = IOBuffer::Allocate(length, &buffer); |
109 if (short_socket_reads) { | 109 if (Dart_IsError(result)) Dart_PropagateError(result); |
110 length = (length + 1) / 2; | 110 ASSERT(buffer != NULL); |
111 } | 111 intptr_t bytes_read = Socket::Read(socket, buffer, length); |
112 uint8_t* buffer = NULL; | 112 if (bytes_read == length) { |
113 Dart_Handle result = IOBuffer::Allocate(length, &buffer); | 113 Dart_SetReturnValue(args, result); |
114 if (Dart_IsError(result)) Dart_PropagateError(result); | 114 } else if (bytes_read < length) { |
115 ASSERT(buffer != NULL); | 115 // On MacOS when reading from a tty Ctrl-D will result in reading one |
116 intptr_t bytes_read = Socket::Read(socket, buffer, length); | 116 // less byte then reported as available. |
117 if (bytes_read == length) { | 117 if (bytes_read == 0) { |
118 Dart_SetReturnValue(args, result); | 118 Dart_SetReturnValue(args, Dart_Null()); |
119 } else if (bytes_read < length) { | |
120 // On MacOS when reading from a tty Ctrl-D will result in reading one | |
121 // less byte then reported as available. | |
122 if (bytes_read == 0) { | |
123 Dart_SetReturnValue(args, Dart_Null()); | |
124 } else { | |
125 uint8_t* new_buffer = NULL; | |
126 Dart_Handle new_result = IOBuffer::Allocate(bytes_read, &new_buffer); | |
127 if (Dart_IsError(new_result)) Dart_PropagateError(new_result); | |
128 ASSERT(new_buffer != NULL); | |
129 memmove(new_buffer, buffer, bytes_read); | |
130 Dart_SetReturnValue(args, new_result); | |
131 } | |
132 } else { | 119 } else { |
133 ASSERT(bytes_read == -1); | 120 uint8_t* new_buffer = NULL; |
134 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 121 Dart_Handle new_result = IOBuffer::Allocate(bytes_read, &new_buffer); |
| 122 if (Dart_IsError(new_result)) Dart_PropagateError(new_result); |
| 123 ASSERT(new_buffer != NULL); |
| 124 memmove(new_buffer, buffer, bytes_read); |
| 125 Dart_SetReturnValue(args, new_result); |
135 } | 126 } |
136 } else { | 127 } else { |
137 OSError os_error(-1, "Invalid argument", OSError::kUnknown); | 128 ASSERT(bytes_read == -1); |
138 Dart_Handle err = DartUtils::NewDartOSError(&os_error); | 129 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
139 if (Dart_IsError(err)) Dart_PropagateError(err); | |
140 Dart_SetReturnValue(args, err); | |
141 } | 130 } |
142 } else if (available == 0) { | |
143 Dart_SetReturnValue(args, Dart_Null()); | |
144 } else { | 131 } else { |
145 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 132 OSError os_error(-1, "Invalid argument", OSError::kUnknown); |
| 133 Dart_Handle err = DartUtils::NewDartOSError(&os_error); |
| 134 if (Dart_IsError(err)) Dart_PropagateError(err); |
| 135 Dart_SetReturnValue(args, err); |
146 } | 136 } |
147 } | 137 } |
148 | 138 |
149 | 139 |
150 void FUNCTION_NAME(Socket_RecvFrom)(Dart_NativeArguments args) { | 140 void FUNCTION_NAME(Socket_RecvFrom)(Dart_NativeArguments args) { |
151 intptr_t socket = | 141 intptr_t socket = |
152 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 142 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
153 | 143 |
154 // TODO(sgjesse): Use a MTU value here. Only the loopback adapter can | 144 // TODO(sgjesse): Use a MTU value here. Only the loopback adapter can |
155 // handle 64k datagrams. | 145 // handle 64k datagrams. |
(...skipping 542 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
698 intptr_t Socket::GetSocketIdNativeField(Dart_Handle socket_obj) { | 688 intptr_t Socket::GetSocketIdNativeField(Dart_Handle socket_obj) { |
699 intptr_t socket = 0; | 689 intptr_t socket = 0; |
700 Dart_Handle err = | 690 Dart_Handle err = |
701 Dart_GetNativeInstanceField(socket_obj, kSocketIdNativeField, &socket); | 691 Dart_GetNativeInstanceField(socket_obj, kSocketIdNativeField, &socket); |
702 if (Dart_IsError(err)) Dart_PropagateError(err); | 692 if (Dart_IsError(err)) Dart_PropagateError(err); |
703 return socket; | 693 return socket; |
704 } | 694 } |
705 | 695 |
706 } // namespace bin | 696 } // namespace bin |
707 } // namespace dart | 697 } // namespace dart |
OLD | NEW |