Http2WebTransportStream.h (3788B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 #ifndef mozilla_net_Http2WebTransportStream_h 7 #define mozilla_net_Http2WebTransportStream_h 8 9 #include <functional> 10 11 #include "mozilla/CheckedInt.h" 12 #include "mozilla/Queue.h" 13 #include "WebTransportFlowControl.h" 14 #include "WebTransportStreamBase.h" 15 16 namespace mozilla::net { 17 18 class Capsule; 19 class Http2WebTransportSessionImpl; 20 21 class StreamData final { 22 public: 23 explicit StreamData(nsTArray<uint8_t>&& aData) : mData(std::move(aData)) { 24 MOZ_COUNT_CTOR(StreamData); 25 } 26 27 MOZ_COUNTED_DTOR(StreamData) 28 29 const nsTArray<uint8_t>& GetData() const { return mData; } 30 31 private: 32 nsTArray<uint8_t> mData; 33 }; 34 35 class Http2WebTransportStream final : public WebTransportStreamBase { 36 public: 37 NS_DECL_THREADSAFE_ISUPPORTS 38 NS_DECL_NSIINPUTSTREAMCALLBACK 39 NS_DECL_NSIOUTPUTSTREAMCALLBACK 40 41 explicit Http2WebTransportStream( 42 Http2WebTransportSessionImpl* aWebTransportSession, StreamId aStreamId, 43 uint64_t aInitialMaxStreamData, uint64_t aInitialLocalMaxStreamData, 44 std::function<void(Result<RefPtr<WebTransportStreamBase>, nsresult>&&)>&& 45 aCallback); 46 47 explicit Http2WebTransportStream( 48 Http2WebTransportSessionImpl* aWebTransportSession, 49 uint64_t aInitialMaxStreamData, uint64_t aInitialLocalMaxStreamData, 50 StreamId aStreamId); 51 52 nsresult Init(); 53 54 StreamId WebTransportStreamId() const override; 55 uint64_t GetStreamId() const override; 56 void SendStopSending(uint8_t aErrorCode) override; 57 void SendFin() override; 58 void Reset(uint64_t aErrorCode) override; 59 already_AddRefed<nsIWebTransportSendStreamStats> GetSendStreamStats() 60 override; 61 already_AddRefed<nsIWebTransportReceiveStreamStats> GetReceiveStreamStats() 62 override; 63 bool RecvDone() const override; 64 void SetSendOrder(Maybe<int64_t> aSendOrder) override; 65 SenderFlowControlBase* SenderFc() override { return &mFc; } 66 ReceiverFlowControlBase* ReceiverFc() override { return &mReceiverFc; } 67 68 nsresult OnCapsule(Capsule&& aCapsule); 69 void Close(nsresult aResult); 70 void WriteMaintenanceCapsules( 71 mozilla::Queue<UniquePtr<CapsuleEncoder>>& aOutput); 72 void TakeOutputCapsule(mozilla::Queue<UniquePtr<CapsuleEncoder>>& aOutput); 73 74 void OnStopSending(); 75 void OnReset(uint64_t aSize); 76 void OnStreamDataSent(size_t aCount); 77 78 private: 79 virtual ~Http2WebTransportStream(); 80 81 static nsresult ReadRequestSegment(nsIInputStream*, void*, const char*, 82 uint32_t, uint32_t, uint32_t*); 83 84 nsresult HandleStreamData(bool aFin, nsTArray<uint8_t>&& aData); 85 nsresult HandleMaxStreamData(uint64_t aLimit); 86 nsresult HandleStopSending(uint64_t aError); 87 88 RefPtr<Http2WebTransportSessionImpl> mWebTransportSession; 89 class StreamId mStreamId{0u}; 90 nsTArray<uint8_t> mBuffer; 91 CheckedUint64 mTotalSent{0}; 92 uint64_t mTotalReceived = 0; 93 Maybe<uint64_t> mReliableSize; 94 uint32_t mWriteOffset = 0; 95 bool mSentStopSending = false; 96 bool mSentReset = false; 97 // The queue used for passing data to the upper layer. 98 // When mReceiveStreamPipeOut->Write() returns NS_BASE_STREAM_WOULD_BLOCK, we 99 // need to store the data in this queue. 100 mozilla::Queue<UniquePtr<StreamData>> mOutgoingQueue; 101 mozilla::Queue<UniquePtr<CapsuleEncoder>> mCapsuleQueue; 102 UniquePtr<StreamData> mCurrentOut; 103 const RefPtr<nsISerialEventTarget> mOwnerThread; 104 SenderFlowControlStreamId mFc; 105 ReceiverFlowControlStreamId mReceiverFc; 106 Maybe<Capsule> mStopSendingCapsule; 107 Maybe<Capsule> mStreamResetCapsule; 108 }; 109 } // namespace mozilla::net 110 111 #endif