15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "remoting/base/socket_reader.h" 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/compiler_specific.h" 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/location.h" 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/single_thread_task_runner.h" 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/thread_task_runner_handle.h" 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/io_buffer.h" 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_errors.h" 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/socket/socket.h" 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace remoting { 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace { 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int kReadBufferSize = 4096; 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)SocketReader::SocketReader() 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) : socket_(NULL), 23c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) weak_factory_(this) { 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)SocketReader::~SocketReader() { 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void SocketReader::Init(net::Socket* socket, 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ReadResultCallback read_result_callback) { 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(socket); 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(!socket_); 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) socket_ = socket; 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) read_result_callback_ = read_result_callback; 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DoRead(); 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void SocketReader::DoRead() { 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) while (true) { 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) read_buffer_ = new net::IOBuffer(kReadBufferSize); 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int result = socket_->Read( 43868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) read_buffer_.get(), 44868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) kReadBufferSize, 45868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) base::Bind(&SocketReader::OnRead, weak_factory_.GetWeakPtr())); 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) HandleReadResult(result); 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (result <= 0) 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) break; 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void SocketReader::OnRead(int result) { 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) HandleReadResult(result); 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (result > 0) 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DoRead(); 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void SocketReader::HandleReadResult(int result) { 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (result != net::ERR_IO_PENDING) { 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (result < 0) 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) read_buffer_ = NULL; 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::ThreadTaskRunnerHandle::Get()->PostTask( 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FROM_HERE, 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::Bind(&SocketReader::CallCallback, weak_factory_.GetWeakPtr(), 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) read_buffer_, result)); 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void SocketReader::CallCallback(scoped_refptr<net::IOBuffer> data, int result) { 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) read_result_callback_.Run(data, result); 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace remoting 74