Detailed:
As base used jsonrpccxx implementation paired with TCP socket
TCP socket updated to handle dynamic sized buffers
TCP communication protocol changed to serialized packet size after
which json string is presented
58 lines
1.7 KiB
C++
58 lines
1.7 KiB
C++
#include "cloud_point_rpc/serialize.hpp"
|
|
#include "cloud_point_rpc/tcp_connector.hpp"
|
|
#include "cloud_point_rpc/tcp_server.hpp"
|
|
#include <asio.hpp>
|
|
#include <cstdint>
|
|
#include <gmock/gmock.h>
|
|
#include <gtest/gtest.h>
|
|
#include <memory>
|
|
|
|
class TcpTest : public ::testing::Test {
|
|
public:
|
|
void ExpectedResponse(const std::string &expected_response) {
|
|
expected_ = expected_response;
|
|
}
|
|
|
|
protected:
|
|
void SetUp() override {
|
|
server_ = std::make_unique<cloud_point_rpc::TcpServer>(
|
|
"127.0.0.1", 12345, [this](const std::string &request) {
|
|
EXPECT_EQ(request, expected_);
|
|
std::string msg = "Echo: " + request;
|
|
auto v = cloud_point_rpc::serialize(msg.length());
|
|
std::string res(v.begin(), v.end());
|
|
res += msg;
|
|
return res;
|
|
});
|
|
server_->start();
|
|
}
|
|
|
|
void TearDown() override {
|
|
server_->stop();
|
|
server_.reset();
|
|
}
|
|
|
|
std::string expected_;
|
|
std::unique_ptr<cloud_point_rpc::TcpServer> server_;
|
|
};
|
|
|
|
TEST(SerializeTest, Base) {
|
|
uint64_t value{123};
|
|
auto res = cloud_point_rpc::serialize(value);
|
|
EXPECT_EQ(value, cloud_point_rpc::deserialize<uint64_t>(res));
|
|
}
|
|
|
|
TEST_F(TcpTest, EchoTest) {
|
|
constexpr std::string_view msg = "Hello, TCP Server!";
|
|
ExpectedResponse(msg.data());
|
|
cloud_point_rpc::TCPConnector connector("127.0.0.1", 12345);
|
|
auto res = connector.Send(msg.data());
|
|
}
|
|
|
|
TEST_F(TcpTest, HugeBuffer) {
|
|
static constexpr uint64_t w = 1920, h = 1080, c = 3;
|
|
std::string data(w * h * c, 77);
|
|
ExpectedResponse(data);
|
|
cloud_point_rpc::TCPConnector connector("127.0.0.1", 12345);
|
|
auto res = connector.Send(data);
|
|
} |