Chromium Code Reviews| 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 "bin/builtin.h" | 8 #include "bin/builtin.h" |
| 9 #include "bin/utils.h" | 9 #include "bin/utils.h" |
| 10 | 10 |
| 11 #include "platform/globals.h" | 11 #include "platform/globals.h" |
| 12 #include "platform/thread.h" | 12 #include "platform/thread.h" |
| 13 // Declare the OS-specific types ahead of defining the generic class. | 13 // Declare the OS-specific types ahead of defining the generic class. |
| 14 #if defined(TARGET_OS_ANDROID) | 14 #if defined(TARGET_OS_ANDROID) |
| 15 #include "bin/socket_android.h" | 15 #include "bin/socket_android.h" |
| 16 #elif defined(TARGET_OS_LINUX) | 16 #elif defined(TARGET_OS_LINUX) |
| 17 #include "bin/socket_linux.h" | 17 #include "bin/socket_linux.h" |
| 18 #elif defined(TARGET_OS_MACOS) | 18 #elif defined(TARGET_OS_MACOS) |
| 19 #include "bin/socket_macos.h" | 19 #include "bin/socket_macos.h" |
| 20 #elif defined(TARGET_OS_WINDOWS) | 20 #elif defined(TARGET_OS_WINDOWS) |
| 21 #include "bin/socket_win.h" | 21 #include "bin/socket_win.h" |
| 22 #else | 22 #else |
| 23 #error Unknown target os. | 23 #error Unknown target os. |
| 24 #endif | 24 #endif |
| 25 | 25 |
| 26 #define SOCKADDR_STORAGE_LENGTH(sa) \ | |
|
Søren Gjesse
2013/04/19 10:36:06
Maybe make this a static method on SocketAddress i
Anders Johnsen
2013/04/22 14:05:15
Done.
| |
| 27 sa.ss_family == AF_INET6 ? INET6_ADDRSTRLEN : INET_ADDRSTRLEN | |
| 28 | |
| 29 class SocketAddress { | |
| 30 public: | |
| 31 explicit SocketAddress(struct addrinfo* addrinfo); | |
| 32 | |
| 33 int GetType() { | |
| 34 if (addr_.ss_family == AF_INET6) return 1; | |
| 35 return 0; | |
| 36 } | |
| 37 | |
| 38 const char* AsString() const { return as_string_; } | |
| 39 const sockaddr_storage& GetAddr() const { return addr_; } | |
| 40 | |
| 41 private: | |
| 42 char as_string_[INET6_ADDRSTRLEN]; | |
| 43 sockaddr_storage addr_; | |
| 44 | |
| 45 DISALLOW_COPY_AND_ASSIGN(SocketAddress); | |
| 46 }; | |
| 47 | |
| 48 class SocketAddresses { | |
| 49 public: | |
| 50 explicit SocketAddresses(intptr_t count) | |
| 51 : count_(count), | |
| 52 addresses_(new SocketAddress*[count_]) {} | |
| 53 | |
| 54 ~SocketAddresses() { | |
| 55 for (intptr_t i = 0; i < count_; i++) { | |
| 56 delete addresses_[i]; | |
| 57 } | |
| 58 delete[] addresses_; | |
| 59 } | |
| 60 | |
| 61 intptr_t GetCount() const { return count_; } | |
| 62 SocketAddress* GetAt(intptr_t i) const { return addresses_[i]; } | |
| 63 void SetAt(intptr_t i, SocketAddress* addr) { addresses_[i] = addr; } | |
| 64 | |
| 65 private: | |
| 66 const intptr_t count_; | |
| 67 SocketAddress** addresses_; | |
| 68 | |
| 69 DISALLOW_COPY_AND_ASSIGN(SocketAddresses); | |
| 70 }; | |
| 26 | 71 |
| 27 class Socket { | 72 class Socket { |
| 28 public: | 73 public: |
| 29 enum SocketRequest { | 74 enum SocketRequest { |
| 30 kLookupRequest = 0, | 75 kLookupRequest = 0, |
| 31 }; | 76 }; |
| 32 | 77 |
| 33 static bool Initialize(); | 78 static bool Initialize(); |
| 34 static intptr_t Available(intptr_t fd); | 79 static intptr_t Available(intptr_t fd); |
| 35 static int Read(intptr_t fd, void* buffer, intptr_t num_bytes); | 80 static int Read(intptr_t fd, void* buffer, intptr_t num_bytes); |
| 36 static int Write(intptr_t fd, const void* buffer, intptr_t num_bytes); | 81 static int Write(intptr_t fd, const void* buffer, intptr_t num_bytes); |
| 37 static intptr_t CreateConnect(const char* host, const intptr_t port); | 82 static intptr_t CreateConnect(sockaddr_storage addr, |
| 83 const intptr_t port); | |
| 38 static intptr_t GetPort(intptr_t fd); | 84 static intptr_t GetPort(intptr_t fd); |
| 39 static bool GetRemotePeer(intptr_t fd, char* host, intptr_t* port); | 85 static bool GetRemotePeer(intptr_t fd, char* host, intptr_t* port); |
| 40 static void GetError(intptr_t fd, OSError* os_error); | 86 static void GetError(intptr_t fd, OSError* os_error); |
| 41 static int GetType(intptr_t fd); | 87 static int GetType(intptr_t fd); |
| 42 static intptr_t GetStdioHandle(int num); | 88 static intptr_t GetStdioHandle(int num); |
| 43 static void Close(intptr_t fd); | 89 static void Close(intptr_t fd); |
| 44 static bool SetNonBlocking(intptr_t fd); | 90 static bool SetNonBlocking(intptr_t fd); |
| 45 static bool SetBlocking(intptr_t fd); | 91 static bool SetBlocking(intptr_t fd); |
| 46 static bool SetNoDelay(intptr_t fd, bool enabled); | 92 static bool SetNoDelay(intptr_t fd, bool enabled); |
| 47 | 93 |
| 48 // Perform a IPv4 hostname lookup. Returns the hostname string in | 94 // Perform a hostname lookup. Returns the SocketAddresses. |
| 49 // IPv4 dotted-decimal format. | 95 static SocketAddresses* LookupAddress(char* host, OSError** os_error); |
| 50 static const char* LookupIPv4Address(char* host, OSError** os_error); | |
| 51 | 96 |
| 52 static Dart_Port GetServicePort(); | 97 static Dart_Port GetServicePort(); |
| 53 | 98 |
| 54 static Dart_Handle SetSocketIdNativeField(Dart_Handle socket, intptr_t id); | 99 static Dart_Handle SetSocketIdNativeField(Dart_Handle socket, intptr_t id); |
| 55 static Dart_Handle GetSocketIdNativeField(Dart_Handle socket, intptr_t* id); | 100 static Dart_Handle GetSocketIdNativeField(Dart_Handle socket, intptr_t* id); |
| 56 | 101 |
| 57 private: | 102 private: |
| 58 static dart::Mutex mutex_; | 103 static dart::Mutex mutex_; |
| 59 static int service_ports_size_; | 104 static int service_ports_size_; |
| 60 static Dart_Port* service_ports_; | 105 static Dart_Port* service_ports_; |
| 61 static int service_ports_index_; | 106 static int service_ports_index_; |
| 62 | 107 |
| 63 DISALLOW_ALLOCATION(); | 108 DISALLOW_ALLOCATION(); |
| 64 DISALLOW_IMPLICIT_CONSTRUCTORS(Socket); | 109 DISALLOW_IMPLICIT_CONSTRUCTORS(Socket); |
| 65 }; | 110 }; |
| 66 | 111 |
| 67 | 112 |
| 68 class ServerSocket { | 113 class ServerSocket { |
| 69 public: | 114 public: |
| 70 static const intptr_t kTemporaryFailure = -2; | 115 static const intptr_t kTemporaryFailure = -2; |
| 71 | 116 |
| 72 static intptr_t Accept(intptr_t fd); | 117 static intptr_t Accept(intptr_t fd); |
| 73 | 118 |
| 74 // Returns a positive integer if the call is successful. In case of failure | 119 // Returns a positive integer if the call is successful. In case of failure |
| 75 // it returns: | 120 // it returns: |
| 76 // | 121 // |
| 77 // -1: system error (errno set) | 122 // -1: system error (errno set) |
| 78 // -5: invalid bindAddress | 123 // -5: invalid bindAddress |
| 79 static intptr_t CreateBindListen(const char* bindAddress, | 124 static intptr_t CreateBindListen(sockaddr_storage addr, |
| 80 intptr_t port, | 125 intptr_t port, |
| 81 intptr_t backlog); | 126 intptr_t backlog); |
| 82 | 127 |
| 83 DISALLOW_ALLOCATION(); | 128 DISALLOW_ALLOCATION(); |
| 84 DISALLOW_IMPLICIT_CONSTRUCTORS(ServerSocket); | 129 DISALLOW_IMPLICIT_CONSTRUCTORS(ServerSocket); |
| 85 }; | 130 }; |
| 86 | 131 |
| 87 #endif // BIN_SOCKET_H_ | 132 #endif // BIN_SOCKET_H_ |
| OLD | NEW |