Chromium Code Reviews| Index: extensions/browser/api/cast_channel/cast_socket_framer.h |
| diff --git a/extensions/browser/api/cast_channel/cast_socket_framer.h b/extensions/browser/api/cast_channel/cast_socket_framer.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1241aa587a23d5fb7d49fc1beb638a3e2ff757a5 |
| --- /dev/null |
| +++ b/extensions/browser/api/cast_channel/cast_socket_framer.h |
| @@ -0,0 +1,103 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_SOCKET_FRAMER_H_ |
| +#define EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_SOCKET_FRAMER_H_ |
| + |
| +#include "base/basictypes.h" |
| +#include "extensions/common/api/cast_channel.h" |
| +#include "net/base/io_buffer.h" |
| + |
| +namespace extensions { |
| +namespace core_api { |
| +namespace cast_channel { |
| +// Size of a CastSocket header payload. |
| +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
|
| +// 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.
|
| +extern size_t const kMaxMessageSizeBytes; |
| + |
| +class CastMessage; |
| + |
| +// Class for constructing and parsing CastMessage packet data. |
| +class MessageFramer { |
| + public: |
| + // |input_buffer|: The input buffer used by all socket read operations that |
| + // feed data into the framer. |
| + explicit MessageFramer(scoped_refptr<net::GrowableIOBuffer> input_buffer); |
| + ~MessageFramer(); |
| + |
| + // 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.
|
| + // CastMessage being read. |
| + size_t BytesRequested(); |
| + |
| + // Serializes |message_proto| into |message_data|. |
| + // Returns true if the message was serialized successfully, false otherwise. |
| + static bool Serialize(const CastMessage& message_proto, |
| + std::string* message_data); |
| + |
| + // Reads bytes from |input_buffer_| and returns a new CastMessage if one |
| + // is fully read. |
| + // |
| + // |num_bytes| The number of bytes received by a read operation. |
| + // Value must be <= BytesRequested(). |
| + // |message_length| Size of the deserialized message object, in bytes. For |
| + // logging purposes. Set to zero if no message was parsed. |
| + // |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.
|
| + // if no error occurred. |
| + // |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.
|
| + // in its entirety, NULL otherwise. |
| + scoped_ptr<CastMessage> Ingest(size_t num_bytes, |
| + size_t* message_length, |
| + ChannelError* error); |
| + |
| + // Message header struct. If fields are added, be sure to update |
| + // 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.
|
| + // |
| + // MessageHeader is publicly visible for testing purposes. |
| + struct MessageHeader { |
| + MessageHeader(); |
| + // Sets the message size. |
| + void SetMessageSize(size_t message_size); |
| + // Prepends this header to |str|. |
| + void PrependToString(std::string* str); |
| + // Reads |header| from the bytes specified by |data|. |
| + static void Deserialize(char* data, MessageHeader* header); |
| + // Size (in bytes) of the message header. |
| + static size_t header_size(); |
| + // Maximum size (in bytes) of a message payload on the wire (does not |
| + // include header). |
| + static size_t max_message_size(); |
| + std::string ToString(); |
| + // The size of the following protocol message in bytes, in host byte order. |
| + size_t message_size; |
| + }; |
| + |
| + private: |
| + enum MessageElement { HEADER, BODY }; |
| + |
| + // Prepares the framer for ingesting a new message. |
| + void Reset(); |
| + |
| + // The element of the message that will be read on the next call to Ingest(). |
| + MessageElement current_element_; |
| + |
| + // Total size of the message, in bytes (head + body). |
| + size_t message_bytes_received_; |
| + |
| + // Size of the body alone, in bytes. |
| + size_t body_size_; |
| + |
| + // Input buffer which carries message data read from the socket. |
| + // Caller is responsible for writing into this buffer. |
| + scoped_refptr<net::GrowableIOBuffer> input_buffer_; |
| + |
| + // Disables Ingest functionality is the parser receives invalid data. |
| + bool error_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(MessageFramer); |
| +}; |
| +} // namespace cast_channel |
| +} // namespace core_api |
| +} // namespace extensions |
| +#endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_SOCKET_FRAMER_H_ |