1 // Copyright (c) 2012 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.
5 #include "remoting/base/socket_reader.h"
7 #include "base/compiler_specific.h"
8 #include "base/location.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "net/base/io_buffer.h"
12 #include "net/base/net_errors.h"
13 #include "net/socket/socket.h"
18 int kReadBufferSize
= 4096;
21 SocketReader::SocketReader()
26 SocketReader::~SocketReader() {
29 void SocketReader::Init(net::Socket
* socket
,
30 ReadResultCallback read_result_callback
) {
35 read_result_callback_
= read_result_callback
;
39 void SocketReader::DoRead() {
41 read_buffer_
= new net::IOBuffer(kReadBufferSize
);
42 int result
= socket_
->Read(
45 base::Bind(&SocketReader::OnRead
, weak_factory_
.GetWeakPtr()));
46 HandleReadResult(result
);
52 void SocketReader::OnRead(int result
) {
53 HandleReadResult(result
);
58 void SocketReader::HandleReadResult(int result
) {
59 if (result
!= net::ERR_IO_PENDING
) {
62 base::ThreadTaskRunnerHandle::Get()->PostTask(
64 base::Bind(&SocketReader::CallCallback
, weak_factory_
.GetWeakPtr(),
65 read_buffer_
, result
));
69 void SocketReader::CallCallback(scoped_refptr
<net::IOBuffer
> data
, int result
) {
70 read_result_callback_
.Run(data
, result
);
73 } // namespace remoting