OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef NET_SOCKET_WEBSOCKET_ENDPOINT_LOCK_MANAGER_H_ |
| 6 #define NET_SOCKET_WEBSOCKET_ENDPOINT_LOCK_MANAGER_H_ |
| 7 |
| 8 #include <map> |
| 9 |
| 10 #include "base/containers/linked_list.h" |
| 11 #include "base/logging.h" |
| 12 #include "base/macros.h" |
| 13 #include "base/memory/singleton.h" |
| 14 #include "net/base/ip_endpoint.h" |
| 15 #include "net/base/net_export.h" |
| 16 #include "net/socket/websocket_transport_client_socket_pool.h" |
| 17 |
| 18 namespace net { |
| 19 |
| 20 class StreamSocket; |
| 21 |
| 22 class NET_EXPORT_PRIVATE WebSocketEndpointLockManager { |
| 23 public: |
| 24 class NET_EXPORT_PRIVATE Waiter : public base::LinkNode<Waiter> { |
| 25 public: |
| 26 // If the node is in a list, removes it. |
| 27 virtual ~Waiter(); |
| 28 |
| 29 virtual void GotEndpointLock() = 0; |
| 30 }; |
| 31 |
| 32 static WebSocketEndpointLockManager* GetInstance(); |
| 33 |
| 34 // Returns OK if lock was acquired immediately, ERR_IO_PENDING if not. If the |
| 35 // lock was not acquired, then |waiter->GotEndpointLock()| will be called when |
| 36 // it is. A Waiter automatically removes itself from the list of waiters when |
| 37 // its destructor is called. |
| 38 int LockEndpoint(const IPEndPoint& endpoint, Waiter* waiter); |
| 39 |
| 40 // Records the IPEndPoint associated with a particular socket. This is |
| 41 // necessary because TCPClientSocket refuses to return the PeerAddress after |
| 42 // the connection is disconnected. The association will be forgotten when |
| 43 // UnlockSocket() is called. The |socket| pointer must not be deleted between |
| 44 // the call to RememberSocket() and the call to UnlockSocket(). |
| 45 void RememberSocket(StreamSocket* socket, const IPEndPoint& endpoint); |
| 46 |
| 47 // Releases the lock on an endpoint, and, if appropriate, triggers the next |
| 48 // socket connection. For a successful WebSocket connection, this method will |
| 49 // be called once when the handshake completes, and again when the connection |
| 50 // is closed. Calls after the first are safely ignored. |
| 51 void UnlockSocket(StreamSocket* socket); |
| 52 |
| 53 // Releases the lock on |endpoint|. If RememberSocket() has been called for |
| 54 // this endpoint, then UnlockSocket() must be used instead of this method. |
| 55 void UnlockEndpoint(const IPEndPoint& endpoint); |
| 56 |
| 57 // Checks that |endpoint_waiter_map_| and |socket_endpoint_map_| are |
| 58 // empty. For tests. |
| 59 bool IsEmpty() const; |
| 60 |
| 61 private: |
| 62 typedef base::LinkedList<Waiter> ConnectJobQueue; |
| 63 typedef std::map<IPEndPoint, ConnectJobQueue*> EndPointWaiterMap; |
| 64 typedef std::map<StreamSocket*, IPEndPoint> SocketEndPointMap; |
| 65 |
| 66 WebSocketEndpointLockManager(); |
| 67 ~WebSocketEndpointLockManager(); |
| 68 |
| 69 // If an entry is present in the map for a particular endpoint, then that |
| 70 // endpoint is locked. If the list is non-empty, then one or more Waiters are |
| 71 // waiting for the lock. |
| 72 EndPointWaiterMap endpoint_waiter_map_; |
| 73 |
| 74 // Store sockets remembered by RememberSocket() and not yet unlocked by |
| 75 // UnlockSocket(). |
| 76 SocketEndPointMap socket_endpoint_map_; |
| 77 |
| 78 friend struct DefaultSingletonTraits<WebSocketEndpointLockManager>; |
| 79 |
| 80 DISALLOW_COPY_AND_ASSIGN(WebSocketEndpointLockManager); |
| 81 }; |
| 82 |
| 83 } // namespace net |
| 84 |
| 85 #endif // NET_SOCKET_WEBSOCKET_ENDPOINT_LOCK_MANAGER_H_ |
OLD | NEW |