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 RUNTIME_BIN_SOCKET_H_ | 5 #ifndef RUNTIME_BIN_SOCKET_H_ |
6 #define RUNTIME_BIN_SOCKET_H_ | 6 #define RUNTIME_BIN_SOCKET_H_ |
7 | 7 |
8 #if defined(DART_IO_DISABLED) | 8 #if defined(DART_IO_DISABLED) |
9 #error "socket.h can only be included on builds with IO enabled" | 9 #error "socket.h can only be included on builds with IO enabled" |
10 #endif | 10 #endif |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 | 81 |
82 static const int kClosedFd = -1; | 82 static const int kClosedFd = -1; |
83 | 83 |
84 intptr_t fd_; | 84 intptr_t fd_; |
85 Dart_Port port_; | 85 Dart_Port port_; |
86 | 86 |
87 friend class ReferenceCounted<Socket>; | 87 friend class ReferenceCounted<Socket>; |
88 DISALLOW_COPY_AND_ASSIGN(Socket); | 88 DISALLOW_COPY_AND_ASSIGN(Socket); |
89 }; | 89 }; |
90 | 90 |
91 | |
92 class ServerSocket { | 91 class ServerSocket { |
93 public: | 92 public: |
94 static const intptr_t kTemporaryFailure = -2; | 93 static const intptr_t kTemporaryFailure = -2; |
95 | 94 |
96 static intptr_t Accept(intptr_t fd); | 95 static intptr_t Accept(intptr_t fd); |
97 | 96 |
98 // Creates a socket which is bound and listens. The port to listen on is | 97 // Creates a socket which is bound and listens. The port to listen on is |
99 // specified in the port component of the passed RawAddr structure. | 98 // specified in the port component of the passed RawAddr structure. |
100 // | 99 // |
101 // Returns a positive integer if the call is successful. In case of failure | 100 // Returns a positive integer if the call is successful. In case of failure |
102 // it returns: | 101 // it returns: |
103 // | 102 // |
104 // -1: system error (errno set) | 103 // -1: system error (errno set) |
105 // -5: invalid bindAddress | 104 // -5: invalid bindAddress |
106 static intptr_t CreateBindListen(const RawAddr& addr, | 105 static intptr_t CreateBindListen(const RawAddr& addr, |
107 intptr_t backlog, | 106 intptr_t backlog, |
108 bool v6_only = false); | 107 bool v6_only = false); |
109 | 108 |
110 // Start accepting on a newly created listening socket. If it was unable to | 109 // Start accepting on a newly created listening socket. If it was unable to |
111 // start accepting incoming sockets, the fd is invalidated. | 110 // start accepting incoming sockets, the fd is invalidated. |
112 static bool StartAccept(intptr_t fd); | 111 static bool StartAccept(intptr_t fd); |
113 | 112 |
114 private: | 113 private: |
115 DISALLOW_ALLOCATION(); | 114 DISALLOW_ALLOCATION(); |
116 DISALLOW_IMPLICIT_CONSTRUCTORS(ServerSocket); | 115 DISALLOW_IMPLICIT_CONSTRUCTORS(ServerSocket); |
117 }; | 116 }; |
118 | 117 |
119 | |
120 class ListeningSocketRegistry { | 118 class ListeningSocketRegistry { |
121 public: | 119 public: |
122 ListeningSocketRegistry() | 120 ListeningSocketRegistry() |
123 : sockets_by_port_(SameIntptrValue, kInitialSocketsCount), | 121 : sockets_by_port_(SameIntptrValue, kInitialSocketsCount), |
124 sockets_by_fd_(SameIntptrValue, kInitialSocketsCount), | 122 sockets_by_fd_(SameIntptrValue, kInitialSocketsCount), |
125 mutex_(new Mutex()) {} | 123 mutex_(new Mutex()) {} |
126 | 124 |
127 ~ListeningSocketRegistry() { | 125 ~ListeningSocketRegistry() { |
128 CloseAllSafe(); | 126 CloseAllSafe(); |
129 delete mutex_; | 127 delete mutex_; |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 } | 194 } |
197 | 195 |
198 static bool SameIntptrValue(void* key1, void* key2) { | 196 static bool SameIntptrValue(void* key1, void* key2) { |
199 return reinterpret_cast<intptr_t>(key1) == reinterpret_cast<intptr_t>(key2); | 197 return reinterpret_cast<intptr_t>(key1) == reinterpret_cast<intptr_t>(key2); |
200 } | 198 } |
201 | 199 |
202 static uint32_t GetHashmapHashFromIntptr(intptr_t i) { | 200 static uint32_t GetHashmapHashFromIntptr(intptr_t i) { |
203 return static_cast<uint32_t>((i + 1) & 0xFFFFFFFF); | 201 return static_cast<uint32_t>((i + 1) & 0xFFFFFFFF); |
204 } | 202 } |
205 | 203 |
206 | |
207 static void* GetHashmapKeyFromIntptr(intptr_t i) { | 204 static void* GetHashmapKeyFromIntptr(intptr_t i) { |
208 return reinterpret_cast<void*>(i + 1); | 205 return reinterpret_cast<void*>(i + 1); |
209 } | 206 } |
210 | 207 |
211 OSSocket* LookupByPort(intptr_t port); | 208 OSSocket* LookupByPort(intptr_t port); |
212 void InsertByPort(intptr_t port, OSSocket* socket); | 209 void InsertByPort(intptr_t port, OSSocket* socket); |
213 void RemoveByPort(intptr_t port); | 210 void RemoveByPort(intptr_t port); |
214 | 211 |
215 OSSocket* LookupByFd(Socket* fd); | 212 OSSocket* LookupByFd(Socket* fd); |
216 void InsertByFd(Socket* fd, OSSocket* socket); | 213 void InsertByFd(Socket* fd, OSSocket* socket); |
217 void RemoveByFd(Socket* fd); | 214 void RemoveByFd(Socket* fd); |
218 | 215 |
219 bool CloseOneSafe(OSSocket* os_socket, bool update_hash_maps); | 216 bool CloseOneSafe(OSSocket* os_socket, bool update_hash_maps); |
220 void CloseAllSafe(); | 217 void CloseAllSafe(); |
221 | 218 |
222 HashMap sockets_by_port_; | 219 HashMap sockets_by_port_; |
223 HashMap sockets_by_fd_; | 220 HashMap sockets_by_fd_; |
224 | 221 |
225 Mutex* mutex_; | 222 Mutex* mutex_; |
226 | 223 |
227 DISALLOW_COPY_AND_ASSIGN(ListeningSocketRegistry); | 224 DISALLOW_COPY_AND_ASSIGN(ListeningSocketRegistry); |
228 }; | 225 }; |
229 | 226 |
230 } // namespace bin | 227 } // namespace bin |
231 } // namespace dart | 228 } // namespace dart |
232 | 229 |
233 #endif // RUNTIME_BIN_SOCKET_H_ | 230 #endif // RUNTIME_BIN_SOCKET_H_ |
OLD | NEW |