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_endpoints/transports/bluetooth/copresence_socket
_bluetooth.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/message_loop/message_loop.h" | |
9 #include "base/strings/string_piece.h" | |
10 #include "device/bluetooth/bluetooth_socket.h" | |
11 #include "net/base/io_buffer.h" | |
12 | |
13 namespace { | |
14 | |
15 // TODO(rkc): This number is totally arbitrary. Figure out what this should be. | |
16 const int kMaxReceiveBytes = 4096; | |
17 | |
18 } // namespace | |
19 | |
20 namespace copresence_endpoints { | |
21 | |
22 CopresenceSocketBluetooth::CopresenceSocketBluetooth( | |
23 const scoped_refptr<device::BluetoothSocket>& socket) | |
24 : socket_(socket), receiving_(false), weak_ptr_factory_(this) { | |
25 } | |
26 | |
27 CopresenceSocketBluetooth::~CopresenceSocketBluetooth() { | |
28 receiving_ = false; | |
29 } | |
30 | |
31 bool CopresenceSocketBluetooth::Send(const scoped_refptr<net::IOBuffer>& buffer, | |
32 int buffer_size) { | |
33 VLOG(3) << "Starting sending of data with size = " << buffer_size; | |
34 socket_->Send(buffer, buffer_size, | |
35 base::Bind(&CopresenceSocketBluetooth::OnSendComplete, | |
36 weak_ptr_factory_.GetWeakPtr()), | |
37 base::Bind(&CopresenceSocketBluetooth::OnSendError, | |
38 weak_ptr_factory_.GetWeakPtr())); | |
39 return true; | |
40 } | |
41 | |
42 void CopresenceSocketBluetooth::Receive(const ReceiveCallback& callback) { | |
43 VLOG(3) << "Starting Receive."; | |
44 receiving_ = true; | |
45 receive_callback_ = callback; | |
46 socket_->Receive(kMaxReceiveBytes, | |
47 base::Bind(&CopresenceSocketBluetooth::OnReceive, | |
48 weak_ptr_factory_.GetWeakPtr()), | |
49 base::Bind(&CopresenceSocketBluetooth::OnReceiveError, | |
50 weak_ptr_factory_.GetWeakPtr())); | |
51 } | |
52 | |
53 void CopresenceSocketBluetooth::OnSendComplete(int status) { | |
54 VLOG(3) << "Send Completed. Status = " << status; | |
55 } | |
56 | |
57 void CopresenceSocketBluetooth::OnSendError(const std::string& message) { | |
58 LOG(ERROR) << "Bluetooth send error: " << message; | |
59 } | |
60 | |
61 void CopresenceSocketBluetooth::OnReceive( | |
62 int size, | |
63 scoped_refptr<net::IOBuffer> io_buffer) { | |
64 VLOG(3) << "Data received with size = " << size | |
65 << " and receiving_ = " << receiving_; | |
66 // Dispatch the data to the callback and go back to listening for more data. | |
67 receive_callback_.Run(io_buffer, size); | |
68 | |
69 // We cancelled receiving due to an error. Don't post more receive tasks. | |
70 if (!receiving_) | |
71 return; | |
72 | |
73 // Post a task to delay the read until the socket is available, as | |
74 // calling Receive again at this point would error with ERR_IO_PENDING. | |
75 base::MessageLoop::current()->PostTask( | |
76 FROM_HERE, | |
77 base::Bind(&device::BluetoothSocket::Receive, socket_, kMaxReceiveBytes, | |
78 base::Bind(&CopresenceSocketBluetooth::OnReceive, | |
79 weak_ptr_factory_.GetWeakPtr()), | |
80 base::Bind(&CopresenceSocketBluetooth::OnReceiveError, | |
81 weak_ptr_factory_.GetWeakPtr()))); | |
82 } | |
83 | |
84 void CopresenceSocketBluetooth::OnReceiveError( | |
85 device::BluetoothSocket::ErrorReason reason, | |
86 const std::string& message) { | |
87 LOG(ERROR) << "Bluetooth receive error: " << message; | |
88 if (reason == device::BluetoothSocket::kIOPending) | |
89 return; | |
90 | |
91 receiving_ = false; | |
92 } | |
93 | |
94 } // namespace copresence_endpoints | |
OLD | NEW |