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

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

Issue 879353003: Introduce optional 'bool shared' parameter to ServerSocket.bind() ... (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Addressed comments 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"
10 #include "bin/utils.h" 11 #include "bin/utils.h"
11 12
12 #include "platform/globals.h" 13 #include "platform/globals.h"
13 #include "platform/utils.h" 14 #include "platform/utils.h"
14 15
15 #include "include/dart_api.h" 16 #include "include/dart_api.h"
16 17
17 namespace dart { 18 namespace dart {
18 namespace bin { 19 namespace bin {
19 20
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
20 static const int kSocketIdNativeField = 0; 159 static const int kSocketIdNativeField = 0;
21 160
22 void FUNCTION_NAME(InternetAddress_Parse)(Dart_NativeArguments args) { 161 void FUNCTION_NAME(InternetAddress_Parse)(Dart_NativeArguments args) {
23 const char* address = 162 const char* address =
24 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); 163 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0));
25 ASSERT(address != NULL); 164 ASSERT(address != NULL);
26 RawAddr raw; 165 RawAddr raw;
27 memset(&raw, 0, sizeof(raw)); 166 memset(&raw, 0, sizeof(raw));
28 int type = strchr(address, ':') == NULL ? SocketAddress::TYPE_IPV4 167 int type = strchr(address, ':') == NULL ? SocketAddress::TYPE_IPV4
29 : SocketAddress::TYPE_IPV6; 168 : SocketAddress::TYPE_IPV6;
(...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after
369 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); 508 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr);
370 int64_t port = DartUtils::GetInt64ValueCheckRange( 509 int64_t port = DartUtils::GetInt64ValueCheckRange(
371 Dart_GetNativeArgument(args, 2), 510 Dart_GetNativeArgument(args, 2),
372 0, 511 0,
373 65535); 512 65535);
374 int64_t backlog = DartUtils::GetInt64ValueCheckRange( 513 int64_t backlog = DartUtils::GetInt64ValueCheckRange(
375 Dart_GetNativeArgument(args, 3), 514 Dart_GetNativeArgument(args, 3),
376 0, 515 0,
377 65535); 516 65535);
378 bool v6_only = DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 4)); 517 bool v6_only = DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 4));
379 intptr_t socket = ServerSocket::CreateBindListen( 518 bool shared = DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 5));
380 addr, port, backlog, v6_only); 519
381 OSError error; 520 Dart_Handle socket_object = Dart_GetNativeArgument(args, 0);
382 if (socket >= 0 && ServerSocket::StartAccept(socket)) { 521 Dart_Handle result = globalTcpListeningSocketRegistry.CreateBindListen(
383 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket); 522 socket_object, addr, port, backlog, v6_only, shared);
384 Dart_SetReturnValue(args, Dart_True()); 523 Dart_SetReturnValue(args, result);
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 }
393 } 524 }
394 525
395 526
396 void FUNCTION_NAME(ServerSocket_Accept)(Dart_NativeArguments args) { 527 void FUNCTION_NAME(ServerSocket_Accept)(Dart_NativeArguments args) {
397 intptr_t socket = 528 intptr_t socket =
398 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); 529 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0));
399 intptr_t new_socket = ServerSocket::Accept(socket); 530 intptr_t new_socket = ServerSocket::Accept(socket);
400 if (new_socket >= 0) { 531 if (new_socket >= 0) {
401 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 1), new_socket); 532 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 1), new_socket);
402 Dart_SetReturnValue(args, Dart_True()); 533 Dart_SetReturnValue(args, Dart_True());
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after
671 int interfaceIndex = 802 int interfaceIndex =
672 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); 803 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3));
673 if (Socket::LeaveMulticast(socket, &addr, &interface, interfaceIndex)) { 804 if (Socket::LeaveMulticast(socket, &addr, &interface, interfaceIndex)) {
674 Dart_SetReturnValue(args, Dart_Null()); 805 Dart_SetReturnValue(args, Dart_Null());
675 } else { 806 } else {
676 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); 807 Dart_SetReturnValue(args, DartUtils::NewDartOSError());
677 } 808 }
678 } 809 }
679 810
680 811
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
681 void Socket::SetSocketIdNativeField(Dart_Handle socket, intptr_t id) { 821 void Socket::SetSocketIdNativeField(Dart_Handle socket, intptr_t id) {
682 Dart_Handle err = 822 Dart_Handle err =
683 Dart_SetNativeInstanceField(socket, kSocketIdNativeField, id); 823 Dart_SetNativeInstanceField(socket, kSocketIdNativeField, id);
684 if (Dart_IsError(err)) Dart_PropagateError(err); 824 if (Dart_IsError(err)) Dart_PropagateError(err);
685 } 825 }
686 826
687 827
688 intptr_t Socket::GetSocketIdNativeField(Dart_Handle socket_obj) { 828 intptr_t Socket::GetSocketIdNativeField(Dart_Handle socket_obj) {
689 intptr_t socket = 0; 829 intptr_t socket = 0;
690 Dart_Handle err = 830 Dart_Handle err =
691 Dart_GetNativeInstanceField(socket_obj, kSocketIdNativeField, &socket); 831 Dart_GetNativeInstanceField(socket_obj, kSocketIdNativeField, &socket);
692 if (Dart_IsError(err)) Dart_PropagateError(err); 832 if (Dart_IsError(err)) Dart_PropagateError(err);
693 return socket; 833 return socket;
694 } 834 }
695 835
696 } // namespace bin 836 } // namespace bin
697 } // namespace dart 837 } // 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