OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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/io_buffer.h" | 5 #include "bin/io_buffer.h" |
6 #include "bin/socket.h" | 6 #include "bin/socket.h" |
7 #include "bin/dartutils.h" | 7 #include "bin/dartutils.h" |
8 #include "bin/thread.h" | 8 #include "bin/thread.h" |
9 #include "bin/utils.h" | 9 #include "bin/utils.h" |
10 | 10 |
11 #include "platform/globals.h" | 11 #include "platform/globals.h" |
12 #include "platform/thread.h" | 12 #include "platform/thread.h" |
13 #include "platform/utils.h" | 13 #include "platform/utils.h" |
14 | 14 |
15 #include "include/dart_api.h" | 15 #include "include/dart_api.h" |
16 | 16 |
17 static const int kSocketIdNativeField = 0; | 17 static const int kSocketIdNativeField = 0; |
18 | 18 |
19 dart::Mutex Socket::mutex_; | 19 dart::Mutex Socket::mutex_; |
20 int Socket::service_ports_size_ = 0; | 20 int Socket::service_ports_size_ = 0; |
21 Dart_Port* Socket::service_ports_ = NULL; | 21 Dart_Port* Socket::service_ports_ = NULL; |
22 int Socket::service_ports_index_ = 0; | 22 int Socket::service_ports_index_ = 0; |
23 | 23 |
24 void FUNCTION_NAME(Socket_CreateConnect)(Dart_NativeArguments args) { | 24 void FUNCTION_NAME(Socket_CreateConnect)(Dart_NativeArguments args) { |
25 Dart_EnterScope(); | 25 Dart_EnterScope(); |
26 Dart_Handle socket_obj = Dart_GetNativeArgument(args, 0); | 26 Dart_Handle socket_obj = Dart_GetNativeArgument(args, 0); |
27 const char* host = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); | 27 int type = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); |
| 28 Dart_Handle host_obj = Dart_GetNativeArgument(args, 2); |
| 29 Dart_TypedData_Type data_type; |
| 30 uint8_t* data = NULL; |
| 31 intptr_t len; |
| 32 Dart_Handle result = Dart_TypedDataAcquireData( |
| 33 host_obj, &data_type, reinterpret_cast<void**>(&data), &len); |
28 int64_t port = 0; | 34 int64_t port = 0; |
29 if (DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 2), &port)) { | 35 if (!Dart_IsError(result) && |
30 intptr_t socket = Socket::CreateConnect(host, port); | 36 DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 3), &port)) { |
| 37 intptr_t socket = Socket::CreateConnect( |
| 38 type == 1 ? SocketAddress::IPv6 : SocketAddress::IPv4, |
| 39 data, |
| 40 len, |
| 41 port); |
| 42 Dart_TypedDataReleaseData(host_obj); |
31 if (socket >= 0) { | 43 if (socket >= 0) { |
32 Dart_Handle err = Socket::SetSocketIdNativeField(socket_obj, socket); | 44 Dart_Handle err = Socket::SetSocketIdNativeField(socket_obj, socket); |
33 if (Dart_IsError(err)) Dart_PropagateError(err); | 45 if (Dart_IsError(err)) Dart_PropagateError(err); |
34 Dart_SetReturnValue(args, Dart_True()); | 46 Dart_SetReturnValue(args, Dart_True()); |
35 } else { | 47 } else { |
36 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 48 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
37 } | 49 } |
38 } else { | 50 } else { |
39 OSError os_error(-1, "Invalid argument", OSError::kUnknown); | 51 OSError os_error(-1, "Invalid argument", OSError::kUnknown); |
40 Dart_Handle err = DartUtils::NewDartOSError(&os_error); | 52 Dart_Handle err = DartUtils::NewDartOSError(&os_error); |
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 Dart_Handle err = Socket::SetSocketIdNativeField(socket_obj, socket); | 315 Dart_Handle err = Socket::SetSocketIdNativeField(socket_obj, socket); |
304 if (Dart_IsError(err)) Dart_PropagateError(err); | 316 if (Dart_IsError(err)) Dart_PropagateError(err); |
305 Dart_SetReturnValue(args, Dart_NewBoolean(socket >= 0)); | 317 Dart_SetReturnValue(args, Dart_NewBoolean(socket >= 0)); |
306 Dart_ExitScope(); | 318 Dart_ExitScope(); |
307 } | 319 } |
308 | 320 |
309 | 321 |
310 void FUNCTION_NAME(ServerSocket_CreateBindListen)(Dart_NativeArguments args) { | 322 void FUNCTION_NAME(ServerSocket_CreateBindListen)(Dart_NativeArguments args) { |
311 Dart_EnterScope(); | 323 Dart_EnterScope(); |
312 Dart_Handle socket_obj = Dart_GetNativeArgument(args, 0); | 324 Dart_Handle socket_obj = Dart_GetNativeArgument(args, 0); |
313 Dart_Handle bind_address_obj = Dart_GetNativeArgument(args, 1); | 325 int type = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); |
314 Dart_Handle port_obj = Dart_GetNativeArgument(args, 2); | 326 Dart_Handle host_obj = Dart_GetNativeArgument(args, 2); |
315 Dart_Handle backlog_obj = Dart_GetNativeArgument(args, 3); | 327 Dart_TypedData_Type data_type; |
| 328 uint8_t* data = NULL; |
| 329 intptr_t len; |
| 330 Dart_Handle result = Dart_TypedDataAcquireData( |
| 331 host_obj, &data_type, reinterpret_cast<void**>(&data), &len); |
| 332 Dart_Handle port_obj = Dart_GetNativeArgument(args, 3); |
| 333 Dart_Handle backlog_obj = Dart_GetNativeArgument(args, 4); |
316 int64_t port = 0; | 334 int64_t port = 0; |
317 int64_t backlog = 0; | 335 int64_t backlog = 0; |
318 if (Dart_IsString(bind_address_obj) && | 336 if (!Dart_IsError(result) && |
319 DartUtils::GetInt64Value(port_obj, &port) && | 337 DartUtils::GetInt64Value(port_obj, &port) && |
320 DartUtils::GetInt64Value(backlog_obj, &backlog)) { | 338 DartUtils::GetInt64Value(backlog_obj, &backlog)) { |
321 const char* bind_address = DartUtils::GetStringValue(bind_address_obj); | 339 intptr_t socket = ServerSocket::CreateBindListen( |
322 intptr_t socket = | 340 type == 1 ? SocketAddress::IPv6 : SocketAddress::IPv4, |
323 ServerSocket::CreateBindListen(bind_address, port, backlog); | 341 data, |
| 342 len, |
| 343 port, |
| 344 backlog); |
| 345 Dart_TypedDataReleaseData(host_obj); |
324 if (socket >= 0) { | 346 if (socket >= 0) { |
325 Dart_Handle err = Socket::SetSocketIdNativeField(socket_obj, socket); | 347 Dart_Handle err = Socket::SetSocketIdNativeField(socket_obj, socket); |
326 if (Dart_IsError(err)) Dart_PropagateError(err); | 348 if (Dart_IsError(err)) Dart_PropagateError(err); |
327 Dart_SetReturnValue(args, Dart_True()); | 349 Dart_SetReturnValue(args, Dart_True()); |
328 } else { | 350 } else { |
329 if (socket == -5) { | 351 if (socket == -5) { |
330 OSError os_error(-1, "Invalid host", OSError::kUnknown); | 352 OSError os_error(-1, "Invalid host", OSError::kUnknown); |
331 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); | 353 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); |
332 } else { | 354 } else { |
333 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 355 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
(...skipping 29 matching lines...) Expand all Loading... |
363 } | 385 } |
364 Dart_ExitScope(); | 386 Dart_ExitScope(); |
365 } | 387 } |
366 | 388 |
367 | 389 |
368 static CObject* LookupRequest(const CObjectArray& request) { | 390 static CObject* LookupRequest(const CObjectArray& request) { |
369 if (request.Length() == 2 && request[1]->IsString()) { | 391 if (request.Length() == 2 && request[1]->IsString()) { |
370 CObjectString host(request[1]); | 392 CObjectString host(request[1]); |
371 CObject* result = NULL; | 393 CObject* result = NULL; |
372 OSError* os_error = NULL; | 394 OSError* os_error = NULL; |
373 const char* ip_address = | 395 SocketAddresses* addresses = |
374 Socket::LookupIPv4Address(host.CString(), &os_error); | 396 Socket::LookupAddress(host.CString(), &os_error); |
375 if (ip_address != NULL) { | 397 if (addresses != NULL) { |
376 result = new CObjectString(CObject::NewString(ip_address)); | 398 CObjectArray* array = new CObjectArray( |
377 free(const_cast<char*>(ip_address)); | 399 CObject::NewArray(addresses->count)); |
| 400 for (intptr_t i = 0; i < addresses->count; i++) { |
| 401 CObjectArray* entry = new CObjectArray(CObject::NewArray(3)); |
| 402 |
| 403 CObjectInt32* type = new CObjectInt32(CObject::NewInt32( |
| 404 static_cast<int>(addresses->addresses[i]->type))); |
| 405 entry->SetAt(0, type); |
| 406 |
| 407 CObjectString* as_string = new CObjectString(CObject::NewString( |
| 408 addresses->addresses[i]->as_string)); |
| 409 entry->SetAt(1, as_string); |
| 410 |
| 411 CObjectUint8Array* data = new CObjectUint8Array(CObject::NewUint8Array( |
| 412 addresses->addresses[i]->length)); |
| 413 memmove(data->Buffer(), |
| 414 addresses->addresses[i]->data, |
| 415 addresses->addresses[i]->length); |
| 416 |
| 417 entry->SetAt(2, data); |
| 418 array->SetAt(i, entry); |
| 419 } |
| 420 result = array; |
| 421 delete addresses; |
378 } else { | 422 } else { |
379 result = CObject::NewOSError(os_error); | 423 result = CObject::NewOSError(os_error); |
380 delete os_error; | 424 delete os_error; |
381 } | 425 } |
382 return result; | 426 return result; |
383 } | 427 } |
384 return CObject::IllegalArgumentError(); | 428 return CObject::IllegalArgumentError(); |
385 } | 429 } |
386 | 430 |
387 | 431 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
473 | 517 |
474 | 518 |
475 Dart_Handle Socket::SetSocketIdNativeField(Dart_Handle socket, intptr_t id) { | 519 Dart_Handle Socket::SetSocketIdNativeField(Dart_Handle socket, intptr_t id) { |
476 return Dart_SetNativeInstanceField(socket, kSocketIdNativeField, id); | 520 return Dart_SetNativeInstanceField(socket, kSocketIdNativeField, id); |
477 } | 521 } |
478 | 522 |
479 | 523 |
480 Dart_Handle Socket::GetSocketIdNativeField(Dart_Handle socket, intptr_t* id) { | 524 Dart_Handle Socket::GetSocketIdNativeField(Dart_Handle socket, intptr_t* id) { |
481 return Dart_GetNativeInstanceField(socket, kSocketIdNativeField, id); | 525 return Dart_GetNativeInstanceField(socket, kSocketIdNativeField, id); |
482 } | 526 } |
OLD | NEW |