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 class SocketAddress { | |
| 27 public: | |
|
Søren Gjesse
2013/04/22 15:02:08
Add an enum for type which matched the enum in Dar
| |
| 28 explicit SocketAddress(struct addrinfo* addrinfo); | |
| 29 | |
| 30 int GetType() { | |
| 31 if (addr_.ss_family == AF_INET6) return 1; | |
| 32 return 0; | |
| 33 } | |
| 34 | |
| 35 const char* AsString() const { return as_string_; } | |
|
Søren Gjesse
2013/04/22 15:02:08
This accessor should be called as_string (see http
Anders Johnsen
2013/04/23 08:03:57
Done.
| |
| 36 const sockaddr_storage& GetAddr() const { return addr_; } | |
|
Søren Gjesse
2013/04/22 15:02:08
Ditto, should be called addr.
Anders Johnsen
2013/04/23 08:03:57
Done.
| |
| 37 | |
| 38 static intptr_t GetAddrLength(const sockaddr_storage& addr) { | |
| 39 return addr.ss_family == AF_INET6 ? INET6_ADDRSTRLEN : INET_ADDRSTRLEN; | |
| 40 } | |
| 41 | |
| 42 static int16_t FromType(int type) { | |
|
Søren Gjesse
2013/04/22 15:02:08
ASSERT type is -1, 0 or 1.
Anders Johnsen
2013/04/23 08:03:57
Done.
| |
| 43 if (type == -1) return AF_UNSPEC; | |
| 44 if (type == 1) return AF_INET6; | |
| 45 return AF_INET; | |
| 46 } | |
| 47 | |
| 48 private: | |
| 49 char as_string_[INET6_ADDRSTRLEN]; | |
| 50 sockaddr_storage addr_; | |
| 51 | |
| 52 DISALLOW_COPY_AND_ASSIGN(SocketAddress); | |
| 53 }; | |
| 54 | |
| 55 class SocketAddresses { | |
| 56 public: | |
| 57 explicit SocketAddresses(intptr_t count) | |
| 58 : count_(count), | |
| 59 addresses_(new SocketAddress*[count_]) {} | |
| 60 | |
| 61 ~SocketAddresses() { | |
| 62 for (intptr_t i = 0; i < count_; i++) { | |
| 63 delete addresses_[i]; | |
| 64 } | |
| 65 delete[] addresses_; | |
| 66 } | |
| 67 | |
| 68 intptr_t GetCount() const { return count_; } | |
|
Søren Gjesse
2013/04/22 15:02:08
GetCount -> count
Anders Johnsen
2013/04/23 08:03:57
Done.
| |
| 69 SocketAddress* GetAt(intptr_t i) const { return addresses_[i]; } | |
| 70 void SetAt(intptr_t i, SocketAddress* addr) { addresses_[i] = addr; } | |
| 71 | |
| 72 private: | |
| 73 const intptr_t count_; | |
| 74 SocketAddress** addresses_; | |
| 75 | |
| 76 DISALLOW_COPY_AND_ASSIGN(SocketAddresses); | |
| 77 }; | |
| 26 | 78 |
| 27 class Socket { | 79 class Socket { |
| 28 public: | 80 public: |
| 29 enum SocketRequest { | 81 enum SocketRequest { |
| 30 kLookupRequest = 0, | 82 kLookupRequest = 0, |
| 31 }; | 83 }; |
| 32 | 84 |
| 33 static bool Initialize(); | 85 static bool Initialize(); |
| 34 static intptr_t Available(intptr_t fd); | 86 static intptr_t Available(intptr_t fd); |
| 35 static int Read(intptr_t fd, void* buffer, intptr_t num_bytes); | 87 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); | 88 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); | 89 static intptr_t CreateConnect(sockaddr_storage addr, |
| 90 const intptr_t port); | |
| 38 static intptr_t GetPort(intptr_t fd); | 91 static intptr_t GetPort(intptr_t fd); |
| 39 static bool GetRemotePeer(intptr_t fd, char* host, intptr_t* port); | 92 static bool GetRemotePeer(intptr_t fd, char* host, intptr_t* port); |
| 40 static void GetError(intptr_t fd, OSError* os_error); | 93 static void GetError(intptr_t fd, OSError* os_error); |
| 41 static int GetType(intptr_t fd); | 94 static int GetType(intptr_t fd); |
| 42 static intptr_t GetStdioHandle(int num); | 95 static intptr_t GetStdioHandle(int num); |
| 43 static void Close(intptr_t fd); | 96 static void Close(intptr_t fd); |
| 44 static bool SetNonBlocking(intptr_t fd); | 97 static bool SetNonBlocking(intptr_t fd); |
| 45 static bool SetBlocking(intptr_t fd); | 98 static bool SetBlocking(intptr_t fd); |
| 46 static bool SetNoDelay(intptr_t fd, bool enabled); | 99 static bool SetNoDelay(intptr_t fd, bool enabled); |
| 47 | 100 |
| 48 // Perform a IPv4 hostname lookup. Returns the hostname string in | 101 // Perform a hostname lookup. Returns the SocketAddresses. |
| 49 // IPv4 dotted-decimal format. | 102 static SocketAddresses* LookupAddress(const char* host, |
| 50 static const char* LookupIPv4Address(char* host, OSError** os_error); | 103 int type, |
| 104 OSError** os_error); | |
| 51 | 105 |
| 52 static Dart_Port GetServicePort(); | 106 static Dart_Port GetServicePort(); |
| 53 | 107 |
| 54 static Dart_Handle SetSocketIdNativeField(Dart_Handle socket, intptr_t id); | 108 static Dart_Handle SetSocketIdNativeField(Dart_Handle socket, intptr_t id); |
| 55 static Dart_Handle GetSocketIdNativeField(Dart_Handle socket, intptr_t* id); | 109 static Dart_Handle GetSocketIdNativeField(Dart_Handle socket, intptr_t* id); |
| 56 | 110 |
| 57 private: | 111 private: |
| 58 static dart::Mutex mutex_; | 112 static dart::Mutex mutex_; |
| 59 static int service_ports_size_; | 113 static int service_ports_size_; |
| 60 static Dart_Port* service_ports_; | 114 static Dart_Port* service_ports_; |
| 61 static int service_ports_index_; | 115 static int service_ports_index_; |
| 62 | 116 |
| 63 DISALLOW_ALLOCATION(); | 117 DISALLOW_ALLOCATION(); |
| 64 DISALLOW_IMPLICIT_CONSTRUCTORS(Socket); | 118 DISALLOW_IMPLICIT_CONSTRUCTORS(Socket); |
| 65 }; | 119 }; |
| 66 | 120 |
| 67 | 121 |
| 68 class ServerSocket { | 122 class ServerSocket { |
| 69 public: | 123 public: |
| 70 static const intptr_t kTemporaryFailure = -2; | 124 static const intptr_t kTemporaryFailure = -2; |
| 71 | 125 |
| 72 static intptr_t Accept(intptr_t fd); | 126 static intptr_t Accept(intptr_t fd); |
| 73 | 127 |
| 74 // Returns a positive integer if the call is successful. In case of failure | 128 // Returns a positive integer if the call is successful. In case of failure |
| 75 // it returns: | 129 // it returns: |
| 76 // | 130 // |
| 77 // -1: system error (errno set) | 131 // -1: system error (errno set) |
| 78 // -5: invalid bindAddress | 132 // -5: invalid bindAddress |
| 79 static intptr_t CreateBindListen(const char* bindAddress, | 133 static intptr_t CreateBindListen(sockaddr_storage addr, |
| 80 intptr_t port, | 134 intptr_t port, |
| 81 intptr_t backlog); | 135 intptr_t backlog); |
| 82 | 136 |
| 83 DISALLOW_ALLOCATION(); | 137 DISALLOW_ALLOCATION(); |
| 84 DISALLOW_IMPLICIT_CONSTRUCTORS(ServerSocket); | 138 DISALLOW_IMPLICIT_CONSTRUCTORS(ServerSocket); |
| 85 }; | 139 }; |
| 86 | 140 |
| 87 #endif // BIN_SOCKET_H_ | 141 #endif // BIN_SOCKET_H_ |
| OLD | NEW |