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> | |
11 #include <string> | 10 #include <string> |
12 | 11 |
13 #include "base/memory/linked_ptr.h" | |
14 #include "base/memory/ref_counted.h" | 12 #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_service_record_win.h" |
17 #include "device/bluetooth/bluetooth_socket.h" | 14 #include "device/bluetooth/bluetooth_socket.h" |
| 15 #include "device/bluetooth/bluetooth_socket_net.h" |
18 #include "net/base/ip_endpoint.h" | 16 #include "net/base/ip_endpoint.h" |
19 #include "net/base/net_log.h" | |
20 #include "net/socket/tcp_socket.h" | 17 #include "net/socket/tcp_socket.h" |
21 | 18 |
22 namespace net { | |
23 class IOBuffer; | |
24 class IOBufferWithSize; | |
25 } // namespace net | |
26 | |
27 namespace device { | 19 namespace device { |
28 | 20 |
29 class BluetoothServiceRecord; | 21 class BluetoothServiceRecord; |
30 class BluetoothSocketThreadWin; | |
31 | 22 |
32 // This class is an implementation of BluetoothSocket class for the Windows | 23 // The BluetoothSocketWin class implements BluetoothSocket for the Microsoft |
33 // platform. All public methods (including the factory method) must be called | 24 // Windows platform. |
34 // on the UI thread, while underlying socket operations are performed on a | 25 class BluetoothSocketWin : public BluetoothSocketNet { |
35 // separated thread. | |
36 class BluetoothSocketWin : public BluetoothSocket { | |
37 public: | 26 public: |
38 typedef base::Callback<void(scoped_refptr<BluetoothSocketWin>, | 27 typedef base::Callback<void(scoped_refptr<BluetoothSocketWin>, |
39 const net::IPEndPoint&)> OnNewConnectionCallback; | 28 const net::IPEndPoint&)> OnNewConnectionCallback; |
40 | 29 |
41 static scoped_refptr<BluetoothSocketWin> CreateBluetoothSocket( | 30 static scoped_refptr<BluetoothSocketWin> CreateBluetoothSocket( |
42 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, | 31 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, |
43 scoped_refptr<BluetoothSocketThreadWin> socket_thread, | 32 scoped_refptr<BluetoothSocketThread> socket_thread, |
44 net::NetLog* net_log, | 33 net::NetLog* net_log, |
45 const net::NetLog::Source& source); | 34 const net::NetLog::Source& source); |
46 | 35 |
47 // Starts a service with the given uuid, name and rfcomm_channel. | 36 // Starts a service with the given uuid, name and rfcomm_channel. |
48 // |success_callback| is invoked when the underlying socket is created | 37 // |success_callback| is invoked when the underlying socket is created |
49 // and the service is published successfully. Otherwise, |error_callback| is | 38 // and the service is published successfully. Otherwise, |error_callback| is |
50 // called with an error message. |new_connection_callback| is invoked when | 39 // called with an error message. |new_connection_callback| is invoked when |
51 // an incoming connection is accepted by the underlying socket. | 40 // an incoming connection is accepted by the underlying socket. |
52 void StartService( | 41 void StartService( |
53 const BluetoothUUID& uuid, | 42 const BluetoothUUID& uuid, |
54 const std::string& name, | 43 const std::string& name, |
55 int rfcomm_channel, | 44 int rfcomm_channel, |
56 const base::Closure& success_callback, | 45 const base::Closure& success_callback, |
57 const ErrorCompletionCallback& error_callback, | 46 const ErrorCompletionCallback& error_callback, |
58 const OnNewConnectionCallback& new_connection_callback); | 47 const OnNewConnectionCallback& new_connection_callback); |
59 | 48 |
60 // connection has been established successfully. If an error occurs, calls | 49 // connection has been established successfully. If an error occurs, calls |
61 // |error_callback| with a system error message. | 50 // |error_callback| with a system error message. |
62 void Connect(const BluetoothServiceRecord& service_record, | 51 void Connect(const BluetoothServiceRecord& service_record, |
63 const base::Closure& success_callback, | 52 const base::Closure& success_callback, |
64 const ErrorCompletionCallback& error_callback); | 53 const ErrorCompletionCallback& error_callback); |
65 | 54 |
66 // Overriden from BluetoothSocket: | 55 // BluetoothSocketNet: |
67 virtual void Close() OVERRIDE; | 56 void ResetData(); |
68 | |
69 virtual void Disconnect(const base::Closure& callback) OVERRIDE; | |
70 | |
71 virtual void Receive(int buffer_size, | |
72 const ReceiveCompletionCallback& success_callback, | |
73 const ReceiveErrorCompletionCallback& error_callback) | |
74 OVERRIDE; | |
75 virtual void Send(scoped_refptr<net::IOBuffer> buffer, | |
76 int buffer_size, | |
77 const SendCompletionCallback& success_callback, | |
78 const ErrorCompletionCallback& error_callback) OVERRIDE; | |
79 | 57 |
80 protected: | 58 protected: |
81 virtual ~BluetoothSocketWin(); | 59 virtual ~BluetoothSocketWin(); |
82 | 60 |
83 private: | 61 private: |
84 struct ServiceRegData; | 62 struct ServiceRegData; |
85 | 63 |
86 struct WriteRequest { | |
87 scoped_refptr<net::IOBuffer> buffer; | |
88 int buffer_size; | |
89 SendCompletionCallback success_callback; | |
90 ErrorCompletionCallback error_callback; | |
91 }; | |
92 | |
93 BluetoothSocketWin(scoped_refptr<base::SequencedTaskRunner> ui_task_runner, | 64 BluetoothSocketWin(scoped_refptr<base::SequencedTaskRunner> ui_task_runner, |
94 scoped_refptr<BluetoothSocketThreadWin> socket_thread, | 65 scoped_refptr<BluetoothSocketThread> socket_thread, |
95 net::NetLog* net_log, | 66 net::NetLog* net_log, |
96 const net::NetLog::Source& source); | 67 const net::NetLog::Source& source); |
97 | 68 |
98 void DoClose(); | 69 |
99 void DoConnect(const base::Closure& success_callback, | 70 void DoConnect(const base::Closure& success_callback, |
100 const ErrorCompletionCallback& error_callback); | 71 const ErrorCompletionCallback& error_callback); |
101 void DoDisconnect(const base::Closure& callback); | |
102 void DoReceive(int buffer_size, | |
103 const ReceiveCompletionCallback& success_callback, | |
104 const ReceiveErrorCompletionCallback& error_callback); | |
105 void DoSend(scoped_refptr<net::IOBuffer> buffer, | |
106 int buffer_size, | |
107 const SendCompletionCallback& success_callback, | |
108 const ErrorCompletionCallback& error_callback); | |
109 | |
110 void PostSuccess(const base::Closure& callback); | |
111 void PostErrorCompletion(const ErrorCompletionCallback& callback, | |
112 const std::string& error); | |
113 void PostReceiveCompletion(const ReceiveCompletionCallback& callback, | |
114 int io_buffer_size, | |
115 scoped_refptr<net::IOBuffer> io_buffer); | |
116 void PostReceiveErrorCompletion( | |
117 const ReceiveErrorCompletionCallback& callback, | |
118 ErrorReason reason, | |
119 const std::string& error_message); | |
120 void PostSendCompletion(const SendCompletionCallback& callback, | |
121 int bytes_written); | |
122 | |
123 void SendFrontWriteRequest(); | |
124 void OnSocketWriteComplete(const SendCompletionCallback& success_callback, | |
125 const ErrorCompletionCallback& error_callback, | |
126 int net_status); | |
127 void OnSocketReadComplete( | |
128 const ReceiveCompletionCallback& success_callback, | |
129 const ReceiveErrorCompletionCallback& error_callback, | |
130 int send_result); | |
131 | |
132 void DoStartService(const BluetoothUUID& uuid, | 72 void DoStartService(const BluetoothUUID& uuid, |
133 const std::string& name, | 73 const std::string& name, |
134 int rfcomm_channel, | 74 int rfcomm_channel, |
135 const base::Closure& success_callback, | 75 const base::Closure& success_callback, |
136 const ErrorCompletionCallback& error_callback, | 76 const ErrorCompletionCallback& error_callback, |
137 const OnNewConnectionCallback& new_connection_callback); | 77 const OnNewConnectionCallback& new_connection_callback); |
138 void DoAccept(); | 78 void DoAccept(); |
139 void OnAcceptOnSocketThread(int accept_result); | 79 void OnAcceptOnSocketThread(int accept_result); |
140 void OnAcceptOnUI(scoped_ptr<net::TCPSocket> accept_socket, | 80 void OnAcceptOnUI(scoped_ptr<net::TCPSocket> accept_socket, |
141 const net::IPEndPoint& peer_address); | 81 const net::IPEndPoint& peer_address); |
142 | 82 |
143 scoped_refptr<base::SequencedTaskRunner> ui_task_runner_; | |
144 scoped_refptr<BluetoothSocketThreadWin> socket_thread_; | |
145 net::NetLog* net_log_; | |
146 const net::NetLog::Source source_; | |
147 std::string device_address_; | 83 std::string device_address_; |
148 bool supports_rfcomm_; | 84 bool supports_rfcomm_; |
149 uint8 rfcomm_channel_; | 85 uint8 rfcomm_channel_; |
150 BTH_ADDR bth_addr_; | 86 BTH_ADDR bth_addr_; |
151 scoped_ptr<net::TCPSocket> tcp_socket_; | |
152 // Queue of pending writes. The buffer at the front of the queue is the one | |
153 // being written. | |
154 std::queue<linked_ptr<WriteRequest> > write_queue_; | |
155 scoped_refptr<net::IOBufferWithSize> read_buffer_; | |
156 | 87 |
157 scoped_ptr<ServiceRegData> service_reg_data_; | 88 scoped_ptr<ServiceRegData> service_reg_data_; |
158 scoped_ptr<net::TCPSocket> accept_socket_; | 89 scoped_ptr<net::TCPSocket> accept_socket_; |
159 net::IPEndPoint accept_address_; | 90 net::IPEndPoint accept_address_; |
160 OnNewConnectionCallback on_new_connection_callback_; | 91 OnNewConnectionCallback on_new_connection_callback_; |
161 | 92 |
162 DISALLOW_COPY_AND_ASSIGN(BluetoothSocketWin); | 93 DISALLOW_COPY_AND_ASSIGN(BluetoothSocketWin); |
163 }; | 94 }; |
164 | 95 |
165 } // namespace device | 96 } // namespace device |
166 | 97 |
167 #endif // DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_WIN_H_ | 98 #endif // DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_WIN_H_ |
OLD | NEW |