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 #include "remoting/protocol/message_serialization.h"
6 
7 #include <stdint.h>
8 
9 #include "net/base/io_buffer.h"
10 #include "third_party/webrtc/rtc_base/byte_order.h"
11 
12 namespace remoting {
13 namespace protocol {
14 
SerializeAndFrameMessage(const google::protobuf::MessageLite & msg)15 scoped_refptr<net::IOBufferWithSize> SerializeAndFrameMessage(
16     const google::protobuf::MessageLite& msg) {
17   // Create a buffer with 4 extra bytes. This is used as prefix to write an
18   // int32_t of the serialized message size for framing.
19   const int kExtraBytes = sizeof(int32_t);
20   int size = msg.ByteSize() + kExtraBytes;
21   scoped_refptr<net::IOBufferWithSize> buffer =
22       base::MakeRefCounted<net::IOBufferWithSize>(size);
23   rtc::SetBE32(buffer->data(), msg.GetCachedSize());
24   msg.SerializeWithCachedSizesToArray(
25       reinterpret_cast<uint8_t*>(buffer->data()) + kExtraBytes);
26   return buffer;
27 }
28 
29 }  // namespace protocol
30 }  // namespace remoting
31