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