OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/quic_simple_server.h" | 5 #include "net/tools/quic/quic_simple_server.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
11 #include "base/threading/thread_task_runner_handle.h" | 11 #include "base/threading/thread_task_runner_handle.h" |
12 #include "net/base/ip_endpoint.h" | 12 #include "net/base/ip_endpoint.h" |
13 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
14 #include "net/quic/core/crypto/crypto_handshake.h" | 14 #include "net/quic/core/crypto/crypto_handshake.h" |
15 #include "net/quic/core/crypto/quic_random.h" | 15 #include "net/quic/core/crypto/quic_random.h" |
16 #include "net/quic/core/quic_crypto_stream.h" | 16 #include "net/quic/core/quic_crypto_stream.h" |
17 #include "net/quic/core/quic_data_reader.h" | 17 #include "net/quic/core/quic_data_reader.h" |
18 #include "net/quic/core/quic_protocol.h" | 18 #include "net/quic/core/quic_protocol.h" |
19 #include "net/tools/quic/quic_simple_dispatcher.h" | 19 #include "net/tools/quic/quic_simple_dispatcher.h" |
20 #include "net/tools/quic/quic_simple_per_connection_packet_writer.h" | 20 #include "net/tools/quic/quic_simple_per_connection_packet_writer.h" |
21 #include "net/tools/quic/quic_simple_server_packet_writer.h" | 21 #include "net/tools/quic/quic_simple_server_packet_writer.h" |
22 #include "net/tools/quic/quic_simple_server_session_helper.h" | 22 #include "net/tools/quic/quic_simple_server_session_helper.h" |
23 #include "net/udp/udp_server_socket.h" | 23 #include "net/udp/udp_server_socket.h" |
24 | 24 |
25 namespace net { | 25 namespace net { |
26 | 26 |
27 namespace { | 27 namespace { |
28 | 28 |
29 const char kSourceAddressTokenSecret[] = "secret"; | 29 const char kSourceAddressTokenSecret[] = "secret"; |
| 30 const size_t kNumSessionsToCreatePerSocketEvent = 16; |
30 | 31 |
31 // Allocate some extra space so we can send an error if the client goes over | 32 // Allocate some extra space so we can send an error if the client goes over |
32 // the limit. | 33 // the limit. |
33 const int kReadBufferSize = 2 * kMaxPacketSize; | 34 const int kReadBufferSize = 2 * kMaxPacketSize; |
34 | 35 |
35 } // namespace | 36 } // namespace |
36 | 37 |
37 QuicSimpleServer::QuicSimpleServer( | 38 QuicSimpleServer::QuicSimpleServer( |
38 std::unique_ptr<ProofSource> proof_source, | 39 std::unique_ptr<ProofSource> proof_source, |
39 const QuicConfig& config, | 40 const QuicConfig& config, |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 void QuicSimpleServer::Shutdown() { | 141 void QuicSimpleServer::Shutdown() { |
141 // Before we shut down the epoll server, give all active sessions a chance to | 142 // Before we shut down the epoll server, give all active sessions a chance to |
142 // notify clients that they're closing. | 143 // notify clients that they're closing. |
143 dispatcher_->Shutdown(); | 144 dispatcher_->Shutdown(); |
144 | 145 |
145 socket_->Close(); | 146 socket_->Close(); |
146 socket_.reset(); | 147 socket_.reset(); |
147 } | 148 } |
148 | 149 |
149 void QuicSimpleServer::StartReading() { | 150 void QuicSimpleServer::StartReading() { |
| 151 if (synchronous_read_count_ == 0) { |
| 152 // Only process buffered packets once per message loop. |
| 153 dispatcher_->ProcessBufferedChlos(kNumSessionsToCreatePerSocketEvent); |
| 154 } |
| 155 |
150 if (read_pending_) { | 156 if (read_pending_) { |
151 return; | 157 return; |
152 } | 158 } |
153 read_pending_ = true; | 159 read_pending_ = true; |
154 | 160 |
155 int result = socket_->RecvFrom( | 161 int result = socket_->RecvFrom( |
156 read_buffer_.get(), read_buffer_->size(), &client_address_, | 162 read_buffer_.get(), read_buffer_->size(), &client_address_, |
157 base::Bind(&QuicSimpleServer::OnReadComplete, base::Unretained(this))); | 163 base::Bind(&QuicSimpleServer::OnReadComplete, base::Unretained(this))); |
158 | 164 |
159 if (result == ERR_IO_PENDING) { | 165 if (result == ERR_IO_PENDING) { |
160 synchronous_read_count_ = 0; | 166 synchronous_read_count_ = 0; |
| 167 if (dispatcher_->HasChlosBuffered()) { |
| 168 // No more packets to read, so yield before processing buffered packets. |
| 169 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 170 FROM_HERE, base::Bind(&QuicSimpleServer::StartReading, |
| 171 weak_factory_.GetWeakPtr())); |
| 172 } |
161 return; | 173 return; |
162 } | 174 } |
163 | 175 |
164 if (++synchronous_read_count_ > 32) { | 176 if (++synchronous_read_count_ > 32) { |
165 synchronous_read_count_ = 0; | 177 synchronous_read_count_ = 0; |
166 // Schedule the processing through the message loop to 1) prevent infinite | 178 // Schedule the processing through the message loop to 1) prevent infinite |
167 // recursion and 2) avoid blocking the thread for too long. | 179 // recursion and 2) avoid blocking the thread for too long. |
168 base::ThreadTaskRunnerHandle::Get()->PostTask( | 180 base::ThreadTaskRunnerHandle::Get()->PostTask( |
169 FROM_HERE, base::Bind(&QuicSimpleServer::OnReadComplete, | 181 FROM_HERE, base::Bind(&QuicSimpleServer::OnReadComplete, |
170 weak_factory_.GetWeakPtr(), result)); | 182 weak_factory_.GetWeakPtr(), result)); |
(...skipping 14 matching lines...) Expand all Loading... |
185 } | 197 } |
186 | 198 |
187 QuicReceivedPacket packet(read_buffer_->data(), result, | 199 QuicReceivedPacket packet(read_buffer_->data(), result, |
188 helper_->GetClock()->Now(), false); | 200 helper_->GetClock()->Now(), false); |
189 dispatcher_->ProcessPacket(server_address_, client_address_, packet); | 201 dispatcher_->ProcessPacket(server_address_, client_address_, packet); |
190 | 202 |
191 StartReading(); | 203 StartReading(); |
192 } | 204 } |
193 | 205 |
194 } // namespace net | 206 } // namespace net |
OLD | NEW |