| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 NET_SOCKET_TCP_CLIENT_SOCKET_LIBEVENT_H_ | 5 #ifndef NET_SOCKET_TCP_CLIENT_SOCKET_LIBEVENT_H_ |
| 6 #define NET_SOCKET_TCP_CLIENT_SOCKET_LIBEVENT_H_ | 6 #define NET_SOCKET_TCP_CLIENT_SOCKET_LIBEVENT_H_ |
| 7 | 7 |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/ref_counted.h" | 9 #include "base/ref_counted.h" |
| 10 #include "base/scoped_ptr.h" | 10 #include "base/scoped_ptr.h" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 | 40 |
| 41 // Socket methods: | 41 // Socket methods: |
| 42 // Multiple outstanding requests are not supported. | 42 // Multiple outstanding requests are not supported. |
| 43 // Full duplex mode (reading and writing at the same time) is supported | 43 // Full duplex mode (reading and writing at the same time) is supported |
| 44 virtual int Read(IOBuffer* buf, int buf_len, CompletionCallback* callback); | 44 virtual int Read(IOBuffer* buf, int buf_len, CompletionCallback* callback); |
| 45 virtual int Write(IOBuffer* buf, int buf_len, CompletionCallback* callback); | 45 virtual int Write(IOBuffer* buf, int buf_len, CompletionCallback* callback); |
| 46 virtual bool SetReceiveBufferSize(int32 size); | 46 virtual bool SetReceiveBufferSize(int32 size); |
| 47 virtual bool SetSendBufferSize(int32 size); | 47 virtual bool SetSendBufferSize(int32 size); |
| 48 | 48 |
| 49 private: | 49 private: |
| 50 // State machine for connecting the socket. |
| 51 enum ConnectState { |
| 52 CONNECT_STATE_CONNECT, |
| 53 CONNECT_STATE_CONNECT_COMPLETE, |
| 54 CONNECT_STATE_NONE, |
| 55 }; |
| 56 |
| 50 class ReadWatcher : public MessageLoopForIO::Watcher { | 57 class ReadWatcher : public MessageLoopForIO::Watcher { |
| 51 public: | 58 public: |
| 52 explicit ReadWatcher(TCPClientSocketLibevent* socket) : socket_(socket) {} | 59 explicit ReadWatcher(TCPClientSocketLibevent* socket) : socket_(socket) {} |
| 53 | 60 |
| 54 // MessageLoopForIO::Watcher methods | 61 // MessageLoopForIO::Watcher methods |
| 55 | 62 |
| 56 virtual void OnFileCanReadWithoutBlocking(int /* fd */) { | 63 virtual void OnFileCanReadWithoutBlocking(int /* fd */) { |
| 57 if (socket_->read_callback_) | 64 if (socket_->read_callback_) |
| 58 socket_->DidCompleteRead(); | 65 socket_->DidCompleteRead(); |
| 59 } | 66 } |
| 60 | 67 |
| 61 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) {} | 68 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) {} |
| 62 | 69 |
| 63 private: | 70 private: |
| 64 TCPClientSocketLibevent* const socket_; | 71 TCPClientSocketLibevent* const socket_; |
| 65 | 72 |
| 66 DISALLOW_COPY_AND_ASSIGN(ReadWatcher); | 73 DISALLOW_COPY_AND_ASSIGN(ReadWatcher); |
| 67 }; | 74 }; |
| 68 | 75 |
| 69 class WriteWatcher : public MessageLoopForIO::Watcher { | 76 class WriteWatcher : public MessageLoopForIO::Watcher { |
| 70 public: | 77 public: |
| 71 explicit WriteWatcher(TCPClientSocketLibevent* socket) : socket_(socket) {} | 78 explicit WriteWatcher(TCPClientSocketLibevent* socket) : socket_(socket) {} |
| 72 | 79 |
| 73 // MessageLoopForIO::Watcher methods | 80 // MessageLoopForIO::Watcher methods |
| 74 | 81 |
| 75 virtual void OnFileCanReadWithoutBlocking(int /* fd */) {} | 82 virtual void OnFileCanReadWithoutBlocking(int /* fd */) {} |
| 76 | 83 |
| 77 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) { | 84 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) { |
| 78 if (socket_->waiting_connect_) { | 85 if (socket_->waiting_connect()) { |
| 79 socket_->DidCompleteConnect(); | 86 socket_->DidCompleteConnect(); |
| 80 } else if (socket_->write_callback_) { | 87 } else if (socket_->write_callback_) { |
| 81 socket_->DidCompleteWrite(); | 88 socket_->DidCompleteWrite(); |
| 82 } | 89 } |
| 83 } | 90 } |
| 84 | 91 |
| 85 private: | 92 private: |
| 86 TCPClientSocketLibevent* const socket_; | 93 TCPClientSocketLibevent* const socket_; |
| 87 | 94 |
| 88 DISALLOW_COPY_AND_ASSIGN(WriteWatcher); | 95 DISALLOW_COPY_AND_ASSIGN(WriteWatcher); |
| 89 }; | 96 }; |
| 90 | 97 |
| 91 // Performs the actual connect(). Returns a net error code. | 98 // State machine used by Connect(). |
| 99 int DoConnectLoop(int result); |
| 92 int DoConnect(); | 100 int DoConnect(); |
| 101 int DoConnectComplete(int result); |
| 102 |
| 103 // Helper used by Disconnect(), which disconnects minus the logging and |
| 104 // resetting of current_ai_. |
| 105 void DoDisconnect(); |
| 106 |
| 93 | 107 |
| 94 void DoReadCallback(int rv); | 108 void DoReadCallback(int rv); |
| 95 void DoWriteCallback(int rv); | 109 void DoWriteCallback(int rv); |
| 96 void DidCompleteRead(); | 110 void DidCompleteRead(); |
| 97 void DidCompleteWrite(); | 111 void DidCompleteWrite(); |
| 98 void DidCompleteConnect(); | 112 void DidCompleteConnect(); |
| 99 | 113 |
| 114 // Returns true if a Connect() is in progress. |
| 115 bool waiting_connect() const { |
| 116 return next_connect_state_ != CONNECT_STATE_NONE; |
| 117 } |
| 118 |
| 119 // Returns the OS error code (or 0 on success). |
| 100 int CreateSocket(const struct addrinfo* ai); | 120 int CreateSocket(const struct addrinfo* ai); |
| 101 | 121 |
| 102 int socket_; | 122 int socket_; |
| 103 | 123 |
| 104 // The list of addresses we should try in order to establish a connection. | 124 // The list of addresses we should try in order to establish a connection. |
| 105 AddressList addresses_; | 125 AddressList addresses_; |
| 106 | 126 |
| 107 // Where we are in above list, or NULL if all addrinfos have been tried. | 127 // Where we are in above list, or NULL if all addrinfos have been tried. |
| 108 const struct addrinfo* current_ai_; | 128 const struct addrinfo* current_ai_; |
| 109 | 129 |
| 110 // Whether we're currently waiting for connect() to complete | |
| 111 bool waiting_connect_; | |
| 112 | |
| 113 // The socket's libevent wrappers | 130 // The socket's libevent wrappers |
| 114 MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_; | 131 MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_; |
| 115 MessageLoopForIO::FileDescriptorWatcher write_socket_watcher_; | 132 MessageLoopForIO::FileDescriptorWatcher write_socket_watcher_; |
| 116 | 133 |
| 117 // The corresponding watchers for reads and writes. | 134 // The corresponding watchers for reads and writes. |
| 118 ReadWatcher read_watcher_; | 135 ReadWatcher read_watcher_; |
| 119 WriteWatcher write_watcher_; | 136 WriteWatcher write_watcher_; |
| 120 | 137 |
| 121 // The buffer used by OnSocketReady to retry Read requests | 138 // The buffer used by OnSocketReady to retry Read requests |
| 122 scoped_refptr<IOBuffer> read_buf_; | 139 scoped_refptr<IOBuffer> read_buf_; |
| 123 int read_buf_len_; | 140 int read_buf_len_; |
| 124 | 141 |
| 125 // The buffer used by OnSocketReady to retry Write requests | 142 // The buffer used by OnSocketReady to retry Write requests |
| 126 scoped_refptr<IOBuffer> write_buf_; | 143 scoped_refptr<IOBuffer> write_buf_; |
| 127 int write_buf_len_; | 144 int write_buf_len_; |
| 128 | 145 |
| 129 // External callback; called when read is complete. | 146 // External callback; called when read is complete. |
| 130 CompletionCallback* read_callback_; | 147 CompletionCallback* read_callback_; |
| 131 | 148 |
| 132 // External callback; called when write is complete. | 149 // External callback; called when write is complete. |
| 133 CompletionCallback* write_callback_; | 150 CompletionCallback* write_callback_; |
| 134 | 151 |
| 152 // The next state for the Connect() state machine. |
| 153 ConnectState next_connect_state_; |
| 154 |
| 135 BoundNetLog net_log_; | 155 BoundNetLog net_log_; |
| 136 | 156 |
| 137 DISALLOW_COPY_AND_ASSIGN(TCPClientSocketLibevent); | 157 DISALLOW_COPY_AND_ASSIGN(TCPClientSocketLibevent); |
| 138 }; | 158 }; |
| 139 | 159 |
| 140 } // namespace net | 160 } // namespace net |
| 141 | 161 |
| 142 #endif // NET_SOCKET_TCP_CLIENT_SOCKET_LIBEVENT_H_ | 162 #endif // NET_SOCKET_TCP_CLIENT_SOCKET_LIBEVENT_H_ |
| OLD | NEW |