CapsuleEncoder.h (1940B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set sw=2 ts=8 et tw=80 : */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_net_capsule_encoder_h 8 #define mozilla_net_capsule_encoder_h 9 10 #include "mozilla/Maybe.h" 11 #include "mozilla/net/NeqoHttp3Conn.h" 12 #include "mozilla/RefPtr.h" 13 #include "mozilla/Span.h" 14 #include "mozilla/net/NeqoHttp3Conn.h" 15 #include "nsTArray.h" 16 17 namespace mozilla::net { 18 19 struct StreamMetadata { 20 uint64_t mID{0}; 21 size_t mDataSize{0}; 22 uint64_t mStartOfData{0}; 23 }; 24 25 // A wrapper of neqo_common::Encoder. 26 class CapsuleEncoder final { 27 public: 28 CapsuleEncoder(); 29 ~CapsuleEncoder(); 30 31 // Serializes the given capsule into the internal buffer owned by mEncoder. 32 void EncodeCapsule(Capsule& aCapsule); 33 // Provides external access to the mEncoder's internal buffer which contains 34 // the serialized capsule data. This function outputs a pointer to the start 35 // of the buffer along with its current length. 36 mozilla::Span<const uint8_t> GetBuffer(); 37 38 Maybe<StreamMetadata>& GetStreamMetadata() { return mStreamMetadata; } 39 40 private: 41 RefPtr<NeqoEncoder> mEncoder; 42 43 CapsuleEncoder& EncodeByte(uint8_t aData); 44 45 template <typename T> 46 CapsuleEncoder& EncodeUint(uint32_t aSize, T aValue); 47 48 template <typename T> 49 CapsuleEncoder& EncodeVarint(T aValue); 50 51 CapsuleEncoder& EncodeString(const nsACString& aData); 52 CapsuleEncoder& EncodeBuffer(nsTArray<uint8_t>& aData); 53 CapsuleEncoder& EncodeBufferWithVarintLen(nsTArray<uint8_t>& aData); 54 55 static size_t VarintLength(uint64_t aValue); 56 57 // When this encoder contains a WT_STREAM capsule, this field tracks metadata 58 // for the stream data being sent. 59 Maybe<StreamMetadata> mStreamMetadata; 60 }; 61 62 } // namespace mozilla::net 63 64 #endif