forked from facebookexperimental/moxygen
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMoQTypes.h
More file actions
1248 lines (1071 loc) · 34.5 KB
/
Copy pathMoQTypes.h
File metadata and controls
1248 lines (1071 loc) · 34.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* This source code is licensed under the Apache 2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <folly/Expected.h>
#include <folly/hash/Hash.h>
#include <folly/io/IOBuf.h>
#include <algorithm>
#include <optional>
#include <vector>
namespace moxygen {
//////// Constants ////////
const size_t kMaxNamespaceLength = 32;
const uint8_t kDefaultPriority = 128;
// From QUIC, until MOQ supports 64 bit varint
constexpr uint64_t kEightByteLimit = 0x3FFFFFFFFFFFFFFF;
// Extension types
const uint64_t kImmutableExtensionType = 0xB;
const uint64_t kPriorGroupIdGapExtensionType = 0x3C;
const uint64_t kPriorObjectIdGapExtensionType = 0x3E;
// Track Property Extension Types (Draft-16+)
constexpr uint64_t kDeliveryTimeoutExtensionType = 0x02;
constexpr uint64_t kMaxCacheDurationExtensionType = 0x04;
constexpr uint64_t kPublisherPriorityExtensionType = 0x0E;
constexpr uint64_t kPublisherGroupOrderExtensionType = 0x22;
constexpr uint64_t kDynamicGroupsExtensionType = 0x30;
// End of Range serialization flags (MOQT spec)
// Used in FETCH responses to indicate ranges of missing/unknown objects
constexpr uint64_t kSerializationFlagEndOfNonExistentRange = 0x8C;
constexpr uint64_t kSerializationFlagEndOfUnknownRange = 0x10C;
//////// Types ////////
using Payload = std::unique_ptr<folly::IOBuf>;
using Priority = uint8_t;
// Undefine Windows macros that conflict with our enum values
#ifdef NO_ERROR
#undef NO_ERROR
#endif
#ifdef DELETE
#undef DELETE
#endif
enum class SessionCloseErrorCode : uint32_t {
NO_ERROR = 0,
INTERNAL_ERROR = 1,
UNAUTHORIZED = 2,
PROTOCOL_VIOLATION = 3,
INVALID_REQUEST_ID = 4,
DUPLICATE_TRACK_ALIAS = 5,
KEY_VALUE_FORMATTING_ERROR = 6,
TOO_MANY_REQUESTS = 7,
INVALID_PATH = 8,
MALFORMED_PATH = 9,
GOAWAY_TIMEOUT = 0x10,
CONTROL_MESSAGE_TIMEOUT = 0x11,
DATA_STREAM_TIMEOUT = 0x12,
AUTH_TOKEN_CACHE_OVERFLOW = 0x13,
DUPLICATE_AUTH_TOKEN_ALIAS = 0x14,
VERSION_NEGOTIATION_FAILED = 0x15,
MALFORMED_AUTH_TOKEN = 0x16,
UNKNOWN_AUTH_TOKEN_ALIAS = 0x17,
EXPIRED_AUTH_TOKEN = 0x18,
INVALID_AUTHORITY = 0x19,
MALFORMED_AUTHORITY = 0x1A,
PARSE_UNDERFLOW = std::numeric_limits<uint32_t>::max(),
};
using ErrorCode = SessionCloseErrorCode;
// SubscribeErrorCode is now an alias for RequestErrorCode - see below
enum class PublishDoneStatusCode : uint32_t {
INTERNAL_ERROR = 0x0,
UNAUTHORIZED = 0x1,
TRACK_ENDED = 0x2,
SUBSCRIPTION_ENDED = 0x3,
GOING_AWAY = 0x4,
EXPIRED = 0x5,
TOO_FAR_BEHIND = 0x6,
UPDATE_FAILED = 0x8,
//
SESSION_CLOSED = std::numeric_limits<uint32_t>::max()
};
enum class TrackStatusCode : uint32_t {
IN_PROGRESS = 0x0,
TRACK_NOT_EXIST = 0x1,
TRACK_NOT_STARTED = 0x2,
TRACK_ENDED = 0x3,
UNKNOWN = 0x4
};
// FetchErrorCode is now an alias for RequestErrorCode - see below
// SubscribeNamespaceErrorCode is now an alias for RequestErrorCode - see below
// PublishNamespaceErrorCode is now an alias for RequestErrorCode - see below
// PublishError is now an alias for RequestError - see below
// Consolidated error code enum for all request types
enum class RequestErrorCode : uint32_t {
// Shared error codes (same semantic meaning across request types)
INTERNAL_ERROR = 0,
UNAUTHORIZED = 1,
TIMEOUT = 2,
NOT_SUPPORTED = 3,
TRACK_NOT_EXIST = 4,
INVALID_RANGE = 5,
// Note: Pending draft update
GOING_AWAY = 6,
// SubscribeNamespace-specific codes
NAMESPACE_PREFIX_UNKNOWN = 4, // Same value as TRACK_NOT_EXIST
// PublishNamespace-specific codes
UNINTERESTED = 4, // Same value as TRACK_NOT_EXIST
// Special values
CANCELLED = std::numeric_limits<uint32_t>::max(),
};
enum class ResetStreamErrorCode : uint32_t {
INTERNAL_ERROR = 0,
DELIVERY_TIMEOUT = 1,
SESSION_CLOSED = 2,
CANCELLED = 3, // received UNSUBSCRIBE / FETCH_CANCEL / STOP_SENDING
MALFORMED_TRACK = 12, // track violated protocol ordering constraints
};
enum class FrameType : uint64_t {
SUBSCRIBE_UPDATE = 2,
REQUEST_UPDATE = 2,
SUBSCRIBE = 3,
SUBSCRIBE_OK = 4,
SUBSCRIBE_ERROR = 5,
REQUEST_ERROR = 5,
PUBLISH_NAMESPACE = 0x6,
PUBLISH_NAMESPACE_OK = 0x7,
REQUEST_OK = 0x7,
PUBLISH_NAMESPACE_ERROR = 0x8, // Draft 15 and below
NAMESPACE = 0x8, // Draft 16 and above
PUBLISH_NAMESPACE_DONE = 9,
UNSUBSCRIBE = 0xA,
PUBLISH_DONE = 0xB,
PUBLISH_NAMESPACE_CANCEL = 0xC,
TRACK_STATUS = 0xD,
TRACK_STATUS_OK = 0xE, // Draft 15 and below
NAMESPACE_DONE = 0xE, // Draft 16 and above
TRACK_STATUS_ERROR = 0xF,
GOAWAY = 0x10,
SUBSCRIBE_NAMESPACE = 0x11,
SUBSCRIBE_NAMESPACE_OK = 0x12,
SUBSCRIBE_NAMESPACE_ERROR = 0x13,
UNSUBSCRIBE_NAMESPACE = 0x14,
MAX_REQUEST_ID = 0x15,
FETCH = 0x16,
FETCH_CANCEL = 0x17,
FETCH_OK = 0x18,
FETCH_ERROR = 0x19,
REQUESTS_BLOCKED = 0x1A,
PUBLISH = 0x1D,
PUBLISH_OK = 0x1E,
PUBLISH_ERROR = 0x1F,
CLIENT_SETUP = 0x20,
SERVER_SETUP = 0x21,
LEGACY_CLIENT_SETUP = 0x40,
LEGACY_SERVER_SETUP = 0x41,
};
enum class DatagramType : uint64_t {
OBJECT_DATAGRAM_NO_EXT = 0x0,
OBJECT_DATAGRAM_EXT = 0x1,
OBJECT_DATAGRAM_NO_EXT_EOG = 0x2,
OBJECT_DATAGRAM_EXT_EOG = 0x3,
OBJECT_DATAGRAM_NO_EXT_ID_ZERO = 0x4,
OBJECT_DATAGRAM_EXT_ID_ZERO = 0x5,
OBJECT_DATAGRAM_NO_EXT_EOG_ID_ZERO = 0x6,
OBJECT_DATAGRAM_EXT_EOG_ID_ZERO = 0x7,
OBJECT_DATAGRAM_STATUS = 0x20,
OBJECT_DATAGRAM_STATUS_EXT = 0x21,
OBJECT_DATAGRAM_STATUS_ID_ZERO = 0x24,
OBJECT_DATAGRAM_STATUS_EXT_ID_ZERO = 0x25,
// Version 15+ datagram types without priority
OBJECT_DATAGRAM_NO_EXT_NO_PRI = 0x8,
OBJECT_DATAGRAM_EXT_NO_PRI = 0x9,
OBJECT_DATAGRAM_NO_EXT_EOG_NO_PRI = 0xA,
OBJECT_DATAGRAM_EXT_EOG_NO_PRI = 0xB,
OBJECT_DATAGRAM_NO_EXT_ID_ZERO_NO_PRI = 0xC,
OBJECT_DATAGRAM_EXT_ID_ZERO_NO_PRI = 0xD,
OBJECT_DATAGRAM_NO_EXT_EOG_ID_ZERO_NO_PRI = 0xE,
OBJECT_DATAGRAM_EXT_EOG_ID_ZERO_NO_PRI = 0xF,
OBJECT_DATAGRAM_STATUS_NO_PRI = 0x28,
OBJECT_DATAGRAM_STATUS_EXT_NO_PRI = 0x29,
OBJECT_DATAGRAM_STATUS_ID_ZERO_NO_PRI = 0x2C,
OBJECT_DATAGRAM_STATUS_EXT_ID_ZERO_NO_PRI = 0x2D,
};
enum class StreamType : uint64_t {
FETCH_HEADER = 0x5,
SUBGROUP_HEADER_MASK = 0x10,
SUBGROUP_HEADER_SG_ZERO = 0x10,
SUBGROUP_HEADER_SG_ZERO_EXT = 0x11,
SUBGROUP_HEADER_SG_FIRST = 0x12,
SUBGROUP_HEADER_SG_FIRST_EXT = 0x13,
SUBGROUP_HEADER_SG = 0x14,
SUBGROUP_HEADER_SG_EXT = 0x15,
SUBGROUP_HEADER_SG_ZERO_EOG = 0x18,
SUBGROUP_HEADER_SG_ZERO_EXT_EOG = 0x19,
SUBGROUP_HEADER_SG_FIRST_EOG = 0x1A,
SUBGROUP_HEADER_SG_FIRST_EXT_EOG = 0x1B,
SUBGROUP_HEADER_SG_EOG = 0x1C,
SUBGROUP_HEADER_SG_EXT_EOG = 0x1D,
// Version 15+ subgroup types without priority
SUBGROUP_HEADER_SG_ZERO_NO_PRI = 0x30,
SUBGROUP_HEADER_SG_ZERO_EXT_NO_PRI = 0x31,
SUBGROUP_HEADER_SG_FIRST_NO_PRI = 0x32,
SUBGROUP_HEADER_SG_FIRST_EXT_NO_PRI = 0x33,
SUBGROUP_HEADER_SG_NO_PRI = 0x34,
SUBGROUP_HEADER_SG_EXT_NO_PRI = 0x35,
SUBGROUP_HEADER_SG_ZERO_EOG_NO_PRI = 0x38,
SUBGROUP_HEADER_SG_ZERO_EXT_EOG_NO_PRI = 0x39,
SUBGROUP_HEADER_SG_FIRST_EOG_NO_PRI = 0x3A,
SUBGROUP_HEADER_SG_FIRST_EXT_EOG_NO_PRI = 0x3B,
SUBGROUP_HEADER_SG_EOG_NO_PRI = 0x3C,
SUBGROUP_HEADER_SG_EXT_EOG_NO_PRI = 0x3D,
};
// Subgroup Bit Fields
constexpr uint8_t SG_HAS_EXTENSIONS = 0x1;
constexpr uint8_t SG_SUBGROUP_VALUE = 0x2;
constexpr uint8_t SG_HAS_SUBGROUP_ID = 0x4;
constexpr uint8_t SG_HAS_END_OF_GROUP = 0x8;
constexpr uint8_t SG_PRIORITY_NOT_PRESENT = 0x20;
// Datagram Type Bit Fields
constexpr uint8_t DG_HAS_EXTENSIONS = 0x1;
constexpr uint8_t DG_HAS_STATUS_V11 = 0x2;
constexpr uint8_t DG_HAS_END_OF_GROUP = 0x2;
constexpr uint8_t DG_OBJECT_ID_ZERO = 0x4;
constexpr uint8_t DG_PRIORITY_NOT_PRESENT = 0x8;
constexpr uint8_t DG_IS_STATUS = 0x20;
enum class SubgroupIDFormat : uint8_t { Present, Zero, FirstObject };
struct SubgroupOptions {
bool hasExtensions{false};
SubgroupIDFormat subgroupIDFormat{SubgroupIDFormat::Present};
bool hasEndOfGroup{false};
bool priorityPresent{true};
};
std::ostream& operator<<(std::ostream& os, FrameType type);
std::ostream& operator<<(std::ostream& os, StreamType type);
enum class SetupKey : uint64_t {
PATH = 1,
MAX_REQUEST_ID = 2,
AUTHORIZATION_TOKEN = 3,
MAX_AUTH_TOKEN_CACHE_SIZE = 4,
AUTHORITY = 5,
MOQT_IMPLEMENTATION = 7
};
enum class AliasType : uint8_t {
DELETE_ALIAS = 0x0,
REGISTER = 0x1,
USE_ALIAS = 0x2,
USE_VALUE = 0x3
};
struct AuthToken {
uint64_t tokenType;
std::string tokenValue;
std::optional<uint64_t> alias;
// Set alias to one of these constants when sending an AuthToken parameter
// Register: Will attempt to save the token in the cache, there is space
// DontRegister: Will not attempt to save the token in the cache
static constexpr uint64_t Register = 1;
static constexpr std::optional<uint64_t> DontRegister = std::nullopt;
};
struct AbsoluteLocation {
uint64_t group{0};
uint64_t object{0};
AbsoluteLocation() = default;
constexpr AbsoluteLocation(uint64_t g, uint64_t o) : group(g), object(o) {}
bool operator==(const AbsoluteLocation& other) const {
return group == other.group && object == other.object;
}
bool operator!=(const AbsoluteLocation& other) const {
return !(*this == other);
}
bool operator<(const AbsoluteLocation& other) const {
if (group < other.group) {
return true;
} else if (group == other.group) {
return object < other.object;
}
return false;
}
bool operator<=(const AbsoluteLocation& other) const {
return *this < other || *this == other;
}
bool operator>(const AbsoluteLocation& other) const {
return !(*this <= other);
}
bool operator>=(const AbsoluteLocation& other) const {
return !(*this < other);
}
friend std::ostream& operator<<(
std::ostream& os,
const AbsoluteLocation& loc) {
os << loc.describe();
return os;
}
std::string describe() const;
};
constexpr AbsoluteLocation kLocationMin;
constexpr AbsoluteLocation kLocationMax{kEightByteLimit, kEightByteLimit};
enum class LocationType : uint8_t {
NextGroupStart = 1,
LargestObject = 2,
AbsoluteStart = 3,
AbsoluteRange = 4,
LargestGroup = 250,
};
std::string toString(LocationType locType);
struct SubscriptionFilter {
LocationType filterType{LocationType::AbsoluteStart};
std::optional<AbsoluteLocation> location;
std::optional<uint64_t> endGroup;
SubscriptionFilter() = default;
SubscriptionFilter(
LocationType ft,
std::optional<AbsoluteLocation> loc,
std::optional<uint64_t> eg)
: filterType(ft), location(loc), endGroup(std::move(eg)) {}
};
struct Parameter {
uint64_t key = 0;
std::string asString;
uint64_t asUint64 = 0;
AuthToken asAuthToken;
SubscriptionFilter asSubscriptionFilter;
std::optional<AbsoluteLocation> largestObject;
// Constructors for Parameter for each type, provided the key and the type
// String parameter
Parameter(uint64_t keyIn, const std::string& str)
: key(keyIn),
asString(str),
asUint64(0),
asAuthToken(),
asSubscriptionFilter(),
largestObject() {}
// uint64_t parameter
Parameter(uint64_t keyIn, uint64_t uint64)
: key(keyIn),
asString(),
asUint64(uint64),
asAuthToken(),
asSubscriptionFilter(),
largestObject() {}
// AuthToken parameter
Parameter(uint64_t keyIn, const AuthToken& token)
: key(keyIn),
asString(),
asUint64(0),
asAuthToken(token),
asSubscriptionFilter(),
largestObject() {}
// SubscriptionFilter parameter
Parameter(uint64_t keyIn, const SubscriptionFilter& filter)
: key(keyIn),
asString(),
asUint64(0),
asAuthToken(),
asSubscriptionFilter(filter),
largestObject() {}
// LargestObject parameter (std::optional<AbsoluteLocation>)
Parameter(uint64_t keyIn, const std::optional<AbsoluteLocation>& loc)
: key(keyIn),
asString(),
asUint64(0),
asAuthToken(),
asSubscriptionFilter(),
largestObject(loc) {}
// Default constructor
Parameter() = default;
};
using SetupParameter = Parameter;
using TrackRequestParameter = Parameter;
enum class TrackRequestParamKey : uint64_t {
AUTHORIZATION_TOKEN = 3,
DELIVERY_TIMEOUT = 2,
MAX_CACHE_DURATION = 4,
PUBLISHER_PRIORITY = 0x0E,
SUBSCRIBER_PRIORITY = 0x20,
SUBSCRIPTION_FILTER = 0x21,
EXPIRES = 8,
GROUP_ORDER = 0x22,
LARGEST_OBJECT = 0x9,
FORWARD = 0x10,
NEW_GROUP_REQUEST = 0x32,
};
class Parameters {
public:
using const_iterator = std::vector<Parameter>::const_iterator;
explicit Parameters(FrameType frameType) : frameType_(frameType) {}
FrameType getFrameType() const {
return frameType_;
}
void setMajorVersion(uint64_t majorVersion) {
majorVersion_ = majorVersion;
}
// Validates if a parameter is allowed for frameType_
bool isParamAllowed(TrackRequestParamKey key) const;
// Returns true if key is a known parameter key in kParamAllowlist
static bool isKnownParamKey(uint64_t key);
const Parameter& getParam(size_t position) const {
return params_.at(position);
}
const Parameter& at(size_t position) const {
return params_.at(position);
}
folly::Expected<folly::Unit, ErrorCode> insertParam(Parameter&& param) {
auto key = static_cast<TrackRequestParamKey>(param.key);
if (!isParamAllowed(key)) {
return folly::makeUnexpected(ErrorCode::INVALID_REQUEST_ID);
}
params_.emplace_back(std::move(param));
return folly::unit;
}
folly::Expected<folly::Unit, ErrorCode> insertParam(const Parameter& param) {
auto key = static_cast<TrackRequestParamKey>(param.key);
if (!isParamAllowed(key)) {
return folly::makeUnexpected(ErrorCode::INVALID_REQUEST_ID);
}
params_.emplace_back(param);
return folly::unit;
}
folly::Expected<folly::Unit, ErrorCode> insertParam(
size_t position,
Parameter&& param) {
auto key = static_cast<TrackRequestParamKey>(param.key);
if (!isParamAllowed(key)) {
return folly::makeUnexpected(ErrorCode::INVALID_REQUEST_ID);
}
CHECK_LE(position, params_.size());
params_.insert(params_.begin() + position, std::move(param));
return folly::unit;
}
void eraseParam(size_t position) {
CHECK_LT(position, params_.size());
params_.erase(params_.begin() + position);
}
void modifyString(size_t position, const std::string& newValue) {
params_.at(position).asString = newValue;
}
void eraseAllParamsOfType(TrackRequestParamKey key) {
const auto targetKey = static_cast<uint64_t>(key);
params_.erase(
std::remove_if(
params_.begin(),
params_.end(),
[targetKey](const Parameter& param) {
return param.key == targetKey;
}),
params_.end());
}
void modifyParam(
size_t position,
const std::string& newString,
uint64_t newInt64,
AuthToken newAuthToken) {
params_.at(position).asString = newString;
params_.at(position).asUint64 = newInt64;
params_.at(position).asAuthToken = std::move(newAuthToken);
}
const_iterator begin() const {
return params_.begin();
}
const_iterator end() const {
return params_.end();
}
size_t size() const {
return params_.size();
}
bool empty() const {
return params_.empty();
}
private:
FrameType frameType_{};
std::optional<uint64_t> majorVersion_;
std::vector<Parameter> params_;
};
using SetupParameters = Parameters;
using TrackRequestParameters = Parameters;
// Helper function to extract an integer parameter by key from a parameter list
template <class T>
std::optional<uint64_t> getFirstIntParam(
const T& params,
TrackRequestParamKey key) {
auto keyValue = folly::to_underlying(key);
for (const auto& param : params) {
if (param.key == keyValue) {
return param.asUint64;
}
}
return std::nullopt;
}
struct ClientSetup {
std::vector<uint64_t> supportedVersions;
SetupParameters params{FrameType::CLIENT_SETUP};
};
struct ServerSetup {
uint64_t selectedVersion;
SetupParameters params{FrameType::SERVER_SETUP};
};
enum class ObjectStatus : uint64_t {
NORMAL = 0,
OBJECT_NOT_EXIST = 1,
GROUP_NOT_EXIST = 2,
END_OF_GROUP = 3,
END_OF_TRACK = 4,
};
std::ostream& operator<<(std::ostream& os, ObjectStatus type);
struct TrackAlias {
/* implicit */ TrackAlias(uint64_t v) : value(v) {}
TrackAlias() = default;
uint64_t value{0};
bool operator==(const TrackAlias& a) const {
return value == a.value;
}
bool operator!=(const TrackAlias& a) const {
return !(*this == a);
}
struct hash {
size_t operator()(const TrackAlias& a) const {
return std::hash<uint64_t>{}(a.value);
}
};
};
std::ostream& operator<<(std::ostream& os, TrackAlias alias);
struct RequestID {
/* implicit */ RequestID(uint64_t v) : value(v) {}
RequestID() = default;
uint64_t value{0};
bool operator==(const RequestID& s) const {
return value == s.value;
}
bool operator!=(const RequestID& s) const {
return value != s.value;
}
bool operator<(const RequestID& other) const {
return value < other.value;
}
bool operator<=(const RequestID& other) const {
return value <= other.value;
}
bool operator>(const RequestID& other) const {
return value > other.value;
}
bool operator>=(const RequestID& other) const {
return value >= other.value;
}
struct hash {
size_t operator()(const RequestID& s) const {
return std::hash<uint64_t>{}(s.value);
}
};
};
std::ostream& operator<<(std::ostream& os, RequestID id);
// TrackIdentifier variant removed - ObjectHeader now uses TrackAlias directly
struct Extension {
// Even type => holds value in intValue
// Odd type => holds value in arrayValue
uint64_t type;
uint64_t intValue;
std::unique_ptr<folly::IOBuf> arrayValue;
Extension() noexcept : Extension(0, 0) {}
Extension(uint64_t type, uint64_t intValue) noexcept
: type(type), intValue(intValue), arrayValue(nullptr) {}
Extension(uint64_t type, std::unique_ptr<folly::IOBuf> arrayValue) noexcept
: type(type), intValue(0), arrayValue(std::move(arrayValue)) {}
Extension(const Extension& other) noexcept {
copyFrom(other);
}
Extension& operator=(const Extension& other) noexcept {
copyFrom(other);
return *this;
}
Extension(Extension&& other) noexcept {
moveFrom(std::move(other));
}
Extension& operator=(Extension&& other) noexcept {
moveFrom(std::move(other));
return *this;
}
bool operator==(const Extension& other) const noexcept {
if (type == other.type) {
if (isOddType()) {
folly::IOBufEqualTo eq;
return eq(arrayValue, other.arrayValue);
} else {
return intValue == other.intValue;
}
}
return false;
}
bool isOddType() const {
return type & 0x1;
}
private:
void copyFrom(const Extension& other) {
type = other.type;
intValue = other.intValue;
arrayValue = other.arrayValue ? other.arrayValue->clone() : nullptr;
};
void moveFrom(Extension&& other) {
type = other.type;
intValue = other.intValue;
arrayValue = std::move(other.arrayValue);
}
};
class Extensions {
private:
std::vector<Extension> mutableExtensions;
std::vector<Extension> immutableExtensions;
public:
Extensions() = default;
// Constructor that takes both mutable and immutable extensions
Extensions(
const std::vector<Extension>& mutableExts,
const std::vector<Extension>& immutableExts)
: mutableExtensions(mutableExts), immutableExtensions(immutableExts) {}
// Getter for mutableExtensions (returns reference)
std::vector<Extension>& getMutableExtensions() {
return mutableExtensions;
}
// Getter for mutableExtensions (returns const reference)
const std::vector<Extension>& getMutableExtensions() const {
return mutableExtensions;
}
// Getter for immutableExtensions (returns const reference)
const std::vector<Extension>& getImmutableExtensions() const {
return immutableExtensions;
}
// Member functions to insert extensions
void insertMutableExtension(Extension&& ext) {
mutableExtensions.emplace_back(std::move(ext));
}
void insertImmutableExtension(Extension&& ext) {
immutableExtensions.emplace_back(std::move(ext));
}
void insertMutableExtensions(const std::vector<Extension>& extensions) {
mutableExtensions.insert(
mutableExtensions.end(), extensions.begin(), extensions.end());
}
void insertImmutableExtensions(const std::vector<Extension>& extensions) {
immutableExtensions.insert(
immutableExtensions.end(), extensions.begin(), extensions.end());
}
size_t size() const {
return mutableExtensions.size() + immutableExtensions.size();
}
bool empty() const {
return mutableExtensions.empty() && immutableExtensions.empty();
}
// Returns the integer value of the first extension with the given type
// from either mutable or immutable sections, or nullopt if not found.
// Only valid for even-typed extensions (which use intValue).
std::optional<uint64_t> getIntExtension(uint64_t type) const {
for (const auto& ext : mutableExtensions) {
if (ext.type == type) {
return ext.intValue;
}
}
for (const auto& ext : immutableExtensions) {
if (ext.type == type) {
return ext.intValue;
}
}
return std::nullopt;
}
// For backward compatibility and testing
bool operator==(const Extensions& other) const {
return mutableExtensions == other.mutableExtensions &&
immutableExtensions == other.immutableExtensions;
}
};
Extensions noExtensions();
struct ObjectHeader {
ObjectHeader() = default;
ObjectHeader(
uint64_t groupIn,
uint64_t subgroupIn,
uint64_t idIn,
std::optional<uint8_t> priorityIn = kDefaultPriority,
ObjectStatus statusIn = ObjectStatus::NORMAL,
Extensions extensionsIn = noExtensions(),
std::optional<uint64_t> lengthIn = std::nullopt)
: group(groupIn),
subgroup(subgroupIn),
id(idIn),
priority(priorityIn),
status(statusIn),
extensions(std::move(extensionsIn)),
length(std::move(lengthIn)) {}
ObjectHeader(
uint64_t groupIn,
uint64_t subgroupIn,
uint64_t idIn,
std::optional<uint8_t> priorityIn,
uint64_t lengthIn,
Extensions extensionsIn = noExtensions())
: group(groupIn),
subgroup(subgroupIn),
id(idIn),
priority(priorityIn),
status(ObjectStatus::NORMAL),
extensions(std::move(extensionsIn)),
length(lengthIn) {}
uint64_t group;
uint64_t subgroup{0}; // meaningless for Datagram
uint64_t id;
std::optional<uint8_t> priority{kDefaultPriority};
ObjectStatus status{ObjectStatus::NORMAL};
Extensions extensions;
std::optional<uint64_t> length{std::nullopt};
// Forwarding preference: true = Datagram, false = Subgroup (stream)
// Used in FETCH serialization to indicate how this object should be forwarded
bool forwardingPreferenceIsDatagram{false};
// == Operator For Datagram Testing
bool operator==(const ObjectHeader& other) const {
return group == other.group && subgroup == other.subgroup &&
id == other.id && priority == other.priority &&
status == other.status && extensions == other.extensions &&
length == other.length &&
forwardingPreferenceIsDatagram == other.forwardingPreferenceIsDatagram;
}
};
// Struct to hold both TrackAlias and ObjectHeader for datagram parsing
struct DatagramObjectHeader {
TrackAlias trackAlias;
ObjectHeader objectHeader;
DatagramObjectHeader(TrackAlias alias, ObjectHeader header)
: trackAlias(alias), objectHeader(std::move(header)) {}
};
std::ostream& operator<<(std::ostream& os, const ObjectHeader& type);
struct TrackNamespace {
std::vector<std::string> trackNamespace;
TrackNamespace() = default;
explicit TrackNamespace(std::vector<std::string> tns) {
trackNamespace = std::move(tns);
}
explicit TrackNamespace(std::string tns, std::string delimiter);
bool operator==(const TrackNamespace& other) const {
return trackNamespace == other.trackNamespace;
}
bool operator<(const TrackNamespace& other) const {
return trackNamespace < other.trackNamespace;
}
const std::string& operator[](size_t i) const {
return trackNamespace[i];
}
struct hash {
size_t operator()(const TrackNamespace& tn) const {
return folly::hash::hash_range(
tn.trackNamespace.begin(), tn.trackNamespace.end());
}
};
friend std::ostream& operator<<(
std::ostream& os,
const TrackNamespace& trackNs) {
os << trackNs.describe();
return os;
}
std::string describe() const {
std::string result;
if (trackNamespace.empty()) {
return result;
}
// Iterate through all elements except the last one
for (size_t i = 0; i < trackNamespace.size() - 1; ++i) {
result += trackNamespace[i];
result += '/';
}
// Add the last element without a trailing slash
result += trackNamespace.back();
return result;
}
bool empty() const {
return trackNamespace.empty();
}
size_t size() const {
return trackNamespace.size();
}
void append(std::string token) {
trackNamespace.emplace_back(std::move(token));
}
bool startsWith(const TrackNamespace& other) const {
if (other.trackNamespace.size() > trackNamespace.size()) {
return false;
}
for (size_t i = 0; i < other.trackNamespace.size(); ++i) {
if (other.trackNamespace[i] != trackNamespace[i]) {
return false;
}
}
return true;
}
void trimEnd() {
CHECK_GT(size(), 0);
trackNamespace.pop_back();
}
};
struct FullTrackName {
TrackNamespace trackNamespace;
std::string trackName;
bool operator==(const FullTrackName& other) const {
return trackNamespace == other.trackNamespace &&
trackName == other.trackName;
}
bool operator!=(const FullTrackName& other) const {
return !(*this == other);
}
bool operator<(const FullTrackName& other) const {
return trackNamespace < other.trackNamespace ||
(trackNamespace == other.trackNamespace && trackName < other.trackName);
}
friend std::ostream& operator<<(std::ostream& os, const FullTrackName& ftn) {
os << ftn.describe();
return os;
}
std::string describe() const;
struct hash {
size_t operator()(const FullTrackName& ftn) const {
return folly::hash::hash_combine(
TrackNamespace::hash()(ftn.trackNamespace), ftn.trackName);
}
};
};
enum class GroupOrder : uint8_t {
Default = 0x0,
OldestFirst = 0x1,
NewestFirst = 0x2
};
struct SubscribeRequest {
static SubscribeRequest make(
const FullTrackName& fullTrackName,
uint8_t priority = kDefaultPriority,
GroupOrder groupOrder = GroupOrder::Default,
bool forward = true,
LocationType locType = LocationType::LargestGroup,
std::optional<AbsoluteLocation> start = std::nullopt,
uint64_t endGroup = 0,
const std::vector<Parameter>& inputParams = {});
RequestID requestID;
FullTrackName fullTrackName;
uint8_t priority{kDefaultPriority};
GroupOrder groupOrder;
bool forward{true};
LocationType locType;
std::optional<AbsoluteLocation> start;
uint64_t endGroup;
TrackRequestParameters params{FrameType::SUBSCRIBE};
};
struct RequestUpdate {
RequestID requestID;
RequestID existingRequestID;
std::optional<AbsoluteLocation> start;
std::optional<uint64_t> endGroup;
uint8_t priority{kDefaultPriority};
// Draft 15+: Optional forward field. When absent, existing forward state is
// preserved. For earlier drafts, this is always set during parsing.
std::optional<bool> forward;
TrackRequestParameters params{FrameType::SUBSCRIBE_UPDATE};
};
// Backward compatibility alias
using SubscribeUpdate = RequestUpdate;
struct SubscribeOk {
RequestID requestID;
TrackAlias trackAlias;
std::chrono::milliseconds expires;
GroupOrder groupOrder;
// context exists is inferred from presence of largest