1 // Copyright 2014 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.
5 #ifndef COMPONENTS_CAST_CHANNEL_CAST_FRAMER_H_
6 #define COMPONENTS_CAST_CHANNEL_CAST_FRAMER_H_
14 #include "base/macros.h"
15 #include "components/cast_channel/cast_channel_enum.h"
16 #include "net/base/io_buffer.h"
18 namespace cast_channel {
21 // Class for constructing and parsing CastMessage packet data.
24 using ChannelError = ::cast_channel::ChannelError;
26 // |input_buffer|: The input buffer used by all socket read operations that
27 // feed data into the framer.
28 explicit MessageFramer(scoped_refptr<net::GrowableIOBuffer> input_buffer);
31 // The number of bytes required from |input_buffer| to complete the
32 // CastMessage being read.
33 // Returns zero if |error_| is true (framer is in an invalid state.)
34 size_t BytesRequested();
36 // Serializes |message_proto| into |message_data|.
37 // Returns true if the message was serialized successfully, false otherwise.
38 static bool Serialize(const CastMessage& message_proto,
39 std::string* message_data);
41 // Reads bytes from |input_buffer_| and returns a new CastMessage if one
44 // |num_bytes| The number of bytes received by a read operation.
45 // Value must be <= BytesRequested().
46 // |message_length| Size of the deserialized message object, in bytes. For
47 // logging purposes. Set to zero if no message was parsed.
48 // |error| The result of the ingest operation. Set to CHANNEL_ERROR_NONE
49 // if no error occurred.
50 // Returns A pointer to a parsed CastMessage if a message was received
51 // in its entirety, nullptr otherwise.
52 std::unique_ptr<CastMessage> Ingest(size_t num_bytes,
53 size_t* message_length,
56 // Message header struct. If fields are added, be sure to update
57 // header_size(). Public to allow use of *_size() methods in unit tests.
58 struct MessageHeader {
60 // Sets the message size.
61 void SetMessageSize(size_t message_size);
62 // Prepends this header to |str|.
63 void PrependToString(std::string* str);
64 // Reads |header| from the bytes specified by |data|.
65 static void Deserialize(char* data, MessageHeader* header);
66 // Size (in bytes) of the message header.
67 static size_t header_size();
68 // Maximum size (in bytes) of a message payload on the wire (does not
70 static size_t max_body_size();
71 // Maximum size (in bytes) of a message (header + payload) on the wire.
72 static size_t max_message_size();
73 std::string ToString();
74 // The size of the following protocol message in bytes, in host byte order.
79 enum MessageElement { HEADER, BODY };
81 // Prepares the framer for ingesting a new message.
84 // The element of the message that will be read on the next call to Ingest().
85 MessageElement current_element_;
87 // Total size of the message, in bytes (head + body).
88 size_t message_bytes_received_;
90 // Size of the body alone, in bytes.
93 // Input buffer which carries message data read from the socket.
94 // Caller is responsible for writing into this buffer.
95 scoped_refptr<net::GrowableIOBuffer> input_buffer_;
97 // Disables Ingest functionality is the parser receives invalid data.
100 DISALLOW_COPY_AND_ASSIGN(MessageFramer);
102 } // namespace cast_channel
103 #endif // COMPONENTS_CAST_CHANNEL_CAST_FRAMER_H_