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