| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 #include "bin/dartutils.h" | 6 #include "bin/dartutils.h" |
| 7 | 7 |
| 8 #include "include/dart_api.h" | 8 #include "include/dart_api.h" |
| 9 | 9 |
| 10 | 10 |
| 11 void FUNCTION_NAME(Socket_CreateConnect)(Dart_NativeArguments args) { | 11 void FUNCTION_NAME(Socket_CreateConnect)(Dart_NativeArguments args) { |
| 12 Dart_EnterScope(); | 12 Dart_EnterScope(); |
| 13 Dart_Handle socketobj = Dart_GetNativeArgument(args, 0); | 13 Dart_Handle socketobj = Dart_GetNativeArgument(args, 0); |
| 14 const char* host = | 14 const char* host = |
| 15 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); | 15 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); |
| 16 intptr_t port = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)); | 16 intptr_t port = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)); |
| 17 intptr_t socket = Socket::CreateConnect(host, port); | 17 intptr_t socket = Socket::CreateConnect(host, port); |
| 18 DartUtils::SetIntegerInstanceField( | 18 DartUtils::SetIntegerInstanceField( |
| 19 socketobj, DartUtils::kIdFieldName, socket); | 19 socketobj, DartUtils::kIdFieldName, socket); |
| 20 Dart_SetReturnValue(args, Dart_NewBoolean(socket >= 0)); | 20 Dart_SetReturnValue(args, Dart_NewBoolean(socket >= 0)); |
| 21 Dart_ExitScope(); | 21 Dart_ExitScope(); |
| 22 } | 22 } |
| 23 | 23 |
| 24 | 24 |
| 25 void FUNCTION_NAME(Socket_Available)(Dart_NativeArguments args) { | 25 void FUNCTION_NAME(Socket_Available)(Dart_NativeArguments args) { |
| 26 Dart_EnterScope(); | 26 Dart_EnterScope(); |
| 27 intptr_t socket = | 27 intptr_t socket = |
| 28 DartUtils::GetIntegerInstanceField(Dart_GetNativeArgument(args, 0), | 28 DartUtils::GetIntegerInstanceField(Dart_GetNativeArgument(args, 0), |
| 29 DartUtils::kIdFieldName); | 29 DartUtils::kIdFieldName); |
| 30 intptr_t available = Socket::Available(socket); | 30 intptr_t available = Socket::Available(socket); |
| 31 Dart_SetReturnValue(args, Dart_NewInteger(available)); | 31 Dart_SetReturnValue(args, Dart_NewInteger(available)); |
| 32 Dart_ExitScope(); | 32 Dart_ExitScope(); |
| 33 } | 33 } |
| 34 | 34 |
| 35 | 35 |
| 36 void FUNCTION_NAME(Socket_ReadList)(Dart_NativeArguments args) { | 36 void FUNCTION_NAME(Socket_ReadList)(Dart_NativeArguments args) { |
| 37 Dart_EnterScope(); | 37 Dart_EnterScope(); |
| 38 intptr_t socket = | 38 intptr_t socket = |
| 39 DartUtils::GetIntegerInstanceField(Dart_GetNativeArgument(args, 0), | 39 DartUtils::GetIntegerInstanceField(Dart_GetNativeArgument(args, 0), |
| 40 DartUtils::kIdFieldName); | 40 DartUtils::kIdFieldName); |
| 41 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); | 41 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); |
| 42 assert(Dart_IsArray(buffer_obj)); | 42 ASSERT(Dart_IsArray(buffer_obj)); |
| 43 intptr_t offset = | 43 intptr_t offset = |
| 44 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)); | 44 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)); |
| 45 intptr_t length = | 45 intptr_t length = |
| 46 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); | 46 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); |
| 47 Dart_Result result = Dart_GetLength(buffer_obj); | 47 Dart_Result result = Dart_GetLength(buffer_obj); |
| 48 assert(Dart_IsValidResult(result)); | 48 ASSERT(Dart_IsValidResult(result)); |
| 49 assert((offset + length) <= Dart_GetResultAsCIntptr(result)); | 49 ASSERT((offset + length) <= Dart_GetResultAsCIntptr(result)); |
| 50 | 50 |
| 51 if (Dart_IsVMFlagSet("short_socket_read")) { | 51 if (Dart_IsVMFlagSet("short_socket_read")) { |
| 52 length = (length + 1) / 2; | 52 length = (length + 1) / 2; |
| 53 } | 53 } |
| 54 | 54 |
| 55 uint8_t* buffer = new uint8_t[length]; | 55 uint8_t* buffer = new uint8_t[length]; |
| 56 intptr_t bytes_read = Socket::Read(socket, buffer, length); | 56 intptr_t bytes_read = Socket::Read(socket, buffer, length); |
| 57 if (bytes_read > 0) { | 57 if (bytes_read > 0) { |
| 58 Dart_Result result = Dart_ArraySet(buffer_obj, offset, buffer, bytes_read); | 58 Dart_Result result = Dart_ArraySet(buffer_obj, offset, buffer, bytes_read); |
| 59 ASSERT(Dart_IsValidResult(result)); | 59 ASSERT(Dart_IsValidResult(result)); |
| 60 } else if (bytes_read < 0) { | 60 } else if (bytes_read < 0) { |
| 61 bytes_read = 0; | 61 bytes_read = 0; |
| 62 } | 62 } |
| 63 delete[] buffer; | 63 delete[] buffer; |
| 64 Dart_SetReturnValue(args, Dart_NewInteger(bytes_read)); | 64 Dart_SetReturnValue(args, Dart_NewInteger(bytes_read)); |
| 65 Dart_ExitScope(); | 65 Dart_ExitScope(); |
| 66 } | 66 } |
| 67 | 67 |
| 68 | 68 |
| 69 void FUNCTION_NAME(Socket_WriteList)(Dart_NativeArguments args) { | 69 void FUNCTION_NAME(Socket_WriteList)(Dart_NativeArguments args) { |
| 70 Dart_EnterScope(); | 70 Dart_EnterScope(); |
| 71 intptr_t socket = | 71 intptr_t socket = |
| 72 DartUtils::GetIntegerInstanceField(Dart_GetNativeArgument(args, 0), | 72 DartUtils::GetIntegerInstanceField(Dart_GetNativeArgument(args, 0), |
| 73 DartUtils::kIdFieldName); | 73 DartUtils::kIdFieldName); |
| 74 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); | 74 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); |
| 75 assert(Dart_IsArray(buffer_obj)); | 75 ASSERT(Dart_IsArray(buffer_obj)); |
| 76 intptr_t offset = | 76 intptr_t offset = |
| 77 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)); | 77 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)); |
| 78 intptr_t length = | 78 intptr_t length = |
| 79 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); | 79 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); |
| 80 Dart_Result result = Dart_GetLength(buffer_obj); | 80 Dart_Result result = Dart_GetLength(buffer_obj); |
| 81 assert(Dart_IsValidResult(result)); | 81 ASSERT(Dart_IsValidResult(result)); |
| 82 assert((offset + length) <= Dart_GetResultAsCIntptr(result)); | 82 ASSERT((offset + length) <= Dart_GetResultAsCIntptr(result)); |
| 83 | 83 |
| 84 if (Dart_IsVMFlagSet("short_socket_write")) { | 84 if (Dart_IsVMFlagSet("short_socket_write")) { |
| 85 length = (length + 1) / 2; | 85 length = (length + 1) / 2; |
| 86 } | 86 } |
| 87 | 87 |
| 88 uint8_t* buffer = new uint8_t[length]; | 88 uint8_t* buffer = new uint8_t[length]; |
| 89 result = Dart_ArrayGet(buffer_obj, offset, buffer, length); | 89 result = Dart_ArrayGet(buffer_obj, offset, buffer, length); |
| 90 ASSERT(Dart_IsValidResult(result)); | 90 ASSERT(Dart_IsValidResult(result)); |
| 91 intptr_t total_bytes_written = | 91 intptr_t total_bytes_written = |
| 92 Socket::Write(socket, reinterpret_cast<void*>(buffer), length); | 92 Socket::Write(socket, reinterpret_cast<void*>(buffer), length); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 DartUtils::kIdFieldName); | 134 DartUtils::kIdFieldName); |
| 135 Dart_Handle socketobj = Dart_GetNativeArgument(args, 1); | 135 Dart_Handle socketobj = Dart_GetNativeArgument(args, 1); |
| 136 intptr_t newSocket = ServerSocket::Accept(socket); | 136 intptr_t newSocket = ServerSocket::Accept(socket); |
| 137 if (newSocket >= 0) { | 137 if (newSocket >= 0) { |
| 138 DartUtils::SetIntegerInstanceField( | 138 DartUtils::SetIntegerInstanceField( |
| 139 socketobj, DartUtils::kIdFieldName, newSocket); | 139 socketobj, DartUtils::kIdFieldName, newSocket); |
| 140 } | 140 } |
| 141 Dart_SetReturnValue(args, Dart_NewBoolean(newSocket >= 0)); | 141 Dart_SetReturnValue(args, Dart_NewBoolean(newSocket >= 0)); |
| 142 Dart_ExitScope(); | 142 Dart_ExitScope(); |
| 143 } | 143 } |
| OLD | NEW |