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 "components/copresence_sockets/transports/bluetooth/copresence_socket_b
luetooth.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/strings/string_piece.h" |
| 9 #include "device/bluetooth/bluetooth_socket.h" |
| 10 #include "net/base/io_buffer.h" |
| 11 |
| 12 namespace { |
| 13 |
| 14 // TODO(rkc): This number is totally arbitrary. Figure out what this should be. |
| 15 const int kMaxReceiveBytes = 4096; |
| 16 |
| 17 } // namespace |
| 18 |
| 19 namespace copresence_sockets { |
| 20 |
| 21 CopresenceSocketBluetooth::CopresenceSocketBluetooth( |
| 22 const scoped_refptr<device::BluetoothSocket>& socket) |
| 23 : socket_(socket), weak_ptr_factory_(this) { |
| 24 } |
| 25 |
| 26 CopresenceSocketBluetooth::~CopresenceSocketBluetooth() { |
| 27 } |
| 28 |
| 29 bool CopresenceSocketBluetooth::Send(const scoped_refptr<net::IOBuffer>& buffer, |
| 30 int buffer_size) { |
| 31 socket_->Send(buffer, |
| 32 buffer_size, |
| 33 base::Bind(&CopresenceSocketBluetooth::OnSendComplete, |
| 34 weak_ptr_factory_.GetWeakPtr()), |
| 35 base::Bind(&CopresenceSocketBluetooth::OnSendError, |
| 36 weak_ptr_factory_.GetWeakPtr())); |
| 37 return true; |
| 38 } |
| 39 |
| 40 void CopresenceSocketBluetooth::Receive(const ReceiveCallback& callback) { |
| 41 receive_callback_ = callback; |
| 42 socket_->Receive(kMaxReceiveBytes, |
| 43 base::Bind(&CopresenceSocketBluetooth::OnReceive, |
| 44 weak_ptr_factory_.GetWeakPtr()), |
| 45 base::Bind(&CopresenceSocketBluetooth::OnReceiveError, |
| 46 weak_ptr_factory_.GetWeakPtr())); |
| 47 } |
| 48 |
| 49 void CopresenceSocketBluetooth::OnSendComplete(int status) { |
| 50 } |
| 51 |
| 52 void CopresenceSocketBluetooth::OnSendError(const std::string& message) { |
| 53 LOG(ERROR) << "Bluetooth send error: " << message; |
| 54 } |
| 55 |
| 56 void CopresenceSocketBluetooth::OnReceive( |
| 57 int size, |
| 58 scoped_refptr<net::IOBuffer> io_buffer) { |
| 59 // Dispatch the data to the callback and go back to listening for more data. |
| 60 receive_callback_.Run(io_buffer, size); |
| 61 socket_->Receive(kMaxReceiveBytes, |
| 62 base::Bind(&CopresenceSocketBluetooth::OnReceive, |
| 63 weak_ptr_factory_.GetWeakPtr()), |
| 64 base::Bind(&CopresenceSocketBluetooth::OnReceiveError, |
| 65 weak_ptr_factory_.GetWeakPtr())); |
| 66 } |
| 67 |
| 68 void CopresenceSocketBluetooth::OnReceiveError( |
| 69 device::BluetoothSocket::ErrorReason /* reason */, |
| 70 const std::string& message) { |
| 71 LOG(ERROR) << "Bluetooth receive error: " << message; |
| 72 } |
| 73 |
| 74 } // namespace copresence_sockets |
OLD | NEW |