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 #ifndef BIN_SOCKET_H_ | 5 #ifndef BIN_SOCKET_H_ |
6 #define BIN_SOCKET_H_ | 6 #define BIN_SOCKET_H_ |
7 | 7 |
8 #include <map> | |
Søren Gjesse
2015/01/29 09:05:51
As some point we where avoiding using STL, which i
kustermann
2015/01/29 11:14:05
Yes, I thought there might be a 50/50 chance of be
| |
9 | |
8 #include "platform/globals.h" | 10 #include "platform/globals.h" |
9 | 11 |
10 #include "bin/builtin.h" | 12 #include "bin/builtin.h" |
11 #include "bin/dartutils.h" | 13 #include "bin/dartutils.h" |
12 // Declare the OS-specific types ahead of defining the generic class. | 14 // Declare the OS-specific types ahead of defining the generic class. |
13 #if defined(TARGET_OS_ANDROID) | 15 #if defined(TARGET_OS_ANDROID) |
14 #include "bin/socket_android.h" | 16 #include "bin/socket_android.h" |
15 #elif defined(TARGET_OS_LINUX) | 17 #elif defined(TARGET_OS_LINUX) |
16 #include "bin/socket_linux.h" | 18 #include "bin/socket_linux.h" |
17 #elif defined(TARGET_OS_MACOS) | 19 #elif defined(TARGET_OS_MACOS) |
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
305 | 307 |
306 // Start accepting on a newly created listening socket. If it was unable to | 308 // Start accepting on a newly created listening socket. If it was unable to |
307 // start accepting incoming sockets, the fd is invalidated. | 309 // start accepting incoming sockets, the fd is invalidated. |
308 static bool StartAccept(intptr_t fd); | 310 static bool StartAccept(intptr_t fd); |
309 | 311 |
310 private: | 312 private: |
311 DISALLOW_ALLOCATION(); | 313 DISALLOW_ALLOCATION(); |
312 DISALLOW_IMPLICIT_CONSTRUCTORS(ServerSocket); | 314 DISALLOW_IMPLICIT_CONSTRUCTORS(ServerSocket); |
313 }; | 315 }; |
314 | 316 |
317 class ListeningSocketRegistry { | |
318 private: | |
319 struct OSSocket { | |
320 RawAddr address; | |
321 int port; | |
322 bool v6_only; | |
323 bool shared; | |
324 int ref_count; | |
325 int socketfd; | |
Søren Gjesse
2015/01/29 09:05:51
To support Windows where this is a pointer this sh
kustermann
2015/01/29 11:14:05
Done.
| |
326 | |
327 // Singly linked lists of OSSocket instances which listen on the same port | |
328 // but on different addresses. | |
329 OSSocket *next; | |
330 | |
331 OSSocket(RawAddr address, int port, bool v6_only, bool shared, int socketfd) | |
332 : address(address), port(port), v6_only(v6_only), shared(shared), | |
333 ref_count(0), socketfd(socketfd), next(NULL) {} | |
334 }; | |
335 typedef std::map<int, OSSocket*>::iterator SocketsIterator; | |
336 | |
337 public: | |
338 ListeningSocketRegistry() : mutex_(new Mutex()) {} | |
339 | |
340 // This function should be called from a dart runtime call in order to create | |
341 // a new (potentially shared) socket. | |
342 Dart_Handle CreateBindListen(Dart_Handle socket_object, | |
343 RawAddr addr, | |
344 intptr_t port, | |
345 intptr_t backlog, | |
346 bool v6_only, | |
347 bool shared); | |
348 | |
349 // This should be called from the event handler for every kCloseEvent it gets | |
350 // on listening sockets. | |
351 // | |
352 // Returns `true` if the last reference has been droped and the underlying | |
wibling
2015/01/29 09:42:01
NIT: droped -> dropped
kustermann
2015/01/29 11:14:05
Done.
| |
353 // socket can be closed. | |
354 // | |
355 // The caller is responsible for obtaining the mutex first, before calling | |
356 // this function. | |
357 bool CloseSafe(int socketfd); | |
358 | |
359 // Mark an existing socket as sharable if it is not already marked as | |
360 // sharable. | |
361 // | |
362 // NOTE: This is a temporary measure until ServerSocketReferenc'es are | |
wibling
2015/01/29 09:42:01
NIT: ServerSocketReferenc'es -> ServerSocketRefere
kustermann
2015/01/29 11:14:05
Done.
| |
363 // removed. | |
364 Dart_Handle MarkSocketFdAsSharableHack(int socketfd); | |
365 | |
366 Mutex *mutex() { return mutex_; } | |
367 | |
368 private: | |
369 bool addressesAreEqual(const RawAddr& a, const RawAddr& b); | |
370 | |
371 OSSocket *findOSSocketWithAddress(OSSocket *current, const RawAddr& addr) { | |
372 while (current != NULL) { | |
373 if (addressesAreEqual(current->address, addr)) { | |
374 return current; | |
375 } | |
376 current = current->next; | |
377 } | |
378 return NULL; | |
379 } | |
380 | |
381 std::map<int, OSSocket*> sockets_by_port_; | |
382 std::map<int, OSSocket*> sockets_by_fd_; | |
383 Mutex *mutex_; | |
384 }; | |
385 | |
386 extern ListeningSocketRegistry globalTcpListeningSocketRegistry; | |
387 | |
315 } // namespace bin | 388 } // namespace bin |
316 } // namespace dart | 389 } // namespace dart |
317 | 390 |
318 #endif // BIN_SOCKET_H_ | 391 #endif // BIN_SOCKET_H_ |
OLD | NEW |