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_SOCKET_FRAMER_H_ | |
6 #define EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_SOCKET_FRAMER_H_ | |
7 | |
8 #include "base/basictypes.h" | |
9 #include "extensions/common/api/cast_channel.h" | |
10 #include "net/base/io_buffer.h" | |
11 | |
12 namespace extensions { | |
13 namespace core_api { | |
14 namespace cast_channel { | |
15 // Size of a CastSocket header payload. | |
16 extern size_t const kHeaderSizeBytes; | |
mark a. foltz
2014/09/02 20:27:38
Why do these need to be declared? They can be acc
Kevin M
2014/09/02 23:15:36
To keep the constants stashed in a centralized loc
| |
17 // Maximum byte count for a CastSocket message. | |
mark a. foltz
2014/09/02 20:27:38
Header and body or just body? I think the former.
Kevin M
2014/09/02 23:15:36
You're correct. Done.
| |
18 extern size_t const kMaxMessageSizeBytes; | |
19 | |
20 class CastMessage; | |
21 | |
22 // Class for constructing and parsing CastMessage packet data. | |
23 class MessageFramer { | |
24 public: | |
25 // |input_buffer|: The input buffer used by all socket read operations that | |
26 // feed data into the framer. | |
27 explicit MessageFramer(scoped_refptr<net::GrowableIOBuffer> input_buffer); | |
28 ~MessageFramer(); | |
29 | |
30 // The number of bytes requested from |input_buffer| to complete the | |
mark a. foltz
2014/09/02 20:27:38
s/requested/required/
Kevin M
2014/09/02 23:15:36
Done.
| |
31 // CastMessage being read. | |
32 size_t BytesRequested(); | |
33 | |
34 // Serializes |message_proto| into |message_data|. | |
35 // Returns true if the message was serialized successfully, false otherwise. | |
36 static bool Serialize(const CastMessage& message_proto, | |
37 std::string* message_data); | |
38 | |
39 // Reads bytes from |input_buffer_| and returns a new CastMessage if one | |
40 // is fully read. | |
41 // | |
42 // |num_bytes| The number of bytes received by a read operation. | |
43 // Value must be <= BytesRequested(). | |
44 // |message_length| Size of the deserialized message object, in bytes. For | |
45 // logging purposes. Set to zero if no message was parsed. | |
46 // |error| The status of the ingest operation. Set to CHANNEL_ERROR_NONE | |
mark a. foltz
2014/09/02 20:27:38
s/status/result/
Kevin M
2014/09/02 23:15:35
Done.
| |
47 // if no error occurred. | |
48 // |return value| A pointer to a parsed CastMessage if a message was received | |
mark a. foltz
2014/09/02 20:27:38
Returns a pointer ...
Kevin M
2014/09/02 23:15:36
Done.
| |
49 // in its entirety, NULL otherwise. | |
50 scoped_ptr<CastMessage> Ingest(size_t num_bytes, | |
51 size_t* message_length, | |
52 ChannelError* error); | |
53 | |
54 // Message header struct. If fields are added, be sure to update | |
55 // header_size(). Protected to allow use of *_size() methods in unit tests. | |
mark a. foltz
2014/09/02 20:27:38
s/Protected/Public/ and remove comment below?
Kevin M
2014/09/02 23:15:36
Done.
| |
56 // | |
57 // MessageHeader is publicly visible for testing purposes. | |
58 struct MessageHeader { | |
59 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 | |
69 // include header). | |
70 static size_t max_message_size(); | |
71 std::string ToString(); | |
72 // The size of the following protocol message in bytes, in host byte order. | |
73 size_t message_size; | |
74 }; | |
75 | |
76 private: | |
77 enum MessageElement { HEADER, BODY }; | |
78 | |
79 // Prepares the framer for ingesting a new message. | |
80 void Reset(); | |
81 | |
82 // The element of the message that will be read on the next call to Ingest(). | |
83 MessageElement current_element_; | |
84 | |
85 // Total size of the message, in bytes (head + body). | |
86 size_t message_bytes_received_; | |
87 | |
88 // Size of the body alone, in bytes. | |
89 size_t body_size_; | |
90 | |
91 // Input buffer which carries message data read from the socket. | |
92 // Caller is responsible for writing into this buffer. | |
93 scoped_refptr<net::GrowableIOBuffer> input_buffer_; | |
94 | |
95 // Disables Ingest functionality is the parser receives invalid data. | |
96 bool error_; | |
97 | |
98 DISALLOW_COPY_AND_ASSIGN(MessageFramer); | |
99 }; | |
100 } // namespace cast_channel | |
101 } // namespace core_api | |
102 } // namespace extensions | |
103 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_SOCKET_FRAMER_H_ | |
OLD | NEW |