OLD | NEW |
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 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/quic_test_server.h" | 5 #include "net/tools/quic/test_tools/quic_test_server.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "base/synchronization/lock.h" |
9 #include "base/thread_task_runner_handle.h" | 10 #include "base/thread_task_runner_handle.h" |
10 #include "net/base/ip_endpoint.h" | 11 #include "net/base/ip_endpoint.h" |
11 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
12 #include "net/quic/crypto/crypto_handshake.h" | 13 #include "net/quic/crypto/crypto_handshake.h" |
13 #include "net/quic/crypto/quic_crypto_server_config.h" | 14 #include "net/quic/crypto/quic_crypto_server_config.h" |
14 #include "net/quic/crypto/quic_random.h" | 15 #include "net/quic/crypto/quic_random.h" |
15 #include "net/quic/quic_config.h" | 16 #include "net/quic/quic_config.h" |
16 #include "net/quic/quic_connection.h" | 17 #include "net/quic/quic_connection.h" |
17 #include "net/quic/quic_connection_helper.h" | 18 #include "net/quic/quic_connection_helper.h" |
18 #include "net/quic/quic_packet_writer.h" | 19 #include "net/quic/quic_packet_writer.h" |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 const QuicVersionVector& versions, | 70 const QuicVersionVector& versions, |
70 PacketWriterFactory* factory, | 71 PacketWriterFactory* factory, |
71 QuicConnectionHelperInterface* helper) | 72 QuicConnectionHelperInterface* helper) |
72 : QuicDispatcher(config, crypto_config, versions, factory, helper), | 73 : QuicDispatcher(config, crypto_config, versions, factory, helper), |
73 session_factory_(nullptr), | 74 session_factory_(nullptr), |
74 stream_factory_(nullptr), | 75 stream_factory_(nullptr), |
75 crypto_stream_factory_(nullptr) {} | 76 crypto_stream_factory_(nullptr) {} |
76 | 77 |
77 QuicServerSession* CreateQuicSession(QuicConnectionId id, | 78 QuicServerSession* CreateQuicSession(QuicConnectionId id, |
78 const IPEndPoint& client) override { | 79 const IPEndPoint& client) override { |
| 80 base::AutoLock lock(factory_lock_); |
79 if (session_factory_ == nullptr && stream_factory_ == nullptr && | 81 if (session_factory_ == nullptr && stream_factory_ == nullptr && |
80 crypto_stream_factory_ == nullptr) { | 82 crypto_stream_factory_ == nullptr) { |
81 return QuicDispatcher::CreateQuicSession(id, client); | 83 return QuicDispatcher::CreateQuicSession(id, client); |
82 } | 84 } |
83 QuicConnection* connection = new QuicConnection( | 85 QuicConnection* connection = new QuicConnection( |
84 id, client, helper(), connection_writer_factory(), | 86 id, client, helper(), connection_writer_factory(), |
85 /* owns_writer= */ true, Perspective::IS_SERVER, supported_versions()); | 87 /* owns_writer= */ true, Perspective::IS_SERVER, supported_versions()); |
86 | 88 |
87 QuicServerSession* session = nullptr; | 89 QuicServerSession* session = nullptr; |
88 if (stream_factory_ != nullptr || crypto_stream_factory_ != nullptr) { | 90 if (stream_factory_ != nullptr || crypto_stream_factory_ != nullptr) { |
89 session = | 91 session = |
90 new CustomStreamSession(config(), connection, this, crypto_config(), | 92 new CustomStreamSession(config(), connection, this, crypto_config(), |
91 stream_factory_, crypto_stream_factory_); | 93 stream_factory_, crypto_stream_factory_); |
92 } else { | 94 } else { |
93 session = session_factory_->CreateSession(config(), connection, this, | 95 session = session_factory_->CreateSession(config(), connection, this, |
94 crypto_config()); | 96 crypto_config()); |
95 } | 97 } |
96 session->Initialize(); | 98 session->Initialize(); |
97 return session; | 99 return session; |
98 } | 100 } |
99 | 101 |
100 void set_session_factory(QuicTestServer::SessionFactory* factory) { | 102 void SetSessionFactory(QuicTestServer::SessionFactory* factory) { |
| 103 base::AutoLock lock(factory_lock_); |
101 DCHECK(session_factory_ == nullptr); | 104 DCHECK(session_factory_ == nullptr); |
102 DCHECK(stream_factory_ == nullptr); | 105 DCHECK(stream_factory_ == nullptr); |
103 DCHECK(crypto_stream_factory_ == nullptr); | 106 DCHECK(crypto_stream_factory_ == nullptr); |
104 session_factory_ = factory; | 107 session_factory_ = factory; |
105 } | 108 } |
106 | 109 |
107 void set_stream_factory(QuicTestServer::StreamFactory* factory) { | 110 void SetStreamFactory(QuicTestServer::StreamFactory* factory) { |
| 111 base::AutoLock lock(factory_lock_); |
108 DCHECK(session_factory_ == nullptr); | 112 DCHECK(session_factory_ == nullptr); |
109 DCHECK(stream_factory_ == nullptr); | 113 DCHECK(stream_factory_ == nullptr); |
110 stream_factory_ = factory; | 114 stream_factory_ = factory; |
111 } | 115 } |
112 | 116 |
113 void set_crypto_stream_factory(QuicTestServer::CryptoStreamFactory* factory) { | 117 void SetCryptoStreamFactory(QuicTestServer::CryptoStreamFactory* factory) { |
| 118 base::AutoLock lock(factory_lock_); |
114 DCHECK(session_factory_ == nullptr); | 119 DCHECK(session_factory_ == nullptr); |
115 DCHECK(crypto_stream_factory_ == nullptr); | 120 DCHECK(crypto_stream_factory_ == nullptr); |
116 crypto_stream_factory_ = factory; | 121 crypto_stream_factory_ = factory; |
117 } | 122 } |
118 | 123 |
119 QuicTestServer::SessionFactory* session_factory() { return session_factory_; } | |
120 | |
121 QuicTestServer::StreamFactory* stream_factory() { return stream_factory_; } | |
122 | |
123 private: | 124 private: |
| 125 base::Lock factory_lock_; |
124 QuicTestServer::SessionFactory* session_factory_; // Not owned. | 126 QuicTestServer::SessionFactory* session_factory_; // Not owned. |
125 QuicTestServer::StreamFactory* stream_factory_; // Not owned. | 127 QuicTestServer::StreamFactory* stream_factory_; // Not owned. |
126 QuicTestServer::CryptoStreamFactory* crypto_stream_factory_; // Not owned. | 128 QuicTestServer::CryptoStreamFactory* crypto_stream_factory_; // Not owned. |
127 }; | 129 }; |
128 | 130 |
129 QuicTestServer::QuicTestServer(ProofSource* proof_source) | 131 QuicTestServer::QuicTestServer(ProofSource* proof_source) |
130 : QuicServer(proof_source) {} | 132 : QuicServer(proof_source) {} |
131 | 133 |
132 QuicTestServer::QuicTestServer(ProofSource* proof_source, | 134 QuicTestServer::QuicTestServer(ProofSource* proof_source, |
133 const QuicConfig& config, | 135 const QuicConfig& config, |
134 const QuicVersionVector& supported_versions) | 136 const QuicVersionVector& supported_versions) |
135 : QuicServer(proof_source, config, supported_versions) {} | 137 : QuicServer(proof_source, config, supported_versions) {} |
136 | 138 |
137 QuicDispatcher* QuicTestServer::CreateQuicDispatcher() { | 139 QuicDispatcher* QuicTestServer::CreateQuicDispatcher() { |
138 return new QuicTestDispatcher( | 140 return new QuicTestDispatcher( |
139 config(), &crypto_config(), supported_versions(), | 141 config(), &crypto_config(), supported_versions(), |
140 new QuicDispatcher::DefaultPacketWriterFactory(), | 142 new QuicDispatcher::DefaultPacketWriterFactory(), |
141 new QuicEpollConnectionHelper(epoll_server())); | 143 new QuicEpollConnectionHelper(epoll_server())); |
142 } | 144 } |
143 | 145 |
144 void QuicTestServer::SetSessionFactory(SessionFactory* factory) { | 146 void QuicTestServer::SetSessionFactory(SessionFactory* factory) { |
145 DCHECK(dispatcher()); | 147 DCHECK(dispatcher()); |
146 static_cast<QuicTestDispatcher*>(dispatcher())->set_session_factory(factory); | 148 static_cast<QuicTestDispatcher*>(dispatcher())->SetSessionFactory(factory); |
147 } | 149 } |
148 | 150 |
149 void QuicTestServer::SetSpdyStreamFactory(StreamFactory* factory) { | 151 void QuicTestServer::SetSpdyStreamFactory(StreamFactory* factory) { |
150 static_cast<QuicTestDispatcher*>(dispatcher())->set_stream_factory(factory); | 152 static_cast<QuicTestDispatcher*>(dispatcher())->SetStreamFactory(factory); |
151 } | 153 } |
152 | 154 |
153 void QuicTestServer::SetCryptoStreamFactory(CryptoStreamFactory* factory) { | 155 void QuicTestServer::SetCryptoStreamFactory(CryptoStreamFactory* factory) { |
154 static_cast<QuicTestDispatcher*>(dispatcher()) | 156 static_cast<QuicTestDispatcher*>(dispatcher())->SetCryptoStreamFactory( |
155 ->set_crypto_stream_factory(factory); | 157 factory); |
156 } | 158 } |
157 | 159 |
158 /////////////////////////// TEST SESSIONS /////////////////////////////// | 160 /////////////////////////// TEST SESSIONS /////////////////////////////// |
159 | 161 |
160 ImmediateGoAwaySession::ImmediateGoAwaySession( | 162 ImmediateGoAwaySession::ImmediateGoAwaySession( |
161 const QuicConfig& config, | 163 const QuicConfig& config, |
162 QuicConnection* connection, | 164 QuicConnection* connection, |
163 QuicServerSessionVisitor* visitor, | 165 QuicServerSessionVisitor* visitor, |
164 const QuicCryptoServerConfig* crypto_config) | 166 const QuicCryptoServerConfig* crypto_config) |
165 : QuicServerSession(config, connection, visitor, crypto_config) { | 167 : QuicServerSession(config, connection, visitor, crypto_config) { |
166 SendGoAway(QUIC_PEER_GOING_AWAY, ""); | 168 SendGoAway(QUIC_PEER_GOING_AWAY, ""); |
167 } | 169 } |
168 | 170 |
169 } // namespace test | 171 } // namespace test |
170 } // namespace tools | 172 } // namespace tools |
171 } // namespace net | 173 } // namespace net |
OLD | NEW |