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 #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 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 25 matching lines...) Expand all Loading... |
36 // socket. This object must not be connected. This object takes ownership of | 36 // socket. This object must not be connected. This object takes ownership of |
37 // the given socket and then acts as if Connect() had been called. This | 37 // the given socket and then acts as if Connect() had been called. This |
38 // function is used by TCPServerSocket() to adopt accepted connections | 38 // function is used by TCPServerSocket() to adopt accepted connections |
39 // and for testing. | 39 // and for testing. |
40 int AdoptSocket(int socket); | 40 int AdoptSocket(int socket); |
41 | 41 |
42 // Binds the socket to a local IP address and port. | 42 // Binds the socket to a local IP address and port. |
43 int Bind(const IPEndPoint& address); | 43 int Bind(const IPEndPoint& address); |
44 | 44 |
45 // StreamSocket implementation. | 45 // StreamSocket implementation. |
46 virtual int Connect(OldCompletionCallback* callback) OVERRIDE; | |
47 virtual int Connect(const CompletionCallback& callback) OVERRIDE; | 46 virtual int Connect(const CompletionCallback& callback) OVERRIDE; |
48 virtual void Disconnect() OVERRIDE; | 47 virtual void Disconnect() OVERRIDE; |
49 virtual bool IsConnected() const OVERRIDE; | 48 virtual bool IsConnected() const OVERRIDE; |
50 virtual bool IsConnectedAndIdle() const OVERRIDE; | 49 virtual bool IsConnectedAndIdle() const OVERRIDE; |
51 virtual int GetPeerAddress(AddressList* address) const OVERRIDE; | 50 virtual int GetPeerAddress(AddressList* address) const OVERRIDE; |
52 virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE; | 51 virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE; |
53 virtual const BoundNetLog& NetLog() const OVERRIDE; | 52 virtual const BoundNetLog& NetLog() const OVERRIDE; |
54 virtual void SetSubresourceSpeculation() OVERRIDE; | 53 virtual void SetSubresourceSpeculation() OVERRIDE; |
55 virtual void SetOmniboxSpeculation() OVERRIDE; | 54 virtual void SetOmniboxSpeculation() OVERRIDE; |
56 virtual bool WasEverUsed() const OVERRIDE; | 55 virtual bool WasEverUsed() const OVERRIDE; |
57 virtual bool UsingTCPFastOpen() const OVERRIDE; | 56 virtual bool UsingTCPFastOpen() const OVERRIDE; |
58 virtual int64 NumBytesRead() const OVERRIDE; | 57 virtual int64 NumBytesRead() const OVERRIDE; |
59 virtual base::TimeDelta GetConnectTimeMicros() const OVERRIDE; | 58 virtual base::TimeDelta GetConnectTimeMicros() const OVERRIDE; |
60 | 59 |
61 // Socket implementation. | 60 // Socket implementation. |
62 // Multiple outstanding requests are not supported. | 61 // Multiple outstanding requests are not supported. |
63 // Full duplex mode (reading and writing at the same time) is supported | 62 // Full duplex mode (reading and writing at the same time) is supported |
64 virtual int Read(IOBuffer* buf, | 63 virtual int Read(IOBuffer* buf, |
65 int buf_len, | 64 int buf_len, |
66 OldCompletionCallback* callback) OVERRIDE; | |
67 virtual int Read(IOBuffer* buf, | |
68 int buf_len, | |
69 const CompletionCallback& callback) OVERRIDE; | 65 const CompletionCallback& callback) OVERRIDE; |
70 virtual int Write(IOBuffer* buf, | 66 virtual int Write(IOBuffer* buf, |
71 int buf_len, | 67 int buf_len, |
72 OldCompletionCallback* callback) OVERRIDE; | 68 const CompletionCallback& callback) OVERRIDE; |
73 virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; | 69 virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; |
74 virtual bool SetSendBufferSize(int32 size) OVERRIDE; | 70 virtual bool SetSendBufferSize(int32 size) OVERRIDE; |
75 | 71 |
76 private: | 72 private: |
77 // State machine for connecting the socket. | 73 // State machine for connecting the socket. |
78 enum ConnectState { | 74 enum ConnectState { |
79 CONNECT_STATE_CONNECT, | 75 CONNECT_STATE_CONNECT, |
80 CONNECT_STATE_CONNECT_COMPLETE, | 76 CONNECT_STATE_CONNECT_COMPLETE, |
81 CONNECT_STATE_NONE, | 77 CONNECT_STATE_NONE, |
82 }; | 78 }; |
83 | 79 |
84 class ReadWatcher : public MessageLoopForIO::Watcher { | 80 class ReadWatcher : public MessageLoopForIO::Watcher { |
85 public: | 81 public: |
86 explicit ReadWatcher(TCPClientSocketLibevent* socket) : socket_(socket) {} | 82 explicit ReadWatcher(TCPClientSocketLibevent* socket) : socket_(socket) {} |
87 | 83 |
88 // MessageLoopForIO::Watcher methods | 84 // MessageLoopForIO::Watcher methods |
89 | 85 |
90 virtual void OnFileCanReadWithoutBlocking(int /* fd */) OVERRIDE { | 86 virtual void OnFileCanReadWithoutBlocking(int /* fd */) OVERRIDE { |
91 if (socket_->old_read_callback_) | 87 if (!socket_->read_callback_.is_null()) |
92 socket_->DidCompleteRead(); | 88 socket_->DidCompleteRead(); |
93 } | 89 } |
94 | 90 |
95 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE {} | 91 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE {} |
96 | 92 |
97 private: | 93 private: |
98 TCPClientSocketLibevent* const socket_; | 94 TCPClientSocketLibevent* const socket_; |
99 | 95 |
100 DISALLOW_COPY_AND_ASSIGN(ReadWatcher); | 96 DISALLOW_COPY_AND_ASSIGN(ReadWatcher); |
101 }; | 97 }; |
102 | 98 |
103 class WriteWatcher : public MessageLoopForIO::Watcher { | 99 class WriteWatcher : public MessageLoopForIO::Watcher { |
104 public: | 100 public: |
105 explicit WriteWatcher(TCPClientSocketLibevent* socket) : socket_(socket) {} | 101 explicit WriteWatcher(TCPClientSocketLibevent* socket) : socket_(socket) {} |
106 | 102 |
107 // MessageLoopForIO::Watcher implementation. | 103 // MessageLoopForIO::Watcher implementation. |
108 virtual void OnFileCanReadWithoutBlocking(int /* fd */) OVERRIDE {} | 104 virtual void OnFileCanReadWithoutBlocking(int /* fd */) OVERRIDE {} |
109 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE { | 105 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE { |
110 if (socket_->waiting_connect()) { | 106 if (socket_->waiting_connect()) { |
111 socket_->DidCompleteConnect(); | 107 socket_->DidCompleteConnect(); |
112 } else if (socket_->old_write_callback_ || | 108 } else if (!socket_->write_callback_.is_null()) { |
113 !socket_->write_callback_.is_null()) { | |
114 socket_->DidCompleteWrite(); | 109 socket_->DidCompleteWrite(); |
115 } | 110 } |
116 } | 111 } |
117 | 112 |
118 private: | 113 private: |
119 TCPClientSocketLibevent* const socket_; | 114 TCPClientSocketLibevent* const socket_; |
120 | 115 |
121 DISALLOW_COPY_AND_ASSIGN(WriteWatcher); | 116 DISALLOW_COPY_AND_ASSIGN(WriteWatcher); |
122 }; | 117 }; |
123 | 118 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 | 167 |
173 // The buffer used by OnSocketReady to retry Read requests | 168 // The buffer used by OnSocketReady to retry Read requests |
174 scoped_refptr<IOBuffer> read_buf_; | 169 scoped_refptr<IOBuffer> read_buf_; |
175 int read_buf_len_; | 170 int read_buf_len_; |
176 | 171 |
177 // The buffer used by OnSocketReady to retry Write requests | 172 // The buffer used by OnSocketReady to retry Write requests |
178 scoped_refptr<IOBuffer> write_buf_; | 173 scoped_refptr<IOBuffer> write_buf_; |
179 int write_buf_len_; | 174 int write_buf_len_; |
180 | 175 |
181 // External callback; called when read is complete. | 176 // External callback; called when read is complete. |
182 OldCompletionCallback* old_read_callback_; | |
183 CompletionCallback read_callback_; | 177 CompletionCallback read_callback_; |
184 | 178 |
185 // External callback; called when write is complete. | 179 // External callback; called when write is complete. |
186 OldCompletionCallback* old_write_callback_; | |
187 CompletionCallback write_callback_; | 180 CompletionCallback write_callback_; |
188 | 181 |
189 // The next state for the Connect() state machine. | 182 // The next state for the Connect() state machine. |
190 ConnectState next_connect_state_; | 183 ConnectState next_connect_state_; |
191 | 184 |
192 // The OS error that CONNECT_STATE_CONNECT last completed with. | 185 // The OS error that CONNECT_STATE_CONNECT last completed with. |
193 int connect_os_error_; | 186 int connect_os_error_; |
194 | 187 |
195 BoundNetLog net_log_; | 188 BoundNetLog net_log_; |
196 | 189 |
(...skipping 13 matching lines...) Expand all Loading... |
210 base::TimeTicks connect_start_time_; | 203 base::TimeTicks connect_start_time_; |
211 base::TimeDelta connect_time_micros_; | 204 base::TimeDelta connect_time_micros_; |
212 int64 num_bytes_read_; | 205 int64 num_bytes_read_; |
213 | 206 |
214 DISALLOW_COPY_AND_ASSIGN(TCPClientSocketLibevent); | 207 DISALLOW_COPY_AND_ASSIGN(TCPClientSocketLibevent); |
215 }; | 208 }; |
216 | 209 |
217 } // namespace net | 210 } // namespace net |
218 | 211 |
219 #endif // NET_SOCKET_TCP_CLIENT_SOCKET_LIBEVENT_H_ | 212 #endif // NET_SOCKET_TCP_CLIENT_SOCKET_LIBEVENT_H_ |
OLD | NEW |