| 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" |
| 9 |
| 8 #include "include/dart_api.h" | 10 #include "include/dart_api.h" |
| 9 | 11 |
| 10 | 12 |
| 11 void FUNCTION_NAME(Socket_CreateConnect)(Dart_NativeArguments args) { | 13 void FUNCTION_NAME(Socket_CreateConnect)(Dart_NativeArguments args) { |
| 12 Dart_EnterScope(); | 14 Dart_EnterScope(); |
| 13 Dart_Handle socketobj = Dart_GetNativeArgument(args, 0); | 15 Dart_Handle socketobj = Dart_GetNativeArgument(args, 0); |
| 14 const char* host = | 16 const char* host = |
| 15 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); | 17 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); |
| 16 intptr_t port = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)); | 18 intptr_t port = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)); |
| 17 intptr_t socket = Socket::CreateConnect(host, port); | 19 intptr_t socket = Socket::CreateConnect(host, port); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); | 83 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); |
| 82 intptr_t buffer_len = 0; | 84 intptr_t buffer_len = 0; |
| 83 Dart_Handle result = Dart_ListLength(buffer_obj, &buffer_len); | 85 Dart_Handle result = Dart_ListLength(buffer_obj, &buffer_len); |
| 84 ASSERT(!Dart_IsError(result)); | 86 ASSERT(!Dart_IsError(result)); |
| 85 ASSERT((offset + length) <= buffer_len); | 87 ASSERT((offset + length) <= buffer_len); |
| 86 | 88 |
| 87 if (Dart_IsVMFlagSet("short_socket_write")) { | 89 if (Dart_IsVMFlagSet("short_socket_write")) { |
| 88 length = (length + 1) / 2; | 90 length = (length + 1) / 2; |
| 89 } | 91 } |
| 90 | 92 |
| 91 uint8_t* buffer = new uint8_t[length]; | 93 // Send data in chunks of maximum 16KB. |
| 92 result = Dart_ListGetAsBytes(buffer_obj, offset, buffer, length); | 94 const intptr_t max_chunk_length = dart::Utils::Minimum(length, 16 * KB); |
| 93 ASSERT(!Dart_IsError(result)); | 95 uint8_t* buffer = new uint8_t[max_chunk_length]; |
| 94 intptr_t total_bytes_written = | 96 intptr_t total_bytes_written = 0; |
| 95 Socket::Write(socket, reinterpret_cast<void*>(buffer), length); | 97 intptr_t bytes_written = 0; |
| 98 do { |
| 99 intptr_t chunk_length = |
| 100 dart::Utils::Minimum(max_chunk_length, length - total_bytes_written); |
| 101 result = Dart_ListGetAsBytes(buffer_obj, |
| 102 offset + total_bytes_written, |
| 103 buffer, |
| 104 chunk_length); |
| 105 ASSERT(!Dart_IsError(result)); |
| 106 bytes_written = |
| 107 Socket::Write(socket, reinterpret_cast<void*>(buffer), chunk_length); |
| 108 total_bytes_written += bytes_written; |
| 109 } while (bytes_written > 0 && total_bytes_written < length); |
| 96 delete[] buffer; | 110 delete[] buffer; |
| 97 Dart_SetReturnValue(args, Dart_NewInteger(total_bytes_written)); | 111 Dart_SetReturnValue(args, Dart_NewInteger(total_bytes_written)); |
| 98 Dart_ExitScope(); | 112 Dart_ExitScope(); |
| 99 } | 113 } |
| 100 | 114 |
| 101 | 115 |
| 102 void FUNCTION_NAME(Socket_GetPort)(Dart_NativeArguments args) { | 116 void FUNCTION_NAME(Socket_GetPort)(Dart_NativeArguments args) { |
| 103 Dart_EnterScope(); | 117 Dart_EnterScope(); |
| 104 intptr_t socket = | 118 intptr_t socket = |
| 105 DartUtils::GetIntegerInstanceField(Dart_GetNativeArgument(args, 0), | 119 DartUtils::GetIntegerInstanceField(Dart_GetNativeArgument(args, 0), |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 DartUtils::kIdFieldName); | 162 DartUtils::kIdFieldName); |
| 149 Dart_Handle socketobj = Dart_GetNativeArgument(args, 1); | 163 Dart_Handle socketobj = Dart_GetNativeArgument(args, 1); |
| 150 intptr_t newSocket = ServerSocket::Accept(socket); | 164 intptr_t newSocket = ServerSocket::Accept(socket); |
| 151 if (newSocket >= 0) { | 165 if (newSocket >= 0) { |
| 152 DartUtils::SetIntegerInstanceField( | 166 DartUtils::SetIntegerInstanceField( |
| 153 socketobj, DartUtils::kIdFieldName, newSocket); | 167 socketobj, DartUtils::kIdFieldName, newSocket); |
| 154 } | 168 } |
| 155 Dart_SetReturnValue(args, Dart_NewBoolean(newSocket >= 0)); | 169 Dart_SetReturnValue(args, Dart_NewBoolean(newSocket >= 0)); |
| 156 Dart_ExitScope(); | 170 Dart_ExitScope(); |
| 157 } | 171 } |
| OLD | NEW |