Index: net/tools/quic/quic_simple_server.cc |
diff --git a/net/tools/quic/quic_simple_server.cc b/net/tools/quic/quic_simple_server.cc |
index 91a000b98e7722b8181b0a12be9da2751bc6af35..deb9f5739948ba52022514a9020849d0a76f44f0 100644 |
--- a/net/tools/quic/quic_simple_server.cc |
+++ b/net/tools/quic/quic_simple_server.cc |
@@ -27,6 +27,7 @@ namespace net { |
namespace { |
const char kSourceAddressTokenSecret[] = "secret"; |
+const size_t kNumSessionsToCreatePerSocketEvent = 16; |
// Allocate some extra space so we can send an error if the client goes over |
// the limit. |
@@ -132,7 +133,7 @@ int QuicSimpleServer::Listen(const IPEndPoint& address) { |
new QuicSimpleServerPacketWriter(socket_.get(), dispatcher_.get()); |
dispatcher_->InitializeWithWriter(writer); |
- StartReading(); |
+ StartReading(true); |
return OK; |
} |
@@ -146,7 +147,11 @@ void QuicSimpleServer::Shutdown() { |
socket_.reset(); |
} |
-void QuicSimpleServer::StartReading() { |
+void QuicSimpleServer::StartReading(bool check_packet_buffer) { |
+ if (check_packet_buffer) { |
Ryan Hamilton
2016/09/06 23:21:46
I wonder if you could simply check synchronous_rea
|
+ dispatcher_->ProcessBufferedChlos(kNumSessionsToCreatePerSocketEvent); |
+ } |
+ |
if (read_pending_) { |
return; |
} |
@@ -154,26 +159,43 @@ void QuicSimpleServer::StartReading() { |
int result = socket_->RecvFrom( |
read_buffer_.get(), read_buffer_->size(), &client_address_, |
- base::Bind(&QuicSimpleServer::OnReadComplete, base::Unretained(this))); |
+ base::Bind(&QuicSimpleServer::OnAsyncReadComplete, |
+ base::Unretained(this))); |
if (result == ERR_IO_PENDING) { |
synchronous_read_count_ = 0; |
+ if (dispatcher_->HasChlosBuffered()) { |
+ // No more packets to read, but still keep processing buffered packets in |
+ // next socket event if there is any. |
+ base::ThreadTaskRunnerHandle::Get()->PostTask( |
+ FROM_HERE, base::Bind(&QuicSimpleServer::StartReading, |
+ weak_factory_.GetWeakPtr(), true)); |
+ } |
return; |
} |
+ // For synchronous read, if server has read enough for current socket event, |
+ // yeild and continue processing in next event. |
if (++synchronous_read_count_ > 32) { |
synchronous_read_count_ = 0; |
// Schedule the processing through the message loop to 1) prevent infinite |
// recursion and 2) avoid blocking the thread for too long. |
+ // Check buffer in next event. |
base::ThreadTaskRunnerHandle::Get()->PostTask( |
FROM_HERE, base::Bind(&QuicSimpleServer::OnReadComplete, |
- weak_factory_.GetWeakPtr(), result)); |
+ weak_factory_.GetWeakPtr(), result, true)); |
} else { |
- OnReadComplete(result); |
+ // No need to check buffer for following reads. |
+ OnReadComplete(result, false); |
} |
} |
-void QuicSimpleServer::OnReadComplete(int result) { |
+void QuicSimpleServer::OnAsyncReadComplete( |
+ int result) { // For asynchronous read, check the buffer again. |
+ OnReadComplete(result, true); |
+} |
+ |
+void QuicSimpleServer::OnReadComplete(int result, bool check_packet_buffer) { |
read_pending_ = false; |
if (result == 0) |
result = ERR_CONNECTION_CLOSED; |
@@ -188,7 +210,7 @@ void QuicSimpleServer::OnReadComplete(int result) { |
helper_->GetClock()->Now(), false); |
dispatcher_->ProcessPacket(server_address_, client_address_, packet); |
- StartReading(); |
+ StartReading(check_packet_buffer); |
} |
} // namespace net |