xref: /aosp_15_r20/external/openscreen/cast/common/channel/message_framer.h (revision 3f982cf4871df8771c9d4abe6e9a6f8d829b2736)
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 CAST_COMMON_CHANNEL_MESSAGE_FRAMER_H_
6 #define CAST_COMMON_CHANNEL_MESSAGE_FRAMER_H_
7 
8 #include <stddef.h>
9 #include <stdint.h>
10 
11 #include <memory>
12 #include <vector>
13 
14 #include "absl/types/span.h"
15 #include "cast/common/channel/proto/cast_channel.pb.h"
16 #include "platform/base/error.h"
17 
18 namespace openscreen {
19 namespace cast {
20 namespace message_serialization {
21 
22 // Serializes |message_proto| into |message_data|.
23 // Returns true if the message was serialized successfully, false otherwise.
24 ErrorOr<std::vector<uint8_t>> Serialize(
25     const ::cast::channel::CastMessage& message);
26 
27 struct DeserializeResult {
28   ::cast::channel::CastMessage message;
29   size_t length;
30 };
31 
32 // Reads bytes from |input| and returns a new CastMessage if one is fully
33 // read.  Returns a parsed CastMessage if a message was received in its
34 // entirety, and an error otherwise.  The result also contains the number of
35 // bytes consumed from |input| when a parse succeeds.
36 ErrorOr<DeserializeResult> TryDeserialize(absl::Span<const uint8_t> input);
37 
38 }  // namespace message_serialization
39 }  // namespace cast
40 }  // namespace openscreen
41 
42 #endif  // CAST_COMMON_CHANNEL_MESSAGE_FRAMER_H_
43