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 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 44 | 44 |
| 45 enum { | 45 enum { |
| 46 ADDRESS_LOOPBACK_IP_V4, | 46 ADDRESS_LOOPBACK_IP_V4, |
| 47 ADDRESS_LOOPBACK_IP_V6, | 47 ADDRESS_LOOPBACK_IP_V6, |
| 48 ADDRESS_ANY_IP_V4, | 48 ADDRESS_ANY_IP_V4, |
| 49 ADDRESS_ANY_IP_V6, | 49 ADDRESS_ANY_IP_V6, |
| 50 ADDRESS_FIRST = ADDRESS_LOOPBACK_IP_V4, | 50 ADDRESS_FIRST = ADDRESS_LOOPBACK_IP_V4, |
| 51 ADDRESS_LAST = ADDRESS_ANY_IP_V6, | 51 ADDRESS_LAST = ADDRESS_ANY_IP_V6, |
| 52 }; | 52 }; |
| 53 | 53 |
| 54 explicit SocketAddress(struct addrinfo* addrinfo); | 54 explicit SocketAddress(struct sockaddr* sockaddr); |
| 55 | |
| 56 virtual ~SocketAddress() {} | |
| 55 | 57 |
| 56 int GetType() { | 58 int GetType() { |
| 57 if (addr_.ss.ss_family == AF_INET6) return TYPE_IPV6; | 59 if (addr_.ss.ss_family == AF_INET6) return TYPE_IPV6; |
| 58 return TYPE_IPV4; | 60 return TYPE_IPV4; |
| 59 } | 61 } |
| 60 | 62 |
| 61 const char* as_string() const { return as_string_; } | 63 const char* as_string() const { return as_string_; } |
| 62 const RawAddr& addr() const { return addr_; } | 64 const RawAddr& addr() const { return addr_; } |
| 63 | 65 |
| 64 static intptr_t GetAddrLength(const RawAddr& addr) { | 66 static intptr_t GetAddrLength(const RawAddr& addr) { |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 89 } | 91 } |
| 90 } | 92 } |
| 91 | 93 |
| 92 private: | 94 private: |
| 93 char as_string_[INET6_ADDRSTRLEN]; | 95 char as_string_[INET6_ADDRSTRLEN]; |
| 94 RawAddr addr_; | 96 RawAddr addr_; |
| 95 | 97 |
| 96 DISALLOW_COPY_AND_ASSIGN(SocketAddress); | 98 DISALLOW_COPY_AND_ASSIGN(SocketAddress); |
| 97 }; | 99 }; |
| 98 | 100 |
| 101 class AdaptorSocketAddress : public SocketAddress { | |
|
Søren Gjesse
2013/06/10 12:26:38
Instead of inheriting here why not have the Socket
| |
| 102 public: | |
| 103 explicit AdaptorSocketAddress(struct sockaddr* sockaddr, const char* name) | |
| 104 : SocketAddress(sockaddr), name_(name) {} | |
|
Søren Gjesse
2013/06/10 12:26:38
4 space indent.
| |
| 105 | |
| 106 virtual ~AdaptorSocketAddress() { | |
| 107 delete name_; | |
| 108 } | |
| 109 | |
| 110 const char* name() const { return name_; } | |
| 111 | |
| 112 private: | |
| 113 const char* name_; | |
| 114 | |
| 115 DISALLOW_COPY_AND_ASSIGN(SocketAddress); | |
| 116 }; | |
| 117 | |
| 99 class SocketAddresses { | 118 class SocketAddresses { |
| 100 public: | 119 public: |
| 101 explicit SocketAddresses(intptr_t count) | 120 explicit SocketAddresses(intptr_t count) |
| 102 : count_(count), | 121 : count_(count), |
| 103 addresses_(new SocketAddress*[count_]) {} | 122 addresses_(new SocketAddress*[count_]) {} |
| 104 | 123 |
| 105 ~SocketAddresses() { | 124 ~SocketAddresses() { |
| 106 for (intptr_t i = 0; i < count_; i++) { | 125 for (intptr_t i = 0; i < count_; i++) { |
| 107 delete addresses_[i]; | 126 delete addresses_[i]; |
| 108 } | 127 } |
| 109 delete[] addresses_; | 128 delete[] addresses_; |
| 110 } | 129 } |
| 111 | 130 |
| 112 intptr_t count() const { return count_; } | 131 intptr_t count() const { return count_; } |
| 113 SocketAddress* GetAt(intptr_t i) const { return addresses_[i]; } | 132 SocketAddress* GetAt(intptr_t i) const { return addresses_[i]; } |
| 114 void SetAt(intptr_t i, SocketAddress* addr) { addresses_[i] = addr; } | 133 void SetAt(intptr_t i, SocketAddress* addr) { addresses_[i] = addr; } |
| 115 | 134 |
| 116 private: | 135 private: |
| 117 const intptr_t count_; | 136 const intptr_t count_; |
| 118 SocketAddress** addresses_; | 137 SocketAddress** addresses_; |
| 119 | 138 |
| 120 DISALLOW_COPY_AND_ASSIGN(SocketAddresses); | 139 DISALLOW_COPY_AND_ASSIGN(SocketAddresses); |
| 121 }; | 140 }; |
| 122 | 141 |
| 123 class Socket { | 142 class Socket { |
| 124 public: | 143 public: |
| 125 enum SocketRequest { | 144 enum SocketRequest { |
| 126 kLookupRequest = 0, | 145 kLookupRequest = 0, |
| 146 kListAdaptorsRequest = 1, | |
| 127 }; | 147 }; |
| 128 | 148 |
| 129 static bool Initialize(); | 149 static bool Initialize(); |
| 130 static intptr_t Available(intptr_t fd); | 150 static intptr_t Available(intptr_t fd); |
| 131 static int Read(intptr_t fd, void* buffer, intptr_t num_bytes); | 151 static int Read(intptr_t fd, void* buffer, intptr_t num_bytes); |
| 132 static int Write(intptr_t fd, const void* buffer, intptr_t num_bytes); | 152 static int Write(intptr_t fd, const void* buffer, intptr_t num_bytes); |
| 133 static intptr_t Create(RawAddr addr); | 153 static intptr_t Create(RawAddr addr); |
| 134 static intptr_t Connect(intptr_t fd, RawAddr addr, const intptr_t port); | 154 static intptr_t Connect(intptr_t fd, RawAddr addr, const intptr_t port); |
| 135 static intptr_t CreateConnect(RawAddr addr, | 155 static intptr_t CreateConnect(RawAddr addr, |
| 136 const intptr_t port); | 156 const intptr_t port); |
| 137 static intptr_t GetPort(intptr_t fd); | 157 static intptr_t GetPort(intptr_t fd); |
| 138 static bool GetRemotePeer(intptr_t fd, char* host, intptr_t* port); | 158 static bool GetRemotePeer(intptr_t fd, char* host, intptr_t* port); |
| 139 static void GetError(intptr_t fd, OSError* os_error); | 159 static void GetError(intptr_t fd, OSError* os_error); |
| 140 static int GetType(intptr_t fd); | 160 static int GetType(intptr_t fd); |
| 141 static intptr_t GetStdioHandle(int num); | 161 static intptr_t GetStdioHandle(int num); |
| 142 static void Close(intptr_t fd); | 162 static void Close(intptr_t fd); |
| 143 static bool SetNonBlocking(intptr_t fd); | 163 static bool SetNonBlocking(intptr_t fd); |
| 144 static bool SetBlocking(intptr_t fd); | 164 static bool SetBlocking(intptr_t fd); |
| 145 static bool SetNoDelay(intptr_t fd, bool enabled); | 165 static bool SetNoDelay(intptr_t fd, bool enabled); |
| 146 | 166 |
| 147 // Perform a hostname lookup. Returns the SocketAddresses. | 167 // Perform a hostname lookup. Returns the SocketAddresses. |
| 148 static SocketAddresses* LookupAddress(const char* host, | 168 static SocketAddresses* LookupAddress(const char* host, |
| 149 int type, | 169 int type, |
| 150 OSError** os_error); | 170 OSError** os_error); |
| 151 | 171 |
| 172 // List adaptors. Returns the SocketAddresses. | |
| 173 static SocketAddresses* ListAdaptors(bool include_loopback, | |
| 174 int type, | |
| 175 OSError** os_error); | |
| 176 | |
| 152 static Dart_Port GetServicePort(); | 177 static Dart_Port GetServicePort(); |
| 153 | 178 |
| 154 static Dart_Handle SetSocketIdNativeField(Dart_Handle socket, intptr_t id); | 179 static Dart_Handle SetSocketIdNativeField(Dart_Handle socket, intptr_t id); |
| 155 static Dart_Handle GetSocketIdNativeField(Dart_Handle socket, intptr_t* id); | 180 static Dart_Handle GetSocketIdNativeField(Dart_Handle socket, intptr_t* id); |
| 156 | 181 |
| 157 private: | 182 private: |
| 158 static dart::Mutex mutex_; | 183 static dart::Mutex mutex_; |
| 159 static int service_ports_size_; | 184 static int service_ports_size_; |
| 160 static Dart_Port* service_ports_; | 185 static Dart_Port* service_ports_; |
| 161 static int service_ports_index_; | 186 static int service_ports_index_; |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 174 // Returns a positive integer if the call is successful. In case of failure | 199 // Returns a positive integer if the call is successful. In case of failure |
| 175 // it returns: | 200 // it returns: |
| 176 // | 201 // |
| 177 // -1: system error (errno set) | 202 // -1: system error (errno set) |
| 178 // -5: invalid bindAddress | 203 // -5: invalid bindAddress |
| 179 static intptr_t CreateBindListen(RawAddr addr, | 204 static intptr_t CreateBindListen(RawAddr addr, |
| 180 intptr_t port, | 205 intptr_t port, |
| 181 intptr_t backlog, | 206 intptr_t backlog, |
| 182 bool v6_only = false); | 207 bool v6_only = false); |
| 183 | 208 |
| 209 private: | |
| 184 DISALLOW_ALLOCATION(); | 210 DISALLOW_ALLOCATION(); |
| 185 DISALLOW_IMPLICIT_CONSTRUCTORS(ServerSocket); | 211 DISALLOW_IMPLICIT_CONSTRUCTORS(ServerSocket); |
| 186 }; | 212 }; |
| 187 | 213 |
| 188 } // namespace bin | 214 } // namespace bin |
| 189 } // namespace dart | 215 } // namespace dart |
| 190 | 216 |
| 191 #endif // BIN_SOCKET_H_ | 217 #endif // BIN_SOCKET_H_ |
| OLD | NEW |