 Chromium Code Reviews
 Chromium Code Reviews Issue 1009803003:
  Factor out the QUIC socket reading code into a stand alone QuicPacketReader  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master
    
  
    Issue 1009803003:
  Factor out the QUIC socket reading code into a stand alone QuicPacketReader  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master| OLD | NEW | 
|---|---|
| (Empty) | |
| 1 | |
| 
ramant (doing other things)
2015/03/19 19:12:35
nit: extra blank line.
 
Ryan Hamilton
2015/03/19 19:54:49
Done.
 | |
| 2 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 3 // Use of this source code is governed by a BSD-style license that can be | |
| 4 // found in the LICENSE file. | |
| 5 | |
| 6 #include "net/quic/quic_packet_reader.h" | |
| 7 | |
| 8 #include "base/metrics/histogram.h" | |
| 9 #include "net/base/net_errors.h" | |
| 10 | |
| 11 namespace net { | |
| 12 | |
| 13 QuicPacketReader::QuicPacketReader(DatagramClientSocket* socket, | |
| 14 Visitor* visitor, | |
| 15 const BoundNetLog& net_log) | |
| 16 : socket_(socket), | |
| 17 visitor_(visitor), | |
| 18 read_pending_(false), | |
| 19 num_packets_read_(0), | |
| 20 read_buffer_(new IOBufferWithSize(kMaxPacketSize)), | |
| 21 net_log_(net_log), | |
| 22 weak_factory_(this) { | |
| 23 } | |
| 24 | |
| 25 QuicPacketReader::~QuicPacketReader() { | |
| 26 } | |
| 27 | |
| 28 void QuicPacketReader::StartReading() { | |
| 29 if (read_pending_) { | |
| 30 return; | |
| 31 } | |
| 32 DCHECK(socket_); | |
| 33 read_pending_ = true; | |
| 34 int rv = socket_->Read(read_buffer_.get(), read_buffer_->size(), | |
| 35 base::Bind(&QuicPacketReader::OnReadComplete, | |
| 36 weak_factory_.GetWeakPtr())); | |
| 37 UMA_HISTOGRAM_BOOLEAN("Net.QuicSession.AsyncRead", rv == ERR_IO_PENDING); | |
| 38 if (rv == ERR_IO_PENDING) { | |
| 39 num_packets_read_ = 0; | |
| 40 return; | |
| 41 } | |
| 42 | |
| 43 if (++num_packets_read_ > 32) { | |
| 44 num_packets_read_ = 0; | |
| 45 // Data was read, process it. | |
| 46 // Schedule the work through the message loop to 1) prevent infinite | |
| 47 // recursion and 2) avoid blocking the thread for too long. | |
| 48 base::MessageLoop::current()->PostTask( | |
| 49 FROM_HERE, base::Bind(&QuicPacketReader::OnReadComplete, | |
| 50 weak_factory_.GetWeakPtr(), rv)); | |
| 51 } else { | |
| 52 OnReadComplete(rv); | |
| 53 } | |
| 54 } | |
| 55 | |
| 56 void QuicPacketReader::OnReadComplete(int result) { | |
| 57 read_pending_ = false; | |
| 58 if (result == 0) | |
| 
ramant (doing other things)
2015/03/19 19:12:35
overly nit: this was there in the old code. At lin
 
Ryan Hamilton
2015/03/19 19:54:49
Good catch. I'll fix line 29 to use chrome style s
 | |
| 59 result = ERR_CONNECTION_CLOSED; | |
| 60 | |
| 61 if (result < 0) { | |
| 62 visitor_->OnReadError(result); | |
| 63 return; | |
| 64 } | |
| 65 | |
| 66 QuicEncryptedPacket packet(read_buffer_->data(), result); | |
| 67 IPEndPoint local_address; | |
| 68 IPEndPoint peer_address; | |
| 69 socket_->GetLocalAddress(&local_address); | |
| 70 socket_->GetPeerAddress(&peer_address); | |
| 71 // ProcessUdpPacket might result in |this| being deleted, so we | |
| 
ramant (doing other things)
2015/03/19 19:12:35
nit: Should we change "ProcessUdpPacket" to "OnPac
 
Ryan Hamilton
2015/03/19 19:54:49
Thanks, again. I should have just removed that com
 | |
| 72 // use a weak pointer to be safe. | |
| 73 if (!visitor_->OnPacket(packet, local_address, peer_address)) | |
| 74 return; | |
| 75 | |
| 76 StartReading(); | |
| 77 } | |
| 78 | |
| 79 } // namespace net | |
| OLD | NEW |