/// <summary> /// 发送请求 /// </summary> /// <param name="serviceName"></param> /// <param name="method"></param> /// <param name="args"></param> /// <param name="timeOut"></param> /// <returns></returns> public byte[] Request(string serviceName, string method, byte[] args, int timeOut) { try { byte[] result = null; var msg = new RSocketMsg(RSocketMsgType.Request, serviceName, method) { SequenceNumber = UniqueKeyHelper.Next() }; msg.Data = args; if (_syncHelper.Wait(msg.SequenceNumber, () => { this.BeginSend(msg); }, (r) => { result = r; }, timeOut)) { return(result); } else { ExceptionCollector.Add("Consumer", new RPCSocketException($"serviceName:{serviceName}/method:{method} 调用超时!")); } } catch (Exception ex) { ExceptionCollector.Add("Consumer", new RPCSocketException($"serviceName:{serviceName}/method:{method} 调用出现异常!", ex)); } return(null); }
/// <summary> /// 本地rpc远程代理 /// </summary> /// <param name="serviceName"></param> /// <param name="method"></param> /// <param name="args"></param> /// <returns></returns> public byte[] RemoteCall(string serviceName, string method, byte[] args) { if (!_isConnected) { throw new RPCSocketException("连接到服务器失败"); } byte[] result = null; var msg = new RSocketMsg(RSocketMsgType.Request, serviceName, method) { SequenceNumber = UniqueKeyHelper.Next() }; msg.Data = args; BaseSend(msg); if (_syncHelper.WaitOne(msg.SequenceNumber, (r) => { result = r; }, 10 * 1000)) { return(result); } return(null); }
/// <summary> /// 向注册了接收通知的rpc client 发送通知 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="t"></param> public void Notice <T>(T t) { if (t != null) { var list = _noticeCollection.GetList().GetAwaiter().GetResult(); if (list != null && list.Any()) { var data = SAEASerialize.Serialize(t); var msg = new RSocketMsg(RSocketMsgType.Notice, null, null, data) { SequenceNumber = UniqueKeyHelper.Next() }; foreach (var item in list) { try { _RServer.Reply(item, msg); } catch (Exception ex) { LogHelper.Warn("ServiceProvider.Notice 出现异常", ex); } } } } }
/// <summary> /// 向rpc service provider 注册接收通知 /// </summary> public void RegistReceiveNotice() { var msg = new RSocketMsg(RSocketMsgType.RegistNotice, null, null) { SequenceNumber = UniqueKeyHelper.Next() }; Send(msg); }
/// <summary> /// 发送请求 /// </summary> /// <param name="serviceName"></param> /// <param name="method"></param> /// <param name="args"></param> /// <returns></returns> public byte[] Request(string serviceName, string method, byte[] args) { byte[] result = null; try { var msg = new RSocketMsg(RSocketMsgType.Request, serviceName, method) { SequenceNumber = UniqueKeyHelper.Next() }; msg.Data = args; result = _disorderSyncHelper.Wait(msg.SequenceNumber, () => { SendBase(msg); }); } catch (Exception ex) { ExceptionCollector.Add("Consumer.Request.Error", new RPCSocketException($"serviceName:{serviceName}/method:{method} 调用超时!", ex)); } return(result); }