Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(284)

Side by Side Diff: runtime/bin/socket.cc

Issue 2480793002: clang-format runtime/bin (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « runtime/bin/socket.h ('k') | runtime/bin/socket_android.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #if !defined(DART_IO_DISABLED) 5 #if !defined(DART_IO_DISABLED)
6 6
7 #include "bin/socket.h" 7 #include "bin/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"
11 #include "bin/isolate_data.h" 11 #include "bin/isolate_data.h"
12 #include "bin/lockers.h" 12 #include "bin/lockers.h"
13 #include "bin/thread.h" 13 #include "bin/thread.h"
14 #include "bin/utils.h" 14 #include "bin/utils.h"
15 15
16 #include "include/dart_api.h" 16 #include "include/dart_api.h"
17 17
18 #include "platform/globals.h" 18 #include "platform/globals.h"
19 #include "platform/utils.h" 19 #include "platform/utils.h"
20 20
21 namespace dart { 21 namespace dart {
22 namespace bin { 22 namespace bin {
23 23
24 static const int kSocketIdNativeField = 0; 24 static const int kSocketIdNativeField = 0;
25 25
26 ListeningSocketRegistry *globalTcpListeningSocketRegistry = NULL; 26 ListeningSocketRegistry* globalTcpListeningSocketRegistry = NULL;
27 27
28 bool short_socket_read = false; 28 bool short_socket_read = false;
29 29
30 bool short_socket_write = false; 30 bool short_socket_write = false;
31 31
32 void ListeningSocketRegistry::Initialize() { 32 void ListeningSocketRegistry::Initialize() {
33 ASSERT(globalTcpListeningSocketRegistry == NULL); 33 ASSERT(globalTcpListeningSocketRegistry == NULL);
34 globalTcpListeningSocketRegistry = new ListeningSocketRegistry(); 34 globalTcpListeningSocketRegistry = new ListeningSocketRegistry();
35 } 35 }
36 36
37 37
38 ListeningSocketRegistry *ListeningSocketRegistry::Instance() { 38 ListeningSocketRegistry* ListeningSocketRegistry::Instance() {
39 return globalTcpListeningSocketRegistry; 39 return globalTcpListeningSocketRegistry;
40 } 40 }
41 41
42 42
43 void ListeningSocketRegistry::Cleanup() { 43 void ListeningSocketRegistry::Cleanup() {
44 delete globalTcpListeningSocketRegistry; 44 delete globalTcpListeningSocketRegistry;
45 globalTcpListeningSocketRegistry = NULL; 45 globalTcpListeningSocketRegistry = NULL;
46 } 46 }
47 47
48 48
49 ListeningSocketRegistry::OSSocket* ListeningSocketRegistry::LookupByPort( 49 ListeningSocketRegistry::OSSocket* ListeningSocketRegistry::LookupByPort(
50 intptr_t port) { 50 intptr_t port) {
51 HashMap::Entry* entry = 51 HashMap::Entry* entry = sockets_by_port_.Lookup(
52 sockets_by_port_.Lookup(GetHashmapKeyFromIntptr(port), 52 GetHashmapKeyFromIntptr(port), GetHashmapHashFromIntptr(port), false);
53 GetHashmapHashFromIntptr(port), false);
54 if (entry == NULL) { 53 if (entry == NULL) {
55 return NULL; 54 return NULL;
56 } 55 }
57 return reinterpret_cast<OSSocket*>(entry->value); 56 return reinterpret_cast<OSSocket*>(entry->value);
58 } 57 }
59 58
60 59
61 void ListeningSocketRegistry::InsertByPort(intptr_t port, OSSocket* socket) { 60 void ListeningSocketRegistry::InsertByPort(intptr_t port, OSSocket* socket) {
62 HashMap::Entry* entry = 61 HashMap::Entry* entry = sockets_by_port_.Lookup(
63 sockets_by_port_.Lookup(GetHashmapKeyFromIntptr(port), 62 GetHashmapKeyFromIntptr(port), GetHashmapHashFromIntptr(port), true);
64 GetHashmapHashFromIntptr(port), true);
65 ASSERT(entry != NULL); 63 ASSERT(entry != NULL);
66 entry->value = reinterpret_cast<void*>(socket); 64 entry->value = reinterpret_cast<void*>(socket);
67 } 65 }
68 66
69 67
70 void ListeningSocketRegistry::RemoveByPort(intptr_t port) { 68 void ListeningSocketRegistry::RemoveByPort(intptr_t port) {
71 sockets_by_port_.Remove( 69 sockets_by_port_.Remove(GetHashmapKeyFromIntptr(port),
72 GetHashmapKeyFromIntptr(port), GetHashmapHashFromIntptr(port)); 70 GetHashmapHashFromIntptr(port));
73 } 71 }
74 72
75 73
76 ListeningSocketRegistry::OSSocket* ListeningSocketRegistry::LookupByFd( 74 ListeningSocketRegistry::OSSocket* ListeningSocketRegistry::LookupByFd(
77 intptr_t fd) { 75 intptr_t fd) {
78 HashMap::Entry* entry = 76 HashMap::Entry* entry = sockets_by_fd_.Lookup(
79 sockets_by_fd_.Lookup(GetHashmapKeyFromIntptr(fd), 77 GetHashmapKeyFromIntptr(fd), GetHashmapHashFromIntptr(fd), false);
80 GetHashmapHashFromIntptr(fd), false);
81 if (entry == NULL) { 78 if (entry == NULL) {
82 return NULL; 79 return NULL;
83 } 80 }
84 return reinterpret_cast<OSSocket*>(entry->value); 81 return reinterpret_cast<OSSocket*>(entry->value);
85 } 82 }
86 83
87 84
88 void ListeningSocketRegistry::InsertByFd(intptr_t fd, OSSocket* socket) { 85 void ListeningSocketRegistry::InsertByFd(intptr_t fd, OSSocket* socket) {
89 HashMap::Entry* entry = 86 HashMap::Entry* entry = sockets_by_fd_.Lookup(
90 sockets_by_fd_.Lookup(GetHashmapKeyFromIntptr(fd), 87 GetHashmapKeyFromIntptr(fd), GetHashmapHashFromIntptr(fd), true);
91 GetHashmapHashFromIntptr(fd), true);
92 ASSERT(entry != NULL); 88 ASSERT(entry != NULL);
93 entry->value = reinterpret_cast<void*>(socket); 89 entry->value = reinterpret_cast<void*>(socket);
94 } 90 }
95 91
96 92
97 void ListeningSocketRegistry::RemoveByFd(intptr_t fd) { 93 void ListeningSocketRegistry::RemoveByFd(intptr_t fd) {
98 sockets_by_fd_.Remove( 94 sockets_by_fd_.Remove(GetHashmapKeyFromIntptr(fd),
99 GetHashmapKeyFromIntptr(fd), GetHashmapHashFromIntptr(fd)); 95 GetHashmapHashFromIntptr(fd));
100 } 96 }
101 97
102 98
103 Dart_Handle ListeningSocketRegistry::CreateBindListen(Dart_Handle socket_object, 99 Dart_Handle ListeningSocketRegistry::CreateBindListen(Dart_Handle socket_object,
104 RawAddr addr, 100 RawAddr addr,
105 intptr_t backlog, 101 intptr_t backlog,
106 bool v6_only, 102 bool v6_only,
107 bool shared) { 103 bool shared) {
108 MutexLocker ml(ListeningSocketRegistry::mutex_); 104 MutexLocker ml(ListeningSocketRegistry::mutex_);
109 105
110 intptr_t port = SocketAddress::GetAddrPort(addr); 106 intptr_t port = SocketAddress::GetAddrPort(addr);
111 OSSocket* first_os_socket = LookupByPort(port); 107 OSSocket* first_os_socket = LookupByPort(port);
112 if (first_os_socket != NULL) { 108 if (first_os_socket != NULL) {
113 // There is already a socket listening on this port. We need to ensure 109 // There is already a socket listening on this port. We need to ensure
114 // that if there is one also listening on the same address, it was created 110 // that if there is one also listening on the same address, it was created
115 // with `shared = true`, ... 111 // with `shared = true`, ...
116 OSSocket *os_socket = first_os_socket; 112 OSSocket* os_socket = first_os_socket;
117 OSSocket *os_socket_same_addr = findOSSocketWithAddress(os_socket, addr); 113 OSSocket* os_socket_same_addr = findOSSocketWithAddress(os_socket, addr);
118 114
119 if (os_socket_same_addr != NULL) { 115 if (os_socket_same_addr != NULL) {
120 if (!os_socket_same_addr->shared || !shared) { 116 if (!os_socket_same_addr->shared || !shared) {
121 OSError os_error(-1, 117 OSError os_error(-1,
122 "The shared flag to bind() needs to be `true` if " 118 "The shared flag to bind() needs to be `true` if "
123 "binding multiple times on the same (address, port) " 119 "binding multiple times on the same (address, port) "
124 "combination.", 120 "combination.",
125 OSError::kUnknown); 121 OSError::kUnknown);
126 return DartUtils::NewDartOSError(&os_error); 122 return DartUtils::NewDartOSError(&os_error);
127 } 123 }
128 if (os_socket_same_addr->v6_only != v6_only) { 124 if (os_socket_same_addr->v6_only != v6_only) {
129 OSError os_error(-1, 125 OSError os_error(-1,
130 "The v6Only flag to bind() needs to be the same if " 126 "The v6Only flag to bind() needs to be the same if "
131 "binding multiple times on the same (address, port) " 127 "binding multiple times on the same (address, port) "
132 "combination.", 128 "combination.",
133 OSError::kUnknown); 129 OSError::kUnknown);
134 return DartUtils::NewDartOSError(&os_error); 130 return DartUtils::NewDartOSError(&os_error);
135 } 131 }
136 132
137 // This socket creation is the exact same as the one which originally 133 // This socket creation is the exact same as the one which originally
138 // created the socket. We therefore increment the refcount and reuse 134 // created the socket. We therefore increment the refcount and reuse
139 // the file descriptor. 135 // the file descriptor.
140 os_socket->ref_count++; 136 os_socket->ref_count++;
141 137
142 // We set as a side-effect the file descriptor on the dart socket_object. 138 // We set as a side-effect the file descriptor on the dart socket_object.
143 Socket::SetSocketIdNativeField(socket_object, os_socket->socketfd); 139 Socket::SetSocketIdNativeField(socket_object, os_socket->socketfd);
(...skipping 12 matching lines...) Expand all
156 OSError error; 152 OSError error;
157 return DartUtils::NewDartOSError(&error); 153 return DartUtils::NewDartOSError(&error);
158 } 154 }
159 if (!ServerSocket::StartAccept(socketfd)) { 155 if (!ServerSocket::StartAccept(socketfd)) {
160 OSError os_error(-1, "Failed to start accept", OSError::kUnknown); 156 OSError os_error(-1, "Failed to start accept", OSError::kUnknown);
161 return DartUtils::NewDartOSError(&os_error); 157 return DartUtils::NewDartOSError(&os_error);
162 } 158 }
163 intptr_t allocated_port = Socket::GetPort(socketfd); 159 intptr_t allocated_port = Socket::GetPort(socketfd);
164 ASSERT(allocated_port > 0); 160 ASSERT(allocated_port > 0);
165 161
166 OSSocket *os_socket = 162 OSSocket* os_socket =
167 new OSSocket(addr, allocated_port, v6_only, shared, socketfd); 163 new OSSocket(addr, allocated_port, v6_only, shared, socketfd);
168 os_socket->ref_count = 1; 164 os_socket->ref_count = 1;
169 os_socket->next = first_os_socket; 165 os_socket->next = first_os_socket;
170 166
171 InsertByPort(allocated_port, os_socket); 167 InsertByPort(allocated_port, os_socket);
172 InsertByFd(socketfd, os_socket); 168 InsertByFd(socketfd, os_socket);
173 169
174 // We set as a side-effect the port on the dart socket_object. 170 // We set as a side-effect the port on the dart socket_object.
175 Socket::SetSocketIdNativeField(socket_object, socketfd); 171 Socket::SetSocketIdNativeField(socket_object, socketfd);
176 172
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 } 205 }
210 206
211 ASSERT(os_socket->ref_count == 0); 207 ASSERT(os_socket->ref_count == 0);
212 delete os_socket; 208 delete os_socket;
213 return true; 209 return true;
214 } 210 }
215 211
216 212
217 void ListeningSocketRegistry::CloseAllSafe() { 213 void ListeningSocketRegistry::CloseAllSafe() {
218 MutexLocker ml(mutex_); 214 MutexLocker ml(mutex_);
219 for (HashMap::Entry* p = sockets_by_fd_.Start(); 215 for (HashMap::Entry* p = sockets_by_fd_.Start(); p != NULL;
220 p != NULL;
221 p = sockets_by_fd_.Next(p)) { 216 p = sockets_by_fd_.Next(p)) {
222 CloseOneSafe(reinterpret_cast<OSSocket*>(p->value)); 217 CloseOneSafe(reinterpret_cast<OSSocket*>(p->value));
223 } 218 }
224 } 219 }
225 220
226 221
227 bool ListeningSocketRegistry::CloseSafe(intptr_t socketfd) { 222 bool ListeningSocketRegistry::CloseSafe(intptr_t socketfd) {
228 ASSERT(!mutex_->TryLock()); 223 ASSERT(!mutex_->TryLock());
229 OSSocket* os_socket = LookupByFd(socketfd); 224 OSSocket* os_socket = LookupByFd(socketfd);
230 if (os_socket != NULL) { 225 if (os_socket != NULL) {
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after
433 dart_args[1] = Dart_NewStringFromCString(numeric_address); 428 dart_args[1] = Dart_NewStringFromCString(numeric_address);
434 if (Dart_IsError(dart_args[1])) { 429 if (Dart_IsError(dart_args[1])) {
435 Dart_PropagateError(dart_args[1]); 430 Dart_PropagateError(dart_args[1]);
436 } 431 }
437 dart_args[2] = SocketAddress::ToTypedData(addr); 432 dart_args[2] = SocketAddress::ToTypedData(addr);
438 dart_args[3] = Dart_NewInteger(port); 433 dart_args[3] = Dart_NewInteger(port);
439 if (Dart_IsError(dart_args[3])) { 434 if (Dart_IsError(dart_args[3])) {
440 Dart_PropagateError(dart_args[3]); 435 Dart_PropagateError(dart_args[3]);
441 } 436 }
442 // TODO(sgjesse): Cache the _makeDatagram function somewhere. 437 // TODO(sgjesse): Cache the _makeDatagram function somewhere.
443 Dart_Handle io_lib = 438 Dart_Handle io_lib = Dart_LookupLibrary(DartUtils::NewString("dart:io"));
444 Dart_LookupLibrary(DartUtils::NewString("dart:io"));
445 if (Dart_IsError(io_lib)) { 439 if (Dart_IsError(io_lib)) {
446 Dart_PropagateError(io_lib); 440 Dart_PropagateError(io_lib);
447 } 441 }
448 Dart_Handle result = 442 Dart_Handle result = Dart_Invoke(
449 Dart_Invoke(io_lib, 443 io_lib, DartUtils::NewString("_makeDatagram"), kNumArgs, dart_args);
450 DartUtils::NewString("_makeDatagram"),
451 kNumArgs,
452 dart_args);
453 Dart_SetReturnValue(args, result); 444 Dart_SetReturnValue(args, result);
454 } 445 }
455 446
456 447
457 void FUNCTION_NAME(Socket_WriteList)(Dart_NativeArguments args) { 448 void FUNCTION_NAME(Socket_WriteList)(Dart_NativeArguments args) {
458 intptr_t socket = 449 intptr_t socket =
459 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); 450 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0));
460 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); 451 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1);
461 ASSERT(Dart_IsList(buffer_obj)); 452 ASSERT(Dart_IsList(buffer_obj));
462 intptr_t offset = 453 intptr_t offset = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 2));
463 DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 2)); 454 intptr_t length = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 3));
464 intptr_t length =
465 DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 3));
466 bool short_write = false; 455 bool short_write = false;
467 if (short_socket_write) { 456 if (short_socket_write) {
468 if (length > 1) { 457 if (length > 1) {
469 short_write = true; 458 short_write = true;
470 } 459 }
471 length = (length + 1) / 2; 460 length = (length + 1) / 2;
472 } 461 }
473 Dart_TypedData_Type type; 462 Dart_TypedData_Type type;
474 uint8_t* buffer = NULL; 463 uint8_t* buffer = NULL;
475 intptr_t len; 464 intptr_t len;
(...skipping 20 matching lines...) Expand all
496 Dart_TypedDataReleaseData(buffer_obj); 485 Dart_TypedDataReleaseData(buffer_obj);
497 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); 486 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error));
498 } 487 }
499 } 488 }
500 489
501 490
502 void FUNCTION_NAME(Socket_SendTo)(Dart_NativeArguments args) { 491 void FUNCTION_NAME(Socket_SendTo)(Dart_NativeArguments args) {
503 intptr_t socket = 492 intptr_t socket =
504 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); 493 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0));
505 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); 494 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1);
506 intptr_t offset = 495 intptr_t offset = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 2));
507 DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 2)); 496 intptr_t length = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 3));
508 intptr_t length =
509 DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 3));
510 Dart_Handle address_obj = Dart_GetNativeArgument(args, 4); 497 Dart_Handle address_obj = Dart_GetNativeArgument(args, 4);
511 ASSERT(Dart_IsList(address_obj)); 498 ASSERT(Dart_IsList(address_obj));
512 RawAddr addr; 499 RawAddr addr;
513 SocketAddress::GetSockAddr(address_obj, &addr); 500 SocketAddress::GetSockAddr(address_obj, &addr);
514 int64_t port = DartUtils::GetInt64ValueCheckRange( 501 int64_t port = DartUtils::GetInt64ValueCheckRange(
515 Dart_GetNativeArgument(args, 5), 502 Dart_GetNativeArgument(args, 5), 0, 65535);
516 0,
517 65535);
518 SocketAddress::SetAddrPort(&addr, port); 503 SocketAddress::SetAddrPort(&addr, port);
519 Dart_TypedData_Type type; 504 Dart_TypedData_Type type;
520 uint8_t* buffer = NULL; 505 uint8_t* buffer = NULL;
521 intptr_t len; 506 intptr_t len;
522 Dart_Handle result = Dart_TypedDataAcquireData( 507 Dart_Handle result = Dart_TypedDataAcquireData(
523 buffer_obj, &type, reinterpret_cast<void**>(&buffer), &len); 508 buffer_obj, &type, reinterpret_cast<void**>(&buffer), &len);
524 if (Dart_IsError(result)) { 509 if (Dart_IsError(result)) {
525 Dart_PropagateError(result); 510 Dart_PropagateError(result);
526 } 511 }
527 ASSERT((offset + length) <= len); 512 ASSERT((offset + length) <= len);
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
597 intptr_t type = Socket::GetType(socket); 582 intptr_t type = Socket::GetType(socket);
598 if (type >= 0) { 583 if (type >= 0) {
599 Dart_SetReturnValue(args, Dart_NewInteger(type)); 584 Dart_SetReturnValue(args, Dart_NewInteger(type));
600 } else { 585 } else {
601 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); 586 Dart_SetReturnValue(args, DartUtils::NewDartOSError());
602 } 587 }
603 } 588 }
604 589
605 590
606 void FUNCTION_NAME(Socket_GetStdioHandle)(Dart_NativeArguments args) { 591 void FUNCTION_NAME(Socket_GetStdioHandle)(Dart_NativeArguments args) {
607 int64_t num = DartUtils::GetInt64ValueCheckRange( 592 int64_t num =
608 Dart_GetNativeArgument(args, 1), 0, 2); 593 DartUtils::GetInt64ValueCheckRange(Dart_GetNativeArgument(args, 1), 0, 2);
609 intptr_t socket = Socket::GetStdioHandle(num); 594 intptr_t socket = Socket::GetStdioHandle(num);
610 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket); 595 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket);
611 Dart_SetReturnValue(args, Dart_NewBoolean(socket >= 0)); 596 Dart_SetReturnValue(args, Dart_NewBoolean(socket >= 0));
612 } 597 }
613 598
614 599
615 void FUNCTION_NAME(Socket_GetSocketId)(Dart_NativeArguments args) { 600 void FUNCTION_NAME(Socket_GetSocketId)(Dart_NativeArguments args) {
616 intptr_t id = Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); 601 intptr_t id = Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0));
617 Dart_SetReturnValue(args, Dart_NewInteger(id)); 602 Dart_SetReturnValue(args, Dart_NewInteger(id));
618 } 603 }
619 604
620 605
621 void FUNCTION_NAME(Socket_SetSocketId)(Dart_NativeArguments args) { 606 void FUNCTION_NAME(Socket_SetSocketId)(Dart_NativeArguments args) {
622 intptr_t id = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 1)); 607 intptr_t id = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 1));
623 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), id); 608 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), id);
624 } 609 }
625 610
626 611
627 void FUNCTION_NAME(ServerSocket_CreateBindListen)(Dart_NativeArguments args) { 612 void FUNCTION_NAME(ServerSocket_CreateBindListen)(Dart_NativeArguments args) {
628 RawAddr addr; 613 RawAddr addr;
629 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); 614 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr);
630 int64_t port = DartUtils::GetInt64ValueCheckRange( 615 int64_t port = DartUtils::GetInt64ValueCheckRange(
631 Dart_GetNativeArgument(args, 2), 616 Dart_GetNativeArgument(args, 2), 0, 65535);
632 0,
633 65535);
634 SocketAddress::SetAddrPort(&addr, port); 617 SocketAddress::SetAddrPort(&addr, port);
635 int64_t backlog = DartUtils::GetInt64ValueCheckRange( 618 int64_t backlog = DartUtils::GetInt64ValueCheckRange(
636 Dart_GetNativeArgument(args, 3), 619 Dart_GetNativeArgument(args, 3), 0, 65535);
637 0,
638 65535);
639 bool v6_only = DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 4)); 620 bool v6_only = DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 4));
640 bool shared = DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 5)); 621 bool shared = DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 5));
641 622
642 Dart_Handle socket_object = Dart_GetNativeArgument(args, 0); 623 Dart_Handle socket_object = Dart_GetNativeArgument(args, 0);
643 Dart_Handle result = ListeningSocketRegistry::Instance()->CreateBindListen( 624 Dart_Handle result = ListeningSocketRegistry::Instance()->CreateBindListen(
644 socket_object, addr, backlog, v6_only, shared); 625 socket_object, addr, backlog, v6_only, shared);
645 Dart_SetReturnValue(args, result); 626 Dart_SetReturnValue(args, result);
646 } 627 }
647 628
648 629
649 void FUNCTION_NAME(ServerSocket_Accept)(Dart_NativeArguments args) { 630 void FUNCTION_NAME(ServerSocket_Accept)(Dart_NativeArguments args) {
650 intptr_t socket = 631 intptr_t socket =
651 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); 632 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0));
652 intptr_t new_socket = ServerSocket::Accept(socket); 633 intptr_t new_socket = ServerSocket::Accept(socket);
653 if (new_socket >= 0) { 634 if (new_socket >= 0) {
654 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 1), new_socket); 635 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 1), new_socket);
655 Dart_SetReturnValue(args, Dart_True()); 636 Dart_SetReturnValue(args, Dart_True());
656 } else if (new_socket == ServerSocket::kTemporaryFailure) { 637 } else if (new_socket == ServerSocket::kTemporaryFailure) {
657 Dart_SetReturnValue(args, Dart_False()); 638 Dart_SetReturnValue(args, Dart_False());
658 } else { 639 } else {
659 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); 640 Dart_SetReturnValue(args, DartUtils::NewDartOSError());
660 } 641 }
661 } 642 }
662 643
663 644
664 CObject* Socket::LookupRequest(const CObjectArray& request) { 645 CObject* Socket::LookupRequest(const CObjectArray& request) {
665 if ((request.Length() == 2) && 646 if ((request.Length() == 2) && request[0]->IsString() &&
666 request[0]->IsString() &&
667 request[1]->IsInt32()) { 647 request[1]->IsInt32()) {
668 CObjectString host(request[0]); 648 CObjectString host(request[0]);
669 CObjectInt32 type(request[1]); 649 CObjectInt32 type(request[1]);
670 CObject* result = NULL; 650 CObject* result = NULL;
671 OSError* os_error = NULL; 651 OSError* os_error = NULL;
672 AddressList<SocketAddress>* addresses = 652 AddressList<SocketAddress>* addresses =
673 Socket::LookupAddress(host.CString(), type.Value(), &os_error); 653 Socket::LookupAddress(host.CString(), type.Value(), &os_error);
674 if (addresses != NULL) { 654 if (addresses != NULL) {
675 CObjectArray* array = new CObjectArray( 655 CObjectArray* array =
676 CObject::NewArray(addresses->count() + 1)); 656 new CObjectArray(CObject::NewArray(addresses->count() + 1));
677 array->SetAt(0, new CObjectInt32(CObject::NewInt32(0))); 657 array->SetAt(0, new CObjectInt32(CObject::NewInt32(0)));
678 for (intptr_t i = 0; i < addresses->count(); i++) { 658 for (intptr_t i = 0; i < addresses->count(); i++) {
679 SocketAddress* addr = addresses->GetAt(i); 659 SocketAddress* addr = addresses->GetAt(i);
680 CObjectArray* entry = new CObjectArray(CObject::NewArray(3)); 660 CObjectArray* entry = new CObjectArray(CObject::NewArray(3));
681 661
682 CObjectInt32* type = new CObjectInt32( 662 CObjectInt32* type =
683 CObject::NewInt32(addr->GetType())); 663 new CObjectInt32(CObject::NewInt32(addr->GetType()));
684 entry->SetAt(0, type); 664 entry->SetAt(0, type);
685 665
686 CObjectString* as_string = new CObjectString(CObject::NewString( 666 CObjectString* as_string =
687 addr->as_string())); 667 new CObjectString(CObject::NewString(addr->as_string()));
688 entry->SetAt(1, as_string); 668 entry->SetAt(1, as_string);
689 669
690 RawAddr raw = addr->addr(); 670 RawAddr raw = addr->addr();
691 CObjectUint8Array* data = SocketAddress::ToCObject(raw); 671 CObjectUint8Array* data = SocketAddress::ToCObject(raw);
692 entry->SetAt(2, data); 672 entry->SetAt(2, data);
693 673
694 array->SetAt(i + 1, entry); 674 array->SetAt(i + 1, entry);
695 } 675 }
696 result = array; 676 result = array;
697 delete addresses; 677 delete addresses;
698 } else { 678 } else {
699 result = CObject::NewOSError(os_error); 679 result = CObject::NewOSError(os_error);
700 delete os_error; 680 delete os_error;
701 } 681 }
702 return result; 682 return result;
703 } 683 }
704 return CObject::IllegalArgumentError(); 684 return CObject::IllegalArgumentError();
705 } 685 }
706 686
707 687
708 CObject* Socket::ReverseLookupRequest(const CObjectArray& request) { 688 CObject* Socket::ReverseLookupRequest(const CObjectArray& request) {
709 if ((request.Length() == 1) && 689 if ((request.Length() == 1) && request[0]->IsTypedData()) {
710 request[0]->IsTypedData()) {
711 CObjectUint8Array addr_object(request[0]); 690 CObjectUint8Array addr_object(request[0]);
712 RawAddr addr; 691 RawAddr addr;
713 int len = addr_object.Length(); 692 int len = addr_object.Length();
714 memset(reinterpret_cast<void*>(&addr), 0, sizeof(RawAddr)); 693 memset(reinterpret_cast<void*>(&addr), 0, sizeof(RawAddr));
715 if (len == sizeof(in_addr)) { 694 if (len == sizeof(in_addr)) {
716 addr.in.sin_family = AF_INET; 695 addr.in.sin_family = AF_INET;
717 memmove(reinterpret_cast<void*>(&addr.in.sin_addr), 696 memmove(reinterpret_cast<void*>(&addr.in.sin_addr), addr_object.Buffer(),
718 addr_object.Buffer(),
719 len); 697 len);
720 } else { 698 } else {
721 ASSERT(len == sizeof(in6_addr)); 699 ASSERT(len == sizeof(in6_addr));
722 addr.in6.sin6_family = AF_INET6; 700 addr.in6.sin6_family = AF_INET6;
723 memmove(reinterpret_cast<void*>(&addr.in6.sin6_addr), 701 memmove(reinterpret_cast<void*>(&addr.in6.sin6_addr),
724 addr_object.Buffer(), 702 addr_object.Buffer(), len);
725 len);
726 } 703 }
727 704
728 OSError* os_error = NULL; 705 OSError* os_error = NULL;
729 const intptr_t kMaxHostLength = 1025; 706 const intptr_t kMaxHostLength = 1025;
730 char host[kMaxHostLength]; 707 char host[kMaxHostLength];
731 if (Socket::ReverseLookup(addr, host, kMaxHostLength, &os_error)) { 708 if (Socket::ReverseLookup(addr, host, kMaxHostLength, &os_error)) {
732 return new CObjectString(CObject::NewString(host)); 709 return new CObjectString(CObject::NewString(host));
733 } else { 710 } else {
734 CObject* result = CObject::NewOSError(os_error); 711 CObject* result = CObject::NewOSError(os_error);
735 delete os_error; 712 delete os_error;
736 return result; 713 return result;
737 } 714 }
738 } 715 }
739 return CObject::IllegalArgumentError(); 716 return CObject::IllegalArgumentError();
740 } 717 }
741 718
742 719
743 CObject* Socket::ListInterfacesRequest(const CObjectArray& request) { 720 CObject* Socket::ListInterfacesRequest(const CObjectArray& request) {
744 if ((request.Length() == 1) && 721 if ((request.Length() == 1) && request[0]->IsInt32()) {
745 request[0]->IsInt32()) {
746 CObjectInt32 type(request[0]); 722 CObjectInt32 type(request[0]);
747 CObject* result = NULL; 723 CObject* result = NULL;
748 OSError* os_error = NULL; 724 OSError* os_error = NULL;
749 AddressList<InterfaceSocketAddress>* addresses = Socket::ListInterfaces( 725 AddressList<InterfaceSocketAddress>* addresses =
750 type.Value(), &os_error); 726 Socket::ListInterfaces(type.Value(), &os_error);
751 if (addresses != NULL) { 727 if (addresses != NULL) {
752 CObjectArray* array = new CObjectArray( 728 CObjectArray* array =
753 CObject::NewArray(addresses->count() + 1)); 729 new CObjectArray(CObject::NewArray(addresses->count() + 1));
754 array->SetAt(0, new CObjectInt32(CObject::NewInt32(0))); 730 array->SetAt(0, new CObjectInt32(CObject::NewInt32(0)));
755 for (intptr_t i = 0; i < addresses->count(); i++) { 731 for (intptr_t i = 0; i < addresses->count(); i++) {
756 InterfaceSocketAddress* interface = addresses->GetAt(i); 732 InterfaceSocketAddress* interface = addresses->GetAt(i);
757 SocketAddress* addr = interface->socket_address(); 733 SocketAddress* addr = interface->socket_address();
758 CObjectArray* entry = new CObjectArray(CObject::NewArray(5)); 734 CObjectArray* entry = new CObjectArray(CObject::NewArray(5));
759 735
760 CObjectInt32* type = new CObjectInt32( 736 CObjectInt32* type =
761 CObject::NewInt32(addr->GetType())); 737 new CObjectInt32(CObject::NewInt32(addr->GetType()));
762 entry->SetAt(0, type); 738 entry->SetAt(0, type);
763 739
764 CObjectString* as_string = new CObjectString(CObject::NewString( 740 CObjectString* as_string =
765 addr->as_string())); 741 new CObjectString(CObject::NewString(addr->as_string()));
766 entry->SetAt(1, as_string); 742 entry->SetAt(1, as_string);
767 743
768 RawAddr raw = addr->addr(); 744 RawAddr raw = addr->addr();
769 CObjectUint8Array* data = SocketAddress::ToCObject(raw); 745 CObjectUint8Array* data = SocketAddress::ToCObject(raw);
770 entry->SetAt(2, data); 746 entry->SetAt(2, data);
771 747
772 CObjectString* interface_name = new CObjectString(CObject::NewString( 748 CObjectString* interface_name =
773 interface->interface_name())); 749 new CObjectString(CObject::NewString(interface->interface_name()));
774 entry->SetAt(3, interface_name); 750 entry->SetAt(3, interface_name);
775 751
776 CObjectInt64* interface_index = new CObjectInt64(CObject::NewInt64( 752 CObjectInt64* interface_index =
777 interface->interface_index())); 753 new CObjectInt64(CObject::NewInt64(interface->interface_index()));
778 entry->SetAt(4, interface_index); 754 entry->SetAt(4, interface_index);
779 755
780 array->SetAt(i + 1, entry); 756 array->SetAt(i + 1, entry);
781 } 757 }
782 result = array; 758 result = array;
783 delete addresses; 759 delete addresses;
784 } else { 760 } else {
785 result = CObject::NewOSError(os_error); 761 result = CObject::NewOSError(os_error);
786 delete os_error; 762 delete os_error;
787 } 763 }
788 return result; 764 return result;
789 } 765 }
790 return CObject::IllegalArgumentError(); 766 return CObject::IllegalArgumentError();
791 } 767 }
792 768
793 769
794 void FUNCTION_NAME(Socket_GetOption)(Dart_NativeArguments args) { 770 void FUNCTION_NAME(Socket_GetOption)(Dart_NativeArguments args) {
795 intptr_t socket = 771 intptr_t socket =
796 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); 772 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0));
797 int64_t option = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); 773 int64_t option = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1));
798 intptr_t protocol = 774 intptr_t protocol = static_cast<intptr_t>(
799 static_cast<intptr_t>( 775 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)));
800 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)));
801 bool ok = false; 776 bool ok = false;
802 switch (option) { 777 switch (option) {
803 case 0: { // TCP_NODELAY. 778 case 0: { // TCP_NODELAY.
804 bool enabled; 779 bool enabled;
805 ok = Socket::GetNoDelay(socket, &enabled); 780 ok = Socket::GetNoDelay(socket, &enabled);
806 if (ok) { 781 if (ok) {
807 Dart_SetReturnValue(args, enabled ? Dart_True() : Dart_False()); 782 Dart_SetReturnValue(args, enabled ? Dart_True() : Dart_False());
808 } 783 }
809 break; 784 break;
810 } 785 }
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
846 } 821 }
847 } 822 }
848 823
849 824
850 void FUNCTION_NAME(Socket_SetOption)(Dart_NativeArguments args) { 825 void FUNCTION_NAME(Socket_SetOption)(Dart_NativeArguments args) {
851 bool result = false; 826 bool result = false;
852 intptr_t socket = 827 intptr_t socket =
853 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); 828 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0));
854 int64_t option = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); 829 int64_t option = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1));
855 int64_t protocol = DartUtils::GetInt64ValueCheckRange( 830 int64_t protocol = DartUtils::GetInt64ValueCheckRange(
856 Dart_GetNativeArgument(args, 2), 831 Dart_GetNativeArgument(args, 2), SocketAddress::TYPE_IPV4,
857 SocketAddress::TYPE_IPV4,
858 SocketAddress::TYPE_IPV6); 832 SocketAddress::TYPE_IPV6);
859 switch (option) { 833 switch (option) {
860 case 0: // TCP_NODELAY. 834 case 0: // TCP_NODELAY.
861 result = Socket::SetNoDelay( 835 result = Socket::SetNoDelay(
862 socket, DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 3))); 836 socket, DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 3)));
863 break; 837 break;
864 case 1: // IP_MULTICAST_LOOP. 838 case 1: // IP_MULTICAST_LOOP.
865 result = Socket::SetMulticastLoop( 839 result = Socket::SetMulticastLoop(
866 socket, 840 socket, protocol,
867 protocol,
868 DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 3))); 841 DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 3)));
869 break; 842 break;
870 case 2: // IP_MULTICAST_TTL. 843 case 2: // IP_MULTICAST_TTL.
871 result = Socket::SetMulticastHops( 844 result = Socket::SetMulticastHops(
872 socket, 845 socket, protocol,
873 protocol,
874 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3))); 846 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)));
875 break; 847 break;
876 case 3: { // IP_MULTICAST_IF. 848 case 3: { // IP_MULTICAST_IF.
877 UNIMPLEMENTED(); 849 UNIMPLEMENTED();
878 break; 850 break;
879 } 851 }
880 case 4: // IP_BROADCAST. 852 case 4: // IP_BROADCAST.
881 result = Socket::SetBroadcast( 853 result = Socket::SetBroadcast(
882 socket, DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 3))); 854 socket, DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 3)));
883 break; 855 break;
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
947 if (Dart_IsError(err)) { 919 if (Dart_IsError(err)) {
948 Dart_PropagateError(err); 920 Dart_PropagateError(err);
949 } 921 }
950 return socket; 922 return socket;
951 } 923 }
952 924
953 } // namespace bin 925 } // namespace bin
954 } // namespace dart 926 } // namespace dart
955 927
956 #endif // !defined(DART_IO_DISABLED) 928 #endif // !defined(DART_IO_DISABLED)
OLDNEW
« no previous file with comments | « runtime/bin/socket.h ('k') | runtime/bin/socket_android.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698