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

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

Issue 896213002: Revert "Introduce optional 'bool shared' parameter to ServerSocket.bind() ..." (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 5 years, 10 months 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 | Annotate | Revision Log
« no previous file with comments | « dart/runtime/bin/socket.h ('k') | dart/runtime/bin/socket_patch.dart » ('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 #include "bin/io_buffer.h" 5 #include "bin/io_buffer.h"
6 #include "bin/isolate_data.h" 6 #include "bin/isolate_data.h"
7 #include "bin/dartutils.h" 7 #include "bin/dartutils.h"
8 #include "bin/socket.h" 8 #include "bin/socket.h"
9 #include "bin/thread.h" 9 #include "bin/thread.h"
10 #include "bin/lockers.h"
11 #include "bin/utils.h" 10 #include "bin/utils.h"
12 11
13 #include "platform/globals.h" 12 #include "platform/globals.h"
14 #include "platform/utils.h" 13 #include "platform/utils.h"
15 14
16 #include "include/dart_api.h" 15 #include "include/dart_api.h"
17 16
18 namespace dart { 17 namespace dart {
19 namespace bin { 18 namespace bin {
20 19
21 Dart_Handle ListeningSocketRegistry::CreateBindListen(Dart_Handle socket_object,
22 RawAddr addr,
23 intptr_t port,
24 intptr_t backlog,
25 bool v6_only,
26 bool shared) {
27 MutexLocker ml(ListeningSocketRegistry::mutex_);
28
29 SocketsIterator it = sockets_by_port_.find(port);
30 OSSocket *first_os_socket = NULL;
31 if (it != sockets_by_port_.end()) {
32 first_os_socket = it->second;
33 }
34
35 if (first_os_socket != NULL) {
36 // There is already a socket listening on this port. We need to ensure
37 // that if there is one also listening on the same address, it was created
38 // with `shared = true`, ...
39
40 OSSocket *os_socket = it->second;
41 OSSocket *os_socket_same_addr = findOSSocketWithAddress(os_socket, addr);
42
43 if (os_socket_same_addr != NULL) {
44 if (!os_socket_same_addr->shared || !shared) {
45 OSError os_error(-1,
46 "The shared flag to bind() needs to be `true` if "
47 "binding multiple times on the same (address, port) "
48 "combination.",
49 OSError::kUnknown);
50 return DartUtils::NewDartOSError(&os_error);
51 }
52 if (os_socket_same_addr->v6_only != v6_only) {
53 OSError os_error(-1,
54 "The v6Only flag to bind() needs to be the same if "
55 "binding multiple times on the same (address, port) "
56 "combination.",
57 OSError::kUnknown);
58 return DartUtils::NewDartOSError(&os_error);
59 }
60
61 // This socket creation is the exact same as the one which originally
62 // created the socket. We therefore increment the refcount and return
63 // the file descriptor.
64 os_socket->ref_count++;
65
66 // We set as a side-effect the file descriptor on the dart socket_object.
67 Socket::SetSocketIdNativeField(socket_object, os_socket->socketfd);
68
69 return Dart_True();
70 }
71 }
72
73 // There is no socket listening on that (address, port), so we create new one.
74 intptr_t socketfd = ServerSocket::CreateBindListen(
75 addr, port, backlog, v6_only);
76 if (socketfd == -5) {
77 OSError os_error(-1, "Invalid host", OSError::kUnknown);
78 return DartUtils::NewDartOSError(&os_error);
79 }
80 if (socketfd < 0) {
81 OSError error;
82 return DartUtils::NewDartOSError(&error);
83 }
84 if (!ServerSocket::StartAccept(socketfd)) {
85 OSError os_error(-1, "Failed to start accept", OSError::kUnknown);
86 return DartUtils::NewDartOSError(&os_error);
87 }
88 intptr_t allocated_port = Socket::GetPort(socketfd);
89 ASSERT(allocated_port > 0);
90
91 OSSocket *os_socket =
92 new OSSocket(addr, allocated_port, v6_only, shared, socketfd);
93 os_socket->ref_count = 1;
94 os_socket->next = first_os_socket;
95 sockets_by_port_[allocated_port] = os_socket;
96 sockets_by_fd_[socketfd] = os_socket;
97
98 // We set as a side-effect the port on the dart socket_object.
99 Socket::SetSocketIdNativeField(socket_object, socketfd);
100
101 return Dart_True();
102 }
103
104 bool ListeningSocketRegistry::CloseSafe(int socketfd) {
105 ASSERT(!mutex_->TryLock());
106
107 SocketsIterator it = sockets_by_fd_.find(socketfd);
108 if (it != sockets_by_fd_.end()) {
109 OSSocket *os_socket = it->second;
110
111 ASSERT(os_socket->ref_count > 0);
112 os_socket->ref_count--;
113 if (os_socket->ref_count == 0) {
114 // We free the OS socket by removing it from two datastructures.
115 sockets_by_fd_.erase(socketfd);
116
117 OSSocket *prev = NULL;
118 OSSocket *current = sockets_by_port_[os_socket->port];
119 while (current != os_socket) {
120 ASSERT(current != NULL);
121 prev = current;
122 current = current->next;
123 }
124
125 if (prev == NULL && current->next == NULL) {
126 sockets_by_port_.erase(os_socket->port);
127 } else if (prev == NULL) {
128 sockets_by_port_[os_socket->port] = current->next;
129 } else {
130 prev->next = os_socket->next;
131 }
132
133 delete os_socket;
134 return true;
135 }
136 return false;
137 } else {
138 // It should be impossible for the event handler to close something that
139 // hasn't been created before.
140 UNREACHABLE();
141 return false;
142 }
143 }
144
145 Dart_Handle ListeningSocketRegistry::MarkSocketFdAsSharableHack(int socketfd) {
146 MutexLocker ml(ListeningSocketRegistry::mutex_);
147
148 SocketsIterator it = sockets_by_fd_.find(socketfd);
149 if (it != sockets_by_fd_.end()) {
150 it->second->shared = true;
151 return Dart_True();
152 } else {
153 return Dart_False();
154 }
155 }
156
157 ListeningSocketRegistry globalTcpListeningSocketRegistry;
158
159 static const int kSocketIdNativeField = 0; 20 static const int kSocketIdNativeField = 0;
160 21
161 void FUNCTION_NAME(InternetAddress_Parse)(Dart_NativeArguments args) { 22 void FUNCTION_NAME(InternetAddress_Parse)(Dart_NativeArguments args) {
162 const char* address = 23 const char* address =
163 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); 24 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0));
164 ASSERT(address != NULL); 25 ASSERT(address != NULL);
165 RawAddr raw; 26 RawAddr raw;
166 memset(&raw, 0, sizeof(raw)); 27 memset(&raw, 0, sizeof(raw));
167 int type = strchr(address, ':') == NULL ? SocketAddress::TYPE_IPV4 28 int type = strchr(address, ':') == NULL ? SocketAddress::TYPE_IPV4
168 : SocketAddress::TYPE_IPV6; 29 : SocketAddress::TYPE_IPV6;
(...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after
508 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); 369 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr);
509 int64_t port = DartUtils::GetInt64ValueCheckRange( 370 int64_t port = DartUtils::GetInt64ValueCheckRange(
510 Dart_GetNativeArgument(args, 2), 371 Dart_GetNativeArgument(args, 2),
511 0, 372 0,
512 65535); 373 65535);
513 int64_t backlog = DartUtils::GetInt64ValueCheckRange( 374 int64_t backlog = DartUtils::GetInt64ValueCheckRange(
514 Dart_GetNativeArgument(args, 3), 375 Dart_GetNativeArgument(args, 3),
515 0, 376 0,
516 65535); 377 65535);
517 bool v6_only = DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 4)); 378 bool v6_only = DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 4));
518 bool shared = DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 5)); 379 intptr_t socket = ServerSocket::CreateBindListen(
519 380 addr, port, backlog, v6_only);
520 Dart_Handle socket_object = Dart_GetNativeArgument(args, 0); 381 OSError error;
521 Dart_Handle result = globalTcpListeningSocketRegistry.CreateBindListen( 382 if (socket >= 0 && ServerSocket::StartAccept(socket)) {
522 socket_object, addr, port, backlog, v6_only, shared); 383 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket);
523 Dart_SetReturnValue(args, result); 384 Dart_SetReturnValue(args, Dart_True());
385 } else {
386 if (socket == -5) {
387 OSError os_error(-1, "Invalid host", OSError::kUnknown);
388 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error));
389 } else {
390 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&error));
391 }
392 }
524 } 393 }
525 394
526 395
527 void FUNCTION_NAME(ServerSocket_Accept)(Dart_NativeArguments args) { 396 void FUNCTION_NAME(ServerSocket_Accept)(Dart_NativeArguments args) {
528 intptr_t socket = 397 intptr_t socket =
529 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); 398 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0));
530 intptr_t new_socket = ServerSocket::Accept(socket); 399 intptr_t new_socket = ServerSocket::Accept(socket);
531 if (new_socket >= 0) { 400 if (new_socket >= 0) {
532 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 1), new_socket); 401 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 1), new_socket);
533 Dart_SetReturnValue(args, Dart_True()); 402 Dart_SetReturnValue(args, Dart_True());
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after
802 int interfaceIndex = 671 int interfaceIndex =
803 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); 672 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3));
804 if (Socket::LeaveMulticast(socket, &addr, &interface, interfaceIndex)) { 673 if (Socket::LeaveMulticast(socket, &addr, &interface, interfaceIndex)) {
805 Dart_SetReturnValue(args, Dart_Null()); 674 Dart_SetReturnValue(args, Dart_Null());
806 } else { 675 } else {
807 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); 676 Dart_SetReturnValue(args, DartUtils::NewDartOSError());
808 } 677 }
809 } 678 }
810 679
811 680
812 void FUNCTION_NAME(Socket_MarkSocketAsSharedHack)(Dart_NativeArguments args) {
813 intptr_t socketfd =
814 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0));
815
816 Dart_SetReturnValue(args,
817 globalTcpListeningSocketRegistry.MarkSocketFdAsSharableHack(socketfd));
818 }
819
820
821 void Socket::SetSocketIdNativeField(Dart_Handle socket, intptr_t id) { 681 void Socket::SetSocketIdNativeField(Dart_Handle socket, intptr_t id) {
822 Dart_Handle err = 682 Dart_Handle err =
823 Dart_SetNativeInstanceField(socket, kSocketIdNativeField, id); 683 Dart_SetNativeInstanceField(socket, kSocketIdNativeField, id);
824 if (Dart_IsError(err)) Dart_PropagateError(err); 684 if (Dart_IsError(err)) Dart_PropagateError(err);
825 } 685 }
826 686
827 687
828 intptr_t Socket::GetSocketIdNativeField(Dart_Handle socket_obj) { 688 intptr_t Socket::GetSocketIdNativeField(Dart_Handle socket_obj) {
829 intptr_t socket = 0; 689 intptr_t socket = 0;
830 Dart_Handle err = 690 Dart_Handle err =
831 Dart_GetNativeInstanceField(socket_obj, kSocketIdNativeField, &socket); 691 Dart_GetNativeInstanceField(socket_obj, kSocketIdNativeField, &socket);
832 if (Dart_IsError(err)) Dart_PropagateError(err); 692 if (Dart_IsError(err)) Dart_PropagateError(err);
833 return socket; 693 return socket;
834 } 694 }
835 695
836 } // namespace bin 696 } // namespace bin
837 } // namespace dart 697 } // namespace dart
OLDNEW
« no previous file with comments | « dart/runtime/bin/socket.h ('k') | dart/runtime/bin/socket_patch.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698