happy-ip开发tcp client样例(protobuf版)

主函数代码如下

#include <ip/client/tcp_client.h>
#include "proto_client_message_factory.h"
#include <iostream>
#include <utils.h>
#include <thread>
#include <chrono>
#include "dmo.pb.h"
#include <network/proto_network_convert.h>
#ifdef GLOG_OUTPUT
#include <config_glog.h>
#endif
using namespace happy::utils::network;
using namespace std;
using namespace happy::asio::ip;

int main(int argc, char* argv[])
{
    // 默认创建线程池的大小为4。
    //IoServicePool::singleton::Create(4); 
#ifdef GLOG_OUTPUT
    happy::utils::ConfigGlog(argv[0]);
#endif
    auto tcp_client = make_shared <TcpClient>();
    tcp_client->SetMessageFactory(dynamic_pointer_cast <happy::asio::ip::MessageFactory>(make_shared <ProtoClientMessageFactory>("", true)));
    auto const ip = "127.0.0.1";
    auto const port = 1234;
    cout << "connect to " << ip << ":" << port << endl;
    tcp_client->Connect(ip, port);
    cout << "connection is successful" << endl;
    thread t([]() { IoServicePool::singleton::GetInstance()->Run(); });
    t.detach();
    while (true)
    {
        cout << "please input send server string(enter to end): ";
        char buffer[1024];
        cin >> buffer;
        auto request_info = make_shared <RequestInfo>();
        request_info->set_info(buffer);
        string out_buffer;
        ProtoNetworkConvert::singleton::GetInstance()->ToNetwork(request_info, out_buffer);
        tcp_client->AsyncWrite(out_buffer);
        this_thread::sleep_for(chrono::seconds(1));
        //cin.getline(buffer, size(buffer));
    }
    return EXIT_SUCCESS;
}

        主函数中只需要实例化TcpClient类(TcpClient类构造函数提供参数auto_reconnect,默认为true,自动发起异步重连),设置TcpClient类的MessageFactory。IoServicePool如果没有调用create函数, 默认创建线程池的大小为4。一定记得在最后调用IoServicePool类的run函数,启动io线程池。本例使用的是Connect函数(同步连接),也可以使用AsyncConnect发起异步连接。 ProtoClientMessageFactory类构造函数提供了is_read_print参数,设置为true代表网络层自动打印收到的protobuf消息。默认打印消息到终端窗口,如果定义了GLOG_OUTPUT宏,日志就会通过glog方式输出。使用glog方式输出日志,需在使用前调用ConfigGlog函数。

ProtoClientMessageFactory.h代码如下

#pragma once
#include <network/client_message_factory.h>
#include <utils.h>
using namespace happy::utils::network;

class ProtoClientMessageFactory : public ClientMessageFactory
{
public:
    ProtoClientMessageFactory(const string& closed_print = "", const bool is_read_print = false);
private:
    virtual shared_ptr <Message> Produce(const shared_ptr <News> news) override final;
    shared_ptr <Message> ResponseInfoHandler(const shared_ptr<Message> message);
};

        继承于ClientMessageFactory类,不用实现Create虚函数。定义处理各种protobuf消息的函数,此出为ResponseInfoHandler函数,专门处理ResponseInfo消息。

ProtoClientMessageFactory.cpp代码如下

#include "proto_client_message_factory.h"
#include "dmo.pb.h"

ProtoClientMessageFactory::ProtoClientMessageFactory(const string& closed_print, const bool is_read_print)
    : ClientMessageFactory(closed_print, is_read_print)
{
    handler_[RequestInfo::descriptor()->full_name()] = std::bind(&ProtoClientMessageFactory::ResponseInfoHandler, this, std::placeholders::_1);
}

shared_ptr <Message> ProtoClientMessageFactory::Produce(const shared_ptr <News> news)
{
    return nullptr;
}

shared_ptr <Message> ProtoClientMessageFactory::ResponseInfoHandler(const shared_ptr<Message> message)
{
    //auto response_info = dynamic_pointer_cast <ResponseInfo>(message);
    return nullptr;
}

        构造函数中动态绑定protobuf消息的RPC函数。RPC函数ResponseInfoHandler将收到ResponseInfo消息,通过动态转换即可使用。由于ClientMessageFactory类已设置无应答消息,所以RequestInfoHandler函数必须返回nullptr消息。

运行结果如下(打开了GLOG_OUTPUT开关)

connect to 127.0.0.1:1234
connection is successful
please input send server string(enter to end): 1
I0930 20:37:22.878049  4340 util_message_factory.cpp:19] receive message: happy.utils.network.ResponseInfo
I0930 20:37:22.878049  4340 util_message_factory.cpp:22] info: "server response data"

please input send server string(enter to end): 333
I0930 20:37:25.224450  4340 util_message_factory.cpp:19] receive message: happy.utils.network.ResponseInfo
I0930 20:37:25.224450  4340 util_message_factory.cpp:22] info: "server response data"

please input send server string(enter to end):

        表示连接服务器成功,收到服务器2条应答信息。每条消息占2行输出,第1行表示收到服务端的protobuf消息全称,第2行表示消息的具体内容。是不是很简单,欢迎大家使用。

资源下载

      git地址为:https://github.com/wangziwen333/happy-ip

      happy-ip使用的第三方库和头文件:https://download.csdn.net/download/wang19840301/10698515

windows编译参数

cmake .. -LA -DBOOST_INCLUDE_DIR='E:\git\3rdparty\include' -DBOOST_LIB_DIR='E:\git\3rdparty\lib\win32_debug' -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_FLAGS_DEBUG="/MTd /Zi /Ob0 /Od /RTC1" -DBUILD_EXAMPLES=ON -DBUILD_NETWORK=ON -DGLOG_OUTPUT=ON

linux编译参数

cmake .. -LA -DBOOST_INCLUDE_DIR='/mnt/e/git/3rdparty/include' -DBOOST_LIB_DIR='/mnt/e/git/3rdparty/lib/linux_debug' -DCMAKE_BUILD_TYPE=Debug -DBUILD_EXAMPLES=ON -DBUILD_EXAMPLES=ON -DBUILD_NETWORK=ON -DGLOG_OUTPUT=ON

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值