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