OLD | NEW |
1 // Copyright (c) 2012 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 #include "net/tools/quic/quic_client.h" | 5 #include "net/tools/quic/quic_client.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <netinet/in.h> | 8 #include <netinet/in.h> |
9 #include <string.h> | 9 #include <string.h> |
10 #include <sys/epoll.h> | 10 #include <sys/epoll.h> |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 bool QuicClient::Connect() { | 120 bool QuicClient::Connect() { |
121 if (!StartConnect()) { | 121 if (!StartConnect()) { |
122 return false; | 122 return false; |
123 } | 123 } |
124 while (CryptoHandshakeInProgress()) { | 124 while (CryptoHandshakeInProgress()) { |
125 WaitForEvents(); | 125 WaitForEvents(); |
126 } | 126 } |
127 return session_->connection()->connected(); | 127 return session_->connection()->connected(); |
128 } | 128 } |
129 | 129 |
130 | |
131 bool QuicClient::StartConnect() { | 130 bool QuicClient::StartConnect() { |
132 DCHECK(!connected() && initialized_); | 131 DCHECK(!connected() && initialized_); |
133 | 132 |
134 QuicGuid guid = QuicRandom::GetInstance()->RandUint64(); | 133 QuicGuid guid = QuicRandom::GetInstance()->RandUint64(); |
135 session_.reset(new QuicClientSession( | 134 session_.reset(new QuicClientSession( |
136 server_hostname_, | 135 server_hostname_, |
137 config_, | 136 config_, |
138 new QuicConnection(guid, server_address_, | 137 new QuicConnection(guid, server_address_, |
139 new QuicEpollConnectionHelper(fd_, &epoll_server_), | 138 new QuicEpollConnectionHelper(fd_, &epoll_server_), |
140 false), | 139 false), |
141 &crypto_config_)); | 140 &crypto_config_)); |
142 return session_->CryptoConnect(); | 141 return session_->CryptoConnect(); |
143 } | 142 } |
144 | 143 |
145 bool QuicClient::CryptoHandshakeInProgress() { | 144 bool QuicClient::CryptoHandshakeInProgress() { |
146 return !session_->IsCryptoHandshakeComplete() && | 145 return !session_->IsCryptoHandshakeComplete() && |
147 session_->connection()->connected(); | 146 session_->connection()->connected(); |
148 } | 147 } |
149 | 148 |
150 void QuicClient::Disconnect() { | 149 void QuicClient::Disconnect() { |
151 DCHECK(connected()); | 150 DCHECK(connected()); |
152 | 151 |
153 session()->connection()->SendConnectionClose(QUIC_PEER_GOING_AWAY); | 152 session()->connection()->SendConnectionClose(QUIC_PEER_GOING_AWAY); |
154 epoll_server_.UnregisterFD(fd_); | 153 epoll_server_.UnregisterFD(fd_); |
155 close(fd_); | 154 close(fd_); |
156 fd_ = -1; | 155 fd_ = -1; |
157 session_.reset(); | |
158 } | 156 } |
159 | 157 |
160 void QuicClient::SendRequestsAndWaitForResponse(int argc, char *argv[]) { | 158 void QuicClient::SendRequestsAndWaitForResponse(int argc, char *argv[]) { |
161 for (int i = 1; i < argc; ++i) { | 159 for (int i = 1; i < argc; ++i) { |
162 BalsaHeaders headers; | 160 BalsaHeaders headers; |
163 headers.SetRequestFirstlineFromStringPieces("GET", argv[i], "HTTP/1.1"); | 161 headers.SetRequestFirstlineFromStringPieces("GET", argv[i], "HTTP/1.1"); |
164 CreateReliableClientStream()->SendRequest(headers, "", true); | 162 CreateReliableClientStream()->SendRequest(headers, "", true); |
165 } | 163 } |
166 | 164 |
167 while (WaitForEvents()) { } | 165 while (WaitForEvents()) { } |
(...skipping 17 matching lines...) Expand all Loading... |
185 DCHECK(connected()); | 183 DCHECK(connected()); |
186 | 184 |
187 epoll_server_.WaitForEventsAndExecuteCallbacks(); | 185 epoll_server_.WaitForEventsAndExecuteCallbacks(); |
188 return session_->num_active_requests() != 0; | 186 return session_->num_active_requests() != 0; |
189 } | 187 } |
190 | 188 |
191 void QuicClient::OnEvent(int fd, EpollEvent* event) { | 189 void QuicClient::OnEvent(int fd, EpollEvent* event) { |
192 DCHECK_EQ(fd, fd_); | 190 DCHECK_EQ(fd, fd_); |
193 | 191 |
194 if (event->in_events & EPOLLIN) { | 192 if (event->in_events & EPOLLIN) { |
195 while (ReadAndProcessPacket()) { | 193 while (connected() && ReadAndProcessPacket()) { |
196 } | 194 } |
197 } | 195 } |
198 if (event->in_events & EPOLLOUT) { | 196 if (connected() && (event->in_events & EPOLLOUT)) { |
199 session_->connection()->OnCanWrite(); | 197 session_->connection()->OnCanWrite(); |
200 } | 198 } |
201 if (event->in_events & EPOLLERR) { | 199 if (event->in_events & EPOLLERR) { |
202 DLOG(INFO) << "Epollerr"; | 200 DLOG(INFO) << "Epollerr"; |
203 } | 201 } |
204 } | 202 } |
205 | 203 |
206 QuicPacketCreator::Options* QuicClient::options() { | 204 QuicPacketCreator::Options* QuicClient::options() { |
207 if (session() == NULL) { | 205 if (session() == NULL) { |
208 return NULL; | 206 return NULL; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 } | 244 } |
247 | 245 |
248 IPEndPoint client_address(client_ip, client_address_.port()); | 246 IPEndPoint client_address(client_ip, client_address_.port()); |
249 session_->connection()->ProcessUdpPacket( | 247 session_->connection()->ProcessUdpPacket( |
250 client_address, server_address, packet); | 248 client_address, server_address, packet); |
251 return true; | 249 return true; |
252 } | 250 } |
253 | 251 |
254 } // namespace tools | 252 } // namespace tools |
255 } // namespace net | 253 } // namespace net |
OLD | NEW |