blob: 6473467d5094d7406de7f12f12d3038748dc3946 [file] [log] [blame]
David Ghandehari9e5b5872016-07-28 09:50:04 -07001// 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
3// found in the LICENSE file.
4//
5// A QuicSession, which demuxes a single connection to individual streams.
6
7#ifndef NET_QUIC_QUIC_SESSION_H_
8#define NET_QUIC_QUIC_SESSION_H_
9
10#include <vector>
11
12#include "base/compiler_specific.h"
13#include "base/hash_tables.h"
14#include "net/base/ip_endpoint.h"
15#include "net/quic/quic_connection.h"
16#include "net/quic/quic_crypto_stream.h"
17#include "net/quic/quic_packet_creator.h"
18#include "net/quic/quic_protocol.h"
19#include "net/quic/reliable_quic_stream.h"
20
21namespace net {
22
23class QuicCryptoStream;
24class ReliableQuicStream;
25
26class NET_EXPORT_PRIVATE QuicSession : public QuicConnectionVisitorInterface {
27 public:
28 QuicSession(QuicConnection* connection, bool is_server);
29
30 virtual ~QuicSession();
31
32 // QuicConnectionVisitorInterface methods:
33 virtual bool OnPacket(const IPEndPoint& self_address,
34 const IPEndPoint& peer_address,
35 const QuicPacketHeader& header,
36 const std::vector<QuicStreamFrame>& frame) OVERRIDE;
37 virtual void OnRstStream(const QuicRstStreamFrame& frame) OVERRIDE;
38 virtual void ConnectionClose(QuicErrorCode error, bool from_peer) OVERRIDE;
39 // Not needed for HTTP.
40 virtual void OnAck(AckedPackets acked_packets) OVERRIDE {}
41 virtual bool OnCanWrite() OVERRIDE;
42
43 // Called by streams when they want to write data to the peer.
44 virtual int WriteData(QuicStreamId id, base::StringPiece data,
45 QuicStreamOffset offset, bool fin);
46 // Called by streams when they want to close the stream in both directions.
47 void SendRstStream(QuicStreamId id,
48 QuicErrorCode error,
49 QuicStreamOffset offset);
50
51 // Removes the stream associated with 'stream_id' from the active stream map.
52 virtual void CloseStream(QuicStreamId stream_id);
53
54 // Returns true once the crypto handshake is complete.
55 virtual bool IsCryptoHandshakeComplete();
56
57 // Called by the QuicCryptoStream when the handshake completes.
58 // If |error| is QUIC_NO_ERROR then the handshake was succesful,
59 // otherwise it failed.
60 virtual void OnCryptoHandshakeComplete(QuicErrorCode error);
61
62 // Returns true if the stream existed previously and has been closed.
63 // Returns false if the stream is still active or if the stream has
64 // not yet been created.
65 bool IsClosedStream(QuicStreamId id);
66
67 QuicConnection* connection() { return connection_.get(); }
68 size_t num_active_requests() const { return stream_map_.size(); }
69 const IPEndPoint& peer_address() const {
70 return connection_->peer_address();
71 }
72
73 QuicPacketCreator::Options* options() { return connection()->options(); }
74
75 // Returns the number of currently open streams, including those which have
76 // been implicitly created.
77 virtual size_t GetNumOpenStreams();
78
79 void MarkWriteBlocked(QuicStreamId id);
80
81 protected:
82 // Creates a new stream, owned by the caller, to handle a peer-initiated
83 // stream. Returns NULL and does error handling if the stream can not be
84 // created.
85 virtual ReliableQuicStream* CreateIncomingReliableStream(QuicStreamId id) = 0;
86
87 // Create a new stream, owned by the caller, to handle a locally-initiated
88 // stream. Returns NULL if max streams have already been opened.
89 virtual ReliableQuicStream* CreateOutgoingReliableStream() = 0;
90
91 // Return the reserved crypto stream.
92 virtual QuicCryptoStream* GetCryptoStream() = 0;
93
94 // Adds 'stream' to the active stream map.
95 void ActivateStream(ReliableQuicStream* stream);
96
97 // Returns the stream id for a new stream.
98 QuicStreamId GetNextStreamId();
99
100 ReliableQuicStream* GetIncomingReliableStream(QuicStreamId stream_id);
101
102 size_t get_max_open_streams() const {
103 return max_open_streams_;
104 }
105
106 private:
107 friend class QuicSessionPeer;
108
109 typedef base::hash_map<QuicStreamId, ReliableQuicStream*> ReliableStreamMap;
110
111 ReliableQuicStream* GetStream(const QuicStreamId stream_id);
112
113 scoped_ptr<QuicConnection> connection_;
114
115 // Returns the maximum number of streams this connection can open.
116 const size_t max_open_streams_;
117
118 // Map from StreamId to pointers to streams that are owned by the caller.
119 ReliableStreamMap stream_map_;
120 QuicStreamId next_stream_id_;
121 bool is_server_;
122
123 // Set of stream ids that have been "implicitly created" by receipt
124 // of a stream id larger than the next expected stream id.
125 base::hash_set<QuicStreamId> implicitly_created_streams_;
126
127 // A list of streams which need to write more data.
128 std::list<QuicStreamId> write_blocked_streams_;
129
130 QuicStreamId largest_peer_created_stream_id_;
131};
132
133} // namespace net
134
135#endif // NET_QUIC_QUIC_SESSION_H_