OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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 NET_TOOLS_QUIC_TEST_TOOLS_SERVER_THREAD_H_ | 5 #ifndef NET_TOOLS_QUIC_TEST_TOOLS_SERVER_THREAD_H_ |
6 #define NET_TOOLS_QUIC_TEST_TOOLS_SERVER_THREAD_H_ | 6 #define NET_TOOLS_QUIC_TEST_TOOLS_SERVER_THREAD_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/synchronization/lock.h" |
11 #include "base/threading/simple_thread.h" | 12 #include "base/threading/simple_thread.h" |
12 #include "net/base/ip_endpoint.h" | 13 #include "net/base/ip_endpoint.h" |
13 #include "net/quic/core/quic_config.h" | 14 #include "net/quic/core/quic_config.h" |
14 #include "net/tools/quic/quic_server.h" | 15 #include "net/tools/quic/quic_server.h" |
15 | 16 |
16 namespace net { | 17 namespace net { |
17 namespace test { | 18 namespace test { |
18 | 19 |
19 // Simple wrapper class to run QuicServer in a dedicated thread. | 20 // Simple wrapper class to run QuicServer in a dedicated thread. |
20 class ServerThread : public base::SimpleThread { | 21 class ServerThread : public base::SimpleThread { |
21 public: | 22 public: |
22 ServerThread(QuicServer* server, | 23 ServerThread(QuicServer* server, |
23 const IPEndPoint& address, | 24 const IPEndPoint& address, |
24 bool strike_register_no_startup_period); | 25 bool strike_register_no_startup_period); |
25 | 26 |
26 ~ServerThread() override; | 27 ~ServerThread() override; |
27 | 28 |
28 // Prepares the server, but does not start accepting connections. Useful for | 29 // Prepares the server, but does not start accepting connections. Useful for |
29 // injecting mocks. | 30 // injecting mocks. |
30 void Initialize(); | 31 void Initialize(); |
31 | 32 |
32 // Runs the event loop. Will initialize if necessary. | 33 // Runs the event loop. Will initialize if necessary. |
33 void Run() override; | 34 void Run() override; |
34 | 35 |
| 36 // Schedules the given action for execution in the event loop. |
| 37 void Schedule(std::function<void()> action); |
| 38 |
35 // Waits for the handshake to be confirmed for the first session created. | 39 // Waits for the handshake to be confirmed for the first session created. |
36 void WaitForCryptoHandshakeConfirmed(); | 40 void WaitForCryptoHandshakeConfirmed(); |
37 | 41 |
38 // Pauses execution of the server until Resume() is called. May only be | 42 // Pauses execution of the server until Resume() is called. May only be |
39 // called once. | 43 // called once. |
40 void Pause(); | 44 void Pause(); |
41 | 45 |
42 // Resumes execution of the server after Pause() has been called. May only | 46 // Resumes execution of the server after Pause() has been called. May only |
43 // be called once. | 47 // be called once. |
44 void Resume(); | 48 void Resume(); |
45 | 49 |
46 // Stops the server from executing and shuts it down, destroying all | 50 // Stops the server from executing and shuts it down, destroying all |
47 // server objects. | 51 // server objects. |
48 void Quit(); | 52 void Quit(); |
49 | 53 |
50 // Returns the underlying server. Care must be taken to avoid data races | 54 // Returns the underlying server. Care must be taken to avoid data races |
51 // when accessing the server. It is always safe to access the server | 55 // when accessing the server. It is always safe to access the server |
52 // after calling Pause() and before calling Resume(). | 56 // after calling Pause() and before calling Resume(). |
53 QuicServer* server() { return server_.get(); } | 57 QuicServer* server() { return server_.get(); } |
54 | 58 |
55 // Returns the port that the server is listening on. | 59 // Returns the port that the server is listening on. |
56 int GetPort(); | 60 int GetPort(); |
57 | 61 |
58 private: | 62 private: |
59 void MaybeNotifyOfHandshakeConfirmation(); | 63 void MaybeNotifyOfHandshakeConfirmation(); |
| 64 void ExecuteScheduledActions(); |
60 | 65 |
61 base::WaitableEvent confirmed_; // Notified when the first handshake is | 66 base::WaitableEvent confirmed_; // Notified when the first handshake is |
62 // confirmed. | 67 // confirmed. |
63 base::WaitableEvent pause_; // Notified when the server should pause. | 68 base::WaitableEvent pause_; // Notified when the server should pause. |
64 base::WaitableEvent paused_; // Notitied when the server has paused | 69 base::WaitableEvent paused_; // Notitied when the server has paused |
65 base::WaitableEvent resume_; // Notified when the server should resume. | 70 base::WaitableEvent resume_; // Notified when the server should resume. |
66 base::WaitableEvent quit_; // Notified when the server should quit. | 71 base::WaitableEvent quit_; // Notified when the server should quit. |
67 | 72 |
68 std::unique_ptr<QuicServer> server_; | 73 std::unique_ptr<QuicServer> server_; |
69 IPEndPoint address_; | 74 IPEndPoint address_; |
70 base::Lock port_lock_; | 75 base::Lock port_lock_; |
71 int port_; | 76 int port_; |
72 | 77 |
73 bool initialized_; | 78 bool initialized_; |
74 | 79 |
| 80 base::Lock scheduled_actions_lock_; |
| 81 std::deque<std::function<void()>> scheduled_actions_; |
| 82 |
75 DISALLOW_COPY_AND_ASSIGN(ServerThread); | 83 DISALLOW_COPY_AND_ASSIGN(ServerThread); |
76 }; | 84 }; |
77 | 85 |
78 } // namespace test | 86 } // namespace test |
79 } // namespace net | 87 } // namespace net |
80 | 88 |
81 #endif // NET_TOOLS_QUIC_TEST_TOOLS_SERVER_THREAD_H_ | 89 #endif // NET_TOOLS_QUIC_TEST_TOOLS_SERVER_THREAD_H_ |
OLD | NEW |