1 /* 2 * Copyright 2019 The Chromium Authors. All rights reserved. 3 * Copyright (c) 2021 The WebRTC project authors. All Rights Reserved. 4 * 5 * Use of this source code is governed by a BSD-style license 6 * that can be found in the LICENSE file in the root of the source 7 * tree. An additional intellectual property rights grant can be found 8 * in the file PATENTS. All contributing project authors may 9 * be found in the AUTHORS file in the root of the source tree. 10 */ 11 #ifndef NET_DCSCTP_PUBLIC_TYPES_H_ 12 #define NET_DCSCTP_PUBLIC_TYPES_H_ 13 14 #include <cstdint> 15 #include <limits> 16 17 #include "rtc_base/strong_alias.h" 18 19 namespace dcsctp { 20 21 // Stream Identifier 22 using StreamID = webrtc::StrongAlias<class StreamIDTag, uint16_t>; 23 24 // Payload Protocol Identifier (PPID) 25 using PPID = webrtc::StrongAlias<class PPIDTag, uint32_t>; 26 27 // Timeout Identifier 28 using TimeoutID = webrtc::StrongAlias<class TimeoutTag, uint64_t>; 29 30 // Indicates if a message is allowed to be received out-of-order compared to 31 // other messages on the same stream. 32 using IsUnordered = webrtc::StrongAlias<class IsUnorderedTag, bool>; 33 34 // Stream priority, where higher values indicate higher priority. The meaning of 35 // this value and how it's used depends on the stream scheduler. 36 using StreamPriority = webrtc::StrongAlias<class StreamPriorityTag, uint16_t>; 37 38 // Duration, as milliseconds. Overflows after 24 days. 39 class DurationMs : public webrtc::StrongAlias<class DurationMsTag, int32_t> { 40 public: DurationMs(const UnderlyingType & v)41 constexpr explicit DurationMs(const UnderlyingType& v) 42 : webrtc::StrongAlias<class DurationMsTag, int32_t>(v) {} 43 44 // Convenience methods for working with time. 45 constexpr DurationMs& operator+=(DurationMs d) { 46 value_ += d.value_; 47 return *this; 48 } 49 constexpr DurationMs& operator-=(DurationMs d) { 50 value_ -= d.value_; 51 return *this; 52 } 53 template <typename T> 54 constexpr DurationMs& operator*=(T factor) { 55 value_ *= factor; 56 return *this; 57 } 58 }; 59 60 constexpr inline DurationMs operator+(DurationMs lhs, DurationMs rhs) { 61 return lhs += rhs; 62 } 63 constexpr inline DurationMs operator-(DurationMs lhs, DurationMs rhs) { 64 return lhs -= rhs; 65 } 66 template <typename T> 67 constexpr inline DurationMs operator*(DurationMs lhs, T rhs) { 68 return lhs *= rhs; 69 } 70 template <typename T> 71 constexpr inline DurationMs operator*(T lhs, DurationMs rhs) { 72 return rhs *= lhs; 73 } 74 constexpr inline int32_t operator/(DurationMs lhs, DurationMs rhs) { 75 return lhs.value() / rhs.value(); 76 } 77 78 // Represents time, in milliseconds since a client-defined epoch. 79 class TimeMs : public webrtc::StrongAlias<class TimeMsTag, int64_t> { 80 public: TimeMs(const UnderlyingType & v)81 constexpr explicit TimeMs(const UnderlyingType& v) 82 : webrtc::StrongAlias<class TimeMsTag, int64_t>(v) {} 83 84 // Convenience methods for working with time. 85 constexpr TimeMs& operator+=(DurationMs d) { 86 value_ += *d; 87 return *this; 88 } 89 constexpr TimeMs& operator-=(DurationMs d) { 90 value_ -= *d; 91 return *this; 92 } 93 InfiniteFuture()94 static constexpr TimeMs InfiniteFuture() { 95 return TimeMs(std::numeric_limits<int64_t>::max()); 96 } 97 }; 98 99 constexpr inline TimeMs operator+(TimeMs lhs, DurationMs rhs) { 100 return lhs += rhs; 101 } 102 constexpr inline TimeMs operator+(DurationMs lhs, TimeMs rhs) { 103 return rhs += lhs; 104 } 105 constexpr inline TimeMs operator-(TimeMs lhs, DurationMs rhs) { 106 return lhs -= rhs; 107 } 108 constexpr inline DurationMs operator-(TimeMs lhs, TimeMs rhs) { 109 return DurationMs(*lhs - *rhs); 110 } 111 112 // The maximum number of times the socket should attempt to retransmit a 113 // message which fails the first time in unreliable mode. 114 class MaxRetransmits 115 : public webrtc::StrongAlias<class MaxRetransmitsTag, uint16_t> { 116 public: MaxRetransmits(const UnderlyingType & v)117 constexpr explicit MaxRetransmits(const UnderlyingType& v) 118 : webrtc::StrongAlias<class MaxRetransmitsTag, uint16_t>(v) {} 119 120 // There should be no limit - the message should be sent reliably. NoLimit()121 static constexpr MaxRetransmits NoLimit() { 122 return MaxRetransmits(std::numeric_limits<uint16_t>::max()); 123 } 124 }; 125 126 // An identifier that can be set on sent messages, and picked by the sending 127 // client. If different from `::NotSet()`, lifecycle events will be generated, 128 // and eventually `DcSctpSocketCallbacks::OnLifecycleEnd` will be called to 129 // indicate that the lifecycle isn't tracked any longer. The value zero (0) is 130 // not a valid lifecycle identifier, and will be interpreted as not having it 131 // set. 132 class LifecycleId : public webrtc::StrongAlias<class LifecycleIdTag, uint64_t> { 133 public: LifecycleId(const UnderlyingType & v)134 constexpr explicit LifecycleId(const UnderlyingType& v) 135 : webrtc::StrongAlias<class LifecycleIdTag, uint64_t>(v) {} 136 IsSet()137 constexpr bool IsSet() const { return value_ != 0; } 138 NotSet()139 static constexpr LifecycleId NotSet() { return LifecycleId(0); } 140 }; 141 } // namespace dcsctp 142 143 #endif // NET_DCSCTP_PUBLIC_TYPES_H_ 144