| OLD | NEW |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 #if !defined(DART_IO_DISABLED) | 5 #if !defined(DART_IO_DISABLED) |
| 6 | 6 |
| 7 #include "bin/sync_socket.h" | 7 #include "bin/sync_socket.h" |
| 8 | 8 |
| 9 #include "bin/dartutils.h" | 9 #include "bin/dartutils.h" |
| 10 #include "bin/io_buffer.h" | 10 #include "bin/io_buffer.h" |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 intptr_t offset = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 2)); | 136 intptr_t offset = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 2)); |
| 137 intptr_t length = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 3)); | 137 intptr_t length = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 3)); |
| 138 Dart_TypedData_Type type; | 138 Dart_TypedData_Type type; |
| 139 uint8_t* buffer = NULL; | 139 uint8_t* buffer = NULL; |
| 140 intptr_t len; | 140 intptr_t len; |
| 141 result = Dart_TypedDataAcquireData(buffer_obj, &type, | 141 result = Dart_TypedDataAcquireData(buffer_obj, &type, |
| 142 reinterpret_cast<void**>(&buffer), &len); | 142 reinterpret_cast<void**>(&buffer), &len); |
| 143 DART_CHECK_ERROR(result); | 143 DART_CHECK_ERROR(result); |
| 144 ASSERT((offset + length) <= len); | 144 ASSERT((offset + length) <= len); |
| 145 buffer += offset; | 145 buffer += offset; |
| 146 intptr_t bytes_written = SocketBase::Write(socket->fd(), buffer, length); | 146 intptr_t bytes_written = |
| 147 SynchronousSocket::Write(socket->fd(), buffer, length); |
| 147 if (bytes_written >= 0) { | 148 if (bytes_written >= 0) { |
| 148 Dart_SetIntegerReturnValue(args, bytes_written); | 149 Dart_SetIntegerReturnValue(args, bytes_written); |
| 149 } else { | 150 } else { |
| 150 OSError os_error; | 151 OSError os_error; |
| 151 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); | 152 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); |
| 152 } | 153 } |
| 153 Dart_TypedDataReleaseData(buffer_obj); | 154 Dart_TypedDataReleaseData(buffer_obj); |
| 154 } | 155 } |
| 155 | 156 |
| 156 | 157 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 167 return; | 168 return; |
| 168 } | 169 } |
| 169 intptr_t offset = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 2)); | 170 intptr_t offset = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 2)); |
| 170 intptr_t bytes = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 3)); | 171 intptr_t bytes = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 3)); |
| 171 intptr_t array_len = 0; | 172 intptr_t array_len = 0; |
| 172 | 173 |
| 173 result = Dart_ListLength(buffer_obj, &array_len); | 174 result = Dart_ListLength(buffer_obj, &array_len); |
| 174 DART_CHECK_ERROR(result); | 175 DART_CHECK_ERROR(result); |
| 175 | 176 |
| 176 uint8_t* buffer = Dart_ScopeAllocate(bytes); | 177 uint8_t* buffer = Dart_ScopeAllocate(bytes); |
| 177 intptr_t bytes_read = SocketBase::Read(socket->fd(), buffer, bytes); | 178 intptr_t bytes_read = SynchronousSocket::Read(socket->fd(), buffer, bytes); |
| 178 if (bytes_read < 0) { | 179 if (bytes_read < 0) { |
| 179 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 180 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
| 180 return; | 181 return; |
| 181 } | 182 } |
| 182 if (bytes_read > 0) { | 183 if (bytes_read > 0) { |
| 183 result = Dart_ListSetAsBytes(buffer_obj, offset, buffer, bytes_read); | 184 result = Dart_ListSetAsBytes(buffer_obj, offset, buffer, bytes_read); |
| 184 DART_CHECK_ERROR(result); | 185 DART_CHECK_ERROR(result); |
| 185 } | 186 } |
| 186 Dart_SetIntegerReturnValue(args, bytes_read); | 187 Dart_SetIntegerReturnValue(args, bytes_read); |
| 187 } | 188 } |
| 188 | 189 |
| 189 | 190 |
| 190 void FUNCTION_NAME(SynchronousSocket_Available)(Dart_NativeArguments args) { | 191 void FUNCTION_NAME(SynchronousSocket_Available)(Dart_NativeArguments args) { |
| 191 SynchronousSocket* socket = NULL; | 192 SynchronousSocket* socket = NULL; |
| 192 Dart_Handle result = SynchronousSocket::GetSocketIdNativeField( | 193 Dart_Handle result = SynchronousSocket::GetSocketIdNativeField( |
| 193 Dart_GetNativeArgument(args, 0), &socket); | 194 Dart_GetNativeArgument(args, 0), &socket); |
| 194 DART_CHECK_ERROR(result); | 195 DART_CHECK_ERROR(result); |
| 195 | 196 |
| 196 intptr_t available = SocketBase::Available(socket->fd()); | 197 intptr_t available = SynchronousSocket::Available(socket->fd()); |
| 197 if (available >= 0) { | 198 if (available >= 0) { |
| 198 Dart_SetIntegerReturnValue(args, available); | 199 Dart_SetIntegerReturnValue(args, available); |
| 199 } else { | 200 } else { |
| 200 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 201 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
| 201 } | 202 } |
| 202 } | 203 } |
| 203 | 204 |
| 204 | 205 |
| 205 void FUNCTION_NAME(SynchronousSocket_CloseSync)(Dart_NativeArguments args) { | 206 void FUNCTION_NAME(SynchronousSocket_CloseSync)(Dart_NativeArguments args) { |
| 206 SynchronousSocket* socket = NULL; | 207 SynchronousSocket* socket = NULL; |
| 207 Dart_Handle result = SynchronousSocket::GetSocketIdNativeField( | 208 Dart_Handle result = SynchronousSocket::GetSocketIdNativeField( |
| 208 Dart_GetNativeArgument(args, 0), &socket); | 209 Dart_GetNativeArgument(args, 0), &socket); |
| 209 DART_CHECK_ERROR(result); | 210 DART_CHECK_ERROR(result); |
| 210 | 211 |
| 211 SocketBase::Close(socket->fd()); | 212 SynchronousSocket::Close(socket->fd()); |
| 212 socket->SetClosedFd(); | 213 socket->SetClosedFd(); |
| 213 } | 214 } |
| 214 | 215 |
| 215 | 216 |
| 216 void FUNCTION_NAME(SynchronousSocket_Read)(Dart_NativeArguments args) { | 217 void FUNCTION_NAME(SynchronousSocket_Read)(Dart_NativeArguments args) { |
| 217 SynchronousSocket* socket = NULL; | 218 SynchronousSocket* socket = NULL; |
| 218 Dart_Handle result = SynchronousSocket::GetSocketIdNativeField( | 219 Dart_Handle result = SynchronousSocket::GetSocketIdNativeField( |
| 219 Dart_GetNativeArgument(args, 0), &socket); | 220 Dart_GetNativeArgument(args, 0), &socket); |
| 220 DART_CHECK_ERROR(result); | 221 DART_CHECK_ERROR(result); |
| 221 | 222 |
| 222 int64_t length = 0; | 223 int64_t length = 0; |
| 223 if (!DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 1), &length)) { | 224 if (!DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 1), &length)) { |
| 224 Dart_SetReturnValue(args, DartUtils::NewDartArgumentError( | 225 Dart_SetReturnValue(args, DartUtils::NewDartArgumentError( |
| 225 "First parameter must be an integer.")); | 226 "First parameter must be an integer.")); |
| 226 return; | 227 return; |
| 227 } | 228 } |
| 228 uint8_t* buffer = NULL; | 229 uint8_t* buffer = NULL; |
| 229 result = IOBuffer::Allocate(length, &buffer); | 230 result = IOBuffer::Allocate(length, &buffer); |
| 230 ASSERT(buffer != NULL); | 231 ASSERT(buffer != NULL); |
| 231 intptr_t bytes_read = SocketBase::Read(socket->fd(), buffer, length); | 232 intptr_t bytes_read = SynchronousSocket::Read(socket->fd(), buffer, length); |
| 232 if (bytes_read == length) { | 233 if (bytes_read == length) { |
| 233 Dart_SetReturnValue(args, result); | 234 Dart_SetReturnValue(args, result); |
| 234 } else if (bytes_read > 0) { | 235 } else if (bytes_read > 0) { |
| 235 uint8_t* new_buffer = NULL; | 236 uint8_t* new_buffer = NULL; |
| 236 Dart_Handle new_result = IOBuffer::Allocate(bytes_read, &new_buffer); | 237 Dart_Handle new_result = IOBuffer::Allocate(bytes_read, &new_buffer); |
| 237 ASSERT(new_buffer != NULL); | 238 ASSERT(new_buffer != NULL); |
| 238 memmove(new_buffer, buffer, bytes_read); | 239 memmove(new_buffer, buffer, bytes_read); |
| 239 Dart_SetReturnValue(args, new_result); | 240 Dart_SetReturnValue(args, new_result); |
| 240 } else if (bytes_read == -1) { | 241 } else if (bytes_read == -1) { |
| 241 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 242 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 262 SynchronousSocket::ShutdownWrite(socket->fd()); | 263 SynchronousSocket::ShutdownWrite(socket->fd()); |
| 263 } | 264 } |
| 264 | 265 |
| 265 | 266 |
| 266 void FUNCTION_NAME(SynchronousSocket_GetPort)(Dart_NativeArguments args) { | 267 void FUNCTION_NAME(SynchronousSocket_GetPort)(Dart_NativeArguments args) { |
| 267 SynchronousSocket* socket = NULL; | 268 SynchronousSocket* socket = NULL; |
| 268 Dart_Handle result = SynchronousSocket::GetSocketIdNativeField( | 269 Dart_Handle result = SynchronousSocket::GetSocketIdNativeField( |
| 269 Dart_GetNativeArgument(args, 0), &socket); | 270 Dart_GetNativeArgument(args, 0), &socket); |
| 270 DART_CHECK_ERROR(result); | 271 DART_CHECK_ERROR(result); |
| 271 | 272 |
| 272 intptr_t port = SocketBase::GetPort(socket->fd()); | 273 intptr_t port = SynchronousSocket::GetPort(socket->fd()); |
| 273 if (port > 0) { | 274 if (port > 0) { |
| 274 Dart_SetReturnValue(args, Dart_NewInteger(port)); | 275 Dart_SetReturnValue(args, Dart_NewInteger(port)); |
| 275 } else { | 276 } else { |
| 276 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 277 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
| 277 } | 278 } |
| 278 } | 279 } |
| 279 | 280 |
| 280 | 281 |
| 281 void FUNCTION_NAME(SynchronousSocket_GetRemotePeer)(Dart_NativeArguments args) { | 282 void FUNCTION_NAME(SynchronousSocket_GetRemotePeer)(Dart_NativeArguments args) { |
| 282 SynchronousSocket* socket = NULL; | 283 SynchronousSocket* socket = NULL; |
| 283 Dart_Handle result = SynchronousSocket::GetSocketIdNativeField( | 284 Dart_Handle result = SynchronousSocket::GetSocketIdNativeField( |
| 284 Dart_GetNativeArgument(args, 0), &socket); | 285 Dart_GetNativeArgument(args, 0), &socket); |
| 285 DART_CHECK_ERROR(result); | 286 DART_CHECK_ERROR(result); |
| 286 | 287 |
| 287 intptr_t port = 0; | 288 intptr_t port = 0; |
| 288 SocketAddress* addr = SocketBase::GetRemotePeer(socket->fd(), &port); | 289 SocketAddress* addr = SynchronousSocket::GetRemotePeer(socket->fd(), &port); |
| 289 if (addr == NULL) { | 290 if (addr == NULL) { |
| 290 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 291 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
| 291 return; | 292 return; |
| 292 } | 293 } |
| 293 Dart_Handle list = Dart_NewList(2); | 294 Dart_Handle list = Dart_NewList(2); |
| 294 DART_CHECK_ERROR_AND_CLEANUP(list, addr); | 295 DART_CHECK_ERROR_AND_CLEANUP(list, addr); |
| 295 | 296 |
| 296 Dart_Handle entry = Dart_NewList(3); | 297 Dart_Handle entry = Dart_NewList(3); |
| 297 DART_CHECK_ERROR_AND_CLEANUP(entry, addr); | 298 DART_CHECK_ERROR_AND_CLEANUP(entry, addr); |
| 298 | 299 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 314 Dart_SetReturnValue(args, list); | 315 Dart_SetReturnValue(args, list); |
| 315 delete addr; | 316 delete addr; |
| 316 } | 317 } |
| 317 | 318 |
| 318 | 319 |
| 319 static void SynchronousSocketFinalizer(void* isolate_data, | 320 static void SynchronousSocketFinalizer(void* isolate_data, |
| 320 Dart_WeakPersistentHandle handle, | 321 Dart_WeakPersistentHandle handle, |
| 321 void* data) { | 322 void* data) { |
| 322 SynchronousSocket* socket = reinterpret_cast<SynchronousSocket*>(data); | 323 SynchronousSocket* socket = reinterpret_cast<SynchronousSocket*>(data); |
| 323 if (socket->fd() >= 0) { | 324 if (socket->fd() >= 0) { |
| 324 SocketBase::Close(socket->fd()); | 325 SynchronousSocket::Close(socket->fd()); |
| 325 socket->SetClosedFd(); | 326 socket->SetClosedFd(); |
| 326 } | 327 } |
| 327 delete socket; | 328 delete socket; |
| 328 } | 329 } |
| 329 | 330 |
| 330 | 331 |
| 331 Dart_Handle SynchronousSocket::SetSocketIdNativeField( | 332 Dart_Handle SynchronousSocket::SetSocketIdNativeField( |
| 332 Dart_Handle handle, | 333 Dart_Handle handle, |
| 333 SynchronousSocket* socket) { | 334 SynchronousSocket* socket) { |
| 334 Dart_Handle error = Dart_SetNativeInstanceField( | 335 Dart_Handle error = Dart_SetNativeInstanceField( |
| (...skipping 21 matching lines...) Expand all Loading... |
| 356 return result; | 357 return result; |
| 357 } | 358 } |
| 358 *socket = reinterpret_cast<SynchronousSocket*>(id); | 359 *socket = reinterpret_cast<SynchronousSocket*>(id); |
| 359 return result; | 360 return result; |
| 360 } | 361 } |
| 361 | 362 |
| 362 } // namespace bin | 363 } // namespace bin |
| 363 } // namespace dart | 364 } // namespace dart |
| 364 | 365 |
| 365 #endif // !defined(DART_IO_DISABLED) | 366 #endif // !defined(DART_IO_DISABLED) |
| OLD | NEW |