Chromium Code Reviews| 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" |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 41 DartUtils::GetIntegerField(Dart_GetNativeArgument(args, 0), | 41 DartUtils::GetIntegerField(Dart_GetNativeArgument(args, 0), |
| 42 DartUtils::kIdFieldName); | 42 DartUtils::kIdFieldName); |
| 43 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); | 43 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); |
| 44 ASSERT(Dart_IsList(buffer_obj)); | 44 ASSERT(Dart_IsList(buffer_obj)); |
| 45 intptr_t offset = | 45 intptr_t offset = |
| 46 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)); | 46 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)); |
| 47 intptr_t length = | 47 intptr_t length = |
| 48 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); | 48 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); |
| 49 intptr_t buffer_len = 0; | 49 intptr_t buffer_len = 0; |
| 50 Dart_Handle result = Dart_ListLength(buffer_obj, &buffer_len); | 50 Dart_Handle result = Dart_ListLength(buffer_obj, &buffer_len); |
| 51 ASSERT(!Dart_IsError(result)); | 51 if (Dart_IsError(result)) { |
| 52 Dart_PropagateError(result); | |
| 53 } | |
| 52 ASSERT((offset + length) <= buffer_len); | 54 ASSERT((offset + length) <= buffer_len); |
| 53 | 55 |
| 54 if (Dart_IsVMFlagSet("short_socket_read")) { | 56 if (Dart_IsVMFlagSet("short_socket_read")) { |
| 55 length = (length + 1) / 2; | 57 length = (length + 1) / 2; |
| 56 } | 58 } |
| 57 | 59 |
| 58 uint8_t* buffer = new uint8_t[length]; | 60 uint8_t* buffer = new uint8_t[length]; |
| 59 intptr_t bytes_read = Socket::Read(socket, buffer, length); | 61 intptr_t bytes_read = Socket::Read(socket, buffer, length); |
| 60 if (bytes_read > 0) { | 62 if (bytes_read > 0) { |
| 61 Dart_Handle result = | 63 Dart_Handle result = |
| 62 Dart_ListSetAsBytes(buffer_obj, offset, buffer, bytes_read); | 64 Dart_ListSetAsBytes(buffer_obj, offset, buffer, bytes_read); |
| 63 if (Dart_IsError(result)) { | 65 if (Dart_IsError(result)) { |
|
Søren Gjesse
2012/03/08 21:18:46
Missing delete[] buffer
| |
| 64 bytes_read = -1; | 66 Dart_PropagateError(result); |
| 65 } | 67 } |
| 66 } | 68 } |
| 67 delete[] buffer; | 69 delete[] buffer; |
| 68 Dart_SetReturnValue(args, Dart_NewInteger(bytes_read)); | 70 Dart_SetReturnValue(args, Dart_NewInteger(bytes_read)); |
| 69 Dart_ExitScope(); | 71 Dart_ExitScope(); |
| 70 } | 72 } |
| 71 | 73 |
| 72 | 74 |
| 73 void FUNCTION_NAME(Socket_WriteList)(Dart_NativeArguments args) { | 75 void FUNCTION_NAME(Socket_WriteList)(Dart_NativeArguments args) { |
| 74 Dart_EnterScope(); | 76 Dart_EnterScope(); |
| 75 intptr_t socket = | 77 intptr_t socket = |
| 76 DartUtils::GetIntegerField(Dart_GetNativeArgument(args, 0), | 78 DartUtils::GetIntegerField(Dart_GetNativeArgument(args, 0), |
| 77 DartUtils::kIdFieldName); | 79 DartUtils::kIdFieldName); |
| 78 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); | 80 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); |
| 79 ASSERT(Dart_IsList(buffer_obj)); | 81 ASSERT(Dart_IsList(buffer_obj)); |
| 80 intptr_t offset = | 82 intptr_t offset = |
| 81 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)); | 83 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)); |
| 82 intptr_t length = | 84 intptr_t length = |
| 83 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); | 85 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); |
| 84 intptr_t buffer_len = 0; | 86 intptr_t buffer_len = 0; |
| 85 Dart_Handle result = Dart_ListLength(buffer_obj, &buffer_len); | 87 Dart_Handle result = Dart_ListLength(buffer_obj, &buffer_len); |
| 86 ASSERT(!Dart_IsError(result)); | 88 if (Dart_IsError(result)) { |
| 89 Dart_PropagateError(result); | |
| 90 } | |
| 87 ASSERT((offset + length) <= buffer_len); | 91 ASSERT((offset + length) <= buffer_len); |
| 88 | 92 |
| 89 if (Dart_IsVMFlagSet("short_socket_write")) { | 93 if (Dart_IsVMFlagSet("short_socket_write")) { |
| 90 length = (length + 1) / 2; | 94 length = (length + 1) / 2; |
| 91 } | 95 } |
| 92 | 96 |
| 93 // Send data in chunks of maximum 16KB. | 97 // Send data in chunks of maximum 16KB. |
| 94 const intptr_t max_chunk_length = | 98 const intptr_t max_chunk_length = |
| 95 dart::Utils::Minimum(length, static_cast<intptr_t>(16 * KB)); | 99 dart::Utils::Minimum(length, static_cast<intptr_t>(16 * KB)); |
| 96 uint8_t* buffer = new uint8_t[max_chunk_length]; | 100 uint8_t* buffer = new uint8_t[max_chunk_length]; |
| 97 intptr_t total_bytes_written = 0; | 101 intptr_t total_bytes_written = 0; |
| 98 intptr_t bytes_written = 0; | 102 intptr_t bytes_written = 0; |
| 99 do { | 103 do { |
| 100 intptr_t chunk_length = | 104 intptr_t chunk_length = |
| 101 dart::Utils::Minimum(max_chunk_length, length - total_bytes_written); | 105 dart::Utils::Minimum(max_chunk_length, length - total_bytes_written); |
| 102 result = Dart_ListGetAsBytes(buffer_obj, | 106 result = Dart_ListGetAsBytes(buffer_obj, |
| 103 offset + total_bytes_written, | 107 offset + total_bytes_written, |
| 104 buffer, | 108 buffer, |
| 105 chunk_length); | 109 chunk_length); |
| 106 ASSERT(!Dart_IsError(result)); | 110 if (Dart_IsError(result)) { |
|
Søren Gjesse
2012/03/08 21:18:46
Missing delete[] buffer.
| |
| 111 Dart_PropagateError(result); | |
| 112 } | |
| 107 bytes_written = | 113 bytes_written = |
| 108 Socket::Write(socket, reinterpret_cast<void*>(buffer), chunk_length); | 114 Socket::Write(socket, reinterpret_cast<void*>(buffer), chunk_length); |
| 109 total_bytes_written += bytes_written; | 115 total_bytes_written += bytes_written; |
| 110 } while (bytes_written > 0 && total_bytes_written < length); | 116 } while (bytes_written > 0 && total_bytes_written < length); |
| 111 delete[] buffer; | 117 delete[] buffer; |
| 112 Dart_SetReturnValue(args, Dart_NewInteger(total_bytes_written)); | 118 Dart_SetReturnValue(args, Dart_NewInteger(total_bytes_written)); |
| 113 Dart_ExitScope(); | 119 Dart_ExitScope(); |
| 114 } | 120 } |
| 115 | 121 |
| 116 | 122 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 163 DartUtils::kIdFieldName); | 169 DartUtils::kIdFieldName); |
| 164 Dart_Handle socketobj = Dart_GetNativeArgument(args, 1); | 170 Dart_Handle socketobj = Dart_GetNativeArgument(args, 1); |
| 165 intptr_t newSocket = ServerSocket::Accept(socket); | 171 intptr_t newSocket = ServerSocket::Accept(socket); |
| 166 if (newSocket >= 0) { | 172 if (newSocket >= 0) { |
| 167 DartUtils::SetIntegerField( | 173 DartUtils::SetIntegerField( |
| 168 socketobj, DartUtils::kIdFieldName, newSocket); | 174 socketobj, DartUtils::kIdFieldName, newSocket); |
| 169 } | 175 } |
| 170 Dart_SetReturnValue(args, Dart_NewBoolean(newSocket >= 0)); | 176 Dart_SetReturnValue(args, Dart_NewBoolean(newSocket >= 0)); |
| 171 Dart_ExitScope(); | 177 Dart_ExitScope(); |
| 172 } | 178 } |
| OLD | NEW |