1 // Copyright 2019 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef OSP_PUBLIC_ENDPOINT_REQUEST_IDS_H_ 6 #define OSP_PUBLIC_ENDPOINT_REQUEST_IDS_H_ 7 8 #include <cstdint> 9 #include <map> 10 11 #include "platform/base/macros.h" 12 13 namespace openscreen { 14 namespace osp { 15 16 // Tracks the next available message request ID per endpoint by its endpoint ID. 17 // These can only be incremented while an endpoint is connected but can be reset 18 // on disconnection. This is necessary because all APIs that use CBOR messages 19 // across a QUIC stream share the |request_id| field, which must be unique 20 // within a pair of endpoints. 21 class EndpointRequestIds { 22 public: 23 enum class Role { 24 kClient, 25 kServer, 26 }; 27 28 explicit EndpointRequestIds(Role role); 29 ~EndpointRequestIds(); 30 31 uint64_t GetNextRequestId(uint64_t endpoint_id); 32 void ResetRequestId(uint64_t endpoint_id); 33 void Reset(); 34 35 private: 36 const Role role_; 37 std::map<uint64_t, uint64_t> request_ids_by_endpoint_id_; 38 39 OSP_DISALLOW_COPY_AND_ASSIGN(EndpointRequestIds); 40 }; 41 42 } // namespace osp 43 } // namespace openscreen 44 45 #endif // OSP_PUBLIC_ENDPOINT_REQUEST_IDS_H_ 46