OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_TEST_CHROMEDRIVER_NET_PORT_SERVER_H_ | 5 #ifndef CHROME_TEST_CHROMEDRIVER_NET_PORT_SERVER_H_ |
6 #define CHROME_TEST_CHROMEDRIVER_NET_PORT_SERVER_H_ | 6 #define CHROME_TEST_CHROMEDRIVER_NET_PORT_SERVER_H_ |
7 | 7 |
8 #include <list> | 8 #include <list> |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
15 | 15 |
16 class Status; | 16 class Status; |
17 | 17 |
18 class PortReservation { | 18 class PortReservation { |
19 public: | 19 public: |
20 PortReservation(const base::Closure& on_free_func, int port); | 20 PortReservation(const base::Closure& on_free_func, uint16 port); |
21 ~PortReservation(); | 21 ~PortReservation(); |
22 | 22 |
23 void Leak(); | 23 void Leak(); |
24 | 24 |
25 private: | 25 private: |
26 base::Closure on_free_func_; | 26 base::Closure on_free_func_; |
27 int port_; | 27 uint16 port_; |
28 }; | 28 }; |
29 | 29 |
30 // Communicates with a port reservation management server. | 30 // Communicates with a port reservation management server. |
31 class PortServer { | 31 class PortServer { |
32 public: | 32 public: |
33 // Construct a port server that communicates via the unix domain socket with | 33 // Construct a port server that communicates via the unix domain socket with |
34 // the given path. Must use the Linux abstract namespace. | 34 // the given path. Must use the Linux abstract namespace. |
35 explicit PortServer(const std::string& path); | 35 explicit PortServer(const std::string& path); |
36 ~PortServer(); | 36 ~PortServer(); |
37 | 37 |
38 Status ReservePort(int* port, scoped_ptr<PortReservation>* reservation); | 38 Status ReservePort(uint16* port, scoped_ptr<PortReservation>* reservation); |
39 | 39 |
40 private: | 40 private: |
41 Status RequestPort(int* port); | 41 Status RequestPort(uint16* port); |
42 void ReleasePort(int port); | 42 void ReleasePort(uint16 port); |
43 | 43 |
44 std::string path_; | 44 std::string path_; |
45 | 45 |
46 base::Lock free_lock_; | 46 base::Lock free_lock_; |
47 std::list<int> free_; | 47 std::list<uint16> free_; |
48 }; | 48 }; |
49 | 49 |
50 // Manages reservation of a block of local ports. | 50 // Manages reservation of a block of local ports. |
51 class PortManager { | 51 class PortManager { |
52 public: | 52 public: |
53 PortManager(int min_port, int max_port); | 53 PortManager(uint16 min_port, uint16 max_port); |
54 ~PortManager(); | 54 ~PortManager(); |
55 | 55 |
56 Status ReservePort(int* port, scoped_ptr<PortReservation>* reservation); | 56 Status ReservePort(uint16* port, scoped_ptr<PortReservation>* reservation); |
57 // Since we cannot remove forwarded adb ports on older SDKs, | 57 // Since we cannot remove forwarded adb ports on older SDKs, |
58 // maintain a pool of forwarded ports for reuse. | 58 // maintain a pool of forwarded ports for reuse. |
59 Status ReservePortFromPool(int* port, | 59 Status ReservePortFromPool(uint16* port, |
60 scoped_ptr<PortReservation>* reservation); | 60 scoped_ptr<PortReservation>* reservation); |
61 | 61 |
62 private: | 62 private: |
63 int FindAvailablePort() const; | 63 uint16 FindAvailablePort() const; |
64 void ReleasePort(int port); | 64 void ReleasePort(uint16 port); |
65 void ReleasePortToPool(int port); | 65 void ReleasePortToPool(uint16 port); |
66 | 66 |
67 base::Lock lock_; | 67 base::Lock lock_; |
68 std::set<int> taken_; | 68 std::set<uint16> taken_; |
69 std::list<int> unused_forwarded_port_; | 69 std::list<uint16> unused_forwarded_port_; |
70 int min_port_; | 70 uint16 min_port_; |
71 int max_port_; | 71 uint16 max_port_; |
72 }; | 72 }; |
73 | 73 |
74 #endif // CHROME_TEST_CHROMEDRIVER_NET_PORT_SERVER_H_ | 74 #endif // CHROME_TEST_CHROMEDRIVER_NET_PORT_SERVER_H_ |
OLD | NEW |