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 QUICBidirectionalStream : public QUICStreamVConnection, public QUICTransferProgressProvider
29 {
30 public:
31   QUICBidirectionalStream(QUICRTTProvider *rtt_provider, QUICConnectionInfoProvider *cinfo, QUICStreamId sid,
32                           uint64_t recv_max_stream_data, uint64_t send_max_stream_data);
QUICBidirectionalStream()33   QUICBidirectionalStream()
34     : QUICStreamVConnection(),
35       _remote_flow_controller(0, 0),
36       _local_flow_controller(nullptr, 0, 0),
37       _state(nullptr, nullptr, nullptr, nullptr)
38   {
39   }
40 
~QUICBidirectionalStream()41   ~QUICBidirectionalStream() {}
42 
43   int state_stream_open(int event, void *data);
44   int state_stream_closed(int event, void *data);
45 
46   // QUICFrameGenerator
47   bool will_generate_frame(QUICEncryptionLevel level, size_t current_packet_size, bool ack_eliciting, uint32_t seq_num) override;
48   QUICFrame *generate_frame(uint8_t *buf, QUICEncryptionLevel level, uint64_t connection_credit, uint16_t maximum_frame_size,
49                             size_t current_packet_size, uint32_t seq_num) override;
50 
51   virtual QUICConnectionErrorUPtr recv(const QUICStreamFrame &frame) override;
52   virtual QUICConnectionErrorUPtr recv(const QUICMaxStreamDataFrame &frame) override;
53   virtual QUICConnectionErrorUPtr recv(const QUICStreamDataBlockedFrame &frame) override;
54   virtual QUICConnectionErrorUPtr recv(const QUICStopSendingFrame &frame) override;
55   virtual QUICConnectionErrorUPtr recv(const QUICRstStreamFrame &frame) override;
56 
57   // Implement VConnection Interface.
58   VIO *do_io_read(Continuation *c, int64_t nbytes = INT64_MAX, MIOBuffer *buf = 0) override;
59   VIO *do_io_write(Continuation *c = nullptr, int64_t nbytes = INT64_MAX, IOBufferReader *buf = 0, bool owner = false) override;
60   void do_io_close(int lerrno = -1) override;
61   void do_io_shutdown(ShutdownHowTo_t howto) override;
62   void reenable(VIO *vio) override;
63 
64   void stop_sending(QUICStreamErrorUPtr error) override;
65   void reset(QUICStreamErrorUPtr error) override;
66 
67   // QUICTransferProgressProvider
68   bool is_transfer_goal_set() const override;
69   uint64_t transfer_progress() const override;
70   uint64_t transfer_goal() const override;
71   bool is_cancelled() const override;
72 
73   /*
74    * QUICApplication need to call one of these functions when it process VC_EVENT_*
75    */
76   virtual void on_read() override;
77   virtual void on_eos() override;
78 
79   QUICOffset largest_offset_received() const override;
80   QUICOffset largest_offset_sent() const override;
81 
82 private:
83   QUICStreamErrorUPtr _reset_reason        = nullptr;
84   bool _is_reset_sent                      = false;
85   QUICStreamErrorUPtr _stop_sending_reason = nullptr;
86   bool _is_stop_sending_sent               = false;
87 
88   bool _is_transfer_complete = false;
89   bool _is_reset_complete    = false;
90 
91   QUICTransferProgressProviderVIO _progress_vio = {this->_write_vio};
92 
93   QUICRemoteStreamFlowController _remote_flow_controller;
94   QUICLocalStreamFlowController _local_flow_controller;
95   uint64_t _flow_control_buffer_size = 1024;
96 
97   // Fragments of received STREAM frame (offset is unmatched)
98   // TODO: Consider to replace with ts/RbTree.h or other data structure
99   QUICIncomingStreamFrameBuffer _received_stream_frame_buffer;
100 
101   // FIXME Unidirectional streams should use either ReceiveStreamState or SendStreamState
102   QUICBidirectionalStreamStateMachine _state;
103 
104   // QUICFrameGenerator
105   void _on_frame_acked(QUICFrameInformationUPtr &info) override;
106   void _on_frame_lost(QUICFrameInformationUPtr &info) override;
107 };
108