/******************************************************************
*
- * Copyright 2017 Samsung Electronics All Rights Reserved.
+ * Copyright 2017 - 2020 Samsung Electronics All Rights Reserved.
*
* Author: Jaroslaw Pelczar <j.pelczar@samsung.com>
*
*
******************************************************************/
-#include "protobuf_asio.h"
-#include <google/protobuf/io/coded_stream.h>
#include <stdexcept>
+#include <google/protobuf/io/coded_stream.h>
+
+#include "protobuf_asio.h"
+
protobuf_sync_message_serialization::protobuf_sync_message_serialization(
boost::asio::local::stream_protocol::socket& socket) :
fSocket(socket)
boost::asio::read(fSocket, boost::asio::buffer(header));
google::protobuf::io::CodedInputStream::ReadLittleEndian32FromArray(header, &length);
- if(length < 1 || length > kDCMMessageMaxLength) {
+ if(length < 1 || length > MESSAGE_LENGHT_MAX) {
throw std::length_error("Invalid message length");
}
/******************************************************************
*
- * Copyright 2017 - 2019 Samsung Electronics All Rights Reserved.
+ * Copyright 2017 - 2020 Samsung Electronics All Rights Reserved.
*
* Author: Jaroslaw Pelczar <j.pelczar@samsung.com>
*
#ifndef SHARED_PROTOBUF_ASIO_H_
#define SHARED_PROTOBUF_ASIO_H_
+#include <vector>
+
#include <boost/asio.hpp>
#include <boost/noncopyable.hpp>
#include <google/protobuf/message_lite.h>
#include <google/protobuf/io/zero_copy_stream_impl_lite.h>
#include <google/protobuf/io/coded_stream.h>
-#include <vector>
-static const size_t kDCMMessageMaxLength = 65536;
+static const size_t MESSAGE_LENGHT_MAX = 65536;
class protobuf_sync_message_serialization final :
boost::noncopyable,
boost::asio::local::stream_protocol::socket& fSocket;
public:
protobuf_sync_message_serialization(boost::asio::local::stream_protocol::socket& socket);
-
void encodeMessage(const google::protobuf::MessageLite& message);
private:
virtual bool Write(const void* buffer, int size) override;
};
-class protobuf_sync_message_deserialization final :
- boost::noncopyable
+class protobuf_sync_message_deserialization final : boost::noncopyable
{
boost::asio::local::stream_protocol::socket& fSocket;
public:
protobuf_sync_message_deserialization(boost::asio::local::stream_protocol::socket& socket);
-
void decodeMessage(google::protobuf::MessageLite& message);
};
class protobuf_async_message_serialization final : boost::noncopyable
{
- std::vector<char> fBuffer;
+ std::vector<char> fBuffer;
public:
void encodeMessage(const google::protobuf::MessageLite& message);
class protobuf_async_message_deserialization final : public boost::noncopyable
{
- std::vector<char> fBuffer;
+ std::vector<char> fBuffer;
google::protobuf::uint32 fMessageLength = 0;
public:
protobuf_async_message_deserialization();
- template<typename ReadSocket, typename ReadHandler> void read_message(ReadSocket& socket,
- ReadHandler&& handler) {
+ template<typename ReadSocket, typename ReadHandler> void read_message(ReadSocket& socket, ReadHandler&& handler) {
fBuffer.resize(4);
fMessageLength = 0;
boost::asio::async_read(socket, boost::asio::buffer(fBuffer),
- [this, handler, &socket](const boost::system::error_code& error, std::size_t bytes_read) {
- if(!error) {
- assert(bytes_read == fBuffer.size());
- (void)bytes_read;
- assert(fMessageLength == 0);
-
- google::protobuf::io::CodedInputStream::ReadLittleEndian32FromArray(
- (const google::protobuf::uint8 *)&fBuffer[0],
- &fMessageLength);
-
- if(fMessageLength >= 1 && fMessageLength < kDCMMessageMaxLength) {
- try {
- fBuffer.resize(fMessageLength);
- } catch(...) {
- handler(
- boost::system::errc::make_error_code(
- boost::system::errc::not_enough_memory),
- 0);
- return;
- }
-
- boost::asio::async_read(socket,
- boost::asio::buffer(fBuffer),
- handler);
- } else {
- handler(
- boost::system::errc::make_error_code(
- boost::system::errc::message_size),
- 0);
+ [this, handler, &socket](const boost::system::error_code& error, std::size_t bytes_read) {
+ if(!error) {
+ assert(bytes_read == fBuffer.size());
+ (void)bytes_read;
+ assert(fMessageLength == 0);
+
+ google::protobuf::io::CodedInputStream::ReadLittleEndian32FromArray(
+ (const google::protobuf::uint8 *)&fBuffer[0], &fMessageLength);
+
+ if(fMessageLength >= 1 && fMessageLength < MESSAGE_LENGHT_MAX) {
+ try {
+ fBuffer.resize(fMessageLength);
+ } catch(...) {
+ handler(boost::system::errc::make_error_code(boost::system::errc::not_enough_memory), 0);
+ return;
}
+
+ boost::asio::async_read(socket, boost::asio::buffer(fBuffer), handler);
} else {
- handler(error, 0);
+ handler(boost::system::errc::make_error_code(boost::system::errc::message_size), 0);
}
- });
+ } else {
+ handler(error, 0);
+ }
+ });
}
bool decode_received_message(google::protobuf::MessageLite& message);