message_serialization.cc 1.0 KB

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