OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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/host/gnubby_connection.h" |
| 6 |
| 7 #include "net/base/net_errors.h" |
| 8 #include "remoting/base/logging.h" |
| 9 #include "remoting/host/gnubby_auth_handler.h" |
| 10 |
| 11 namespace remoting { |
| 12 |
| 13 GnubbyConnection::GnubbyConnection( |
| 14 GnubbyAuthHandler* auth_handler, |
| 15 int connection_id, |
| 16 net::StreamSocket* socket) |
| 17 : auth_handler_(auth_handler), |
| 18 connection_id_(connection_id), |
| 19 socket_(socket) { |
| 20 DCHECK(auth_handler_); |
| 21 DCHECK(socket_); |
| 22 |
| 23 in_buffer_ = new net::IOBufferWithSize(512); |
| 24 } |
| 25 |
| 26 GnubbyConnection::~GnubbyConnection() {} |
| 27 |
| 28 void GnubbyConnection::Read() { |
| 29 DCHECK(CalledOnValidThread()); |
| 30 |
| 31 int result = socket_->Read( |
| 32 in_buffer_.get(), |
| 33 in_buffer_->size(), |
| 34 base::Bind(&GnubbyConnection::OnRead, base::Unretained(this))); |
| 35 if (result != net::ERR_IO_PENDING) { |
| 36 OnRead(result); |
| 37 } |
| 38 } |
| 39 |
| 40 void GnubbyConnection::Write(const std::string& data) { |
| 41 DCHECK(CalledOnValidThread()); |
| 42 |
| 43 scoped_refptr<net::StringIOBuffer> buffer = new net::StringIOBuffer(data); |
| 44 |
| 45 int result = socket_->Write( |
| 46 buffer.get(), |
| 47 buffer->size(), |
| 48 base::Bind(&GnubbyConnection::OnWrite, base::Unretained(this))); |
| 49 if (result != net::ERR_IO_PENDING) { |
| 50 OnWrite(result); |
| 51 } |
| 52 } |
| 53 |
| 54 void GnubbyConnection::OnRead(int result) { |
| 55 DCHECK(CalledOnValidThread()); |
| 56 |
| 57 if (result > 0) { |
| 58 std::string data(in_buffer_->data(), result); |
| 59 |
| 60 auth_handler_->DeliverHostDataMessage(connection_id_, data); |
| 61 |
| 62 Read(); |
| 63 } else if (result == 0) { |
| 64 auth_handler_->ConnectionClosed(connection_id_); |
| 65 } else { |
| 66 HOST_LOG << "Failed to read from gnubbyd: " << result; |
| 67 Error(result); |
| 68 } |
| 69 } |
| 70 |
| 71 void GnubbyConnection::OnWrite(int result) { |
| 72 DCHECK(CalledOnValidThread()); |
| 73 |
| 74 if (result < 0) { |
| 75 HOST_LOG << "Failed to write to gnubbyd: " << result; |
| 76 Error(result); |
| 77 } |
| 78 } |
| 79 |
| 80 void GnubbyConnection::Error(int result) { |
| 81 DCHECK(CalledOnValidThread()); |
| 82 |
| 83 auth_handler_->ConnectionError(connection_id_, result); |
| 84 } |
| 85 |
| 86 } // namespace remoting |
OLD | NEW |