Thrift 双向通信实现(c++版)
来源:互联网 发布:mac的照片如何导出 编辑:程序博客网 时间:2024/06/05 20:05
1. thrift文件
thrift IDL文件源码:
service HelloWorldBidirectionService{ oneway void SayHello(1:string msg);}
2. 开发环境设置
3. thrift服务端编写
thrift服务端修改,将HelloWorldBidirectionService_server.skeleton.cpp文件修改为:
// This autogenerated skeleton file illustrates how to build a server.// You should copy it to another filename to avoid overwriting it.#include "HelloWorldBidirectionService.h"#include <thrift/protocol/TBinaryProtocol.h>#include <thrift/server/TSimpleServer.h>#include <thrift/transport/TServerSocket.h>#include <thrift/transport/TBufferTransports.h>#include <thrift/server/TThreadPoolServer.h>#include <thrift/concurrency/ThreadManager.h>#include <thrift/concurrency/PosixThreadFactory.h> #include <thrift/concurrency/StdThreadFactory.h>using namespace ::apache::thrift;using namespace ::apache::thrift::protocol;using namespace ::apache::thrift::transport;using namespace ::apache::thrift::server;using namespace ::apache::thrift::concurrency;using boost::shared_ptr;static boost::shared_ptr<transport::TTransport> g_CurrentTransport = nullptr;class HelloWorldBidirectionServiceHandler : virtual public HelloWorldBidirectionServiceIf {public: HelloWorldBidirectionServiceHandler() { // Your initialization goes here } void SayHello(const std::string& msg) { // Your implementation goes here printf("%s\n", msg); SayToClient(msg); } void SayToClient(const std::string& msg) { shared_ptr<TBinaryProtocol> protocol(new TBinaryProtocol(g_CurrentTransport)); HelloWorldBidirectionServiceClient client(protocol); //Thread.Sleep(1000); client.SayHello(msg); }};class HelloWorldBidirectionProcessor : public TProcessorFactory{public: boost::shared_ptr<TProcessor> getProcessor(const TConnectionInfo& connInfo) { g_CurrentTransport = connInfo.transport; printf("接收到连接信号\n"); shared_ptr<HelloWorldBidirectionServiceHandler> handler(new HelloWorldBidirectionServiceHandler()); shared_ptr<TProcessor> processor(new HelloWorldBidirectionServiceProcessor(handler)); return processor; }};static boost::shared_ptr<TProcessorFactory> getProcessorFactory(){ shared_ptr<TProcessorFactory> processorFactory(new HelloWorldBidirectionProcessor()); return processorFactory;}int main_tt(int argc, char **argv) { int port = 9090; shared_ptr<TServerTransport> serverTransport(new TServerSocket(port)); shared_ptr<TTransportFactory> transportFactory(new TTransportFactory()); shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory()); shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(10);//指定10个线程数 shared_ptr<StdThreadFactory> threadFactory = shared_ptr<StdThreadFactory>(new StdThreadFactory()); threadManager->threadFactory(threadFactory); threadManager->start(); shared_ptr<TThreadPoolServer> server(new TThreadPoolServer(getProcessorFactory(), serverTransport, transportFactory, protocolFactory, threadManager)); /* shared_ptr<HelloWorldBidirectionServiceHandler> handler(new HelloWorldBidirectionServiceHandler()); shared_ptr<TProcessor> processor(new HelloWorldBidirectionServiceProcessor(handler)); shared_ptr<TServerTransport> serverTransport(new TServerSocket(port)); shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory()); shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory()); TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);*/ server->serve(); return 0;}
4. thrift 客户端编写
thrift 客户端修改,新建文件 HelloWorldBidirectionService_client.cpp,编码:
#include <iostream>#include <thrift/protocol/TBinaryProtocol.h>#include <thrift/transport/TSocket.h>#include <thrift/transport/TTransportUtils.h>#include "HelloWorldBidirectionService.h"#include <boost/thread.hpp>using namespace std;using namespace apache::thrift;using namespace apache::thrift::protocol;using namespace apache::thrift::transport;//using namespace tutorial;using boost::shared_ptr;//using namespace boost;using boost::thread;#define BOOST_DATE_TIME_SOURCE#define BOOST_THREAD_NO_LIBclass HelloWorldBidirectionFace :public HelloWorldBidirectionServiceIf{public: void SayHello(const std::string& msg) { // Your implementation goes here /*printf("%s\n", msg); printf("%s\n", &msg);*/ cout << msg << endl; //cout << &msg << endl; }};static void Run(boost::shared_ptr<TTransport> sock){ boost::shared_ptr<HelloWorldBidirectionFace> handler(new HelloWorldBidirectionFace()); boost::shared_ptr<TProcessor> processor(new HelloWorldBidirectionServiceProcessor(handler)); //boost::shared_ptr<HelloWorldBidirectionServiceProcessor> processor(new HelloWorldBidirectionFace()); try { boost::shared_ptr<TProtocol> inProtocol(new TBinaryProtocol(sock)); boost::shared_ptr<TProtocol> outProtocol(new TBinaryProtocol(sock)); while (processor->process(inProtocol, outProtocol, "proc")) { printf("wait next msg\n"); } } catch (TException& tx) { printf("connect close\n"); cout << "ERROR: " << tx.what() << endl; }}static void RecFromConsole(boost::shared_ptr<HelloWorldBidirectionServiceClient> client){ string str; cout << "input exit stop" << endl; cin >> str; while (str.compare("exit")) { client->SayHello(str); cin >> str; }}int main(){ boost::shared_ptr<TTransport> socket(new TSocket("localhost", 9090)); boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket)); boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport)); boost::shared_ptr<HelloWorldBidirectionServiceClient> client(new HelloWorldBidirectionServiceClient(protocol)); try { transport->open(); boost::thread mythread(Run, socket); boost::thread mythread2(RecFromConsole, client); mythread2.join(); transport->close(); } catch (TException& tx) { cout << "ERROR: " << tx.what() << endl; }}
6. 源码地址:
Thrift_HelloWorld_Cpp_Bidirection
阅读全文
0 0
- Thrift 双向通信实现(C#版)
- Thrift 双向通信实现(Android版)
- Thrift 双向通信实现(c++版)
- 让Thrift支持双向通信
- AIDL实现跨APP通信(双向通信)
- C++实现局域网双向通信(socket)
- 客户端服务器端实现双向通信
- vue实现父子双向通信
- 在android上实现ssl双向通信(https形式)
- lce的回调实现(Bidirectional Connections)双向通信
- 成功将thrift单向通信该为双向通信
- Thrift交流(二)thrift服务端和客户端实现 Nifty
- Thrift交流(二)thrift服务端和客户端实现 Nifty
- Thrift学习笔记(4)--实现Thrift客户端连接池
- Thrift交流(二)thrift服务端和客户端实现 Nifty
- 如何通过Remoting实现双向通信
- 如何通过Remoting实现双向通信
- thrift编写服务器/客户端实现图片传输(c++)
- Mac OS X 下安装使用 Docker
- 【Unity3D】UGUI向导式界面和组件复用
- java构造方法、静态变量初始化顺序的小例子
- cocoaPods更新
- 集合第四步: TreeSet比较顺序和比较重复机制
- Thrift 双向通信实现(c++版)
- 七、输入/输出流--iostream简介
- 简单分析HashMap及其线程安全的Map类
- 用大白话聊聊分布式系统
- Windows中,.net framework 3.5安装
- vector相关的学习整理
- 常用快捷键(IDEA2017)
- Eclipse 上传 删除 下载 分析 hdfs 上的文件
- mac IDEA快捷键