| 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/quic/quic_client_session.h" | 5 #include "net/quic/quic_client_session.h" |
| 6 | 6 |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "net/base/io_buffer.h" | 11 #include "net/base/io_buffer.h" |
| 12 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
| 13 #include "net/quic/quic_connection_helper.h" | 13 #include "net/quic/quic_connection_helper.h" |
| 14 #include "net/quic/quic_stream_factory.h" | 14 #include "net/quic/quic_stream_factory.h" |
| 15 #include "net/udp/datagram_client_socket.h" | 15 #include "net/udp/datagram_client_socket.h" |
| 16 | 16 |
| 17 namespace net { | 17 namespace net { |
| 18 | 18 |
| 19 QuicClientSession::QuicClientSession(QuicConnection* connection, | 19 QuicClientSession::QuicClientSession(QuicConnection* connection, |
| 20 QuicConnectionHelper* helper, | 20 QuicConnectionHelper* helper, |
| 21 QuicStreamFactory* stream_factory, | 21 QuicStreamFactory* stream_factory, |
| 22 const string& server_hostname) | 22 const string& server_hostname, |
| 23 NetLog* net_log) |
| 23 : QuicSession(connection, false), | 24 : QuicSession(connection, false), |
| 24 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), | 25 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), |
| 25 ALLOW_THIS_IN_INITIALIZER_LIST(crypto_stream_(this, server_hostname)), | 26 ALLOW_THIS_IN_INITIALIZER_LIST(crypto_stream_(this, server_hostname)), |
| 26 helper_(helper), | 27 helper_(helper), |
| 27 stream_factory_(stream_factory), | 28 stream_factory_(stream_factory), |
| 28 read_buffer_(new IOBufferWithSize(kMaxPacketSize)), | 29 read_buffer_(new IOBufferWithSize(kMaxPacketSize)), |
| 29 read_pending_(false) { | 30 read_pending_(false), |
| 31 num_total_streams_(0), |
| 32 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_QUIC_SESSION)) { |
| 33 // TODO(rch): pass in full host port proxy pair |
| 34 net_log_.BeginEvent( |
| 35 NetLog::TYPE_QUIC_SESSION, |
| 36 NetLog::StringCallback("host", &server_hostname)); |
| 30 } | 37 } |
| 31 | 38 |
| 32 QuicClientSession::~QuicClientSession() { | 39 QuicClientSession::~QuicClientSession() { |
| 40 net_log_.EndEvent(NetLog::TYPE_QUIC_SESSION); |
| 33 } | 41 } |
| 34 | 42 |
| 35 QuicReliableClientStream* QuicClientSession::CreateOutgoingReliableStream() { | 43 QuicReliableClientStream* QuicClientSession::CreateOutgoingReliableStream() { |
| 36 if (!crypto_stream_.handshake_complete()) { | 44 if (!crypto_stream_.handshake_complete()) { |
| 37 DLOG(INFO) << "Crypto handshake not complete, no outgoing stream created."; | 45 DLOG(INFO) << "Crypto handshake not complete, no outgoing stream created."; |
| 38 return NULL; | 46 return NULL; |
| 39 } | 47 } |
| 40 if (GetNumOpenStreams() >= get_max_open_streams()) { | 48 if (GetNumOpenStreams() >= get_max_open_streams()) { |
| 41 DLOG(INFO) << "Failed to create a new outgoing stream. " | 49 DLOG(INFO) << "Failed to create a new outgoing stream. " |
| 42 << "Already " << GetNumOpenStreams() << " open."; | 50 << "Already " << GetNumOpenStreams() << " open."; |
| 43 return NULL; | 51 return NULL; |
| 44 } | 52 } |
| 45 QuicReliableClientStream* stream = | 53 QuicReliableClientStream* stream = |
| 46 new QuicReliableClientStream(GetNextStreamId(), this); | 54 new QuicReliableClientStream(GetNextStreamId(), this, net_log_); |
| 47 ActivateStream(stream); | 55 ActivateStream(stream); |
| 56 ++num_total_streams_; |
| 48 return stream; | 57 return stream; |
| 49 } | 58 } |
| 50 | 59 |
| 51 QuicCryptoClientStream* QuicClientSession::GetCryptoStream() { | 60 QuicCryptoClientStream* QuicClientSession::GetCryptoStream() { |
| 52 return &crypto_stream_; | 61 return &crypto_stream_; |
| 53 }; | 62 }; |
| 54 | 63 |
| 55 int QuicClientSession::CryptoConnect(const CompletionCallback& callback) { | 64 int QuicClientSession::CryptoConnect(const CompletionCallback& callback) { |
| 56 if (!crypto_stream_.CryptoConnect()) { | 65 if (!crypto_stream_.CryptoConnect()) { |
| 57 // TODO(wtc): change crypto_stream_.CryptoConnect() to return a | 66 // TODO(wtc): change crypto_stream_.CryptoConnect() to return a |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 } | 118 } |
| 110 | 119 |
| 111 void QuicClientSession::CloseSessionOnError(int error) { | 120 void QuicClientSession::CloseSessionOnError(int error) { |
| 112 while (!streams()->empty()) { | 121 while (!streams()->empty()) { |
| 113 ReliableQuicStream* stream = streams()->begin()->second; | 122 ReliableQuicStream* stream = streams()->begin()->second; |
| 114 QuicStreamId id = stream->id(); | 123 QuicStreamId id = stream->id(); |
| 115 static_cast<QuicReliableClientStream*>(stream)->OnError(error); | 124 static_cast<QuicReliableClientStream*>(stream)->OnError(error); |
| 116 CloseStream(id); | 125 CloseStream(id); |
| 117 } | 126 } |
| 118 stream_factory_->OnSessionClose(this); | 127 stream_factory_->OnSessionClose(this); |
| 128 net_log_.BeginEvent( |
| 129 NetLog::TYPE_QUIC_SESSION, |
| 130 NetLog::IntegerCallback("net_error", error)); |
| 119 } | 131 } |
| 120 | 132 |
| 121 Value* QuicClientSession::GetInfoAsValue(const HostPortPair& pair) const { | 133 Value* QuicClientSession::GetInfoAsValue(const HostPortPair& pair) const { |
| 122 DictionaryValue* dict = new DictionaryValue(); | 134 DictionaryValue* dict = new DictionaryValue(); |
| 123 dict->SetString("host_port_pair", pair.ToString()); | 135 dict->SetString("host_port_pair", pair.ToString()); |
| 124 dict->SetInteger("open_streams", GetNumOpenStreams()); | 136 dict->SetInteger("open_streams", GetNumOpenStreams()); |
| 137 dict->SetInteger("total_streams", num_total_streams_); |
| 125 dict->SetString("peer_address", peer_address().ToString()); | 138 dict->SetString("peer_address", peer_address().ToString()); |
| 126 dict->SetString("guid", base::Uint64ToString(guid())); | 139 dict->SetString("guid", base::Uint64ToString(guid())); |
| 127 return dict; | 140 return dict; |
| 128 } | 141 } |
| 129 | 142 |
| 130 void QuicClientSession::OnReadComplete(int result) { | 143 void QuicClientSession::OnReadComplete(int result) { |
| 131 read_pending_ = false; | 144 read_pending_ = false; |
| 132 // TODO(rch): Inform the connection about the result. | 145 // TODO(rch): Inform the connection about the result. |
| 133 if (result > 0) { | 146 if (result > 0) { |
| 134 scoped_refptr<IOBufferWithSize> buffer(read_buffer_); | 147 scoped_refptr<IOBufferWithSize> buffer(read_buffer_); |
| 135 read_buffer_ = new IOBufferWithSize(kMaxPacketSize); | 148 read_buffer_ = new IOBufferWithSize(kMaxPacketSize); |
| 136 QuicEncryptedPacket packet(buffer->data(), result); | 149 QuicEncryptedPacket packet(buffer->data(), result); |
| 137 IPEndPoint local_address; | 150 IPEndPoint local_address; |
| 138 IPEndPoint peer_address; | 151 IPEndPoint peer_address; |
| 139 helper_->GetLocalAddress(&local_address); | 152 helper_->GetLocalAddress(&local_address); |
| 140 helper_->GetPeerAddress(&peer_address); | 153 helper_->GetPeerAddress(&peer_address); |
| 141 // ProcessUdpPacket might result in |this| being deleted, so we | 154 // ProcessUdpPacket might result in |this| being deleted, so we |
| 142 // use a weak pointer to be safe. | 155 // use a weak pointer to be safe. |
| 143 connection()->ProcessUdpPacket(local_address, peer_address, packet); | 156 connection()->ProcessUdpPacket(local_address, peer_address, packet); |
| 144 if (!connection()->connected()) { | 157 if (!connection()->connected()) { |
| 145 stream_factory_->OnSessionClose(this); | 158 stream_factory_->OnSessionClose(this); |
| 146 return; | 159 return; |
| 147 } | 160 } |
| 148 StartReading(); | 161 StartReading(); |
| 149 } | 162 } |
| 150 } | 163 } |
| 151 | 164 |
| 152 } // namespace net | 165 } // namespace net |
| OLD | NEW |