OLD | NEW |
1 // Copyright (c) 2011 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 // An implementation of buzz::AsyncSocket that uses Chrome sockets. | 5 // An implementation of buzz::AsyncSocket that uses Chrome sockets. |
6 | 6 |
7 #ifndef JINGLE_NOTIFIER_BASE_CHROME_ASYNC_SOCKET_H_ | 7 #ifndef JINGLE_NOTIFIER_BASE_CHROME_ASYNC_SOCKET_H_ |
8 #define JINGLE_NOTIFIER_BASE_CHROME_ASYNC_SOCKET_H_ | 8 #define JINGLE_NOTIFIER_BASE_CHROME_ASYNC_SOCKET_H_ |
9 | 9 |
10 #if !defined(FEATURE_ENABLE_SSL) | 10 #if !defined(FEATURE_ENABLE_SSL) |
11 #error ChromeAsyncSocket expects FEATURE_ENABLE_SSL to be defined | 11 #error ChromeAsyncSocket expects FEATURE_ENABLE_SSL to be defined |
(...skipping 15 matching lines...) Expand all Loading... |
27 class IOBufferWithSize; | 27 class IOBufferWithSize; |
28 class StreamSocket; | 28 class StreamSocket; |
29 } // namespace net | 29 } // namespace net |
30 | 30 |
31 namespace notifier { | 31 namespace notifier { |
32 | 32 |
33 class ResolvingClientSocketFactory; | 33 class ResolvingClientSocketFactory; |
34 | 34 |
35 class ChromeAsyncSocket : public buzz::AsyncSocket { | 35 class ChromeAsyncSocket : public buzz::AsyncSocket { |
36 public: | 36 public: |
37 // Takes ownership of |client_socket_factory|. | 37 // Takes ownership of |resolving_client_socket_factory|. |
38 ChromeAsyncSocket(ResolvingClientSocketFactory* client_socket_factory, | 38 ChromeAsyncSocket( |
39 size_t read_buf_size, | 39 ResolvingClientSocketFactory* resolving_client_socket_factory, |
40 size_t write_buf_size); | 40 size_t read_buf_size, |
| 41 size_t write_buf_size); |
41 | 42 |
42 // Does not raise any signals. | 43 // Does not raise any signals. |
43 virtual ~ChromeAsyncSocket(); | 44 virtual ~ChromeAsyncSocket(); |
44 | 45 |
45 // buzz::AsyncSocket implementation. | 46 // buzz::AsyncSocket implementation. |
46 | 47 |
47 // The current state (see buzz::AsyncSocket::State; all but | 48 // The current state (see buzz::AsyncSocket::State; all but |
48 // STATE_CLOSING is used). | 49 // STATE_CLOSING is used). |
49 virtual State state() OVERRIDE; | 50 virtual State state() OVERRIDE; |
50 | 51 |
51 // The last generated error. Errors are generated when the main | 52 // The last generated error. Errors are generated when the main |
52 // functions below return false or when SignalClosed is raised due | 53 // functions below return false or when SignalClosed is raised due |
53 // to an asynchronous error. | 54 // to an asynchronous error. |
54 virtual Error error() OVERRIDE; | 55 virtual Error error() OVERRIDE; |
55 | 56 |
56 // GetError() (which is of type net::Error) != net::OK only when | 57 // GetError() (which is of type net::Error) != net::OK only when |
57 // error() == ERROR_WINSOCK. | 58 // error() == ERROR_WINSOCK. |
58 virtual int GetError() OVERRIDE; | 59 virtual int GetError() OVERRIDE; |
59 | 60 |
60 // Tries to connect to the given address. | 61 // Tries to connect to the given address. |
61 // | 62 // |
62 // If state() is not STATE_CLOSED, sets error to ERROR_WRONGSTATE | 63 // If state() is not STATE_CLOSED, sets error to ERROR_WRONGSTATE |
63 // and returns false. | 64 // and returns false. |
64 // | 65 // |
65 // If |address| is not resolved, sets error to ERROR_DNS and returns | 66 // If |address| has an empty hostname or a zero port, sets error to |
66 // false. | 67 // ERROR_DNS and returns false. (We don't use the IP address even |
| 68 // if it's present, as DNS resolution is done by |
| 69 // |resolving_client_socket_factory_|. But it's perfectly fine if |
| 70 // the hostname is a stringified IP address.) |
67 // | 71 // |
68 // Otherwise, starts the connection process and returns true. | 72 // Otherwise, starts the connection process and returns true. |
69 // SignalConnected will be raised when the connection is successful; | 73 // SignalConnected will be raised when the connection is successful; |
70 // otherwise, SignalClosed will be raised with a net error set. | 74 // otherwise, SignalClosed will be raised with a net error set. |
71 virtual bool Connect(const talk_base::SocketAddress& address) OVERRIDE; | 75 virtual bool Connect(const talk_base::SocketAddress& address) OVERRIDE; |
72 | 76 |
73 // Tries to read at most |len| bytes into |data|. | 77 // Tries to read at most |len| bytes into |data|. |
74 // | 78 // |
75 // If state() is not STATE_TLS_CONNECTING, STATE_OPEN, or | 79 // If state() is not STATE_TLS_CONNECTING, STATE_OPEN, or |
76 // STATE_TLS_OPEN, sets error to ERROR_WRONGSTATE and returns false. | 80 // STATE_TLS_OPEN, sets error to ERROR_WRONGSTATE and returns false. |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 void PostDoWrite(); | 173 void PostDoWrite(); |
170 void DoWrite(); | 174 void DoWrite(); |
171 void ProcessWriteDone(int status); | 175 void ProcessWriteDone(int status); |
172 | 176 |
173 // SSL/TLS connection functions. | 177 // SSL/TLS connection functions. |
174 void ProcessSSLConnectDone(int status); | 178 void ProcessSSLConnectDone(int status); |
175 | 179 |
176 // Close functions. | 180 // Close functions. |
177 void DoClose(); | 181 void DoClose(); |
178 | 182 |
179 scoped_ptr<ResolvingClientSocketFactory> client_socket_factory_; | 183 base::WeakPtrFactory<ChromeAsyncSocket> weak_ptr_factory_; |
| 184 scoped_ptr<ResolvingClientSocketFactory> resolving_client_socket_factory_; |
180 | 185 |
181 // buzz::AsyncSocket state. | 186 // buzz::AsyncSocket state. |
182 buzz::AsyncSocket::State state_; | 187 buzz::AsyncSocket::State state_; |
183 buzz::AsyncSocket::Error error_; | 188 buzz::AsyncSocket::Error error_; |
184 net::Error net_error_; | 189 net::Error net_error_; |
185 | 190 |
186 // Used by read/write loops. | |
187 base::WeakPtrFactory<ChromeAsyncSocket> weak_factory_; | |
188 | |
189 // NULL iff state() == STATE_CLOSED. | 191 // NULL iff state() == STATE_CLOSED. |
190 // | |
191 // TODO(akalin): Use ClientSocketPool. | |
192 scoped_ptr<net::StreamSocket> transport_socket_; | 192 scoped_ptr<net::StreamSocket> transport_socket_; |
193 | 193 |
194 // State for the read loop. |read_start_| <= |read_end_| <= | 194 // State for the read loop. |read_start_| <= |read_end_| <= |
195 // |read_buf_->size()|. There's a read in flight (i.e., | 195 // |read_buf_->size()|. There's a read in flight (i.e., |
196 // |read_state_| != IDLE) iff |read_end_| == 0. | 196 // |read_state_| != IDLE) iff |read_end_| == 0. |
197 AsyncIOState read_state_; | 197 AsyncIOState read_state_; |
198 scoped_refptr<net::IOBufferWithSize> read_buf_; | 198 scoped_refptr<net::IOBufferWithSize> read_buf_; |
199 size_t read_start_, read_end_; | 199 size_t read_start_, read_end_; |
200 | 200 |
201 // State for the write loop. |write_end_| <= |write_buf_->size()|. | 201 // State for the write loop. |write_end_| <= |write_buf_->size()|. |
202 // There's a write in flight (i.e., |write_state_| != IDLE) iff | 202 // There's a write in flight (i.e., |write_state_| != IDLE) iff |
203 // |write_end_| > 0. | 203 // |write_end_| > 0. |
204 AsyncIOState write_state_; | 204 AsyncIOState write_state_; |
205 scoped_refptr<net::IOBufferWithSize> write_buf_; | 205 scoped_refptr<net::IOBufferWithSize> write_buf_; |
206 size_t write_end_; | 206 size_t write_end_; |
207 | 207 |
208 DISALLOW_COPY_AND_ASSIGN(ChromeAsyncSocket); | 208 DISALLOW_COPY_AND_ASSIGN(ChromeAsyncSocket); |
209 }; | 209 }; |
210 | 210 |
211 } // namespace notifier | 211 } // namespace notifier |
212 | 212 |
213 #endif // JINGLE_NOTIFIER_BASE_CHROME_ASYNC_SOCKET_H_ | 213 #endif // JINGLE_NOTIFIER_BASE_CHROME_ASYNC_SOCKET_H_ |
OLD | NEW |