1 // Copyright (c) 2013 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 CHROME_TEST_CHROMEDRIVER_NET_PORT_SERVER_H_
6 #define CHROME_TEST_CHROMEDRIVER_NET_PORT_SERVER_H_
12 #include "base/callback.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/synchronization/lock.h"
18 class PortReservation
{
20 PortReservation(const base::Closure
& on_free_func
, uint16 port
);
26 base::Closure on_free_func_
;
30 // Communicates with a port reservation management server.
33 // Construct a port server that communicates via the unix domain socket with
34 // the given path. Must use the Linux abstract namespace.
35 explicit PortServer(const std::string
& path
);
38 Status
ReservePort(uint16
* port
, scoped_ptr
<PortReservation
>* reservation
);
41 Status
RequestPort(uint16
* port
);
42 void ReleasePort(uint16 port
);
46 base::Lock free_lock_
;
47 std::list
<uint16
> free_
;
50 // Manages reservation of a block of local ports.
53 PortManager(uint16 min_port
, uint16 max_port
);
56 Status
ReservePort(uint16
* port
, scoped_ptr
<PortReservation
>* reservation
);
57 // Since we cannot remove forwarded adb ports on older SDKs,
58 // maintain a pool of forwarded ports for reuse.
59 Status
ReservePortFromPool(uint16
* port
,
60 scoped_ptr
<PortReservation
>* reservation
);
63 uint16
FindAvailablePort() const;
64 void ReleasePort(uint16 port
);
65 void ReleasePortToPool(uint16 port
);
68 std::set
<uint16
> taken_
;
69 std::list
<uint16
> unused_forwarded_port_
;
74 #endif // CHROME_TEST_CHROMEDRIVER_NET_PORT_SERVER_H_