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.
5 #ifndef NET_SOCKET_WEBSOCKET_ENDPOINT_LOCK_MANAGER_H_
6 #define NET_SOCKET_WEBSOCKET_ENDPOINT_LOCK_MANAGER_H_
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"
22 class NET_EXPORT_PRIVATE WebSocketEndpointLockManager
{
24 class NET_EXPORT_PRIVATE Waiter
: public base::LinkNode
<Waiter
> {
26 // If the node is in a list, removes it.
29 virtual void GotEndpointLock() = 0;
32 static WebSocketEndpointLockManager
* GetInstance();
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
);
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
);
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
);
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
);
57 // Checks that |endpoint_waiter_map_| and |socket_endpoint_map_| are
62 typedef base::LinkedList
<Waiter
> ConnectJobQueue
;
63 typedef std::map
<IPEndPoint
, ConnectJobQueue
*> EndPointWaiterMap
;
64 typedef std::map
<StreamSocket
*, IPEndPoint
> SocketEndPointMap
;
66 WebSocketEndpointLockManager();
67 ~WebSocketEndpointLockManager();
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_
;
74 // Store sockets remembered by RememberSocket() and not yet unlocked by
76 SocketEndPointMap socket_endpoint_map_
;
78 friend struct DefaultSingletonTraits
<WebSocketEndpointLockManager
>;
80 DISALLOW_COPY_AND_ASSIGN(WebSocketEndpointLockManager
);
85 #endif // NET_SOCKET_WEBSOCKET_ENDPOINT_LOCK_MANAGER_H_