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 #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/eventhandler.h" |
10 #include "bin/io_buffer.h" | 11 #include "bin/io_buffer.h" |
11 #include "bin/isolate_data.h" | 12 #include "bin/isolate_data.h" |
12 #include "bin/lockers.h" | 13 #include "bin/lockers.h" |
13 #include "bin/thread.h" | 14 #include "bin/thread.h" |
14 #include "bin/utils.h" | 15 #include "bin/utils.h" |
15 | 16 |
16 #include "include/dart_api.h" | 17 #include "include/dart_api.h" |
17 | 18 |
18 #include "platform/globals.h" | 19 #include "platform/globals.h" |
19 #include "platform/utils.h" | 20 #include "platform/utils.h" |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 } | 66 } |
66 | 67 |
67 | 68 |
68 void ListeningSocketRegistry::RemoveByPort(intptr_t port) { | 69 void ListeningSocketRegistry::RemoveByPort(intptr_t port) { |
69 sockets_by_port_.Remove(GetHashmapKeyFromIntptr(port), | 70 sockets_by_port_.Remove(GetHashmapKeyFromIntptr(port), |
70 GetHashmapHashFromIntptr(port)); | 71 GetHashmapHashFromIntptr(port)); |
71 } | 72 } |
72 | 73 |
73 | 74 |
74 ListeningSocketRegistry::OSSocket* ListeningSocketRegistry::LookupByFd( | 75 ListeningSocketRegistry::OSSocket* ListeningSocketRegistry::LookupByFd( |
75 intptr_t fd) { | 76 Socket* fd) { |
76 HashMap::Entry* entry = sockets_by_fd_.Lookup( | 77 HashMap::Entry* entry = sockets_by_fd_.Lookup( |
77 GetHashmapKeyFromIntptr(fd), GetHashmapHashFromIntptr(fd), false); | 78 GetHashmapKeyFromIntptr(reinterpret_cast<intptr_t>(fd)), |
| 79 GetHashmapHashFromIntptr(reinterpret_cast<intptr_t>(fd)), false); |
78 if (entry == NULL) { | 80 if (entry == NULL) { |
79 return NULL; | 81 return NULL; |
80 } | 82 } |
81 return reinterpret_cast<OSSocket*>(entry->value); | 83 return reinterpret_cast<OSSocket*>(entry->value); |
82 } | 84 } |
83 | 85 |
84 | 86 |
85 void ListeningSocketRegistry::InsertByFd(intptr_t fd, OSSocket* socket) { | 87 void ListeningSocketRegistry::InsertByFd(Socket* fd, OSSocket* socket) { |
86 HashMap::Entry* entry = sockets_by_fd_.Lookup( | 88 HashMap::Entry* entry = sockets_by_fd_.Lookup( |
87 GetHashmapKeyFromIntptr(fd), GetHashmapHashFromIntptr(fd), true); | 89 GetHashmapKeyFromIntptr(reinterpret_cast<intptr_t>(fd)), |
| 90 GetHashmapHashFromIntptr(reinterpret_cast<intptr_t>(fd)), true); |
88 ASSERT(entry != NULL); | 91 ASSERT(entry != NULL); |
89 entry->value = reinterpret_cast<void*>(socket); | 92 entry->value = reinterpret_cast<void*>(socket); |
90 } | 93 } |
91 | 94 |
92 | 95 |
93 void ListeningSocketRegistry::RemoveByFd(intptr_t fd) { | 96 void ListeningSocketRegistry::RemoveByFd(Socket* fd) { |
94 sockets_by_fd_.Remove(GetHashmapKeyFromIntptr(fd), | 97 sockets_by_fd_.Remove( |
95 GetHashmapHashFromIntptr(fd)); | 98 GetHashmapKeyFromIntptr(reinterpret_cast<intptr_t>(fd)), |
| 99 GetHashmapHashFromIntptr(reinterpret_cast<intptr_t>(fd))); |
96 } | 100 } |
97 | 101 |
98 | 102 |
99 Dart_Handle ListeningSocketRegistry::CreateBindListen(Dart_Handle socket_object, | 103 Dart_Handle ListeningSocketRegistry::CreateBindListen(Dart_Handle socket_object, |
100 RawAddr addr, | 104 RawAddr addr, |
101 intptr_t backlog, | 105 intptr_t backlog, |
102 bool v6_only, | 106 bool v6_only, |
103 bool shared) { | 107 bool shared) { |
104 MutexLocker ml(mutex_); | 108 MutexLocker ml(mutex_); |
105 | 109 |
106 OSSocket* first_os_socket = NULL; | 110 OSSocket* first_os_socket = NULL; |
107 intptr_t port = SocketAddress::GetAddrPort(addr); | 111 intptr_t port = SocketAddress::GetAddrPort(addr); |
108 if (port > 0) { | 112 if (port > 0) { |
109 first_os_socket = LookupByPort(port); | 113 first_os_socket = LookupByPort(port); |
110 if (first_os_socket != NULL) { | 114 if (first_os_socket != NULL) { |
111 // There is already a socket listening on this port. We need to ensure | 115 // There is already a socket listening on this port. We need to ensure |
112 // that if there is one also listening on the same address, it was created | 116 // that if there is one also listening on the same address, it was created |
113 // with `shared = true`, ... | 117 // with `shared = true`, ... |
114 OSSocket* os_socket = first_os_socket; | 118 OSSocket* os_socket = first_os_socket; |
115 OSSocket* os_socket_same_addr = findOSSocketWithAddress(os_socket, addr); | 119 OSSocket* os_socket_same_addr = FindOSSocketWithAddress(os_socket, addr); |
116 | 120 |
117 if (os_socket_same_addr != NULL) { | 121 if (os_socket_same_addr != NULL) { |
118 if (!os_socket_same_addr->shared || !shared) { | 122 if (!os_socket_same_addr->shared || !shared) { |
119 OSError os_error(-1, | 123 OSError os_error(-1, |
120 "The shared flag to bind() needs to be `true` if " | 124 "The shared flag to bind() needs to be `true` if " |
121 "binding multiple times on the same (address, port) " | 125 "binding multiple times on the same (address, port) " |
122 "combination.", | 126 "combination.", |
123 OSError::kUnknown); | 127 OSError::kUnknown); |
124 return DartUtils::NewDartOSError(&os_error); | 128 return DartUtils::NewDartOSError(&os_error); |
125 } | 129 } |
126 if (os_socket_same_addr->v6_only != v6_only) { | 130 if (os_socket_same_addr->v6_only != v6_only) { |
127 OSError os_error(-1, | 131 OSError os_error(-1, |
128 "The v6Only flag to bind() needs to be the same if " | 132 "The v6Only flag to bind() needs to be the same if " |
129 "binding multiple times on the same (address, port) " | 133 "binding multiple times on the same (address, port) " |
130 "combination.", | 134 "combination.", |
131 OSError::kUnknown); | 135 OSError::kUnknown); |
132 return DartUtils::NewDartOSError(&os_error); | 136 return DartUtils::NewDartOSError(&os_error); |
133 } | 137 } |
134 | 138 |
135 // This socket creation is the exact same as the one which originally | 139 // This socket creation is the exact same as the one which originally |
136 // created the socket. We therefore increment the refcount and reuse | 140 // created the socket. We therefore increment the refcount and reuse |
137 // the file descriptor. | 141 // the file descriptor. |
138 os_socket->ref_count++; | 142 os_socket->ref_count++; |
139 | 143 |
| 144 // The same Socket is used by a second Dart _NativeSocket object. |
| 145 // It Retains a reference. |
| 146 os_socket->socketfd->Retain(); |
140 // We set as a side-effect the file descriptor on the dart | 147 // We set as a side-effect the file descriptor on the dart |
141 // socket_object. | 148 // socket_object. |
142 Socket::SetSocketIdNativeField(socket_object, os_socket->socketfd); | 149 Socket::ReuseSocketIdNativeField(socket_object, os_socket->socketfd, |
| 150 true); |
143 | 151 |
144 return Dart_True(); | 152 return Dart_True(); |
145 } | 153 } |
146 } | 154 } |
147 } | 155 } |
148 | 156 |
149 // There is no socket listening on that (address, port), so we create new one. | 157 // There is no socket listening on that (address, port), so we create new one. |
150 intptr_t socketfd = ServerSocket::CreateBindListen(addr, backlog, v6_only); | 158 intptr_t fd = ServerSocket::CreateBindListen(addr, backlog, v6_only); |
151 if (socketfd == -5) { | 159 if (fd == -5) { |
152 OSError os_error(-1, "Invalid host", OSError::kUnknown); | 160 OSError os_error(-1, "Invalid host", OSError::kUnknown); |
153 return DartUtils::NewDartOSError(&os_error); | 161 return DartUtils::NewDartOSError(&os_error); |
154 } | 162 } |
155 if (socketfd < 0) { | 163 if (fd < 0) { |
156 OSError error; | 164 OSError error; |
157 return DartUtils::NewDartOSError(&error); | 165 return DartUtils::NewDartOSError(&error); |
158 } | 166 } |
159 if (!ServerSocket::StartAccept(socketfd)) { | 167 if (!ServerSocket::StartAccept(fd)) { |
160 OSError os_error(-1, "Failed to start accept", OSError::kUnknown); | 168 OSError os_error(-1, "Failed to start accept", OSError::kUnknown); |
161 return DartUtils::NewDartOSError(&os_error); | 169 return DartUtils::NewDartOSError(&os_error); |
162 } | 170 } |
163 intptr_t allocated_port = Socket::GetPort(socketfd); | 171 intptr_t allocated_port = Socket::GetPort(fd); |
164 ASSERT(allocated_port > 0); | 172 ASSERT(allocated_port > 0); |
165 | 173 |
166 if (allocated_port != port) { | 174 if (allocated_port != port) { |
167 // There are two cases to consider: | 175 // There are two cases to consider: |
168 // | 176 // |
169 // a) The user requested (address, port) where port != 0 which means | 177 // a) The user requested (address, port) where port != 0 which means |
170 // we re-use an existing socket if available (and it is shared) or we | 178 // we re-use an existing socket if available (and it is shared) or we |
171 // create a new one. The new socket is guaranteed to have that | 179 // create a new one. The new socket is guaranteed to have that |
172 // selected port. | 180 // selected port. |
173 // | 181 // |
174 // b) The user requested (address, 0). This will make us *always* create a | 182 // b) The user requested (address, 0). This will make us *always* create a |
175 // new socket. The OS will assign it a new `allocated_port` and we will | 183 // new socket. The OS will assign it a new `allocated_port` and we will |
176 // insert into our data structures. *BUT* There might already be an | 184 // insert into our data structures. *BUT* There might already be an |
177 // existing (address2, `allocated_port`) where address != address2. So | 185 // existing (address2, `allocated_port`) where address != address2. So |
178 // we need to do another `LookupByPort(allocated_port)` and link them | 186 // we need to do another `LookupByPort(allocated_port)` and link them |
179 // via `OSSocket->next`. | 187 // via `OSSocket->next`. |
180 ASSERT(port == 0); | 188 ASSERT(port == 0); |
181 first_os_socket = LookupByPort(allocated_port); | 189 first_os_socket = LookupByPort(allocated_port); |
182 } | 190 } |
183 | 191 |
| 192 Socket* socketfd = new Socket(fd); |
184 OSSocket* os_socket = | 193 OSSocket* os_socket = |
185 new OSSocket(addr, allocated_port, v6_only, shared, socketfd); | 194 new OSSocket(addr, allocated_port, v6_only, shared, socketfd); |
186 os_socket->ref_count = 1; | 195 os_socket->ref_count = 1; |
187 os_socket->next = first_os_socket; | 196 os_socket->next = first_os_socket; |
188 | 197 |
189 InsertByPort(allocated_port, os_socket); | 198 InsertByPort(allocated_port, os_socket); |
190 InsertByFd(socketfd, os_socket); | 199 InsertByFd(socketfd, os_socket); |
191 | 200 |
192 // We set as a side-effect the port on the dart socket_object. | 201 // We set as a side-effect the port on the dart socket_object. |
193 Socket::SetSocketIdNativeField(socket_object, socketfd); | 202 Socket::ReuseSocketIdNativeField(socket_object, socketfd, true); |
194 | 203 |
195 return Dart_True(); | 204 return Dart_True(); |
196 } | 205 } |
197 | 206 |
198 | 207 |
199 bool ListeningSocketRegistry::CloseOneSafe(OSSocket* os_socket, | 208 bool ListeningSocketRegistry::CloseOneSafe(OSSocket* os_socket, |
200 bool update_hash_maps) { | 209 bool update_hash_maps) { |
201 ASSERT(!mutex_->TryLock()); | 210 ASSERT(!mutex_->TryLock()); |
202 ASSERT(os_socket != NULL); | 211 ASSERT(os_socket != NULL); |
203 ASSERT(os_socket->ref_count > 0); | 212 ASSERT(os_socket->ref_count > 0); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 void ListeningSocketRegistry::CloseAllSafe() { | 247 void ListeningSocketRegistry::CloseAllSafe() { |
239 MutexLocker ml(mutex_); | 248 MutexLocker ml(mutex_); |
240 | 249 |
241 for (HashMap::Entry* cursor = sockets_by_fd_.Start(); cursor != NULL; | 250 for (HashMap::Entry* cursor = sockets_by_fd_.Start(); cursor != NULL; |
242 cursor = sockets_by_fd_.Next(cursor)) { | 251 cursor = sockets_by_fd_.Next(cursor)) { |
243 CloseOneSafe(reinterpret_cast<OSSocket*>(cursor->value), false); | 252 CloseOneSafe(reinterpret_cast<OSSocket*>(cursor->value), false); |
244 } | 253 } |
245 } | 254 } |
246 | 255 |
247 | 256 |
248 bool ListeningSocketRegistry::CloseSafe(intptr_t socketfd) { | 257 bool ListeningSocketRegistry::CloseSafe(Socket* socketfd) { |
249 ASSERT(!mutex_->TryLock()); | 258 ASSERT(!mutex_->TryLock()); |
250 OSSocket* os_socket = LookupByFd(socketfd); | 259 OSSocket* os_socket = LookupByFd(socketfd); |
251 if (os_socket != NULL) { | 260 if (os_socket != NULL) { |
252 return CloseOneSafe(os_socket, true); | 261 return CloseOneSafe(os_socket, true); |
253 } else { | 262 } else { |
254 // It should be impossible for the event handler to close something that | 263 // A finalizer may direct the event handler to close a listening socket |
255 // hasn't been created before. | 264 // that it has never seen before. In this case, we return true to direct |
256 UNREACHABLE(); | 265 // the eventhandler to clean up the socket. |
257 return false; | 266 return true; |
258 } | 267 } |
259 } | 268 } |
260 | 269 |
261 | 270 |
262 void FUNCTION_NAME(InternetAddress_Parse)(Dart_NativeArguments args) { | 271 void FUNCTION_NAME(InternetAddress_Parse)(Dart_NativeArguments args) { |
263 const char* address = | 272 const char* address = |
264 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | 273 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
265 ASSERT(address != NULL); | 274 ASSERT(address != NULL); |
266 RawAddr raw; | 275 RawAddr raw; |
267 memset(&raw, 0, sizeof(raw)); | 276 memset(&raw, 0, sizeof(raw)); |
(...skipping 20 matching lines...) Expand all Loading... |
288 | 297 |
289 void FUNCTION_NAME(Socket_CreateConnect)(Dart_NativeArguments args) { | 298 void FUNCTION_NAME(Socket_CreateConnect)(Dart_NativeArguments args) { |
290 RawAddr addr; | 299 RawAddr addr; |
291 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); | 300 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); |
292 Dart_Handle port_arg = Dart_GetNativeArgument(args, 2); | 301 Dart_Handle port_arg = Dart_GetNativeArgument(args, 2); |
293 int64_t port = DartUtils::GetInt64ValueCheckRange(port_arg, 0, 65535); | 302 int64_t port = DartUtils::GetInt64ValueCheckRange(port_arg, 0, 65535); |
294 SocketAddress::SetAddrPort(&addr, static_cast<intptr_t>(port)); | 303 SocketAddress::SetAddrPort(&addr, static_cast<intptr_t>(port)); |
295 intptr_t socket = Socket::CreateConnect(addr); | 304 intptr_t socket = Socket::CreateConnect(addr); |
296 OSError error; | 305 OSError error; |
297 if (socket >= 0) { | 306 if (socket >= 0) { |
298 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket); | 307 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket, |
| 308 false); |
299 Dart_SetReturnValue(args, Dart_True()); | 309 Dart_SetReturnValue(args, Dart_True()); |
300 } else { | 310 } else { |
301 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&error)); | 311 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&error)); |
302 } | 312 } |
303 } | 313 } |
304 | 314 |
305 | 315 |
306 void FUNCTION_NAME(Socket_CreateBindConnect)(Dart_NativeArguments args) { | 316 void FUNCTION_NAME(Socket_CreateBindConnect)(Dart_NativeArguments args) { |
307 RawAddr addr; | 317 RawAddr addr; |
308 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); | 318 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); |
309 Dart_Handle port_arg = Dart_GetNativeArgument(args, 2); | 319 Dart_Handle port_arg = Dart_GetNativeArgument(args, 2); |
310 int64_t port = DartUtils::GetInt64ValueCheckRange(port_arg, 0, 65535); | 320 int64_t port = DartUtils::GetInt64ValueCheckRange(port_arg, 0, 65535); |
311 SocketAddress::SetAddrPort(&addr, static_cast<intptr_t>(port)); | 321 SocketAddress::SetAddrPort(&addr, static_cast<intptr_t>(port)); |
312 RawAddr sourceAddr; | 322 RawAddr sourceAddr; |
313 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 3), &sourceAddr); | 323 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 3), &sourceAddr); |
314 intptr_t socket = Socket::CreateBindConnect(addr, sourceAddr); | 324 intptr_t socket = Socket::CreateBindConnect(addr, sourceAddr); |
315 OSError error; | 325 OSError error; |
316 if (socket >= 0) { | 326 if (socket >= 0) { |
317 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket); | 327 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket, |
| 328 false); |
318 Dart_SetReturnValue(args, Dart_True()); | 329 Dart_SetReturnValue(args, Dart_True()); |
319 } else { | 330 } else { |
320 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&error)); | 331 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&error)); |
321 } | 332 } |
322 } | 333 } |
323 | 334 |
324 void FUNCTION_NAME(Socket_IsBindError)(Dart_NativeArguments args) { | 335 void FUNCTION_NAME(Socket_IsBindError)(Dart_NativeArguments args) { |
325 intptr_t error_number = | 336 intptr_t error_number = |
326 DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 1)); | 337 DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 1)); |
327 bool is_bind_error = Socket::IsBindError(error_number); | 338 bool is_bind_error = Socket::IsBindError(error_number); |
328 Dart_SetReturnValue(args, is_bind_error ? Dart_True() : Dart_False()); | 339 Dart_SetReturnValue(args, is_bind_error ? Dart_True() : Dart_False()); |
329 } | 340 } |
330 | 341 |
331 void FUNCTION_NAME(Socket_CreateBindDatagram)(Dart_NativeArguments args) { | 342 void FUNCTION_NAME(Socket_CreateBindDatagram)(Dart_NativeArguments args) { |
332 RawAddr addr; | 343 RawAddr addr; |
333 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); | 344 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); |
334 Dart_Handle port_arg = Dart_GetNativeArgument(args, 2); | 345 Dart_Handle port_arg = Dart_GetNativeArgument(args, 2); |
335 int64_t port = DartUtils::GetInt64ValueCheckRange(port_arg, 0, 65535); | 346 int64_t port = DartUtils::GetInt64ValueCheckRange(port_arg, 0, 65535); |
336 SocketAddress::SetAddrPort(&addr, port); | 347 SocketAddress::SetAddrPort(&addr, port); |
337 bool reuse_addr = DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 3)); | 348 bool reuse_addr = DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 3)); |
338 intptr_t socket = Socket::CreateBindDatagram(addr, reuse_addr); | 349 intptr_t socket = Socket::CreateBindDatagram(addr, reuse_addr); |
339 if (socket >= 0) { | 350 if (socket >= 0) { |
340 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket); | 351 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket, |
| 352 false); |
341 Dart_SetReturnValue(args, Dart_True()); | 353 Dart_SetReturnValue(args, Dart_True()); |
342 } else { | 354 } else { |
343 OSError error; | 355 OSError error; |
344 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&error)); | 356 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&error)); |
345 } | 357 } |
346 } | 358 } |
347 | 359 |
348 | 360 |
349 void FUNCTION_NAME(Socket_Available)(Dart_NativeArguments args) { | 361 void FUNCTION_NAME(Socket_Available)(Dart_NativeArguments args) { |
350 intptr_t socket = | 362 Socket* socket = |
351 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 363 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
352 intptr_t available = Socket::Available(socket); | 364 intptr_t available = Socket::Available(socket->fd()); |
353 if (available >= 0) { | 365 if (available >= 0) { |
354 Dart_SetReturnValue(args, Dart_NewInteger(available)); | 366 Dart_SetReturnValue(args, Dart_NewInteger(available)); |
355 } else { | 367 } else { |
356 // Available failed. Mark socket as having data, to trigger a future read | 368 // Available failed. Mark socket as having data, to trigger a future read |
357 // event where the actual error can be reported. | 369 // event where the actual error can be reported. |
358 Dart_SetReturnValue(args, Dart_NewInteger(1)); | 370 Dart_SetReturnValue(args, Dart_NewInteger(1)); |
359 } | 371 } |
360 } | 372 } |
361 | 373 |
362 | 374 |
363 void FUNCTION_NAME(Socket_Read)(Dart_NativeArguments args) { | 375 void FUNCTION_NAME(Socket_Read)(Dart_NativeArguments args) { |
364 intptr_t socket = | 376 Socket* socket = |
365 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 377 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
366 int64_t length = 0; | 378 int64_t length = 0; |
367 if (DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 1), &length)) { | 379 if (DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 1), &length)) { |
368 if (short_socket_read) { | 380 if (short_socket_read) { |
369 length = (length + 1) / 2; | 381 length = (length + 1) / 2; |
370 } | 382 } |
371 uint8_t* buffer = NULL; | 383 uint8_t* buffer = NULL; |
372 Dart_Handle result = IOBuffer::Allocate(length, &buffer); | 384 Dart_Handle result = IOBuffer::Allocate(length, &buffer); |
373 if (Dart_IsError(result)) { | 385 if (Dart_IsError(result)) { |
374 Dart_PropagateError(result); | 386 Dart_PropagateError(result); |
375 } | 387 } |
376 ASSERT(buffer != NULL); | 388 ASSERT(buffer != NULL); |
377 intptr_t bytes_read = Socket::Read(socket, buffer, length); | 389 intptr_t bytes_read = Socket::Read(socket->fd(), buffer, length); |
378 if (bytes_read == length) { | 390 if (bytes_read == length) { |
379 Dart_SetReturnValue(args, result); | 391 Dart_SetReturnValue(args, result); |
380 } else if (bytes_read > 0) { | 392 } else if (bytes_read > 0) { |
381 uint8_t* new_buffer = NULL; | 393 uint8_t* new_buffer = NULL; |
382 Dart_Handle new_result = IOBuffer::Allocate(bytes_read, &new_buffer); | 394 Dart_Handle new_result = IOBuffer::Allocate(bytes_read, &new_buffer); |
383 if (Dart_IsError(new_result)) { | 395 if (Dart_IsError(new_result)) { |
384 Dart_PropagateError(new_result); | 396 Dart_PropagateError(new_result); |
385 } | 397 } |
386 ASSERT(new_buffer != NULL); | 398 ASSERT(new_buffer != NULL); |
387 memmove(new_buffer, buffer, bytes_read); | 399 memmove(new_buffer, buffer, bytes_read); |
388 Dart_SetReturnValue(args, new_result); | 400 Dart_SetReturnValue(args, new_result); |
389 } else if (bytes_read == 0) { | 401 } else if (bytes_read == 0) { |
390 // On MacOS when reading from a tty Ctrl-D will result in reading one | 402 // On MacOS when reading from a tty Ctrl-D will result in reading one |
391 // less byte then reported as available. | 403 // less byte then reported as available. |
392 Dart_SetReturnValue(args, Dart_Null()); | 404 Dart_SetReturnValue(args, Dart_Null()); |
393 } else { | 405 } else { |
394 ASSERT(bytes_read == -1); | 406 ASSERT(bytes_read == -1); |
395 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 407 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
396 } | 408 } |
397 } else { | 409 } else { |
398 OSError os_error(-1, "Invalid argument", OSError::kUnknown); | 410 OSError os_error(-1, "Invalid argument", OSError::kUnknown); |
399 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); | 411 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); |
400 } | 412 } |
401 } | 413 } |
402 | 414 |
403 | 415 |
404 void FUNCTION_NAME(Socket_RecvFrom)(Dart_NativeArguments args) { | 416 void FUNCTION_NAME(Socket_RecvFrom)(Dart_NativeArguments args) { |
405 intptr_t socket = | 417 Socket* socket = |
406 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 418 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
407 | 419 |
408 // TODO(sgjesse): Use a MTU value here. Only the loopback adapter can | 420 // TODO(sgjesse): Use a MTU value here. Only the loopback adapter can |
409 // handle 64k datagrams. | 421 // handle 64k datagrams. |
410 IsolateData* isolate_data = | 422 IsolateData* isolate_data = |
411 reinterpret_cast<IsolateData*>(Dart_CurrentIsolateData()); | 423 reinterpret_cast<IsolateData*>(Dart_CurrentIsolateData()); |
412 if (isolate_data->udp_receive_buffer == NULL) { | 424 if (isolate_data->udp_receive_buffer == NULL) { |
413 isolate_data->udp_receive_buffer = | 425 isolate_data->udp_receive_buffer = |
414 reinterpret_cast<uint8_t*>(malloc(65536)); | 426 reinterpret_cast<uint8_t*>(malloc(65536)); |
415 } | 427 } |
416 RawAddr addr; | 428 RawAddr addr; |
417 intptr_t bytes_read = | 429 intptr_t bytes_read = Socket::RecvFrom( |
418 Socket::RecvFrom(socket, isolate_data->udp_receive_buffer, 65536, &addr); | 430 socket->fd(), isolate_data->udp_receive_buffer, 65536, &addr); |
419 if (bytes_read == 0) { | 431 if (bytes_read == 0) { |
420 Dart_SetReturnValue(args, Dart_Null()); | 432 Dart_SetReturnValue(args, Dart_Null()); |
421 return; | 433 return; |
422 } | 434 } |
423 if (bytes_read < 0) { | 435 if (bytes_read < 0) { |
424 ASSERT(bytes_read == -1); | 436 ASSERT(bytes_read == -1); |
425 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 437 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
426 return; | 438 return; |
427 } | 439 } |
428 // Datagram data read. Copy into buffer of the exact size, | 440 // Datagram data read. Copy into buffer of the exact size, |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
465 if (Dart_IsError(io_lib)) { | 477 if (Dart_IsError(io_lib)) { |
466 Dart_PropagateError(io_lib); | 478 Dart_PropagateError(io_lib); |
467 } | 479 } |
468 Dart_Handle result = Dart_Invoke( | 480 Dart_Handle result = Dart_Invoke( |
469 io_lib, DartUtils::NewString("_makeDatagram"), kNumArgs, dart_args); | 481 io_lib, DartUtils::NewString("_makeDatagram"), kNumArgs, dart_args); |
470 Dart_SetReturnValue(args, result); | 482 Dart_SetReturnValue(args, result); |
471 } | 483 } |
472 | 484 |
473 | 485 |
474 void FUNCTION_NAME(Socket_WriteList)(Dart_NativeArguments args) { | 486 void FUNCTION_NAME(Socket_WriteList)(Dart_NativeArguments args) { |
475 intptr_t socket = | 487 Socket* socket = |
476 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 488 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
477 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); | 489 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); |
478 ASSERT(Dart_IsList(buffer_obj)); | 490 ASSERT(Dart_IsList(buffer_obj)); |
479 intptr_t offset = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 2)); | 491 intptr_t offset = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 2)); |
480 intptr_t length = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 3)); | 492 intptr_t length = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 3)); |
481 bool short_write = false; | 493 bool short_write = false; |
482 if (short_socket_write) { | 494 if (short_socket_write) { |
483 if (length > 1) { | 495 if (length > 1) { |
484 short_write = true; | 496 short_write = true; |
485 } | 497 } |
486 length = (length + 1) / 2; | 498 length = (length + 1) / 2; |
487 } | 499 } |
488 Dart_TypedData_Type type; | 500 Dart_TypedData_Type type; |
489 uint8_t* buffer = NULL; | 501 uint8_t* buffer = NULL; |
490 intptr_t len; | 502 intptr_t len; |
491 Dart_Handle result = Dart_TypedDataAcquireData( | 503 Dart_Handle result = Dart_TypedDataAcquireData( |
492 buffer_obj, &type, reinterpret_cast<void**>(&buffer), &len); | 504 buffer_obj, &type, reinterpret_cast<void**>(&buffer), &len); |
493 if (Dart_IsError(result)) { | 505 if (Dart_IsError(result)) { |
494 Dart_PropagateError(result); | 506 Dart_PropagateError(result); |
495 } | 507 } |
496 ASSERT((offset + length) <= len); | 508 ASSERT((offset + length) <= len); |
497 buffer += offset; | 509 buffer += offset; |
498 intptr_t bytes_written = Socket::Write(socket, buffer, length); | 510 intptr_t bytes_written = Socket::Write(socket->fd(), buffer, length); |
499 if (bytes_written >= 0) { | 511 if (bytes_written >= 0) { |
500 Dart_TypedDataReleaseData(buffer_obj); | 512 Dart_TypedDataReleaseData(buffer_obj); |
501 if (short_write) { | 513 if (short_write) { |
502 // If the write was forced 'short', indicate by returning the negative | 514 // If the write was forced 'short', indicate by returning the negative |
503 // number of bytes. A forced short write may not trigger a write event. | 515 // number of bytes. A forced short write may not trigger a write event. |
504 Dart_SetReturnValue(args, Dart_NewInteger(-bytes_written)); | 516 Dart_SetReturnValue(args, Dart_NewInteger(-bytes_written)); |
505 } else { | 517 } else { |
506 Dart_SetReturnValue(args, Dart_NewInteger(bytes_written)); | 518 Dart_SetReturnValue(args, Dart_NewInteger(bytes_written)); |
507 } | 519 } |
508 } else { | 520 } else { |
509 // Extract OSError before we release data, as it may override the error. | 521 // Extract OSError before we release data, as it may override the error. |
510 OSError os_error; | 522 OSError os_error; |
511 Dart_TypedDataReleaseData(buffer_obj); | 523 Dart_TypedDataReleaseData(buffer_obj); |
512 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); | 524 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); |
513 } | 525 } |
514 } | 526 } |
515 | 527 |
516 | 528 |
517 void FUNCTION_NAME(Socket_SendTo)(Dart_NativeArguments args) { | 529 void FUNCTION_NAME(Socket_SendTo)(Dart_NativeArguments args) { |
518 intptr_t socket = | 530 Socket* socket = |
519 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 531 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
520 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); | 532 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); |
521 intptr_t offset = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 2)); | 533 intptr_t offset = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 2)); |
522 intptr_t length = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 3)); | 534 intptr_t length = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 3)); |
523 Dart_Handle address_obj = Dart_GetNativeArgument(args, 4); | 535 Dart_Handle address_obj = Dart_GetNativeArgument(args, 4); |
524 ASSERT(Dart_IsList(address_obj)); | 536 ASSERT(Dart_IsList(address_obj)); |
525 RawAddr addr; | 537 RawAddr addr; |
526 SocketAddress::GetSockAddr(address_obj, &addr); | 538 SocketAddress::GetSockAddr(address_obj, &addr); |
527 int64_t port = DartUtils::GetInt64ValueCheckRange( | 539 int64_t port = DartUtils::GetInt64ValueCheckRange( |
528 Dart_GetNativeArgument(args, 5), 0, 65535); | 540 Dart_GetNativeArgument(args, 5), 0, 65535); |
529 SocketAddress::SetAddrPort(&addr, port); | 541 SocketAddress::SetAddrPort(&addr, port); |
530 Dart_TypedData_Type type; | 542 Dart_TypedData_Type type; |
531 uint8_t* buffer = NULL; | 543 uint8_t* buffer = NULL; |
532 intptr_t len; | 544 intptr_t len; |
533 Dart_Handle result = Dart_TypedDataAcquireData( | 545 Dart_Handle result = Dart_TypedDataAcquireData( |
534 buffer_obj, &type, reinterpret_cast<void**>(&buffer), &len); | 546 buffer_obj, &type, reinterpret_cast<void**>(&buffer), &len); |
535 if (Dart_IsError(result)) { | 547 if (Dart_IsError(result)) { |
536 Dart_PropagateError(result); | 548 Dart_PropagateError(result); |
537 } | 549 } |
538 ASSERT((offset + length) <= len); | 550 ASSERT((offset + length) <= len); |
539 buffer += offset; | 551 buffer += offset; |
540 intptr_t bytes_written = Socket::SendTo(socket, buffer, length, addr); | 552 intptr_t bytes_written = Socket::SendTo(socket->fd(), buffer, length, addr); |
541 if (bytes_written >= 0) { | 553 if (bytes_written >= 0) { |
542 Dart_TypedDataReleaseData(buffer_obj); | 554 Dart_TypedDataReleaseData(buffer_obj); |
543 Dart_SetReturnValue(args, Dart_NewInteger(bytes_written)); | 555 Dart_SetReturnValue(args, Dart_NewInteger(bytes_written)); |
544 } else { | 556 } else { |
545 // Extract OSError before we release data, as it may override the error. | 557 // Extract OSError before we release data, as it may override the error. |
546 OSError os_error; | 558 OSError os_error; |
547 Dart_TypedDataReleaseData(buffer_obj); | 559 Dart_TypedDataReleaseData(buffer_obj); |
548 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); | 560 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); |
549 } | 561 } |
550 } | 562 } |
551 | 563 |
552 | 564 |
553 void FUNCTION_NAME(Socket_GetPort)(Dart_NativeArguments args) { | 565 void FUNCTION_NAME(Socket_GetPort)(Dart_NativeArguments args) { |
554 intptr_t socket = | 566 Socket* socket = |
555 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 567 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
556 OSError os_error; | 568 OSError os_error; |
557 intptr_t port = Socket::GetPort(socket); | 569 intptr_t port = Socket::GetPort(socket->fd()); |
558 if (port > 0) { | 570 if (port > 0) { |
559 Dart_SetReturnValue(args, Dart_NewInteger(port)); | 571 Dart_SetReturnValue(args, Dart_NewInteger(port)); |
560 } else { | 572 } else { |
561 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 573 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
562 } | 574 } |
563 } | 575 } |
564 | 576 |
565 | 577 |
566 void FUNCTION_NAME(Socket_GetRemotePeer)(Dart_NativeArguments args) { | 578 void FUNCTION_NAME(Socket_GetRemotePeer)(Dart_NativeArguments args) { |
567 intptr_t socket = | 579 Socket* socket = |
568 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 580 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
569 OSError os_error; | 581 OSError os_error; |
570 intptr_t port = 0; | 582 intptr_t port = 0; |
571 SocketAddress* addr = Socket::GetRemotePeer(socket, &port); | 583 SocketAddress* addr = Socket::GetRemotePeer(socket->fd(), &port); |
572 if (addr != NULL) { | 584 if (addr != NULL) { |
573 Dart_Handle list = Dart_NewList(2); | 585 Dart_Handle list = Dart_NewList(2); |
574 | 586 |
575 Dart_Handle entry = Dart_NewList(3); | 587 Dart_Handle entry = Dart_NewList(3); |
576 Dart_ListSetAt(entry, 0, Dart_NewInteger(addr->GetType())); | 588 Dart_ListSetAt(entry, 0, Dart_NewInteger(addr->GetType())); |
577 Dart_ListSetAt(entry, 1, Dart_NewStringFromCString(addr->as_string())); | 589 Dart_ListSetAt(entry, 1, Dart_NewStringFromCString(addr->as_string())); |
578 | 590 |
579 RawAddr raw = addr->addr(); | 591 RawAddr raw = addr->addr(); |
580 Dart_ListSetAt(entry, 2, SocketAddress::ToTypedData(raw)); | 592 Dart_ListSetAt(entry, 2, SocketAddress::ToTypedData(raw)); |
581 | 593 |
582 Dart_ListSetAt(list, 0, entry); | 594 Dart_ListSetAt(list, 0, entry); |
583 Dart_ListSetAt(list, 1, Dart_NewInteger(port)); | 595 Dart_ListSetAt(list, 1, Dart_NewInteger(port)); |
584 Dart_SetReturnValue(args, list); | 596 Dart_SetReturnValue(args, list); |
585 delete addr; | 597 delete addr; |
586 } else { | 598 } else { |
587 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 599 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
588 } | 600 } |
589 } | 601 } |
590 | 602 |
591 | 603 |
592 void FUNCTION_NAME(Socket_GetError)(Dart_NativeArguments args) { | 604 void FUNCTION_NAME(Socket_GetError)(Dart_NativeArguments args) { |
593 intptr_t socket = | 605 Socket* socket = |
594 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 606 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
595 OSError os_error; | 607 OSError os_error; |
596 Socket::GetError(socket, &os_error); | 608 Socket::GetError(socket->fd(), &os_error); |
597 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); | 609 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); |
598 } | 610 } |
599 | 611 |
600 | 612 |
601 void FUNCTION_NAME(Socket_GetType)(Dart_NativeArguments args) { | 613 void FUNCTION_NAME(Socket_GetType)(Dart_NativeArguments args) { |
602 intptr_t socket = | 614 Socket* socket = |
603 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 615 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
604 OSError os_error; | 616 OSError os_error; |
605 intptr_t type = Socket::GetType(socket); | 617 intptr_t type = Socket::GetType(socket->fd()); |
606 if (type >= 0) { | 618 if (type >= 0) { |
607 Dart_SetReturnValue(args, Dart_NewInteger(type)); | 619 Dart_SetReturnValue(args, Dart_NewInteger(type)); |
608 } else { | 620 } else { |
609 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 621 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
610 } | 622 } |
611 } | 623 } |
612 | 624 |
613 | 625 |
614 void FUNCTION_NAME(Socket_GetStdioHandle)(Dart_NativeArguments args) { | 626 void FUNCTION_NAME(Socket_GetStdioHandle)(Dart_NativeArguments args) { |
615 int64_t num = | 627 int64_t num = |
616 DartUtils::GetInt64ValueCheckRange(Dart_GetNativeArgument(args, 1), 0, 2); | 628 DartUtils::GetInt64ValueCheckRange(Dart_GetNativeArgument(args, 1), 0, 2); |
617 intptr_t socket = Socket::GetStdioHandle(num); | 629 intptr_t socket = Socket::GetStdioHandle(num); |
618 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket); | 630 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket, |
| 631 false); |
619 Dart_SetReturnValue(args, Dart_NewBoolean(socket >= 0)); | 632 Dart_SetReturnValue(args, Dart_NewBoolean(socket >= 0)); |
620 } | 633 } |
621 | 634 |
622 | 635 |
623 void FUNCTION_NAME(Socket_GetSocketId)(Dart_NativeArguments args) { | 636 void FUNCTION_NAME(Socket_GetSocketId)(Dart_NativeArguments args) { |
624 intptr_t id = Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 637 Socket* socket = |
| 638 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
| 639 intptr_t id = reinterpret_cast<intptr_t>(socket); |
625 Dart_SetReturnValue(args, Dart_NewInteger(id)); | 640 Dart_SetReturnValue(args, Dart_NewInteger(id)); |
626 } | 641 } |
627 | 642 |
628 | 643 |
629 void FUNCTION_NAME(Socket_SetSocketId)(Dart_NativeArguments args) { | 644 void FUNCTION_NAME(Socket_SetSocketId)(Dart_NativeArguments args) { |
630 intptr_t id = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 1)); | 645 intptr_t id = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 1)); |
631 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), id); | 646 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), id, false); |
632 } | 647 } |
633 | 648 |
634 | 649 |
635 void FUNCTION_NAME(ServerSocket_CreateBindListen)(Dart_NativeArguments args) { | 650 void FUNCTION_NAME(ServerSocket_CreateBindListen)(Dart_NativeArguments args) { |
636 RawAddr addr; | 651 RawAddr addr; |
637 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); | 652 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); |
638 int64_t port = DartUtils::GetInt64ValueCheckRange( | 653 int64_t port = DartUtils::GetInt64ValueCheckRange( |
639 Dart_GetNativeArgument(args, 2), 0, 65535); | 654 Dart_GetNativeArgument(args, 2), 0, 65535); |
640 SocketAddress::SetAddrPort(&addr, port); | 655 SocketAddress::SetAddrPort(&addr, port); |
641 int64_t backlog = DartUtils::GetInt64ValueCheckRange( | 656 int64_t backlog = DartUtils::GetInt64ValueCheckRange( |
642 Dart_GetNativeArgument(args, 3), 0, 65535); | 657 Dart_GetNativeArgument(args, 3), 0, 65535); |
643 bool v6_only = DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 4)); | 658 bool v6_only = DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 4)); |
644 bool shared = DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 5)); | 659 bool shared = DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 5)); |
645 | 660 |
646 Dart_Handle socket_object = Dart_GetNativeArgument(args, 0); | 661 Dart_Handle socket_object = Dart_GetNativeArgument(args, 0); |
647 Dart_Handle result = ListeningSocketRegistry::Instance()->CreateBindListen( | 662 Dart_Handle result = ListeningSocketRegistry::Instance()->CreateBindListen( |
648 socket_object, addr, backlog, v6_only, shared); | 663 socket_object, addr, backlog, v6_only, shared); |
649 Dart_SetReturnValue(args, result); | 664 Dart_SetReturnValue(args, result); |
650 } | 665 } |
651 | 666 |
652 | 667 |
653 void FUNCTION_NAME(ServerSocket_Accept)(Dart_NativeArguments args) { | 668 void FUNCTION_NAME(ServerSocket_Accept)(Dart_NativeArguments args) { |
654 intptr_t socket = | 669 Socket* socket = |
655 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 670 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
656 intptr_t new_socket = ServerSocket::Accept(socket); | 671 intptr_t new_socket = ServerSocket::Accept(socket->fd()); |
657 if (new_socket >= 0) { | 672 if (new_socket >= 0) { |
658 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 1), new_socket); | 673 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 1), new_socket, |
| 674 false); |
659 Dart_SetReturnValue(args, Dart_True()); | 675 Dart_SetReturnValue(args, Dart_True()); |
660 } else if (new_socket == ServerSocket::kTemporaryFailure) { | 676 } else if (new_socket == ServerSocket::kTemporaryFailure) { |
661 Dart_SetReturnValue(args, Dart_False()); | 677 Dart_SetReturnValue(args, Dart_False()); |
662 } else { | 678 } else { |
663 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 679 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
664 } | 680 } |
665 } | 681 } |
666 | 682 |
667 | 683 |
668 CObject* Socket::LookupRequest(const CObjectArray& request) { | 684 CObject* Socket::LookupRequest(const CObjectArray& request) { |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
784 result = CObject::NewOSError(os_error); | 800 result = CObject::NewOSError(os_error); |
785 delete os_error; | 801 delete os_error; |
786 } | 802 } |
787 return result; | 803 return result; |
788 } | 804 } |
789 return CObject::IllegalArgumentError(); | 805 return CObject::IllegalArgumentError(); |
790 } | 806 } |
791 | 807 |
792 | 808 |
793 void FUNCTION_NAME(Socket_GetOption)(Dart_NativeArguments args) { | 809 void FUNCTION_NAME(Socket_GetOption)(Dart_NativeArguments args) { |
794 intptr_t socket = | 810 Socket* socket = |
795 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 811 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
796 int64_t option = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); | 812 int64_t option = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); |
797 intptr_t protocol = static_cast<intptr_t>( | 813 intptr_t protocol = static_cast<intptr_t>( |
798 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2))); | 814 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2))); |
799 bool ok = false; | 815 bool ok = false; |
800 switch (option) { | 816 switch (option) { |
801 case 0: { // TCP_NODELAY. | 817 case 0: { // TCP_NODELAY. |
802 bool enabled; | 818 bool enabled; |
803 ok = Socket::GetNoDelay(socket, &enabled); | 819 ok = Socket::GetNoDelay(socket->fd(), &enabled); |
804 if (ok) { | 820 if (ok) { |
805 Dart_SetReturnValue(args, enabled ? Dart_True() : Dart_False()); | 821 Dart_SetReturnValue(args, enabled ? Dart_True() : Dart_False()); |
806 } | 822 } |
807 break; | 823 break; |
808 } | 824 } |
809 case 1: { // IP_MULTICAST_LOOP. | 825 case 1: { // IP_MULTICAST_LOOP. |
810 bool enabled; | 826 bool enabled; |
811 ok = Socket::GetMulticastLoop(socket, protocol, &enabled); | 827 ok = Socket::GetMulticastLoop(socket->fd(), protocol, &enabled); |
812 if (ok) { | 828 if (ok) { |
813 Dart_SetReturnValue(args, enabled ? Dart_True() : Dart_False()); | 829 Dart_SetReturnValue(args, enabled ? Dart_True() : Dart_False()); |
814 } | 830 } |
815 break; | 831 break; |
816 } | 832 } |
817 case 2: { // IP_MULTICAST_TTL. | 833 case 2: { // IP_MULTICAST_TTL. |
818 int value; | 834 int value; |
819 ok = Socket::GetMulticastHops(socket, protocol, &value); | 835 ok = Socket::GetMulticastHops(socket->fd(), protocol, &value); |
820 if (ok) { | 836 if (ok) { |
821 Dart_SetReturnValue(args, Dart_NewInteger(value)); | 837 Dart_SetReturnValue(args, Dart_NewInteger(value)); |
822 } | 838 } |
823 break; | 839 break; |
824 } | 840 } |
825 case 3: { // IP_MULTICAST_IF. | 841 case 3: { // IP_MULTICAST_IF. |
826 UNIMPLEMENTED(); | 842 UNIMPLEMENTED(); |
827 break; | 843 break; |
828 } | 844 } |
829 case 4: { // IP_BROADCAST. | 845 case 4: { // IP_BROADCAST. |
830 bool enabled; | 846 bool enabled; |
831 ok = Socket::GetBroadcast(socket, &enabled); | 847 ok = Socket::GetBroadcast(socket->fd(), &enabled); |
832 if (ok) { | 848 if (ok) { |
833 Dart_SetReturnValue(args, enabled ? Dart_True() : Dart_False()); | 849 Dart_SetReturnValue(args, enabled ? Dart_True() : Dart_False()); |
834 } | 850 } |
835 break; | 851 break; |
836 } | 852 } |
837 default: | 853 default: |
838 UNREACHABLE(); | 854 UNREACHABLE(); |
839 break; | 855 break; |
840 } | 856 } |
841 // In case of failure the return value is not set above. | 857 // In case of failure the return value is not set above. |
842 if (!ok) { | 858 if (!ok) { |
843 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 859 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
844 } | 860 } |
845 } | 861 } |
846 | 862 |
847 | 863 |
848 void FUNCTION_NAME(Socket_SetOption)(Dart_NativeArguments args) { | 864 void FUNCTION_NAME(Socket_SetOption)(Dart_NativeArguments args) { |
849 bool result = false; | 865 bool result = false; |
850 intptr_t socket = | 866 Socket* socket = |
851 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 867 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
852 int64_t option = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); | 868 int64_t option = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); |
853 int64_t protocol = DartUtils::GetInt64ValueCheckRange( | 869 int64_t protocol = DartUtils::GetInt64ValueCheckRange( |
854 Dart_GetNativeArgument(args, 2), SocketAddress::TYPE_IPV4, | 870 Dart_GetNativeArgument(args, 2), SocketAddress::TYPE_IPV4, |
855 SocketAddress::TYPE_IPV6); | 871 SocketAddress::TYPE_IPV6); |
856 switch (option) { | 872 switch (option) { |
857 case 0: // TCP_NODELAY. | 873 case 0: // TCP_NODELAY. |
858 result = Socket::SetNoDelay( | 874 result = Socket::SetNoDelay( |
859 socket, DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 3))); | 875 socket->fd(), |
| 876 DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 3))); |
860 break; | 877 break; |
861 case 1: // IP_MULTICAST_LOOP. | 878 case 1: // IP_MULTICAST_LOOP. |
862 result = Socket::SetMulticastLoop( | 879 result = Socket::SetMulticastLoop( |
863 socket, protocol, | 880 socket->fd(), protocol, |
864 DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 3))); | 881 DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 3))); |
865 break; | 882 break; |
866 case 2: // IP_MULTICAST_TTL. | 883 case 2: // IP_MULTICAST_TTL. |
867 result = Socket::SetMulticastHops( | 884 result = Socket::SetMulticastHops( |
868 socket, protocol, | 885 socket->fd(), protocol, |
869 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3))); | 886 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3))); |
870 break; | 887 break; |
871 case 3: { // IP_MULTICAST_IF. | 888 case 3: { // IP_MULTICAST_IF. |
872 UNIMPLEMENTED(); | 889 UNIMPLEMENTED(); |
873 break; | 890 break; |
874 } | 891 } |
875 case 4: // IP_BROADCAST. | 892 case 4: // IP_BROADCAST. |
876 result = Socket::SetBroadcast( | 893 result = Socket::SetBroadcast( |
877 socket, DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 3))); | 894 socket->fd(), |
| 895 DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 3))); |
878 break; | 896 break; |
879 default: | 897 default: |
880 Dart_PropagateError(Dart_NewApiError("Value outside expected range")); | 898 Dart_PropagateError(Dart_NewApiError("Value outside expected range")); |
881 break; | 899 break; |
882 } | 900 } |
883 if (result) { | 901 if (result) { |
884 Dart_SetReturnValue(args, Dart_Null()); | 902 Dart_SetReturnValue(args, Dart_Null()); |
885 } else { | 903 } else { |
886 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 904 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
887 } | 905 } |
888 } | 906 } |
889 | 907 |
890 | 908 |
891 void FUNCTION_NAME(Socket_JoinMulticast)(Dart_NativeArguments args) { | 909 void FUNCTION_NAME(Socket_JoinMulticast)(Dart_NativeArguments args) { |
892 intptr_t socket = | 910 Socket* socket = |
893 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 911 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
894 RawAddr addr; | 912 RawAddr addr; |
895 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); | 913 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); |
896 RawAddr interface; | 914 RawAddr interface; |
897 if (Dart_GetNativeArgument(args, 2) != Dart_Null()) { | 915 if (Dart_GetNativeArgument(args, 2) != Dart_Null()) { |
898 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 2), &interface); | 916 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 2), &interface); |
899 } | 917 } |
900 int interfaceIndex = | 918 int interfaceIndex = |
901 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); | 919 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); |
902 if (Socket::JoinMulticast(socket, addr, interface, interfaceIndex)) { | 920 if (Socket::JoinMulticast(socket->fd(), addr, interface, interfaceIndex)) { |
903 Dart_SetReturnValue(args, Dart_Null()); | 921 Dart_SetReturnValue(args, Dart_Null()); |
904 } else { | 922 } else { |
905 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 923 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
906 } | 924 } |
907 } | 925 } |
908 | 926 |
909 | 927 |
910 void FUNCTION_NAME(Socket_LeaveMulticast)(Dart_NativeArguments args) { | 928 void FUNCTION_NAME(Socket_LeaveMulticast)(Dart_NativeArguments args) { |
911 intptr_t socket = | 929 Socket* socket = |
912 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 930 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
913 RawAddr addr; | 931 RawAddr addr; |
914 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); | 932 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); |
915 RawAddr interface; | 933 RawAddr interface; |
916 if (Dart_GetNativeArgument(args, 2) != Dart_Null()) { | 934 if (Dart_GetNativeArgument(args, 2) != Dart_Null()) { |
917 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 2), &interface); | 935 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 2), &interface); |
918 } | 936 } |
919 int interfaceIndex = | 937 int interfaceIndex = |
920 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); | 938 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); |
921 if (Socket::LeaveMulticast(socket, addr, interface, interfaceIndex)) { | 939 if (Socket::LeaveMulticast(socket->fd(), addr, interface, interfaceIndex)) { |
922 Dart_SetReturnValue(args, Dart_Null()); | 940 Dart_SetReturnValue(args, Dart_Null()); |
923 } else { | 941 } else { |
924 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 942 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
925 } | 943 } |
926 } | 944 } |
927 | 945 |
928 | 946 |
929 void Socket::SetSocketIdNativeField(Dart_Handle socket, intptr_t id) { | 947 static void SocketFinalizer(void* isolate_data, |
930 Dart_Handle err = | 948 Dart_WeakPersistentHandle handle, |
931 Dart_SetNativeInstanceField(socket, kSocketIdNativeField, id); | 949 void* data) { |
| 950 Socket* socket = reinterpret_cast<Socket*>(data); |
| 951 if (socket->fd() >= 0) { |
| 952 const int64_t flags = 1 << kCloseCommand; |
| 953 socket->Retain(); |
| 954 EventHandler::SendFromNative(reinterpret_cast<intptr_t>(socket), |
| 955 socket->port(), flags); |
| 956 } |
| 957 socket->Release(); |
| 958 } |
| 959 |
| 960 |
| 961 static void ListeningSocketFinalizer(void* isolate_data, |
| 962 Dart_WeakPersistentHandle handle, |
| 963 void* data) { |
| 964 Socket* socket = reinterpret_cast<Socket*>(data); |
| 965 if (socket->fd() >= 0) { |
| 966 const int64_t flags = (1 << kListeningSocket) | (1 << kCloseCommand); |
| 967 socket->Retain(); |
| 968 EventHandler::SendFromNative(reinterpret_cast<intptr_t>(socket), |
| 969 socket->port(), flags); |
| 970 } |
| 971 socket->Release(); |
| 972 } |
| 973 |
| 974 |
| 975 void Socket::ReuseSocketIdNativeField(Dart_Handle handle, |
| 976 Socket* socket, |
| 977 bool listening) { |
| 978 Dart_Handle err = Dart_SetNativeInstanceField( |
| 979 handle, kSocketIdNativeField, reinterpret_cast<intptr_t>(socket)); |
932 if (Dart_IsError(err)) { | 980 if (Dart_IsError(err)) { |
933 Dart_PropagateError(err); | 981 Dart_PropagateError(err); |
934 } | 982 } |
| 983 if (listening) { |
| 984 Dart_NewWeakPersistentHandle(handle, reinterpret_cast<void*>(socket), |
| 985 sizeof(Socket), ListeningSocketFinalizer); |
| 986 } else { |
| 987 Dart_NewWeakPersistentHandle(handle, reinterpret_cast<void*>(socket), |
| 988 sizeof(Socket), SocketFinalizer); |
| 989 } |
935 } | 990 } |
936 | 991 |
937 | 992 |
938 intptr_t Socket::GetSocketIdNativeField(Dart_Handle socket_obj) { | 993 void Socket::SetSocketIdNativeField(Dart_Handle handle, |
939 intptr_t socket = 0; | 994 intptr_t id, |
| 995 bool listening) { |
| 996 Socket* socket = new Socket(id); |
| 997 ReuseSocketIdNativeField(handle, socket, listening); |
| 998 } |
| 999 |
| 1000 |
| 1001 Socket* Socket::GetSocketIdNativeField(Dart_Handle socket_obj) { |
| 1002 intptr_t id; |
940 Dart_Handle err = | 1003 Dart_Handle err = |
941 Dart_GetNativeInstanceField(socket_obj, kSocketIdNativeField, &socket); | 1004 Dart_GetNativeInstanceField(socket_obj, kSocketIdNativeField, &id); |
942 if (Dart_IsError(err)) { | 1005 if (Dart_IsError(err)) { |
943 Dart_PropagateError(err); | 1006 Dart_PropagateError(err); |
944 } | 1007 } |
| 1008 Socket* socket = reinterpret_cast<Socket*>(id); |
945 return socket; | 1009 return socket; |
946 } | 1010 } |
947 | 1011 |
948 } // namespace bin | 1012 } // namespace bin |
949 } // namespace dart | 1013 } // namespace dart |
950 | 1014 |
951 #endif // !defined(DART_IO_DISABLED) | 1015 #endif // !defined(DART_IO_DISABLED) |
OLD | NEW |