OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "device/bluetooth/bluetooth_socket_win.h" | 5 #include "device/bluetooth/bluetooth_socket_win.h" |
6 | 6 |
7 #include <objbase.h> | 7 #include <objbase.h> |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/sequenced_task_runner.h" | 13 #include "base/sequenced_task_runner.h" |
14 #include "base/strings/sys_string_conversions.h" | 14 #include "base/strings/sys_string_conversions.h" |
15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
16 #include "base/threading/thread_restrictions.h" | 16 #include "base/threading/thread_restrictions.h" |
17 #include "device/bluetooth/bluetooth_init_win.h" | 17 #include "device/bluetooth/bluetooth_init_win.h" |
18 #include "device/bluetooth/bluetooth_service_record_win.h" | 18 #include "device/bluetooth/bluetooth_service_record_win.h" |
19 #include "device/bluetooth/bluetooth_socket_thread_win.h" | 19 #include "device/bluetooth/bluetooth_socket_thread.h" |
20 #include "net/base/io_buffer.h" | 20 #include "net/base/io_buffer.h" |
21 #include "net/base/ip_endpoint.h" | 21 #include "net/base/ip_endpoint.h" |
22 #include "net/base/net_errors.h" | 22 #include "net/base/net_errors.h" |
23 #include "net/base/winsock_init.h" | 23 #include "net/base/winsock_init.h" |
24 | 24 |
25 namespace { | 25 namespace { |
26 | 26 |
27 const char kL2CAPNotSupported[] = "Bluetooth L2CAP protocal is not supported"; | 27 const char kL2CAPNotSupported[] = "Bluetooth L2CAP protocal is not supported"; |
28 const char kSocketAlreadyConnected[] = "Socket is already connected."; | 28 const char kSocketAlreadyConnected[] = "Socket is already connected."; |
29 const char kSocketNotConnected[] = "Socket is not connected."; | |
30 const char kInvalidRfcommPort[] = "Invalid RFCCOMM port."; | 29 const char kInvalidRfcommPort[] = "Invalid RFCCOMM port."; |
31 const char kFailedToCreateSocket[] = "Failed to create socket."; | 30 const char kFailedToCreateSocket[] = "Failed to create socket."; |
32 const char kFailedToBindSocket[] = "Failed to bind socket."; | 31 const char kFailedToBindSocket[] = "Failed to bind socket."; |
33 const char kFailedToListenOnSocket[] = "Failed to listen on socket."; | 32 const char kFailedToListenOnSocket[] = "Failed to listen on socket."; |
34 const char kFailedToGetSockNameForSocket[] = "Failed to getsockname."; | 33 const char kFailedToGetSockNameForSocket[] = "Failed to getsockname."; |
35 const char kBadUuid[] = "Bad uuid."; | 34 const char kBadUuid[] = "Bad uuid."; |
36 const char kWsaSetServiceError[] = "WSASetService error."; | 35 const char kWsaSetServiceError[] = "WSASetService error."; |
37 | 36 |
38 using device::BluetoothSocketWin; | |
39 | |
40 static void DeactivateSocket( | |
41 const scoped_refptr<device::BluetoothSocketThreadWin>& socket_thread) { | |
42 socket_thread->OnSocketDeactivate(); | |
43 } | |
44 | |
45 } // namespace | 37 } // namespace |
46 | 38 |
47 namespace device { | 39 namespace device { |
48 | 40 |
49 struct BluetoothSocketWin::ServiceRegData { | 41 struct BluetoothSocketWin::ServiceRegData { |
50 ServiceRegData() { | 42 ServiceRegData() { |
51 ZeroMemory(&address, sizeof(address)); | 43 ZeroMemory(&address, sizeof(address)); |
52 ZeroMemory(&address_info, sizeof(address_info)); | 44 ZeroMemory(&address_info, sizeof(address_info)); |
53 ZeroMemory(&uuid, sizeof(uuid)); | 45 ZeroMemory(&uuid, sizeof(uuid)); |
54 ZeroMemory(&service, sizeof(service)); | 46 ZeroMemory(&service, sizeof(service)); |
55 } | 47 } |
56 | 48 |
57 SOCKADDR_BTH address; | 49 SOCKADDR_BTH address; |
58 CSADDR_INFO address_info; | 50 CSADDR_INFO address_info; |
59 GUID uuid; | 51 GUID uuid; |
60 base::string16 name; | 52 base::string16 name; |
61 WSAQUERYSET service; | 53 WSAQUERYSET service; |
62 }; | 54 }; |
63 | 55 |
64 // static | 56 // static |
65 scoped_refptr<BluetoothSocketWin> BluetoothSocketWin::CreateBluetoothSocket( | 57 scoped_refptr<BluetoothSocketWin> |
| 58 BluetoothSocketWin::CreateBluetoothSocket( |
66 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, | 59 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, |
67 scoped_refptr<BluetoothSocketThreadWin> socket_thread, | 60 scoped_refptr<device::BluetoothSocketThread> socket_thread, |
68 net::NetLog* net_log, | 61 net::NetLog* net_log, |
69 const net::NetLog::Source& source) { | 62 const net::NetLog::Source& source) { |
70 DCHECK(ui_task_runner->RunsTasksOnCurrentThread()); | 63 DCHECK(ui_task_runner->RunsTasksOnCurrentThread()); |
71 | 64 |
72 return make_scoped_refptr( | 65 return make_scoped_refptr( |
73 new BluetoothSocketWin(ui_task_runner, socket_thread, net_log, source)); | 66 new BluetoothSocketWin(ui_task_runner, socket_thread, net_log, source)); |
74 } | 67 } |
75 | 68 |
76 BluetoothSocketWin::BluetoothSocketWin( | 69 BluetoothSocketWin::BluetoothSocketWin( |
77 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, | 70 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, |
78 scoped_refptr<BluetoothSocketThreadWin> socket_thread, | 71 scoped_refptr<BluetoothSocketThread> socket_thread, |
79 net::NetLog* net_log, | 72 net::NetLog* net_log, |
80 const net::NetLog::Source& source) | 73 const net::NetLog::Source& source) |
81 : ui_task_runner_(ui_task_runner), | 74 : BluetoothSocketNet(ui_task_runner, socket_thread, net_log, source), |
82 socket_thread_(socket_thread), | |
83 net_log_(net_log), | |
84 source_(source), | |
85 supports_rfcomm_(false), | 75 supports_rfcomm_(false), |
86 rfcomm_channel_(-1), | 76 rfcomm_channel_(-1), |
87 bth_addr_(BTH_ADDR_NULL) { | 77 bth_addr_(BTH_ADDR_NULL) { |
88 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | |
89 socket_thread->OnSocketActivate(); | |
90 } | 78 } |
91 | 79 |
92 BluetoothSocketWin::~BluetoothSocketWin() { | 80 BluetoothSocketWin::~BluetoothSocketWin() { |
93 ui_task_runner_->PostTask(FROM_HERE, | |
94 base::Bind(&DeactivateSocket, socket_thread_)); | |
95 } | 81 } |
96 | 82 |
97 void BluetoothSocketWin::StartService( | 83 void BluetoothSocketWin::StartService( |
98 const BluetoothUUID& uuid, | 84 const BluetoothUUID& uuid, |
99 const std::string& name, | 85 const std::string& name, |
100 int rfcomm_channel, | 86 int rfcomm_channel, |
101 const base::Closure& success_callback, | 87 const base::Closure& success_callback, |
102 const ErrorCompletionCallback& error_callback, | 88 const ErrorCompletionCallback& error_callback, |
103 const OnNewConnectionCallback& new_connection_callback) { | 89 const OnNewConnectionCallback& new_connection_callback) { |
104 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 90 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
105 | 91 |
106 socket_thread_->task_runner()->PostTask( | 92 socket_thread()->task_runner()->PostTask( |
107 FROM_HERE, | 93 FROM_HERE, |
108 base::Bind(&BluetoothSocketWin::DoStartService, | 94 base::Bind(&BluetoothSocketWin::DoStartService, |
109 this, | 95 this, |
110 uuid, | 96 uuid, |
111 name, | 97 name, |
112 rfcomm_channel, | 98 rfcomm_channel, |
113 success_callback, | 99 success_callback, |
114 error_callback, | 100 error_callback, |
115 new_connection_callback)); | 101 new_connection_callback)); |
116 } | 102 } |
117 | 103 |
118 void BluetoothSocketWin::Close() { | |
119 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | |
120 socket_thread_->task_runner()->PostTask( | |
121 FROM_HERE, base::Bind(&BluetoothSocketWin::DoClose, this)); | |
122 } | |
123 | |
124 void BluetoothSocketWin::Connect( | 104 void BluetoothSocketWin::Connect( |
125 const BluetoothServiceRecord& service_record, | 105 const BluetoothServiceRecord& service_record, |
126 const base::Closure& success_callback, | 106 const base::Closure& success_callback, |
127 const ErrorCompletionCallback& error_callback) { | 107 const ErrorCompletionCallback& error_callback) { |
128 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 108 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
129 | 109 |
130 const BluetoothServiceRecordWin* service_record_win = | 110 const BluetoothServiceRecordWin* service_record_win = |
131 static_cast<const BluetoothServiceRecordWin*>(&service_record); | 111 static_cast<const BluetoothServiceRecordWin*>(&service_record); |
132 device_address_ = service_record_win->address(); | 112 device_address_ = service_record_win->address(); |
133 if (service_record.SupportsRfcomm()) { | 113 if (service_record.SupportsRfcomm()) { |
134 supports_rfcomm_ = true; | 114 supports_rfcomm_ = true; |
135 rfcomm_channel_ = service_record_win->rfcomm_channel(); | 115 rfcomm_channel_ = service_record_win->rfcomm_channel(); |
136 bth_addr_ = service_record_win->bth_addr(); | 116 bth_addr_ = service_record_win->bth_addr(); |
137 } | 117 } |
138 | 118 |
139 socket_thread_->task_runner()->PostTask( | 119 socket_thread()->task_runner()->PostTask( |
140 FROM_HERE, | 120 FROM_HERE, |
141 base::Bind( | 121 base::Bind( |
142 &BluetoothSocketWin::DoConnect, | 122 &BluetoothSocketWin::DoConnect, |
143 this, | 123 this, |
144 base::Bind(&BluetoothSocketWin::PostSuccess, this, success_callback), | 124 base::Bind(&BluetoothSocketWin::PostSuccess, this, success_callback), |
145 base::Bind( | 125 base::Bind( |
146 &BluetoothSocketWin::PostErrorCompletion, this, error_callback))); | 126 &BluetoothSocketWin::PostErrorCompletion, this, error_callback))); |
147 } | 127 } |
148 | 128 |
149 void BluetoothSocketWin::Disconnect(const base::Closure& success_callback) { | |
150 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | |
151 socket_thread_->task_runner()->PostTask( | |
152 FROM_HERE, | |
153 base::Bind( | |
154 &BluetoothSocketWin::DoDisconnect, | |
155 this, | |
156 base::Bind( | |
157 &BluetoothSocketWin::PostSuccess, this, success_callback))); | |
158 } | |
159 | 129 |
160 void BluetoothSocketWin::Receive( | 130 void BluetoothSocketWin::ResetData() { |
161 int buffer_size, | |
162 const ReceiveCompletionCallback& success_callback, | |
163 const ReceiveErrorCompletionCallback& error_callback) { | |
164 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | |
165 socket_thread_->task_runner()->PostTask( | |
166 FROM_HERE, | |
167 base::Bind(&BluetoothSocketWin::DoReceive, | |
168 this, | |
169 buffer_size, | |
170 base::Bind(&BluetoothSocketWin::PostReceiveCompletion, | |
171 this, | |
172 success_callback), | |
173 base::Bind(&BluetoothSocketWin::PostReceiveErrorCompletion, | |
174 this, | |
175 error_callback))); | |
176 } | |
177 | |
178 void BluetoothSocketWin::Send(scoped_refptr<net::IOBuffer> buffer, | |
179 int buffer_size, | |
180 const SendCompletionCallback& success_callback, | |
181 const ErrorCompletionCallback& error_callback) { | |
182 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | |
183 socket_thread_->task_runner()->PostTask( | |
184 FROM_HERE, | |
185 base::Bind( | |
186 &BluetoothSocketWin::DoSend, | |
187 this, | |
188 buffer, | |
189 buffer_size, | |
190 base::Bind( | |
191 &BluetoothSocketWin::PostSendCompletion, this, success_callback), | |
192 base::Bind( | |
193 &BluetoothSocketWin::PostErrorCompletion, this, error_callback))); | |
194 } | |
195 | |
196 void BluetoothSocketWin::DoClose() { | |
197 DCHECK(socket_thread_->task_runner()->RunsTasksOnCurrentThread()); | |
198 base::ThreadRestrictions::AssertIOAllowed(); | |
199 | |
200 if (tcp_socket_) { | |
201 tcp_socket_->Close(); | |
202 tcp_socket_.reset(NULL); | |
203 } | |
204 | |
205 // Note: Closing |tcp_socket_| above released all potential pending | |
206 // Send/Receive operations, so we can no safely release the state associated | |
207 // to those pending operations. | |
208 read_buffer_ = NULL; | |
209 std::queue<linked_ptr<WriteRequest> > empty; | |
210 write_queue_.swap(empty); | |
211 | |
212 if (service_reg_data_) { | 131 if (service_reg_data_) { |
213 if (WSASetService(&service_reg_data_->service,RNRSERVICE_DELETE, 0) == | 132 if (WSASetService(&service_reg_data_->service,RNRSERVICE_DELETE, 0) == |
214 SOCKET_ERROR) { | 133 SOCKET_ERROR) { |
215 LOG(WARNING) << "Failed to unregister service."; | 134 LOG(WARNING) << "Failed to unregister service."; |
216 } | 135 } |
217 service_reg_data_.reset(); | 136 service_reg_data_.reset(); |
218 } | 137 } |
219 } | 138 } |
220 | 139 |
221 void BluetoothSocketWin::DoConnect( | 140 void BluetoothSocketWin::DoConnect( |
222 const base::Closure& success_callback, | 141 const base::Closure& success_callback, |
223 const ErrorCompletionCallback& error_callback) { | 142 const ErrorCompletionCallback& error_callback) { |
224 DCHECK(socket_thread_->task_runner()->RunsTasksOnCurrentThread()); | 143 DCHECK(socket_thread()->task_runner()->RunsTasksOnCurrentThread()); |
225 base::ThreadRestrictions::AssertIOAllowed(); | 144 base::ThreadRestrictions::AssertIOAllowed(); |
226 | 145 |
227 if (tcp_socket_) { | 146 if (tcp_socket()) { |
228 error_callback.Run(kSocketAlreadyConnected); | 147 error_callback.Run(kSocketAlreadyConnected); |
229 return; | 148 return; |
230 } | 149 } |
231 | 150 |
232 if (!supports_rfcomm_) { | 151 if (!supports_rfcomm_) { |
233 // TODO(youngki) add support for L2CAP sockets as well. | 152 // TODO(youngki) add support for L2CAP sockets as well. |
234 error_callback.Run(kL2CAPNotSupported); | 153 error_callback.Run(kL2CAPNotSupported); |
235 return; | 154 return; |
236 } | 155 } |
237 | 156 |
238 tcp_socket_.reset(new net::TCPSocket(net_log_, source_)); | 157 ResetTCPSocket(); |
239 net::EnsureWinsockInit(); | 158 net::EnsureWinsockInit(); |
240 SOCKET socket_fd = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM); | 159 SOCKET socket_fd = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM); |
241 SOCKADDR_BTH sa; | 160 SOCKADDR_BTH sa; |
242 ZeroMemory(&sa, sizeof(sa)); | 161 ZeroMemory(&sa, sizeof(sa)); |
243 sa.addressFamily = AF_BTH; | 162 sa.addressFamily = AF_BTH; |
244 sa.port = rfcomm_channel_; | 163 sa.port = rfcomm_channel_; |
245 sa.btAddr = bth_addr_; | 164 sa.btAddr = bth_addr_; |
246 | 165 |
247 // TODO(rpaquay): Condider making this call non-blocking. | 166 // TODO(rpaquay): Condider making this call non-blocking. |
248 int status = connect(socket_fd, reinterpret_cast<SOCKADDR*>(&sa), sizeof(sa)); | 167 int status = connect(socket_fd, reinterpret_cast<SOCKADDR*>(&sa), sizeof(sa)); |
249 DWORD error_code = WSAGetLastError(); | 168 DWORD error_code = WSAGetLastError(); |
250 if (!(status == 0 || error_code == WSAEINPROGRESS)) { | 169 if (!(status == 0 || error_code == WSAEINPROGRESS)) { |
251 LOG(ERROR) << "Failed to connect bluetooth socket " | 170 LOG(ERROR) << "Failed to connect bluetooth socket " |
252 << "(" << device_address_ << "): " | 171 << "(" << device_address_ << "): " |
253 << logging::SystemErrorCodeToString(error_code); | 172 << logging::SystemErrorCodeToString(error_code); |
254 error_callback.Run("Error connecting to socket: " + | 173 error_callback.Run("Error connecting to socket: " + |
255 logging::SystemErrorCodeToString(error_code)); | 174 logging::SystemErrorCodeToString(error_code)); |
256 closesocket(socket_fd); | 175 closesocket(socket_fd); |
257 return; | 176 return; |
258 } | 177 } |
259 | 178 |
260 // Note: We don't have a meaningful |IPEndPoint|, but that is ok since the | 179 // Note: We don't have a meaningful |IPEndPoint|, but that is ok since the |
261 // TCPSocket implementation does not actually require one. | 180 // TCPSocket implementation does not actually require one. |
262 int net_result = | 181 int net_result = |
263 tcp_socket_->AdoptConnectedSocket(socket_fd, net::IPEndPoint()); | 182 tcp_socket()->AdoptConnectedSocket(socket_fd, net::IPEndPoint()); |
264 if (net_result != net::OK) { | 183 if (net_result != net::OK) { |
265 error_callback.Run("Error connecting to socket: " + | 184 error_callback.Run("Error connecting to socket: " + |
266 std::string(net::ErrorToString(net_result))); | 185 std::string(net::ErrorToString(net_result))); |
267 closesocket(socket_fd); | 186 closesocket(socket_fd); |
268 return; | 187 return; |
269 } | 188 } |
270 | 189 |
271 success_callback.Run(); | 190 success_callback.Run(); |
272 } | 191 } |
273 | 192 |
274 void BluetoothSocketWin::DoDisconnect(const base::Closure& success_callback) { | |
275 DCHECK(socket_thread_->task_runner()->RunsTasksOnCurrentThread()); | |
276 base::ThreadRestrictions::AssertIOAllowed(); | |
277 | |
278 DoClose(); | |
279 success_callback.Run(); | |
280 } | |
281 | |
282 void BluetoothSocketWin::DoReceive( | |
283 int buffer_size, | |
284 const ReceiveCompletionCallback& success_callback, | |
285 const ReceiveErrorCompletionCallback& error_callback) { | |
286 DCHECK(socket_thread_->task_runner()->RunsTasksOnCurrentThread()); | |
287 base::ThreadRestrictions::AssertIOAllowed(); | |
288 | |
289 if (!tcp_socket_) { | |
290 error_callback.Run(BluetoothSocketWin::kDisconnected, kSocketNotConnected); | |
291 return; | |
292 } | |
293 | |
294 // Only one pending read at a time | |
295 if (read_buffer_.get()) { | |
296 error_callback.Run(BluetoothSocketWin::kIOPending, | |
297 net::ErrorToString(net::ERR_IO_PENDING)); | |
298 return; | |
299 } | |
300 | |
301 scoped_refptr<net::IOBufferWithSize> buffer( | |
302 new net::IOBufferWithSize(buffer_size)); | |
303 int read_result = | |
304 tcp_socket_->Read(buffer.get(), | |
305 buffer->size(), | |
306 base::Bind(&BluetoothSocketWin::OnSocketReadComplete, | |
307 this, | |
308 success_callback, | |
309 error_callback)); | |
310 | |
311 if (read_result > 0) { | |
312 success_callback.Run(read_result, buffer); | |
313 } else if (read_result == net::OK || | |
314 read_result == net::ERR_CONNECTION_CLOSED) { | |
315 error_callback.Run(BluetoothSocketWin::kDisconnected, | |
316 net::ErrorToString(net::ERR_CONNECTION_CLOSED)); | |
317 } else if (read_result == net::ERR_IO_PENDING) { | |
318 read_buffer_ = buffer; | |
319 } else { | |
320 error_callback.Run(BluetoothSocketWin::kSystemError, | |
321 net::ErrorToString(read_result)); | |
322 } | |
323 } | |
324 | |
325 void BluetoothSocketWin::OnSocketReadComplete( | |
326 const ReceiveCompletionCallback& success_callback, | |
327 const ReceiveErrorCompletionCallback& error_callback, | |
328 int read_result) { | |
329 DCHECK(socket_thread_->task_runner()->RunsTasksOnCurrentThread()); | |
330 base::ThreadRestrictions::AssertIOAllowed(); | |
331 | |
332 scoped_refptr<net::IOBufferWithSize> buffer; | |
333 buffer.swap(read_buffer_); | |
334 if (read_result > 0) { | |
335 success_callback.Run(read_result, buffer); | |
336 } else if (read_result == net::OK || | |
337 read_result == net::ERR_CONNECTION_CLOSED) { | |
338 error_callback.Run(BluetoothSocketWin::kDisconnected, | |
339 net::ErrorToString(net::ERR_CONNECTION_CLOSED)); | |
340 } else { | |
341 error_callback.Run(BluetoothSocketWin::kSystemError, | |
342 net::ErrorToString(read_result)); | |
343 } | |
344 } | |
345 | |
346 void BluetoothSocketWin::DoSend(scoped_refptr<net::IOBuffer> buffer, | |
347 int buffer_size, | |
348 const SendCompletionCallback& success_callback, | |
349 const ErrorCompletionCallback& error_callback) { | |
350 DCHECK(socket_thread_->task_runner()->RunsTasksOnCurrentThread()); | |
351 base::ThreadRestrictions::AssertIOAllowed(); | |
352 | |
353 if (!tcp_socket_) { | |
354 error_callback.Run(kSocketNotConnected); | |
355 return; | |
356 } | |
357 | |
358 linked_ptr<WriteRequest> request(new WriteRequest()); | |
359 request->buffer = buffer; | |
360 request->buffer_size = buffer_size; | |
361 request->success_callback = success_callback; | |
362 request->error_callback = error_callback; | |
363 | |
364 write_queue_.push(request); | |
365 if (write_queue_.size() == 1) { | |
366 SendFrontWriteRequest(); | |
367 } | |
368 } | |
369 | |
370 void BluetoothSocketWin::SendFrontWriteRequest() { | |
371 DCHECK(socket_thread_->task_runner()->RunsTasksOnCurrentThread()); | |
372 base::ThreadRestrictions::AssertIOAllowed(); | |
373 | |
374 if (!tcp_socket_) | |
375 return; | |
376 | |
377 if (write_queue_.size() == 0) | |
378 return; | |
379 | |
380 linked_ptr<WriteRequest> request = write_queue_.front(); | |
381 net::CompletionCallback callback = | |
382 base::Bind(&BluetoothSocketWin::OnSocketWriteComplete, | |
383 this, | |
384 request->success_callback, | |
385 request->error_callback); | |
386 int send_result = | |
387 tcp_socket_->Write(request->buffer, request->buffer_size, callback); | |
388 if (send_result != net::ERR_IO_PENDING) { | |
389 callback.Run(send_result); | |
390 } | |
391 } | |
392 | |
393 void BluetoothSocketWin::OnSocketWriteComplete( | |
394 const SendCompletionCallback& success_callback, | |
395 const ErrorCompletionCallback& error_callback, | |
396 int send_result) { | |
397 DCHECK(socket_thread_->task_runner()->RunsTasksOnCurrentThread()); | |
398 base::ThreadRestrictions::AssertIOAllowed(); | |
399 | |
400 write_queue_.pop(); | |
401 | |
402 if (send_result >= net::OK) { | |
403 success_callback.Run(send_result); | |
404 } else { | |
405 error_callback.Run(net::ErrorToString(send_result)); | |
406 } | |
407 | |
408 // Don't call directly to avoid potentail large recursion. | |
409 socket_thread_->task_runner()->PostNonNestableTask( | |
410 FROM_HERE, base::Bind(&BluetoothSocketWin::SendFrontWriteRequest, this)); | |
411 } | |
412 | |
413 void BluetoothSocketWin::PostSuccess(const base::Closure& callback) { | |
414 ui_task_runner_->PostTask(FROM_HERE, callback); | |
415 } | |
416 | |
417 void BluetoothSocketWin::PostErrorCompletion( | |
418 const ErrorCompletionCallback& callback, | |
419 const std::string& error) { | |
420 ui_task_runner_->PostTask(FROM_HERE, base::Bind(callback, error)); | |
421 } | |
422 | |
423 void BluetoothSocketWin::PostReceiveCompletion( | |
424 const ReceiveCompletionCallback& callback, | |
425 int io_buffer_size, | |
426 scoped_refptr<net::IOBuffer> io_buffer) { | |
427 ui_task_runner_->PostTask(FROM_HERE, | |
428 base::Bind(callback, io_buffer_size, io_buffer)); | |
429 } | |
430 | |
431 void BluetoothSocketWin::PostReceiveErrorCompletion( | |
432 const ReceiveErrorCompletionCallback& callback, | |
433 ErrorReason reason, | |
434 const std::string& error_message) { | |
435 ui_task_runner_->PostTask(FROM_HERE, | |
436 base::Bind(callback, reason, error_message)); | |
437 } | |
438 | |
439 void BluetoothSocketWin::PostSendCompletion( | |
440 const SendCompletionCallback& callback, | |
441 int bytes_written) { | |
442 ui_task_runner_->PostTask(FROM_HERE, base::Bind(callback, bytes_written)); | |
443 } | |
444 | |
445 void BluetoothSocketWin::DoStartService( | 193 void BluetoothSocketWin::DoStartService( |
446 const BluetoothUUID& uuid, | 194 const BluetoothUUID& uuid, |
447 const std::string& name, | 195 const std::string& name, |
448 int rfcomm_channel, | 196 int rfcomm_channel, |
449 const base::Closure& success_callback, | 197 const base::Closure& success_callback, |
450 const ErrorCompletionCallback& error_callback, | 198 const ErrorCompletionCallback& error_callback, |
451 const OnNewConnectionCallback& new_connection_callback) { | 199 const OnNewConnectionCallback& new_connection_callback) { |
452 DCHECK(socket_thread_->task_runner()->RunsTasksOnCurrentThread()); | 200 DCHECK(socket_thread()->task_runner()->RunsTasksOnCurrentThread()); |
453 DCHECK(!tcp_socket_ && | 201 DCHECK(!tcp_socket() && |
454 !service_reg_data_ && | 202 !service_reg_data_ && |
455 on_new_connection_callback_.is_null()); | 203 on_new_connection_callback_.is_null()); |
456 | 204 |
457 // The valid range is 0-30. 0 means BT_PORT_ANY and 1-30 are the | 205 // The valid range is 0-30. 0 means BT_PORT_ANY and 1-30 are the |
458 // valid RFCOMM port numbers of SOCKADDR_BTH. | 206 // valid RFCOMM port numbers of SOCKADDR_BTH. |
459 if (rfcomm_channel < 0 || rfcomm_channel > 30) { | 207 if (rfcomm_channel < 0 || rfcomm_channel > 30) { |
460 LOG(WARNING) << "Failed to start service: " | 208 LOG(WARNING) << "Failed to start service: " |
461 << "Invalid RFCCOMM port " << rfcomm_channel | 209 << "Invalid RFCCOMM port " << rfcomm_channel |
462 << ", uuid=" << uuid.value(); | 210 << ", uuid=" << uuid.value(); |
463 PostErrorCompletion(error_callback, kInvalidRfcommPort); | 211 PostErrorCompletion(error_callback, kInvalidRfcommPort); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
535 reg_data->service.lpcsaBuffer = ®_data->address_info; | 283 reg_data->service.lpcsaBuffer = ®_data->address_info; |
536 | 284 |
537 if (WSASetService(®_data->service, | 285 if (WSASetService(®_data->service, |
538 RNRSERVICE_REGISTER, 0) == SOCKET_ERROR) { | 286 RNRSERVICE_REGISTER, 0) == SOCKET_ERROR) { |
539 LOG(WARNING) << "Failed to register profile: WSASetService" | 287 LOG(WARNING) << "Failed to register profile: WSASetService" |
540 << "winsock err=" << WSAGetLastError(); | 288 << "winsock err=" << WSAGetLastError(); |
541 PostErrorCompletion(error_callback, kWsaSetServiceError); | 289 PostErrorCompletion(error_callback, kWsaSetServiceError); |
542 return; | 290 return; |
543 } | 291 } |
544 | 292 |
545 tcp_socket_ = scoped_socket.Pass(); | 293 SetTCPSocket(scoped_socket.Pass()); |
546 service_reg_data_ = reg_data.Pass(); | 294 service_reg_data_ = reg_data.Pass(); |
547 on_new_connection_callback_ = new_connection_callback; | 295 on_new_connection_callback_ = new_connection_callback; |
548 DoAccept(); | 296 DoAccept(); |
549 | 297 |
550 PostSuccess(success_callback); | 298 PostSuccess(success_callback); |
551 } | 299 } |
552 | 300 |
553 void BluetoothSocketWin::DoAccept() { | 301 void BluetoothSocketWin::DoAccept() { |
554 DCHECK(socket_thread_->task_runner()->RunsTasksOnCurrentThread()); | 302 DCHECK(socket_thread()->task_runner()->RunsTasksOnCurrentThread()); |
555 int result = tcp_socket_->Accept( | 303 int result = tcp_socket()->Accept( |
556 &accept_socket_, | 304 &accept_socket_, |
557 &accept_address_, | 305 &accept_address_, |
558 base::Bind(&BluetoothSocketWin::OnAcceptOnSocketThread, this)); | 306 base::Bind(&BluetoothSocketWin::OnAcceptOnSocketThread, this)); |
559 if (result != net::OK && result != net::ERR_IO_PENDING) | 307 if (result != net::OK && result != net::ERR_IO_PENDING) |
560 LOG(WARNING) << "Failed to accept, net err=" << result; | 308 LOG(WARNING) << "Failed to accept, net err=" << result; |
561 } | 309 } |
562 | 310 |
563 void BluetoothSocketWin::OnAcceptOnSocketThread(int accept_result) { | 311 void BluetoothSocketWin::OnAcceptOnSocketThread(int accept_result) { |
564 DCHECK(socket_thread_->task_runner()->RunsTasksOnCurrentThread()); | 312 DCHECK(socket_thread()->task_runner()->RunsTasksOnCurrentThread()); |
565 if (accept_result != net::OK) { | 313 if (accept_result != net::OK) { |
566 LOG(WARNING) << "OnAccept error, net err=" << accept_result; | 314 LOG(WARNING) << "OnAccept error, net err=" << accept_result; |
567 return; | 315 return; |
568 } | 316 } |
569 | 317 |
570 ui_task_runner_->PostTask( | 318 ui_task_runner()->PostTask( |
571 FROM_HERE, | 319 FROM_HERE, |
572 base::Bind(&BluetoothSocketWin::OnAcceptOnUI, | 320 base::Bind(&BluetoothSocketWin::OnAcceptOnUI, |
573 this, | 321 this, |
574 base::Passed(&accept_socket_), | 322 base::Passed(&accept_socket_), |
575 accept_address_)); | 323 accept_address_)); |
576 DoAccept(); | 324 DoAccept(); |
577 } | 325 } |
578 | 326 |
579 void BluetoothSocketWin::OnAcceptOnUI( | 327 void BluetoothSocketWin::OnAcceptOnUI( |
580 scoped_ptr<net::TCPSocket> accept_socket, | 328 scoped_ptr<net::TCPSocket> accept_socket, |
581 const net::IPEndPoint& peer_address) { | 329 const net::IPEndPoint& peer_address) { |
582 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 330 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
583 | 331 |
584 scoped_refptr<BluetoothSocketWin> peer = CreateBluetoothSocket( | 332 scoped_refptr<BluetoothSocketWin> peer = CreateBluetoothSocket( |
585 ui_task_runner_, | 333 ui_task_runner(), |
586 socket_thread_, | 334 socket_thread(), |
587 net_log_, | 335 net_log(), |
588 source_); | 336 source()); |
589 peer->tcp_socket_ = accept_socket.Pass(); | 337 peer->SetTCPSocket(accept_socket.Pass()); |
590 | 338 |
591 on_new_connection_callback_.Run(peer, peer_address); | 339 on_new_connection_callback_.Run(peer, peer_address); |
592 } | 340 } |
593 | 341 |
594 } // namespace device | 342 } // namespace device |
OLD | NEW |