| 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 "platform/utils.h" | 8 #include "platform/utils.h" |
| 9 | 9 |
| 10 #include "include/dart_api.h" | 10 #include "include/dart_api.h" |
| 11 | 11 |
| 12 | 12 |
| 13 void FUNCTION_NAME(Socket_CreateConnect)(Dart_NativeArguments args) { | 13 void FUNCTION_NAME(Socket_CreateConnect)(Dart_NativeArguments args) { |
| 14 Dart_EnterScope(); | 14 Dart_EnterScope(); |
| 15 Dart_Handle socketobj = Dart_GetNativeArgument(args, 0); | 15 Dart_Handle socketobj = Dart_GetNativeArgument(args, 0); |
| 16 const char* host = | 16 const char* host = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); |
| 17 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); | 17 int64_t port = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)); |
| 18 intptr_t port = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)); | |
| 19 intptr_t socket = Socket::CreateConnect(host, port); | 18 intptr_t socket = Socket::CreateConnect(host, port); |
| 20 DartUtils::SetIntegerField( | 19 DartUtils::SetIntegerField(socketobj, DartUtils::kIdFieldName, socket); |
| 21 socketobj, DartUtils::kIdFieldName, socket); | |
| 22 Dart_SetReturnValue(args, Dart_NewBoolean(socket >= 0)); | 20 Dart_SetReturnValue(args, Dart_NewBoolean(socket >= 0)); |
| 23 Dart_ExitScope(); | 21 Dart_ExitScope(); |
| 24 } | 22 } |
| 25 | 23 |
| 26 | 24 |
| 27 void FUNCTION_NAME(Socket_Available)(Dart_NativeArguments args) { | 25 void FUNCTION_NAME(Socket_Available)(Dart_NativeArguments args) { |
| 28 Dart_EnterScope(); | 26 Dart_EnterScope(); |
| 29 intptr_t socket = | 27 int64_t socket = DartUtils::GetIntegerField(Dart_GetNativeArgument(args, 0), |
| 30 DartUtils::GetIntegerField(Dart_GetNativeArgument(args, 0), | 28 DartUtils::kIdFieldName); |
| 31 DartUtils::kIdFieldName); | |
| 32 intptr_t available = Socket::Available(socket); | 29 intptr_t available = Socket::Available(socket); |
| 33 Dart_SetReturnValue(args, Dart_NewInteger(available)); | 30 Dart_SetReturnValue(args, Dart_NewInteger(available)); |
| 34 Dart_ExitScope(); | 31 Dart_ExitScope(); |
| 35 } | 32 } |
| 36 | 33 |
| 37 | 34 |
| 38 void FUNCTION_NAME(Socket_ReadList)(Dart_NativeArguments args) { | 35 void FUNCTION_NAME(Socket_ReadList)(Dart_NativeArguments args) { |
| 39 Dart_EnterScope(); | 36 Dart_EnterScope(); |
| 40 intptr_t socket = | 37 intptr_t socket = |
| 41 DartUtils::GetIntegerField(Dart_GetNativeArgument(args, 0), | 38 DartUtils::GetIntegerField(Dart_GetNativeArgument(args, 0), |
| 42 DartUtils::kIdFieldName); | 39 DartUtils::kIdFieldName); |
| 43 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); | 40 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); |
| 44 ASSERT(Dart_IsList(buffer_obj)); | 41 ASSERT(Dart_IsList(buffer_obj)); |
| 45 intptr_t offset = | 42 intptr_t offset = |
| 46 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)); | 43 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)); |
| 47 intptr_t length = | 44 intptr_t length = |
| 48 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); | 45 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); |
| 49 intptr_t buffer_len = 0; | 46 intptr_t buffer_len = 0; |
| 50 Dart_Handle result = Dart_ListLength(buffer_obj, &buffer_len); | 47 Dart_Handle result = Dart_ListLength(buffer_obj, &buffer_len); |
| 51 ASSERT(!Dart_IsError(result)); | 48 if (Dart_IsError(result)) { |
| 49 Dart_PropagateError(result); |
| 50 } |
| 52 ASSERT((offset + length) <= buffer_len); | 51 ASSERT((offset + length) <= buffer_len); |
| 53 | 52 |
| 54 if (Dart_IsVMFlagSet("short_socket_read")) { | 53 if (Dart_IsVMFlagSet("short_socket_read")) { |
| 55 length = (length + 1) / 2; | 54 length = (length + 1) / 2; |
| 56 } | 55 } |
| 57 | 56 |
| 58 uint8_t* buffer = new uint8_t[length]; | 57 uint8_t* buffer = new uint8_t[length]; |
| 59 intptr_t bytes_read = Socket::Read(socket, buffer, length); | 58 intptr_t bytes_read = Socket::Read(socket, buffer, length); |
| 60 if (bytes_read > 0) { | 59 if (bytes_read > 0) { |
| 61 Dart_Handle result = | 60 Dart_Handle result = |
| 62 Dart_ListSetAsBytes(buffer_obj, offset, buffer, bytes_read); | 61 Dart_ListSetAsBytes(buffer_obj, offset, buffer, bytes_read); |
| 63 if (Dart_IsError(result)) { | 62 if (Dart_IsError(result)) { |
| 64 bytes_read = -1; | 63 delete[] buffer; |
| 64 Dart_PropagateError(result); |
| 65 } | 65 } |
| 66 } | 66 } |
| 67 delete[] buffer; | 67 delete[] buffer; |
| 68 Dart_SetReturnValue(args, Dart_NewInteger(bytes_read)); | 68 Dart_SetReturnValue(args, Dart_NewInteger(bytes_read)); |
| 69 Dart_ExitScope(); | 69 Dart_ExitScope(); |
| 70 } | 70 } |
| 71 | 71 |
| 72 | 72 |
| 73 void FUNCTION_NAME(Socket_WriteList)(Dart_NativeArguments args) { | 73 void FUNCTION_NAME(Socket_WriteList)(Dart_NativeArguments args) { |
| 74 Dart_EnterScope(); | 74 Dart_EnterScope(); |
| 75 intptr_t socket = | 75 intptr_t socket = |
| 76 DartUtils::GetIntegerField(Dart_GetNativeArgument(args, 0), | 76 DartUtils::GetIntegerField(Dart_GetNativeArgument(args, 0), |
| 77 DartUtils::kIdFieldName); | 77 DartUtils::kIdFieldName); |
| 78 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); | 78 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); |
| 79 ASSERT(Dart_IsList(buffer_obj)); | 79 ASSERT(Dart_IsList(buffer_obj)); |
| 80 intptr_t offset = | 80 intptr_t offset = |
| 81 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)); | 81 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)); |
| 82 intptr_t length = | 82 intptr_t length = |
| 83 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); | 83 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); |
| 84 intptr_t buffer_len = 0; | 84 intptr_t buffer_len = 0; |
| 85 Dart_Handle result = Dart_ListLength(buffer_obj, &buffer_len); | 85 Dart_Handle result = Dart_ListLength(buffer_obj, &buffer_len); |
| 86 ASSERT(!Dart_IsError(result)); | 86 if (Dart_IsError(result)) { |
| 87 Dart_PropagateError(result); |
| 88 } |
| 87 ASSERT((offset + length) <= buffer_len); | 89 ASSERT((offset + length) <= buffer_len); |
| 88 | 90 |
| 89 if (Dart_IsVMFlagSet("short_socket_write")) { | 91 if (Dart_IsVMFlagSet("short_socket_write")) { |
| 90 length = (length + 1) / 2; | 92 length = (length + 1) / 2; |
| 91 } | 93 } |
| 92 | 94 |
| 93 // Send data in chunks of maximum 16KB. | 95 // Send data in chunks of maximum 16KB. |
| 94 const intptr_t max_chunk_length = | 96 const intptr_t max_chunk_length = |
| 95 dart::Utils::Minimum(length, static_cast<intptr_t>(16 * KB)); | 97 dart::Utils::Minimum(length, static_cast<intptr_t>(16 * KB)); |
| 96 uint8_t* buffer = new uint8_t[max_chunk_length]; | 98 uint8_t* buffer = new uint8_t[max_chunk_length]; |
| 97 intptr_t total_bytes_written = 0; | 99 intptr_t total_bytes_written = 0; |
| 98 intptr_t bytes_written = 0; | 100 intptr_t bytes_written = 0; |
| 99 do { | 101 do { |
| 100 intptr_t chunk_length = | 102 intptr_t chunk_length = |
| 101 dart::Utils::Minimum(max_chunk_length, length - total_bytes_written); | 103 dart::Utils::Minimum(max_chunk_length, length - total_bytes_written); |
| 102 result = Dart_ListGetAsBytes(buffer_obj, | 104 result = Dart_ListGetAsBytes(buffer_obj, |
| 103 offset + total_bytes_written, | 105 offset + total_bytes_written, |
| 104 buffer, | 106 buffer, |
| 105 chunk_length); | 107 chunk_length); |
| 106 ASSERT(!Dart_IsError(result)); | 108 if (Dart_IsError(result)) { |
| 109 delete[] buffer; |
| 110 Dart_PropagateError(result); |
| 111 } |
| 107 bytes_written = | 112 bytes_written = |
| 108 Socket::Write(socket, reinterpret_cast<void*>(buffer), chunk_length); | 113 Socket::Write(socket, reinterpret_cast<void*>(buffer), chunk_length); |
| 109 total_bytes_written += bytes_written; | 114 total_bytes_written += bytes_written; |
| 110 } while (bytes_written > 0 && total_bytes_written < length); | 115 } while (bytes_written > 0 && total_bytes_written < length); |
| 111 delete[] buffer; | 116 delete[] buffer; |
| 112 Dart_SetReturnValue(args, Dart_NewInteger(total_bytes_written)); | 117 Dart_SetReturnValue(args, Dart_NewInteger(total_bytes_written)); |
| 113 Dart_ExitScope(); | 118 Dart_ExitScope(); |
| 114 } | 119 } |
| 115 | 120 |
| 116 | 121 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 DartUtils::kIdFieldName); | 168 DartUtils::kIdFieldName); |
| 164 Dart_Handle socketobj = Dart_GetNativeArgument(args, 1); | 169 Dart_Handle socketobj = Dart_GetNativeArgument(args, 1); |
| 165 intptr_t newSocket = ServerSocket::Accept(socket); | 170 intptr_t newSocket = ServerSocket::Accept(socket); |
| 166 if (newSocket >= 0) { | 171 if (newSocket >= 0) { |
| 167 DartUtils::SetIntegerField( | 172 DartUtils::SetIntegerField( |
| 168 socketobj, DartUtils::kIdFieldName, newSocket); | 173 socketobj, DartUtils::kIdFieldName, newSocket); |
| 169 } | 174 } |
| 170 Dart_SetReturnValue(args, Dart_NewBoolean(newSocket >= 0)); | 175 Dart_SetReturnValue(args, Dart_NewBoolean(newSocket >= 0)); |
| 171 Dart_ExitScope(); | 176 Dart_ExitScope(); |
| 172 } | 177 } |
| OLD | NEW |