| 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 |
| (...skipping 428 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 439 | 439 |
| 440 | 440 |
| 441 static CObject* LookupRequest(const CObjectArray& request) { | 441 static CObject* LookupRequest(const CObjectArray& request) { |
| 442 if (request.Length() == 3 && | 442 if (request.Length() == 3 && |
| 443 request[1]->IsString() && | 443 request[1]->IsString() && |
| 444 request[2]->IsInt32()) { | 444 request[2]->IsInt32()) { |
| 445 CObjectString host(request[1]); | 445 CObjectString host(request[1]); |
| 446 CObjectInt32 type(request[2]); | 446 CObjectInt32 type(request[2]); |
| 447 CObject* result = NULL; | 447 CObject* result = NULL; |
| 448 OSError* os_error = NULL; | 448 OSError* os_error = NULL; |
| 449 SocketAddresses* addresses = | 449 AddressList<SocketAddress>* addresses = |
| 450 Socket::LookupAddress(host.CString(), type.Value(), &os_error); | 450 Socket::LookupAddress(host.CString(), type.Value(), &os_error); |
| 451 if (addresses != NULL) { | 451 if (addresses != NULL) { |
| 452 CObjectArray* array = new CObjectArray( | 452 CObjectArray* array = new CObjectArray( |
| 453 CObject::NewArray(addresses->count() + 1)); | 453 CObject::NewArray(addresses->count() + 1)); |
| 454 array->SetAt(0, new CObjectInt32(CObject::NewInt32(0))); | 454 array->SetAt(0, new CObjectInt32(CObject::NewInt32(0))); |
| 455 for (intptr_t i = 0; i < addresses->count(); i++) { | 455 for (intptr_t i = 0; i < addresses->count(); i++) { |
| 456 SocketAddress* addr = addresses->GetAt(i); | 456 SocketAddress* addr = addresses->GetAt(i); |
| 457 CObjectArray* entry = new CObjectArray(CObject::NewArray(3)); | 457 CObjectArray* entry = new CObjectArray(CObject::NewArray(3)); |
| 458 | 458 |
| 459 CObjectInt32* type = new CObjectInt32( | 459 CObjectInt32* type = new CObjectInt32( |
| (...skipping 19 matching lines...) Expand all Loading... |
| 479 } else { | 479 } else { |
| 480 result = CObject::NewOSError(os_error); | 480 result = CObject::NewOSError(os_error); |
| 481 delete os_error; | 481 delete os_error; |
| 482 } | 482 } |
| 483 return result; | 483 return result; |
| 484 } | 484 } |
| 485 return CObject::IllegalArgumentError(); | 485 return CObject::IllegalArgumentError(); |
| 486 } | 486 } |
| 487 | 487 |
| 488 | 488 |
| 489 static CObject* ListInterfacesRequest(const CObjectArray& request) { |
| 490 if (request.Length() == 2 && |
| 491 request[1]->IsInt32()) { |
| 492 CObjectInt32 type(request[1]); |
| 493 CObject* result = NULL; |
| 494 OSError* os_error = NULL; |
| 495 AddressList<InterfaceSocketAddress>* addresses = Socket::ListInterfaces( |
| 496 type.Value(), &os_error); |
| 497 if (addresses != NULL) { |
| 498 CObjectArray* array = new CObjectArray( |
| 499 CObject::NewArray(addresses->count() + 1)); |
| 500 array->SetAt(0, new CObjectInt32(CObject::NewInt32(0))); |
| 501 for (intptr_t i = 0; i < addresses->count(); i++) { |
| 502 InterfaceSocketAddress* interface = addresses->GetAt(i); |
| 503 SocketAddress* addr = interface->socket_address(); |
| 504 CObjectArray* entry = new CObjectArray(CObject::NewArray(4)); |
| 505 |
| 506 CObjectInt32* type = new CObjectInt32( |
| 507 CObject::NewInt32(addr->GetType())); |
| 508 entry->SetAt(0, type); |
| 509 |
| 510 CObjectString* as_string = new CObjectString(CObject::NewString( |
| 511 addr->as_string())); |
| 512 entry->SetAt(1, as_string); |
| 513 |
| 514 RawAddr raw = addr->addr(); |
| 515 CObjectUint8Array* data = new CObjectUint8Array(CObject::NewUint8Array( |
| 516 SocketAddress::GetAddrLength(raw))); |
| 517 memmove(data->Buffer(), |
| 518 reinterpret_cast<void *>(&raw), |
| 519 SocketAddress::GetAddrLength(raw)); |
| 520 entry->SetAt(2, data); |
| 521 |
| 522 CObjectString* interface_name = new CObjectString(CObject::NewString( |
| 523 interface->interface_name())); |
| 524 entry->SetAt(3, interface_name); |
| 525 |
| 526 array->SetAt(i + 1, entry); |
| 527 } |
| 528 result = array; |
| 529 delete addresses; |
| 530 } else { |
| 531 result = CObject::NewOSError(os_error); |
| 532 delete os_error; |
| 533 } |
| 534 return result; |
| 535 } |
| 536 return CObject::IllegalArgumentError(); |
| 537 } |
| 538 |
| 539 |
| 489 void SocketService(Dart_Port dest_port_id, | 540 void SocketService(Dart_Port dest_port_id, |
| 490 Dart_Port reply_port_id, | 541 Dart_Port reply_port_id, |
| 491 Dart_CObject* message) { | 542 Dart_CObject* message) { |
| 492 CObject* response = CObject::IllegalArgumentError(); | 543 CObject* response = CObject::IllegalArgumentError(); |
| 493 CObjectArray request(message); | 544 CObjectArray request(message); |
| 494 if (message->type == Dart_CObject_kArray) { | 545 if (message->type == Dart_CObject_kArray) { |
| 495 if (request.Length() > 1 && request[0]->IsInt32()) { | 546 if (request.Length() > 1 && request[0]->IsInt32()) { |
| 496 CObjectInt32 request_type(request[0]); | 547 CObjectInt32 request_type(request[0]); |
| 497 switch (request_type.Value()) { | 548 switch (request_type.Value()) { |
| 498 case Socket::kLookupRequest: | 549 case Socket::kLookupRequest: |
| 499 response = LookupRequest(request); | 550 response = LookupRequest(request); |
| 500 break; | 551 break; |
| 552 case Socket::kListInterfacesRequest: |
| 553 response = ListInterfacesRequest(request); |
| 554 break; |
| 501 default: | 555 default: |
| 502 UNREACHABLE(); | 556 UNREACHABLE(); |
| 503 } | 557 } |
| 504 } | 558 } |
| 505 } | 559 } |
| 506 | 560 |
| 507 Dart_PostCObject(reply_port_id, response->AsApiCObject()); | 561 Dart_PostCObject(reply_port_id, response->AsApiCObject()); |
| 508 } | 562 } |
| 509 | 563 |
| 510 | 564 |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 577 return Dart_SetNativeInstanceField(socket, kSocketIdNativeField, id); | 631 return Dart_SetNativeInstanceField(socket, kSocketIdNativeField, id); |
| 578 } | 632 } |
| 579 | 633 |
| 580 | 634 |
| 581 Dart_Handle Socket::GetSocketIdNativeField(Dart_Handle socket, intptr_t* id) { | 635 Dart_Handle Socket::GetSocketIdNativeField(Dart_Handle socket, intptr_t* id) { |
| 582 return Dart_GetNativeInstanceField(socket, kSocketIdNativeField, id); | 636 return Dart_GetNativeInstanceField(socket, kSocketIdNativeField, id); |
| 583 } | 637 } |
| 584 | 638 |
| 585 } // namespace bin | 639 } // namespace bin |
| 586 } // namespace dart | 640 } // namespace dart |
| OLD | NEW |