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 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_WIN_H_ | 5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_WIN_H_ |
6 #define DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_WIN_H_ | 6 #define DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_WIN_H_ |
7 | 7 |
8 #include <WinSock2.h> | 8 #include <WinSock2.h> |
9 | 9 |
| 10 #include <queue> |
10 #include <string> | 11 #include <string> |
11 | 12 |
| 13 #include "base/memory/linked_ptr.h" |
12 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/threading/thread_checker.h" |
| 16 #include "device/bluetooth/bluetooth_service_record_win.h" |
13 #include "device/bluetooth/bluetooth_socket.h" | 17 #include "device/bluetooth/bluetooth_socket.h" |
| 18 #include "net/base/net_log.h" |
14 | 19 |
15 namespace net { | 20 namespace net { |
16 | 21 class IOBuffer; |
17 class DrainableIOBuffer; | 22 class IOBufferWithSize; |
18 class GrowableIOBuffer; | 23 class TCPSocketWin; |
19 | |
20 } // namespace net | 24 } // namespace net |
21 | 25 |
22 namespace device { | 26 namespace device { |
23 | 27 |
24 class BluetoothServiceRecord; | 28 class BluetoothServiceRecord; |
| 29 class BluetoothSocketThreadWin; |
25 | 30 |
26 // This class is an implementation of BluetoothSocket class for Windows | 31 // This class is an implementation of BluetoothSocket class for the Windows |
27 // platform. | 32 // platform. All public methods (including the factory method) must be called |
| 33 // on the UI thread, while underlying socket operations are performed on a |
| 34 // separated thread. |
28 class BluetoothSocketWin : public BluetoothSocket { | 35 class BluetoothSocketWin : public BluetoothSocket { |
29 public: | 36 public: |
30 static scoped_refptr<BluetoothSocket> CreateBluetoothSocket( | 37 static scoped_refptr<BluetoothSocketWin> CreateBluetoothSocket( |
31 const BluetoothServiceRecord& service_record); | 38 const BluetoothServiceRecord& service_record, |
| 39 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, |
| 40 scoped_refptr<BluetoothSocketThreadWin> socket_thread, |
| 41 net::NetLog* net_log, |
| 42 const net::NetLog::Source& source); |
32 | 43 |
33 // BluetoothSocket override | 44 // Connect to the peer device and calls |success_callback| when the |
34 virtual bool Receive(net::GrowableIOBuffer* buffer) OVERRIDE; | 45 // connection has been established successfully. If an error occurs, calls |
35 virtual bool Send(net::DrainableIOBuffer* buffer) OVERRIDE; | 46 // |error_callback| with a system error message. |
36 virtual std::string GetLastErrorMessage() const OVERRIDE; | 47 void Connect(const base::Closure& success_callback, |
| 48 const ErrorCompletionCallback& error_callback); |
| 49 |
| 50 // Overriden from BluetoothSocket: |
| 51 virtual void Release() OVERRIDE; |
| 52 |
| 53 virtual void Disconnect(const base::Closure& callback) OVERRIDE; |
| 54 |
| 55 virtual void Receive(int count, |
| 56 const ReceiveCompletionCallback& success_callback, |
| 57 const ReceiveErrorCompletionCallback& error_callback) |
| 58 OVERRIDE; |
| 59 virtual void Send(scoped_refptr<net::IOBuffer> buffer, |
| 60 int buffer_size, |
| 61 const SendCompletionCallback& success_callback, |
| 62 const ErrorCompletionCallback& error_callback) OVERRIDE; |
37 | 63 |
38 protected: | 64 protected: |
39 virtual ~BluetoothSocketWin(); | 65 virtual ~BluetoothSocketWin(); |
40 | 66 |
41 private: | 67 private: |
42 explicit BluetoothSocketWin(SOCKET fd); | 68 struct WriteRequest { |
| 69 scoped_refptr<net::IOBuffer> buffer; |
| 70 int buffer_size; |
| 71 SendCompletionCallback success_callback; |
| 72 ErrorCompletionCallback error_callback; |
| 73 }; |
43 | 74 |
44 const SOCKET fd_; | 75 BluetoothSocketWin(scoped_refptr<base::SequencedTaskRunner> ui_task_runner, |
45 std::string error_message_; | 76 scoped_refptr<BluetoothSocketThreadWin> socket_thread, |
| 77 net::NetLog* net_log, |
| 78 const net::NetLog::Source& source); |
| 79 |
| 80 void DoRelease(); |
| 81 void DoConnect(const base::Closure& success_callback, |
| 82 const ErrorCompletionCallback& error_callback); |
| 83 void DoDisconnect(const base::Closure& callback); |
| 84 void DoReceive(int count, |
| 85 const ReceiveCompletionCallback& success_callback, |
| 86 const ReceiveErrorCompletionCallback& error_callback); |
| 87 void DoSend(scoped_refptr<net::IOBuffer> buffer, |
| 88 int buffer_size, |
| 89 const SendCompletionCallback& success_callback, |
| 90 const ErrorCompletionCallback& error_callback); |
| 91 |
| 92 void PostSuccess(const base::Closure& callback); |
| 93 void PostErrorCompletion(const ErrorCompletionCallback& callback, |
| 94 const std::string& error); |
| 95 void PostReceiveCompletion(const ReceiveCompletionCallback& callback, |
| 96 int io_buffer_size, |
| 97 scoped_refptr<net::IOBuffer> io_buffer); |
| 98 void PostReceiveErrorCompletion( |
| 99 const ReceiveErrorCompletionCallback& callback, |
| 100 ErrorReason reason, |
| 101 const std::string& error_message); |
| 102 void PostSendCompletion(const SendCompletionCallback& callback, |
| 103 int bytes_written); |
| 104 |
| 105 void SendFrontWriteRequest(); |
| 106 void OnSocketWriteComplete(const SendCompletionCallback& success_callback, |
| 107 const ErrorCompletionCallback& error_callback, |
| 108 int net_status); |
| 109 void OnSocketReadComplete( |
| 110 const ReceiveCompletionCallback& success_callback, |
| 111 const ReceiveErrorCompletionCallback& error_callback, |
| 112 int send_result); |
| 113 |
| 114 scoped_refptr<base::SequencedTaskRunner> ui_task_runner_; |
| 115 scoped_refptr<BluetoothSocketThreadWin> socket_thread_; |
| 116 net::NetLog* net_log_; |
| 117 const net::NetLog::Source source_; |
| 118 std::string device_address_; |
| 119 bool supports_rfcomm_; |
| 120 uint8 rfcomm_channel_; |
| 121 BTH_ADDR bth_addr_; |
| 122 scoped_ptr<net::TCPSocketWin> socket_win_; |
| 123 // Queue of pending writes. The buffer at the front of the queue is the one |
| 124 // being written. |
| 125 std::queue<linked_ptr<WriteRequest> > write_queue_; |
| 126 scoped_refptr<net::IOBufferWithSize> read_buffer_; |
46 | 127 |
47 DISALLOW_COPY_AND_ASSIGN(BluetoothSocketWin); | 128 DISALLOW_COPY_AND_ASSIGN(BluetoothSocketWin); |
48 }; | 129 }; |
49 | 130 |
50 } // namespace device | 131 } // namespace device |
51 | 132 |
52 #endif // DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_WIN_H_ | 133 #endif // DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_WIN_H_ |
OLD | NEW |