| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 |
| 12 #endif | 12 #endif |
| 13 | 13 |
| 14 #include <string> | 14 #include <string> |
| 15 #include <vector> | 15 #include <vector> |
| 16 | 16 |
| 17 #include "base/basictypes.h" | 17 #include "base/basictypes.h" |
| 18 #include "base/compiler_specific.h" |
| 18 #include "base/memory/ref_counted.h" | 19 #include "base/memory/ref_counted.h" |
| 19 #include "base/memory/scoped_ptr.h" | 20 #include "base/memory/scoped_ptr.h" |
| 20 #include "base/memory/weak_ptr.h" | 21 #include "base/memory/weak_ptr.h" |
| 21 #include "net/base/completion_callback.h" | 22 #include "net/base/completion_callback.h" |
| 22 #include "net/base/net_errors.h" | 23 #include "net/base/net_errors.h" |
| 23 #include "talk/xmpp/asyncsocket.h" | 24 #include "talk/xmpp/asyncsocket.h" |
| 24 | 25 |
| 25 namespace net { | 26 namespace net { |
| 26 class IOBufferWithSize; | 27 class IOBufferWithSize; |
| 27 class StreamSocket; | 28 class StreamSocket; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 38 size_t read_buf_size, | 39 size_t read_buf_size, |
| 39 size_t write_buf_size); | 40 size_t write_buf_size); |
| 40 | 41 |
| 41 // Does not raise any signals. | 42 // Does not raise any signals. |
| 42 virtual ~ChromeAsyncSocket(); | 43 virtual ~ChromeAsyncSocket(); |
| 43 | 44 |
| 44 // buzz::AsyncSocket implementation. | 45 // buzz::AsyncSocket implementation. |
| 45 | 46 |
| 46 // The current state (see buzz::AsyncSocket::State; all but | 47 // The current state (see buzz::AsyncSocket::State; all but |
| 47 // STATE_CLOSING is used). | 48 // STATE_CLOSING is used). |
| 48 virtual State state(); | 49 virtual State state() OVERRIDE; |
| 49 | 50 |
| 50 // The last generated error. Errors are generated when the main | 51 // The last generated error. Errors are generated when the main |
| 51 // functions below return false or when SignalClosed is raised due | 52 // functions below return false or when SignalClosed is raised due |
| 52 // to an asynchronous error. | 53 // to an asynchronous error. |
| 53 virtual Error error(); | 54 virtual Error error() OVERRIDE; |
| 54 | 55 |
| 55 // GetError() (which is of type net::Error) != net::OK only when | 56 // GetError() (which is of type net::Error) != net::OK only when |
| 56 // error() == ERROR_WINSOCK. | 57 // error() == ERROR_WINSOCK. |
| 57 virtual int GetError(); | 58 virtual int GetError() OVERRIDE; |
| 58 | 59 |
| 59 // Tries to connect to the given address. | 60 // Tries to connect to the given address. |
| 60 // | 61 // |
| 61 // If state() is not STATE_CLOSED, sets error to ERROR_WRONGSTATE | 62 // If state() is not STATE_CLOSED, sets error to ERROR_WRONGSTATE |
| 62 // and returns false. | 63 // and returns false. |
| 63 // | 64 // |
| 64 // If |address| is not resolved, sets error to ERROR_DNS and returns | 65 // If |address| is not resolved, sets error to ERROR_DNS and returns |
| 65 // false. | 66 // false. |
| 66 // | 67 // |
| 67 // Otherwise, starts the connection process and returns true. | 68 // Otherwise, starts the connection process and returns true. |
| 68 // SignalConnected will be raised when the connection is successful; | 69 // SignalConnected will be raised when the connection is successful; |
| 69 // otherwise, SignalClosed will be raised with a net error set. | 70 // otherwise, SignalClosed will be raised with a net error set. |
| 70 virtual bool Connect(const talk_base::SocketAddress& address); | 71 virtual bool Connect(const talk_base::SocketAddress& address) OVERRIDE; |
| 71 | 72 |
| 72 // Tries to read at most |len| bytes into |data|. | 73 // Tries to read at most |len| bytes into |data|. |
| 73 // | 74 // |
| 74 // If state() is not STATE_TLS_CONNECTING, STATE_OPEN, or | 75 // If state() is not STATE_TLS_CONNECTING, STATE_OPEN, or |
| 75 // STATE_TLS_OPEN, sets error to ERROR_WRONGSTATE and returns false. | 76 // STATE_TLS_OPEN, sets error to ERROR_WRONGSTATE and returns false. |
| 76 // | 77 // |
| 77 // Otherwise, fills in |len_read| with the number of bytes read and | 78 // Otherwise, fills in |len_read| with the number of bytes read and |
| 78 // returns true. If this is called when state() is | 79 // returns true. If this is called when state() is |
| 79 // STATE_TLS_CONNECTING, reads 0 bytes. (We have to handle this | 80 // STATE_TLS_CONNECTING, reads 0 bytes. (We have to handle this |
| 80 // case because StartTls() is called during a slot connected to | 81 // case because StartTls() is called during a slot connected to |
| 81 // SignalRead after parsing the final non-TLS reply from the server | 82 // SignalRead after parsing the final non-TLS reply from the server |
| 82 // [see XmppClient::Private::OnSocketRead()].) | 83 // [see XmppClient::Private::OnSocketRead()].) |
| 83 virtual bool Read(char* data, size_t len, size_t* len_read); | 84 virtual bool Read(char* data, size_t len, size_t* len_read) OVERRIDE; |
| 84 | 85 |
| 85 // Queues up |len| bytes of |data| for writing. | 86 // Queues up |len| bytes of |data| for writing. |
| 86 // | 87 // |
| 87 // If state() is not STATE_TLS_CONNECTING, STATE_OPEN, or | 88 // If state() is not STATE_TLS_CONNECTING, STATE_OPEN, or |
| 88 // STATE_TLS_OPEN, sets error to ERROR_WRONGSTATE and returns false. | 89 // STATE_TLS_OPEN, sets error to ERROR_WRONGSTATE and returns false. |
| 89 // | 90 // |
| 90 // If the given data is too big for the internal write buffer, sets | 91 // If the given data is too big for the internal write buffer, sets |
| 91 // error to ERROR_WINSOCK/net::ERR_INSUFFICIENT_RESOURCES and | 92 // error to ERROR_WINSOCK/net::ERR_INSUFFICIENT_RESOURCES and |
| 92 // returns false. | 93 // returns false. |
| 93 // | 94 // |
| 94 // Otherwise, queues up the data and returns true. If this is | 95 // Otherwise, queues up the data and returns true. If this is |
| 95 // called when state() == STATE_TLS_CONNECTING, the data is will be | 96 // called when state() == STATE_TLS_CONNECTING, the data is will be |
| 96 // sent only after the TLS connection succeeds. (See StartTls() | 97 // sent only after the TLS connection succeeds. (See StartTls() |
| 97 // below for why this happens.) | 98 // below for why this happens.) |
| 98 // | 99 // |
| 99 // Note that there's no guarantee that the data will actually be | 100 // Note that there's no guarantee that the data will actually be |
| 100 // sent; however, it is guaranteed that the any data sent will be | 101 // sent; however, it is guaranteed that the any data sent will be |
| 101 // sent in FIFO order. | 102 // sent in FIFO order. |
| 102 virtual bool Write(const char* data, size_t len); | 103 virtual bool Write(const char* data, size_t len) OVERRIDE; |
| 103 | 104 |
| 104 // If the socket is not already closed, closes the socket and raises | 105 // If the socket is not already closed, closes the socket and raises |
| 105 // SignalClosed. Always returns true. | 106 // SignalClosed. Always returns true. |
| 106 virtual bool Close(); | 107 virtual bool Close() OVERRIDE; |
| 107 | 108 |
| 108 // Tries to change to a TLS connection with the given domain name. | 109 // Tries to change to a TLS connection with the given domain name. |
| 109 // | 110 // |
| 110 // If state() is not STATE_OPEN or there are pending reads or | 111 // If state() is not STATE_OPEN or there are pending reads or |
| 111 // writes, sets error to ERROR_WRONGSTATE and returns false. (In | 112 // writes, sets error to ERROR_WRONGSTATE and returns false. (In |
| 112 // practice, this means that StartTls() can only be called from a | 113 // practice, this means that StartTls() can only be called from a |
| 113 // slot connected to SignalRead.) | 114 // slot connected to SignalRead.) |
| 114 // | 115 // |
| 115 // Otherwise, starts the TLS connection process and returns true. | 116 // Otherwise, starts the TLS connection process and returns true. |
| 116 // SignalSSLConnected will be raised when the connection is | 117 // SignalSSLConnected will be raised when the connection is |
| 117 // successful; otherwise, SignalClosed will be raised with a net | 118 // successful; otherwise, SignalClosed will be raised with a net |
| 118 // error set. | 119 // error set. |
| 119 virtual bool StartTls(const std::string& domain_name); | 120 virtual bool StartTls(const std::string& domain_name) OVERRIDE; |
| 120 | 121 |
| 121 // Signal behavior: | 122 // Signal behavior: |
| 122 // | 123 // |
| 123 // SignalConnected: raised whenever the connect initiated by a call | 124 // SignalConnected: raised whenever the connect initiated by a call |
| 124 // to Connect() is complete. | 125 // to Connect() is complete. |
| 125 // | 126 // |
| 126 // SignalSSLConnected: raised whenever the connect initiated by a | 127 // SignalSSLConnected: raised whenever the connect initiated by a |
| 127 // call to StartTls() is complete. Not actually used by | 128 // call to StartTls() is complete. Not actually used by |
| 128 // XmppClient. (It just assumes that if SignalRead is raised after a | 129 // XmppClient. (It just assumes that if SignalRead is raised after a |
| 129 // call to StartTls(), the connection has been successfully | 130 // call to StartTls(), the connection has been successfully |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 AsyncIOState write_state_; | 210 AsyncIOState write_state_; |
| 210 scoped_refptr<net::IOBufferWithSize> write_buf_; | 211 scoped_refptr<net::IOBufferWithSize> write_buf_; |
| 211 size_t write_end_; | 212 size_t write_end_; |
| 212 | 213 |
| 213 DISALLOW_COPY_AND_ASSIGN(ChromeAsyncSocket); | 214 DISALLOW_COPY_AND_ASSIGN(ChromeAsyncSocket); |
| 214 }; | 215 }; |
| 215 | 216 |
| 216 } // namespace notifier | 217 } // namespace notifier |
| 217 | 218 |
| 218 #endif // JINGLE_NOTIFIER_BASE_CHROME_ASYNC_SOCKET_H_ | 219 #endif // JINGLE_NOTIFIER_BASE_CHROME_ASYNC_SOCKET_H_ |
| OLD | NEW |