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 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_NET_H_ | |
6 #define DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_NET_H_ | |
7 | |
8 #include <queue> | |
9 #include <string> | |
10 | |
11 #include "base/basictypes.h" | |
12 #include "base/memory/linked_ptr.h" | |
13 #include "base/memory/ref_counted.h" | |
14 #include "base/memory/scoped_ptr.h" | |
15 #include "base/sequenced_task_runner.h" | |
16 #include "device/bluetooth/bluetooth_socket.h" | |
17 #include "device/bluetooth/bluetooth_socket_thread.h" | |
18 #include "net/base/net_log.h" | |
19 #include "net/socket/tcp_socket.h" | |
20 | |
21 namespace net { | |
22 class IOBuffer; | |
23 class IOBufferWithSize; | |
24 } // namespace net | |
25 | |
26 namespace device { | |
27 | |
28 // This class is a base-class for implementations of BluetoothSocket that can | |
29 // use net::TCPSocket. All public methods (including the factory method) must | |
30 // be called on the UI thread, while underlying socket operations are | |
31 // performed on a separate thread. | |
32 class BluetoothSocketNet : public BluetoothSocket { | |
33 public: | |
34 static scoped_refptr<BluetoothSocketNet> CreateBluetoothSocket( | |
35 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, | |
36 scoped_refptr<BluetoothSocketThread> socket_thread, | |
37 net::NetLog* net_log, | |
38 const net::NetLog::Source& source); | |
39 | |
40 // BluetoothSocket: | |
41 virtual void Close() OVERRIDE; | |
42 virtual void Disconnect(const base::Closure& callback) OVERRIDE; | |
43 virtual void Receive(int buffer_size, | |
44 const ReceiveCompletionCallback& success_callback, | |
45 const ReceiveErrorCompletionCallback& error_callback) | |
46 OVERRIDE; | |
47 virtual void Send(scoped_refptr<net::IOBuffer> buffer, | |
48 int buffer_size, | |
49 const SendCompletionCallback& success_callback, | |
50 const ErrorCompletionCallback& error_callback) OVERRIDE; | |
51 | |
52 protected: | |
53 BluetoothSocketNet(scoped_refptr<base::SequencedTaskRunner> ui_task_runner, | |
54 scoped_refptr<BluetoothSocketThread> socket_thread, | |
55 net::NetLog* net_log, | |
56 const net::NetLog::Source& source); | |
57 virtual ~BluetoothSocketNet(); | |
58 | |
59 // Resets locally held data after a socket is closed. Default implementation | |
60 // does nothing, subclasses may override. | |
61 void ResetData(); | |
armansito
2014/05/01 00:14:25
You should make this virtual if the intention is t
keybuk
2014/05/01 00:24:09
Done.
| |
62 | |
63 // Methods for subclasses to obtain the members. | |
64 scoped_refptr<base::SequencedTaskRunner> ui_task_runner() { | |
65 return ui_task_runner_; | |
66 } | |
67 | |
68 scoped_refptr<BluetoothSocketThread> socket_thread() { | |
69 return socket_thread_; | |
70 } | |
71 | |
72 net::NetLog* net_log() { return net_log_; } | |
73 const net::NetLog::Source& source() { return source_; } | |
74 | |
75 net::TCPSocket* tcp_socket() { return tcp_socket_.get(); } | |
76 | |
77 void ResetTCPSocket(); | |
78 void SetTCPSocket(scoped_ptr<net::TCPSocket> tcp_socket); | |
79 | |
80 void PostSuccess(const base::Closure& callback); | |
81 void PostErrorCompletion(const ErrorCompletionCallback& callback, | |
82 const std::string& error); | |
83 | |
84 private: | |
85 struct WriteRequest { | |
86 WriteRequest(); | |
87 ~WriteRequest(); | |
88 | |
89 scoped_refptr<net::IOBuffer> buffer; | |
90 int buffer_size; | |
91 SendCompletionCallback success_callback; | |
92 ErrorCompletionCallback error_callback; | |
93 }; | |
94 | |
95 void DoClose(); | |
96 void DoDisconnect(const base::Closure& success_callback); | |
97 void DoReceive(int buffer_size, | |
98 const ReceiveCompletionCallback& success_callback, | |
99 const ReceiveErrorCompletionCallback& error_callback); | |
100 void OnSocketReadComplete( | |
101 const ReceiveCompletionCallback& success_callback, | |
102 const ReceiveErrorCompletionCallback& error_callback, | |
103 int read_result); | |
104 void DoSend(scoped_refptr<net::IOBuffer> buffer, | |
105 int buffer_size, | |
106 const SendCompletionCallback& success_callback, | |
107 const ErrorCompletionCallback& error_callback); | |
108 void SendFrontWriteRequest(); | |
109 void OnSocketWriteComplete(const SendCompletionCallback& success_callback, | |
110 const ErrorCompletionCallback& error_callback, | |
111 int send_result); | |
112 | |
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 scoped_refptr<base::SequencedTaskRunner> ui_task_runner_; | |
124 scoped_refptr<BluetoothSocketThread> socket_thread_; | |
125 net::NetLog* net_log_; | |
126 const net::NetLog::Source source_; | |
127 | |
128 scoped_ptr<net::TCPSocket> tcp_socket_; | |
129 scoped_refptr<net::IOBufferWithSize> read_buffer_; | |
130 std::queue<linked_ptr<WriteRequest> > write_queue_; | |
131 | |
132 DISALLOW_COPY_AND_ASSIGN(BluetoothSocketNet); | |
133 }; | |
134 | |
135 } // namespace device | |
136 | |
137 #endif // DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_NET_H_ | |
OLD | NEW |