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 #include "net/tools/quic/test_tools/server_thread.h" | 5 #include "net/tools/quic/test_tools/server_thread.h" |
6 | 6 |
7 #include "net/tools/quic/test_tools/quic_server_peer.h" | 7 #include "net/tools/quic/test_tools/quic_server_peer.h" |
8 | 8 |
9 namespace net { | 9 namespace net { |
10 namespace tools { | 10 namespace tools { |
11 namespace test { | 11 namespace test { |
12 | 12 |
13 ServerThread::ServerThread(IPEndPoint address, | 13 ServerThread::ServerThread(IPEndPoint address, |
14 const QuicConfig& config, | 14 const QuicConfig& config, |
15 const QuicVersionVector& supported_versions, | 15 const QuicVersionVector& supported_versions, |
16 bool strike_register_no_startup_period) | 16 bool strike_register_no_startup_period) |
17 : SimpleThread("server_thread"), | 17 : SimpleThread("server_thread"), |
18 listening_(true, false), | |
19 confirmed_(true, false), | 18 confirmed_(true, false), |
| 19 pause_(true, false), |
| 20 paused_(true, false), |
| 21 resume_(true, false), |
20 quit_(true, false), | 22 quit_(true, false), |
21 server_(config, supported_versions), | 23 server_(config, supported_versions), |
22 address_(address), | 24 address_(address), |
23 port_(0) { | 25 port_(0), |
| 26 initialized_(false) { |
24 if (strike_register_no_startup_period) { | 27 if (strike_register_no_startup_period) { |
25 server_.SetStrikeRegisterNoStartupPeriod(); | 28 server_.SetStrikeRegisterNoStartupPeriod(); |
26 } | 29 } |
27 } | 30 } |
28 | 31 |
29 ServerThread::~ServerThread() { | 32 ServerThread::~ServerThread() {} |
30 } | |
31 | 33 |
32 void ServerThread::Run() { | 34 void ServerThread::Initialize() { |
| 35 if (initialized_) { |
| 36 return; |
| 37 } |
| 38 |
33 server_.Listen(address_); | 39 server_.Listen(address_); |
34 | 40 |
35 port_lock_.Acquire(); | 41 port_lock_.Acquire(); |
36 port_ = server_.port(); | 42 port_ = server_.port(); |
37 port_lock_.Release(); | 43 port_lock_.Release(); |
38 | 44 |
39 listening_.Signal(); | 45 initialized_ = true; |
| 46 } |
| 47 |
| 48 void ServerThread::Run() { |
| 49 if (!initialized_) { |
| 50 Initialize(); |
| 51 } |
| 52 |
40 while (!quit_.IsSignaled()) { | 53 while (!quit_.IsSignaled()) { |
41 event_loop_mu_.Acquire(); | 54 if (pause_.IsSignaled() && !resume_.IsSignaled()) { |
| 55 paused_.Signal(); |
| 56 resume_.Wait(); |
| 57 } |
42 server_.WaitForEvents(); | 58 server_.WaitForEvents(); |
43 MaybeNotifyOfHandshakeConfirmation(); | 59 MaybeNotifyOfHandshakeConfirmation(); |
44 event_loop_mu_.Release(); | |
45 } | 60 } |
46 | 61 |
47 server_.Shutdown(); | 62 server_.Shutdown(); |
48 } | 63 } |
49 | 64 |
50 int ServerThread::GetPort() { | 65 int ServerThread::GetPort() { |
51 port_lock_.Acquire(); | 66 port_lock_.Acquire(); |
52 int rc = port_; | 67 int rc = port_; |
53 port_lock_.Release(); | 68 port_lock_.Release(); |
54 return rc; | 69 return rc; |
55 } | |
56 | |
57 void ServerThread::WaitForServerStartup() { | |
58 listening_.Wait(); | |
59 } | 70 } |
60 | 71 |
61 void ServerThread::WaitForCryptoHandshakeConfirmed() { | 72 void ServerThread::WaitForCryptoHandshakeConfirmed() { |
62 confirmed_.Wait(); | 73 confirmed_.Wait(); |
63 } | 74 } |
64 | 75 |
65 void ServerThread::Pause() { | 76 void ServerThread::Pause() { |
66 event_loop_mu_.Acquire(); | 77 DCHECK(!pause_.IsSignaled()); |
| 78 pause_.Signal(); |
| 79 paused_.Wait(); |
67 } | 80 } |
68 | 81 |
69 void ServerThread::Resume() { | 82 void ServerThread::Resume() { |
70 event_loop_mu_.AssertAcquired(); // Checks the calling thread only! | 83 DCHECK(!resume_.IsSignaled()); |
71 event_loop_mu_.Release(); | 84 DCHECK(pause_.IsSignaled()); |
| 85 resume_.Signal(); |
72 } | 86 } |
73 | 87 |
74 void ServerThread::Quit() { | 88 void ServerThread::Quit() { |
| 89 if (pause_.IsSignaled() && !resume_.IsSignaled()) { |
| 90 resume_.Signal(); |
| 91 } |
75 quit_.Signal(); | 92 quit_.Signal(); |
76 } | 93 } |
77 | 94 |
78 void ServerThread::MaybeNotifyOfHandshakeConfirmation() { | 95 void ServerThread::MaybeNotifyOfHandshakeConfirmation() { |
79 if (confirmed_.IsSignaled()) { | 96 if (confirmed_.IsSignaled()) { |
80 // Only notify once. | 97 // Only notify once. |
81 return; | 98 return; |
82 } | 99 } |
83 QuicDispatcher* dispatcher = QuicServerPeer::GetDispatcher(server()); | 100 QuicDispatcher* dispatcher = QuicServerPeer::GetDispatcher(server()); |
84 if (dispatcher->session_map().empty()) { | 101 if (dispatcher->session_map().empty()) { |
85 // Wait for a session to be created. | 102 // Wait for a session to be created. |
86 return; | 103 return; |
87 } | 104 } |
88 QuicSession* session = dispatcher->session_map().begin()->second; | 105 QuicSession* session = dispatcher->session_map().begin()->second; |
89 if (session->IsCryptoHandshakeConfirmed()) { | 106 if (session->IsCryptoHandshakeConfirmed()) { |
90 confirmed_.Signal(); | 107 confirmed_.Signal(); |
91 } | 108 } |
92 } | 109 } |
93 | 110 |
94 } // namespace test | 111 } // namespace test |
95 } // namespace tools | 112 } // namespace tools |
96 } // namespace net | 113 } // namespace net |
OLD | NEW |