Thrift 双向通信实现(C#版)
来源:互联网 发布:intouch软件下载 编辑:程序博客网 时间:2024/05/17 20:22
主要参考了:Apache thrift RPC 双向通信,但是这篇是用Java写的,和C#会有一些不一样,所以写了一个C#版的。此博客使用Thrift 0.10.0版本,不同版本可能会有差异。
如何在C#中引用Thrift这些就不写了,可以翻我之前的博客,现在开始直接通过代码来说明:
编写Thrift IDL文件
编写一个非常简单的功能,输入一个字符串参数,返回值为空:
//HelloWorldBidirectionService.thriftservice HelloWorldBidirectionService{ oneway void SayHello(1:string msg);}
oneway不能少,具体oneway的含义可查看我之前的博客,这类似一个异步的实现。
编写服务端代码
HelloWorldBidirectionServer实现了Iface接口用于接收客户端消息,并有一个客户端传输层对象集合用于记录所有已连接的客户端。
//HelloWorldBidirectionServer.cpp public class HelloWorldBidirectionServer : HelloWorldBidirectionService.Iface { public void Run(int port) { try { TServerTransport transport = new TServerSocket(port); TTransportFactory transportFac = new TTransportFactory(); TProtocolFactory inputProtocolFactory = new TBinaryProtocol.Factory(); TThreadPoolServer server = new TThreadPoolServer(getProcessorFactory(), transport, transportFac, inputProtocolFactory); server.Serve(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } public static List<TTransport> TransportCollection = new List<TTransport>(); public void SayHello(string msg) { Console.WriteLine(string.Format("{0:yyyy/MM/dd hh:mm:ss} 服务端接收到消息: {1}", DateTime.Now, msg)); } public void SayToClient(string msg) { try { foreach (TTransport trans in TransportCollection) { TBinaryProtocol protocol = new TBinaryProtocol(trans); HelloWorldBidirectionService.Client client = new HelloWorldBidirectionService.Client(protocol); //Thread.Sleep(1000); client.SayHello(msg); Console.WriteLine("发给了客户端哟"); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } public TProcessorFactory getProcessorFactory() { return new HelloWorldBidirectionProcessor(); } }
自定义一个Processor,在每次客户端连接的时候就能监听到,并能获取到客户端的传输层对象TTransport。
public class HelloWorldBidirectionProcessor : TProcessorFactory { public TProcessor GetProcessor(TTransport trans, TServer server = null) { if (trans.IsOpen) { HelloWorldBidirectionServer.TransportCollection.Add(trans); Console.WriteLine("有客户端上船啦"); } HelloWorldBidirectionServer srv = new HelloWorldBidirectionServer(); return new global::HelloWorldBidirectionService.Processor(srv); } }
客户端代码
首先定义一个可以接收服务端消息的类,里面只有一个实现Iface接口的方法:
public class HelloWorldBidirectionFace : HelloWorldBidirectionService.Iface { public void SayHello(string msg) { Console.WriteLine(string.Format("{0:yyyy/MM/dd hh:mm:ss} 收到服务端响应消息 {1}", DateTime.Now, msg)); } }
实现客户端,ConnectAndListern方法可以与服务端建立连接,并开启客户端端口监听来自服务端的信息。Say方法可将消息发送至服务端。
public class HelloWorldBidirectionClient { static HelloWorldBidirectionService.Client client = null; public void ConnectAndListern(int port, string ip = "127.0.0.1") { //Tsocket: TCP/IP Socket接口 TSocket tSocket = new TSocket(ip, port); //消息结构协议 TProtocol protocol = new TBinaryProtocol(tSocket); try { if (client == null) { client = new global::HelloWorldBidirectionService.Client(protocol); tSocket.Open();//建立连接 StartListern(tSocket);//启动监听线程 } //cli.SayHello(msg); //tSocket.Close(); //cli.Dispose(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } public void Say(string msg) { if (client != null) client.SayHello(msg); } void StartListern(TSocket tSocket) { Thread t = new Thread(new ParameterizedThreadStart(Run)); t.Start(tSocket); } public void Run(object tSocket) { HelloWorldBidirectionService.Processor process = new HelloWorldBidirectionService.Processor(new HelloWorldBidirectionFace()); try { while (process.Process(new TBinaryProtocol((TSocket)tSocket), new TBinaryProtocol((TSocket)tSocket))) { Console.WriteLine("消息接收完成,等下一波,阻塞中......"); } } catch (Exception ex) { Console.WriteLine("连接断开..." + ex.Message); } } }
源码地址:
Thrift_HelloWorld_CSharp
阅读全文
0 0
- Thrift 双向通信实现(C#版)
- Thrift 双向通信实现(Android版)
- Thrift 双向通信实现(c++版)
- 让Thrift支持双向通信
- AIDL实现跨APP通信(双向通信)
- C++实现局域网双向通信(socket)
- Thrift 客户端 C# ---实现zookeeper监视(1)
- 客户端服务器端实现双向通信
- vue实现父子双向通信
- Thrift应用:Windows下C#服务端实现
- C#实现Thrift服务端与客户端
- Thrift c#
- 在android上实现ssl双向通信(https形式)
- lce的回调实现(Bidirectional Connections)双向通信
- 成功将thrift单向通信该为双向通信
- 实现DHTML代码和C#客户端应用程序代码之间的双向通信
- Thrift 客户端 C# ---从zookeeper获取服务器信息、实现负载(2)
- Thrift交流(二)thrift服务端和客户端实现 Nifty
- 屏幕适配--自动生成各种屏幕大小的尺寸文件的jar包
- bmp转rgb565在framebuffer中显示
- ios 证书与 profile
- CI使用阿里云短信服务
- XGBoost探索
- Thrift 双向通信实现(C#版)
- Noip2015 Day1 T2 信息传递(虽然是图论题但是...)
- 注解 @CrossOrigin
- 机器学习第四课(padding)
- 数组模拟链表之P1160 队列安排
- Day8-27.Literals
- Spark以及生态圈介绍
- EasyStream系列1之rtsp转rtmp
- itext实现word转pdf,添加水印倾斜铺满全屏,禁止修改和复制