| 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/socket.h" | 6 #include "bin/socket.h" |
| 7 #include "bin/dartutils.h" | 7 #include "bin/dartutils.h" |
| 8 #include "bin/thread.h" | 8 #include "bin/thread.h" |
| 9 #include "bin/utils.h" | 9 #include "bin/utils.h" |
| 10 | 10 |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 Dart_SetReturnValue(args, Dart_NewInteger(available)); | 123 Dart_SetReturnValue(args, Dart_NewInteger(available)); |
| 124 } else { | 124 } else { |
| 125 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 125 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
| 126 } | 126 } |
| 127 Dart_ExitScope(); | 127 Dart_ExitScope(); |
| 128 } | 128 } |
| 129 | 129 |
| 130 | 130 |
| 131 void FUNCTION_NAME(Socket_Read)(Dart_NativeArguments args) { | 131 void FUNCTION_NAME(Socket_Read)(Dart_NativeArguments args) { |
| 132 Dart_EnterScope(); | 132 Dart_EnterScope(); |
| 133 static bool short_socket_reads = Dart_IsVMFlagSet("short_socket_read"); |
| 133 Dart_Handle socket_obj = Dart_GetNativeArgument(args, 0); | 134 Dart_Handle socket_obj = Dart_GetNativeArgument(args, 0); |
| 134 intptr_t socket = 0; | 135 intptr_t socket = 0; |
| 135 Dart_Handle err = Socket::GetSocketIdNativeField(socket_obj, &socket); | 136 Dart_Handle err = Socket::GetSocketIdNativeField(socket_obj, &socket); |
| 136 if (Dart_IsError(err)) Dart_PropagateError(err); | 137 if (Dart_IsError(err)) Dart_PropagateError(err); |
| 137 intptr_t available = Socket::Available(socket); | 138 intptr_t available = Socket::Available(socket); |
| 138 if (available > 0) { | 139 if (available > 0) { |
| 139 int64_t length = 0; | 140 int64_t length = 0; |
| 140 Dart_Handle length_obj = Dart_GetNativeArgument(args, 1); | 141 Dart_Handle length_obj = Dart_GetNativeArgument(args, 1); |
| 141 if (DartUtils::GetInt64Value(length_obj, &length)) { | 142 if (DartUtils::GetInt64Value(length_obj, &length)) { |
| 142 if (length == -1 || available < length) { | 143 if (length == -1 || available < length) { |
| 143 length = available; | 144 length = available; |
| 144 } | 145 } |
| 146 if (short_socket_reads) { |
| 147 length = (length + 1) / 2; |
| 148 } |
| 145 uint8_t* buffer = NULL; | 149 uint8_t* buffer = NULL; |
| 146 Dart_Handle result = IOBuffer::Allocate(length, &buffer); | 150 Dart_Handle result = IOBuffer::Allocate(length, &buffer); |
| 147 if (Dart_IsError(result)) Dart_PropagateError(result); | 151 if (Dart_IsError(result)) Dart_PropagateError(result); |
| 148 ASSERT(buffer != NULL); | 152 ASSERT(buffer != NULL); |
| 149 intptr_t bytes_read = Socket::Read(socket, buffer, length); | 153 intptr_t bytes_read = Socket::Read(socket, buffer, length); |
| 150 if (bytes_read == length) { | 154 if (bytes_read == length) { |
| 151 Dart_SetReturnValue(args, result); | 155 Dart_SetReturnValue(args, result); |
| 152 } else if (bytes_read == 0) { | 156 } else if (bytes_read == 0) { |
| 153 // On MacOS when reading from a tty Ctrl-D will result in one | 157 // On MacOS when reading from a tty Ctrl-D will result in one |
| 154 // byte reported as available. Attempting to read it out will | 158 // byte reported as available. Attempting to read it out will |
| (...skipping 13 matching lines...) Expand all Loading... |
| 168 } else if (available == 0) { | 172 } else if (available == 0) { |
| 169 Dart_SetReturnValue(args, Dart_Null()); | 173 Dart_SetReturnValue(args, Dart_Null()); |
| 170 } else { | 174 } else { |
| 171 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 175 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
| 172 } | 176 } |
| 173 | 177 |
| 174 Dart_ExitScope(); | 178 Dart_ExitScope(); |
| 175 } | 179 } |
| 176 | 180 |
| 177 | 181 |
| 178 void FUNCTION_NAME(Socket_ReadList)(Dart_NativeArguments args) { | |
| 179 Dart_EnterScope(); | |
| 180 static bool short_socket_reads = Dart_IsVMFlagSet("short_socket_read"); | |
| 181 Dart_Handle socket_obj = Dart_GetNativeArgument(args, 0); | |
| 182 intptr_t socket = 0; | |
| 183 Dart_Handle err = Socket::GetSocketIdNativeField(socket_obj, &socket); | |
| 184 if (Dart_IsError(err)) Dart_PropagateError(err); | |
| 185 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); | |
| 186 int64_t offset = 0; | |
| 187 int64_t length = 0; | |
| 188 Dart_Handle offset_obj = Dart_GetNativeArgument(args, 2); | |
| 189 Dart_Handle length_obj = Dart_GetNativeArgument(args, 3); | |
| 190 if (Dart_IsList(buffer_obj) && | |
| 191 DartUtils::GetInt64Value(offset_obj, &offset) && | |
| 192 DartUtils::GetInt64Value(length_obj, &length)) { | |
| 193 intptr_t buffer_len = 0; | |
| 194 Dart_Handle result = Dart_ListLength(buffer_obj, &buffer_len); | |
| 195 if (Dart_IsError(result)) { | |
| 196 Dart_PropagateError(result); | |
| 197 } | |
| 198 ASSERT((offset + length) <= buffer_len); | |
| 199 if (short_socket_reads) { | |
| 200 length = (length + 1) / 2; | |
| 201 } | |
| 202 uint8_t* buffer = new uint8_t[length]; | |
| 203 intptr_t bytes_read = Socket::Read(socket, buffer, length); | |
| 204 if (bytes_read > 0) { | |
| 205 Dart_Handle result = | |
| 206 Dart_ListSetAsBytes(buffer_obj, offset, buffer, bytes_read); | |
| 207 if (Dart_IsError(result)) { | |
| 208 delete[] buffer; | |
| 209 Dart_PropagateError(result); | |
| 210 } | |
| 211 } | |
| 212 delete[] buffer; | |
| 213 if (bytes_read >= 0) { | |
| 214 Dart_SetReturnValue(args, Dart_NewInteger(bytes_read)); | |
| 215 } else { | |
| 216 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | |
| 217 } | |
| 218 } else { | |
| 219 OSError os_error(-1, "Invalid argument", OSError::kUnknown); | |
| 220 Dart_Handle err = DartUtils::NewDartOSError(&os_error); | |
| 221 if (Dart_IsError(err)) Dart_PropagateError(err); | |
| 222 Dart_SetReturnValue(args, err); | |
| 223 } | |
| 224 | |
| 225 Dart_ExitScope(); | |
| 226 } | |
| 227 | |
| 228 | |
| 229 void FUNCTION_NAME(Socket_WriteList)(Dart_NativeArguments args) { | 182 void FUNCTION_NAME(Socket_WriteList)(Dart_NativeArguments args) { |
| 230 Dart_EnterScope(); | 183 Dart_EnterScope(); |
| 231 static bool short_socket_writes = Dart_IsVMFlagSet("short_socket_write"); | 184 static bool short_socket_writes = Dart_IsVMFlagSet("short_socket_write"); |
| 232 Dart_Handle socket_obj = Dart_GetNativeArgument(args, 0); | 185 Dart_Handle socket_obj = Dart_GetNativeArgument(args, 0); |
| 233 intptr_t socket = 0; | 186 intptr_t socket = 0; |
| 234 Dart_Handle err = Socket::GetSocketIdNativeField(socket_obj, &socket); | 187 Dart_Handle err = Socket::GetSocketIdNativeField(socket_obj, &socket); |
| 235 if (Dart_IsError(err)) Dart_PropagateError(err); | 188 if (Dart_IsError(err)) Dart_PropagateError(err); |
| 236 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); | 189 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); |
| 237 ASSERT(Dart_IsList(buffer_obj)); | 190 ASSERT(Dart_IsList(buffer_obj)); |
| 238 intptr_t offset = | 191 intptr_t offset = |
| (...skipping 392 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 631 return Dart_SetNativeInstanceField(socket, kSocketIdNativeField, id); | 584 return Dart_SetNativeInstanceField(socket, kSocketIdNativeField, id); |
| 632 } | 585 } |
| 633 | 586 |
| 634 | 587 |
| 635 Dart_Handle Socket::GetSocketIdNativeField(Dart_Handle socket, intptr_t* id) { | 588 Dart_Handle Socket::GetSocketIdNativeField(Dart_Handle socket, intptr_t* id) { |
| 636 return Dart_GetNativeInstanceField(socket, kSocketIdNativeField, id); | 589 return Dart_GetNativeInstanceField(socket, kSocketIdNativeField, id); |
| 637 } | 590 } |
| 638 | 591 |
| 639 } // namespace bin | 592 } // namespace bin |
| 640 } // namespace dart | 593 } // namespace dart |
| OLD | NEW |