OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_FRAMER_H_ | |
6 #define EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_FRAMER_H_ | |
7 | |
8 #include <stddef.h> | |
9 #include <stdint.h> | |
10 | |
11 #include <memory> | |
12 #include <string> | |
13 | |
14 #include "base/macros.h" | |
15 #include "components/cast_channel/cast_channel_enum.h" | |
16 #include "net/base/io_buffer.h" | |
17 | |
18 namespace extensions { | |
19 namespace api { | |
20 namespace cast_channel { | |
21 class CastMessage; | |
22 | |
23 // Class for constructing and parsing CastMessage packet data. | |
24 class MessageFramer { | |
25 public: | |
26 using ChannelError = ::cast_channel::ChannelError; | |
27 | |
28 // |input_buffer|: The input buffer used by all socket read operations that | |
29 // feed data into the framer. | |
30 explicit MessageFramer(scoped_refptr<net::GrowableIOBuffer> input_buffer); | |
31 ~MessageFramer(); | |
32 | |
33 // The number of bytes required from |input_buffer| to complete the | |
34 // CastMessage being read. | |
35 // Returns zero if |error_| is true (framer is in an invalid state.) | |
36 size_t BytesRequested(); | |
37 | |
38 // Serializes |message_proto| into |message_data|. | |
39 // Returns true if the message was serialized successfully, false otherwise. | |
40 static bool Serialize(const CastMessage& message_proto, | |
41 std::string* message_data); | |
42 | |
43 // Reads bytes from |input_buffer_| and returns a new CastMessage if one | |
44 // is fully read. | |
45 // | |
46 // |num_bytes| The number of bytes received by a read operation. | |
47 // Value must be <= BytesRequested(). | |
48 // |message_length| Size of the deserialized message object, in bytes. For | |
49 // logging purposes. Set to zero if no message was parsed. | |
50 // |error| The result of the ingest operation. Set to CHANNEL_ERROR_NONE | |
51 // if no error occurred. | |
52 // Returns A pointer to a parsed CastMessage if a message was received | |
53 // in its entirety, nullptr otherwise. | |
54 std::unique_ptr<CastMessage> Ingest(size_t num_bytes, | |
55 size_t* message_length, | |
56 ChannelError* error); | |
57 | |
58 // Message header struct. If fields are added, be sure to update | |
59 // header_size(). Public to allow use of *_size() methods in unit tests. | |
60 struct MessageHeader { | |
61 MessageHeader(); | |
62 // Sets the message size. | |
63 void SetMessageSize(size_t message_size); | |
64 // Prepends this header to |str|. | |
65 void PrependToString(std::string* str); | |
66 // Reads |header| from the bytes specified by |data|. | |
67 static void Deserialize(char* data, MessageHeader* header); | |
68 // Size (in bytes) of the message header. | |
69 static size_t header_size(); | |
70 // Maximum size (in bytes) of a message payload on the wire (does not | |
71 // include header). | |
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. | |
75 size_t message_size; | |
76 }; | |
77 | |
78 private: | |
79 enum MessageElement { HEADER, BODY }; | |
80 | |
81 // Prepares the framer for ingesting a new message. | |
82 void Reset(); | |
83 | |
84 // The element of the message that will be read on the next call to Ingest(). | |
85 MessageElement current_element_; | |
86 | |
87 // Total size of the message, in bytes (head + body). | |
88 size_t message_bytes_received_; | |
89 | |
90 // Size of the body alone, in bytes. | |
91 size_t body_size_; | |
92 | |
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_; | |
96 | |
97 // Disables Ingest functionality is the parser receives invalid data. | |
98 bool error_; | |
99 | |
100 DISALLOW_COPY_AND_ASSIGN(MessageFramer); | |
101 }; | |
102 } // namespace cast_channel | |
103 } // namespace api | |
104 } // namespace extensions | |
105 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_FRAMER_H_ | |
OLD | NEW |