1 /** @file
2  *
3  *  A brief file description
4  *
5  *  @section license License
6  *
7  *  Licensed to the Apache Software Foundation (ASF) under one
8  *  or more contributor license agreements.  See the NOTICE file
9  *  distributed with this work for additional information
10  *  regarding copyright ownership.  The ASF licenses this file
11  *  to you under the Apache License, Version 2.0 (the
12  *  "License"); you may not use this file except in compliance
13  *  with the License.  You may obtain a copy of the License at
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
17  *  Unless required by applicable law or agreed to in writing, software
18  *  distributed under the License is distributed on an "AS IS" BASIS,
19  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  *  See the License for the specific language governing permissions and
21  *  limitations under the License.
22  */
23 
24 #pragma once
25 
26 #include "QUICStream.h"
27 
28 class QUICSendStream : public QUICStreamVConnection
29 {
30 public:
31   QUICSendStream(QUICConnectionInfoProvider *cinfo, QUICStreamId sid, uint64_t send_max_stream_data);
QUICSendStream()32   QUICSendStream() : _remote_flow_controller(0, 0), _state(nullptr, nullptr) {}
33 
~QUICSendStream()34   ~QUICSendStream() {}
35 
36   int state_stream_open(int event, void *data);
37   int state_stream_closed(int event, void *data);
38 
39   // QUICFrameGenerator
40   bool will_generate_frame(QUICEncryptionLevel level, size_t current_packet_size, bool ack_eliciting, uint32_t seq_num) override;
41   QUICFrame *generate_frame(uint8_t *buf, QUICEncryptionLevel level, uint64_t connection_credit, uint16_t maximum_frame_size,
42                             size_t current_packet_size, uint32_t seq_num) override;
43 
44   virtual QUICConnectionErrorUPtr recv(const QUICMaxStreamDataFrame &frame) override;
45   virtual QUICConnectionErrorUPtr recv(const QUICStopSendingFrame &frame) override;
46 
47   // Implement VConnection Interface.
48   VIO *do_io_read(Continuation *c, int64_t nbytes = INT64_MAX, MIOBuffer *buf = 0) override;
49   VIO *do_io_write(Continuation *c = nullptr, int64_t nbytes = INT64_MAX, IOBufferReader *buf = 0, bool owner = false) override;
50   void do_io_close(int lerrno = -1) override;
51   void do_io_shutdown(ShutdownHowTo_t howto) override;
52   void reenable(VIO *vio) override;
53 
54   void reset(QUICStreamErrorUPtr error) override;
55 
56   QUICOffset largest_offset_sent() const override;
57 
58 private:
59   QUICStreamErrorUPtr _reset_reason = nullptr;
60   bool _is_reset_sent               = false;
61 
62   bool _is_transfer_complete = false;
63   bool _is_reset_complete    = false;
64 
65   QUICTransferProgressProviderVIO _progress_vio = {this->_read_vio};
66 
67   QUICRemoteStreamFlowController _remote_flow_controller;
68 
69   QUICSendStreamStateMachine _state;
70 
71   // QUICFrameGenerator
72   void _on_frame_acked(QUICFrameInformationUPtr &info) override;
73   void _on_frame_lost(QUICFrameInformationUPtr &info) override;
74 };
75 
76 class QUICReceiveStream : public QUICStreamVConnection, public QUICTransferProgressProvider
77 {
78 public:
79   QUICReceiveStream(QUICRTTProvider *rtt_provider, QUICConnectionInfoProvider *cinfo, QUICStreamId sid,
80                     uint64_t recv_max_stream_data);
QUICReceiveStream()81   QUICReceiveStream() : _local_flow_controller(nullptr, 0, 0), _state(nullptr, nullptr) {}
82 
~QUICReceiveStream()83   ~QUICReceiveStream() {}
84 
85   int state_stream_open(int event, void *data);
86   int state_stream_closed(int event, void *data);
87 
88   // QUICFrameGenerator
89   bool will_generate_frame(QUICEncryptionLevel level, size_t current_packet_size, bool ack_eliciting, uint32_t seq_num) override;
90   QUICFrame *generate_frame(uint8_t *buf, QUICEncryptionLevel level, uint64_t connection_credit, uint16_t maximum_frame_size,
91                             size_t current_packet_size, uint32_t seq_num) override;
92 
93   virtual QUICConnectionErrorUPtr recv(const QUICStreamFrame &frame) override;
94   virtual QUICConnectionErrorUPtr recv(const QUICStreamDataBlockedFrame &frame) override;
95   virtual QUICConnectionErrorUPtr recv(const QUICRstStreamFrame &frame) override;
96 
97   // Implement VConnection Interface.
98   VIO *do_io_read(Continuation *c, int64_t nbytes = INT64_MAX, MIOBuffer *buf = 0) override;
99   VIO *do_io_write(Continuation *c = nullptr, int64_t nbytes = INT64_MAX, IOBufferReader *buf = 0, bool owner = false) override;
100   void do_io_close(int lerrno = -1) override;
101   void do_io_shutdown(ShutdownHowTo_t howto) override;
102   void reenable(VIO *vio) override;
103 
104   // QUICTransferProgressProvider
105   bool is_transfer_goal_set() const override;
106   uint64_t transfer_progress() const override;
107   uint64_t transfer_goal() const override;
108   bool is_cancelled() const override;
109 
110   /*
111    * QUICApplication need to call one of these functions when it process VC_EVENT_*
112    */
113   virtual void on_read() override;
114   virtual void on_eos() override;
115 
116   QUICOffset largest_offset_received() const override;
117 
118   void stop_sending(QUICStreamErrorUPtr error) override;
119 
120 private:
121   QUICStreamErrorUPtr _stop_sending_reason = nullptr;
122   bool _is_stop_sending_sent               = false;
123   bool _is_stop_sending_complete           = false;
124 
125   QUICLocalStreamFlowController _local_flow_controller;
126   uint64_t _flow_control_buffer_size = 1024;
127 
128   // Fragments of received STREAM frame (offset is unmatched)
129   // TODO: Consider to replace with ts/RbTree.h or other data structure
130   QUICIncomingStreamFrameBuffer _received_stream_frame_buffer;
131 
132   QUICReceiveStreamStateMachine _state;
133 
134   // QUICFrameGenerator
135   void _on_frame_acked(QUICFrameInformationUPtr &info) override;
136   void _on_frame_lost(QUICFrameInformationUPtr &info) override;
137 };
138