| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "remoting/protocol/socket_reader_base.h" | |
| 6 | |
| 7 #include "net/base/completion_callback.h" | |
| 8 #include "net/base/io_buffer.h" | |
| 9 #include "net/base/net_errors.h" | |
| 10 #include "net/socket/socket.h" | |
| 11 | |
| 12 namespace remoting { | |
| 13 | |
| 14 namespace { | |
| 15 int kReadBufferSize = 4096; | |
| 16 } // namespace | |
| 17 | |
| 18 SocketReaderBase::SocketReaderBase() | |
| 19 : socket_(NULL), | |
| 20 closed_(false) { | |
| 21 } | |
| 22 | |
| 23 SocketReaderBase::~SocketReaderBase() { } | |
| 24 | |
| 25 void SocketReaderBase::Init(net::Socket* socket) { | |
| 26 DCHECK(socket); | |
| 27 socket_ = socket; | |
| 28 DoRead(); | |
| 29 } | |
| 30 | |
| 31 void SocketReaderBase::DoRead() { | |
| 32 while (true) { | |
| 33 read_buffer_ = new net::IOBuffer(kReadBufferSize); | |
| 34 int result = socket_->Read( | |
| 35 read_buffer_, kReadBufferSize, base::Bind(&SocketReaderBase::OnRead, | |
| 36 base::Unretained(this))); | |
| 37 HandleReadResult(result); | |
| 38 if (result < 0) | |
| 39 break; | |
| 40 } | |
| 41 } | |
| 42 | |
| 43 void SocketReaderBase::OnRead(int result) { | |
| 44 if (!closed_) { | |
| 45 HandleReadResult(result); | |
| 46 DoRead(); | |
| 47 } | |
| 48 } | |
| 49 | |
| 50 void SocketReaderBase::HandleReadResult(int result) { | |
| 51 if (result > 0) { | |
| 52 OnDataReceived(read_buffer_, result); | |
| 53 } else { | |
| 54 if (result == net::ERR_CONNECTION_CLOSED) { | |
| 55 closed_ = true; | |
| 56 } else if (result != net::ERR_IO_PENDING) { | |
| 57 LOG(ERROR) << "Read() returned error " << result; | |
| 58 } | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 } // namespace remoting | |
| OLD | NEW |