/*
|
* =====================================================================================
|
*
|
* Filename: tcp_test.cpp
|
*
|
* Description:
|
*
|
* Version: 1.0
|
* Created: 2021年05月24日 09时40分14秒
|
* Revision: none
|
* Compiler: gcc
|
*
|
* Author: Li Chao (), lichao@aiotlink.com
|
* Organization:
|
*
|
* =====================================================================================
|
*/
|
|
#include "defs.h"
|
#include "node_center.h"
|
#include "tcp_connection.h"
|
#include "tcp_proxy.h"
|
#include "tcp_server.h"
|
#include "util.h"
|
#include <sys/ioctl.h>
|
|
//////////////////////
|
|
template <class C, class V>
|
void Erase(C &c, V &&v)
|
{
|
c.erase(std::remove(c.begin(), c.end(), v), c.end());
|
}
|
|
BOOST_AUTO_TEST_CASE(TcpTest)
|
{
|
const std::string connect_addr = "127.0.0.1";
|
const uint16_t port = kBHCenterPort;
|
|
IoService io;
|
|
tcp::endpoint dest(ip::address::from_string(connect_addr), port);
|
|
auto NewRequest = [&]() {
|
MsgRequestTopic req;
|
req.set_topic("#center_query_procs");
|
req.set_data("");
|
auto head = InitMsgHead(GetType(req), "#test_proc", 1000000);
|
auto route = head.add_route();
|
route->set_mq_id(12345);
|
route->set_abs_addr(67890);
|
|
head.mutable_dest()->set_ip(connect_addr);
|
head.mutable_dest()->set_port(port);
|
head.mutable_dest()->set_mq_id(201);
|
head.mutable_dest()->set_abs_addr(10072);
|
|
return (MsgI::Serialize(head, req));
|
};
|
auto onReply = [](BHMsgHead &head, std::string body_content) {
|
static int n = 0;
|
printf("reply %d: ", ++n);
|
MsgRequestTopicReply reply;
|
if (reply.ParseFromString(body_content)) {
|
if (IsSuccess(reply.errmsg().errcode())) {
|
printf("\ncontent: %s\n", reply.data().c_str());
|
} else {
|
printf("error: %s\n", reply.errmsg().errstring().c_str());
|
}
|
} else {
|
printf("parse error\n");
|
}
|
};
|
for (int i = 0; i < 100; ++i) {
|
auto request = NewRequest();
|
TcpRequest1::Create(io.io(), dest, request, onReply);
|
}
|
Sleep(2s);
|
printf("-------------------------------------------------------\n");
|
for (int i = 0; i < 3; ++i) {
|
auto request = NewRequest();
|
TcpRequest1::Create(io.io(), dest, request, onReply);
|
}
|
Sleep(2s);
|
|
printf("TcpTest\n");
|
}
|