public ZMsg Request(string service, string token, ZMsg msg, int timeout = 2500) { IntPtr c_msg = C.zmsg_new(); IEnumerator <byte[]> fe = msg.GetEnumerator(); while (fe.MoveNext()) { IntPtr c_frame = ZMsg.ToZFramePtr(fe.Current); C.zmsg_push_back(c_msg, c_frame); } IntPtr res = C.zbuscli_request(this.connection.Handle, service, token, c_msg, timeout); if (res == IntPtr.Zero) { throw new ZBusException("request timeout"); } ZMsg result = null; if (res != IntPtr.Zero) { result = ZMsg.ToZMsg(res); } return(result); }
public static void Main(string[] args) { //1) 创建连接 ConnectionConfig connCfg = new ConnectionConfig(); connCfg.Host = "127.0.0.1"; connCfg.Port = 15555; connCfg.Id = "local_mq"; BusClient client = new BusClient(connCfg); //2)异步处理消息 while (true) { try { //recv参数指定发送心跳时间间隔,(防火墙切断链接) ZMsg msg = client.Recv(2500);//2.5s for ping msg.Dump(); } catch (Exception e) { Console.WriteLine(e); break; } } //3)销毁连接 client.Destroy(); }
public static void Main(string[] args) { ConnectionConfig connCfg = new ConnectionConfig(); connCfg.Host = "127.0.0.1"; connCfg.Port = 15555; Connection conn = new Connection(connCfg); WorkerConfig workerCfg = new WorkerConfig(); workerCfg.Service = "MyPubSub"; workerCfg.Mode = WorkerConfig.MODE_PUBSUB; Worker worker = new Worker(conn, workerCfg); worker.Subscribe("topic1"); Console.WriteLine("C# Subscriber on {0}", "topic1"); while (true) { try { ZMsg msg = worker.Recv(); msg.Dump(); } catch (Exception e) { Console.WriteLine(e); break; } } conn.Destroy(); worker.Destroy(); }
public ZMsg Send(AsynCtrl ctrl, ZMsg msg) { IntPtr c_msg = C.zmsg_new(); IEnumerator <byte[]> fe = msg.GetEnumerator(); while (fe.MoveNext()) { IntPtr c_frame = ZMsg.ToZFramePtr(fe.Current); C.zmsg_push_back(c_msg, c_frame); } IntPtr c_ctrl = C.asyn_ctrl_new(ctrl.Service, ctrl.Token, ctrl.Timeout, ctrl.PeerId, ctrl.MessageId); IntPtr res = C.zbuscli_send(this.connection.Handle, c_ctrl, c_msg); C.asyn_ctrl_destroy(out c_ctrl); if (res == IntPtr.Zero) { throw new ZBusException("send timeout"); } ZMsg result = null; if (res != IntPtr.Zero) { result = ZMsg.ToZMsg(res); } return(result); }
public void Run() { Connection connection = new Connection(this.connCfg); Worker worker = new Worker(connection, this.workerCfg); if (this.handler is ServiceHandler) { ServiceHandler serviceHandler = this.handler as ServiceHandler; while (true) { ZMsg request = worker.Recv(); if (request == null) { break; //interrupted } ZMsg reply = serviceHandler.HandleRequest(request); if (reply != null) { worker.Send(reply); } } } else if (this.handler is WorkerHandler) { WorkerHandler workerHanlder = this.handler as WorkerHandler; workerHanlder.HandleWorker(worker); } else { throw new ZBusException("handler invalid"); } worker.Dispose(); connection.Dispose(); }
public static void Main(string[] args) { //1) 创建连接 ConnectionConfig config = new ConnectionConfig(); config.Host = "127.0.0.1"; config.Port = 15555; BusClient client = new BusClient(config); //2) 组装消息(消息帧数组) ZMsg request = new ZMsg(); request.PushBack("Frame1"); //消息帧1 -- 字符类型 request.PushBack(new byte[10]); //消息帧2 -- 二进制串 request.PushBack("request from C#"); //消息帧3 -- 字符类型 //3) 向ZBUS总线发送请求 ZMsg result = client.Request("MyService", "", request); result.Dump(); //4) 销毁客户端 client.Destroy(); Console.ReadKey(); }
public static void Main(string[] args) { //1) 创建连接 ConnectionConfig config = new ConnectionConfig(); config.Host = "127.0.0.1"; config.Port = 15555; BusClient client = new BusClient(config); //2) 异步控制结构 AsynCtrl ctrl = new AsynCtrl(); ctrl.Service = "MyService"; ctrl.PeerId = "local_mq"; //消息投递目标ID,与接收方协商 ctrl.MessageId = "10000"; ctrl.Timeout = 2500; ////异步投递失败提示最长时间(ms) //3) 消息体(帧组成) ZMsg msg = new ZMsg(); msg.PushBack("asyn call frame1"); msg.PushBack("asyn call frame2"); //4) 发送异步消息 msg = client.Send(ctrl, msg); msg.Dump(); //5)销毁链接 client.Destroy(); Console.ReadKey(); }
public ZMsg HandleRequest(ZMsg request) { request.Dump(); ZMsg result = new ZMsg(); result.PushBack("this is from pooled c# worker"); return(result); }
public ZMsg Recv(int timeout) { IntPtr res = C.zbusconn_recv(this._handle, timeout); ZMsg result = null; if (res != IntPtr.Zero) { result = ZMsg.ToZMsg(res); } return(result); }
public ZMsg Recv(int pingInterval) { IntPtr res = C.zbuscli_recv(this.connection.Handle, pingInterval); ZMsg result = null; if (res != IntPtr.Zero) { result = ZMsg.ToZMsg(res); } return(result); }
public static IntPtr ToZMsgPtr(ZMsg msg) { IntPtr zmsg = C.zmsg_new(); IEnumerator <byte[]> fe = msg.GetEnumerator(); while (fe.MoveNext()) { IntPtr framePtr = ToZFramePtr(fe.Current); C.zmsg_push_back(zmsg, framePtr); } return(zmsg); }
public int Send(ZMsg msg) { IntPtr c_msg = C.zmsg_new(); IEnumerator <byte[]> fe = msg.GetEnumerator(); while (fe.MoveNext()) { IntPtr c_frame = ZMsg.ToZFramePtr(fe.Current); C.zmsg_push_back(c_msg, c_frame); } int res = C.zbusconn_send(this._handle, c_msg); return(res); }
/// <summary> /// Invoke remote procedure via zbus using JSON data format /// </summary> /// <param name="method">method name to invoke</param> /// <param name="args">method arguments</param> /// <returns>primitive type or Dictionary<string,object> instance</returns> public object Invoke(string method, params object[] args) { Dictionary <string, object> req = new Dictionary <string, object>(); req["id"] = "jsonrpc"; req["method"] = method; req["params"] = args; string json = JSON.Instance.ToJSON(req); ZMsg request = new ZMsg(); request.PushBack(this.Encoding.GetBytes(json)); ZMsg result = this.client.Request(this.Service, this.Token, request, this.timeout); if (result == null) { throw new ZBusException("json rpc request timeout"); } if (result.FrameSize != 2) { throw new ZBusException("json rpc result format error"); } Dictionary <string, object> res = null; string status = result.PopFrontStr(); if (status.Equals("200")) { try { json = result.PopBackStr(this.Encoding); res = (Dictionary <string, object>)JSON.Instance.Parse(json); if (res.ContainsKey("result")) { return(res["result"]); } else { throw new ZBusException((string)res["error"]); } } catch (System.Exception ex) { throw new ZBusException(ex.Message); } } else { throw new ZBusException(result.PopBackStr(this.Encoding)); } }
public int Route(byte[] sockId, ZMsg msg) { IntPtr c_sock = ZMsg.ToZFramePtr(sockId); IntPtr c_msg = C.zmsg_new(); IEnumerator <byte[]> fe = msg.GetEnumerator(); while (fe.MoveNext()) { IntPtr frame = ZMsg.ToZFramePtr(fe.Current); C.zmsg_push_back(c_msg, frame); } int res = C.zbusconn_route(this._handle, c_sock, c_msg); return(res); }
public static ZMsg ToZMsg(IntPtr msg) { ZMsg zmsg = new ZMsg(); IntPtr frame = C.zmsg_pop_front(msg); while (frame != IntPtr.Zero) { int frameSize = C.zmq_msg_size(frame); byte[] data = new byte[frameSize]; Marshal.Copy(C.zmq_msg_data(frame), data, 0, frameSize); zmsg.PushBack(data); C.zframe_destroy(out frame); frame = C.zmsg_pop_front(msg); } C.zmsg_destroy(out msg); return(zmsg); }
public static void Main(string[] args) { //1) 创建连接到ZBUS总线 ConnectionConfig connCfg = new ConnectionConfig(); connCfg.Host = "127.0.0.1"; connCfg.Port = 15555; Connection conn = new Connection(connCfg); //2) 注册服务(MyService) WorkerConfig workerCfg = new WorkerConfig(); workerCfg.Service = "MyService"; workerCfg.Mode = WorkerConfig.MODE_LB;//负载均衡模式 Worker worker = new Worker(conn, workerCfg); Console.WriteLine("C# Simple Worker Running..."); //3) 服务业务逻辑循环体(等待ZBUS总线分发请求,处理请求,返回请求结果) while (true) { try { //3.1) 等待ZBUS总线分发请求消息 ZMsg msg = worker.Recv(); //3.2) 业务逻辑处理请求消息 msg.Dump(); //3.3) 返回处理后的消息 msg = new ZMsg(); msg.PushBack("this is from c# loadbalancer"); worker.Send(msg); } catch (Exception e) { Console.WriteLine(e); break; } } //4) 清除连接相关 conn.Destroy(); worker.Destroy(); }
public ZMsg Recv() { IntPtr res = C.zbuswrk_recv(this.connection.Handle, this._handle); IntPtr c_sock_id, c_msg_id; C.zbuswrk_get_address(this._handle, out c_sock_id, out c_msg_id); this.RecvSockId = ZMsg.ToZFrame(c_sock_id); this.RecvMsgId = ZMsg.ToZFrame(c_msg_id); ZMsg result = null; if (res != IntPtr.Zero) { result = ZMsg.ToZMsg(res); } return(result); }
public int Send(ZMsg msg) { IntPtr c_msg = C.zmsg_new(); IEnumerator <byte[]> fe = msg.GetEnumerator(); while (fe.MoveNext()) { IntPtr c_frame = ZMsg.ToZFramePtr(fe.Current); C.zmsg_push_back(c_msg, c_frame); } IntPtr c_sock_id, c_msg_id; c_sock_id = ZMsg.ToZFramePtr(this.RecvSockId); c_msg_id = ZMsg.ToZFramePtr(this.RecvMsgId); C.zbuswrk_set_address(this._handle, c_sock_id, c_msg_id); return(C.zbuswrk_send(this.connection.Handle, this._handle, c_msg)); }
public static void Main(string[] args) { ConnectionConfig config = new ConnectionConfig(); config.Host = "127.0.0.1"; config.Port = 15555; //config.Verbose = true; BusClient client = new BusClient(config); ZMsg msg = new ZMsg(); msg.PushBack("publish from C#"); bool result = client.Publish("MyPubSub", "", "topic1", msg); Console.WriteLine("publish result: " + result); client.Destroy(); Console.ReadKey(); }
public bool Publish(string service, string token, string topic, ZMsg msg, int timeout = 2500) { AsynCtrl ctrl = new AsynCtrl(); ctrl.Service = service; ctrl.Token = token; ctrl.Timeout = timeout; msg.PushFront(topic); ZMsg res = this.Send(ctrl, msg); string status = res.PopFrontStr(); if (status != null && status.Equals("200")) { return(true); } else { return(false); } }
public ZMsg HandleRequest(ZMsg request) { ZMsg reply = new ZMsg(); reply.PushBack("200"); try { IEnumerator <byte[]> fe = request.GetEnumerator(); while (fe.MoveNext()) { string res = this.HandleJsonRequest(encoding.GetString(fe.Current)); reply.PushBack(encoding.GetBytes(res)); } } catch (System.Exception ex) { reply.Clear(); reply.PushBack("500"); string error = string.Format("Internal Error: {0}", ex.Message); reply.PushBack(encoding.GetBytes(error)); } return(reply); }
public int Send(ZMsg msg) { return(this.connection.Value.Send(msg)); }
public int Route(byte[] sockId, ZMsg msg) { return(this.connection.Value.Route(sockId, msg)); }