/// <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) { byte[] result = null; var msg = new RSocketMsg(RSocketMsgType.Request, serviceName, method) { SequenceNumber = UniqueKeyHelper.Next() }; msg.Data = args; if (_syncHelper.Wait(msg.SequenceNumber, () => { this.Send(msg); }, (r) => { result = r; }, timeOut)) { return(result); } else { ExceptionCollector.Add("Consumer.Request.Error", new RPCSocketException($"serviceName:{serviceName}/method:{method} 调用超时!")); } return(result); }
/// <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); }
/// <summary> /// socket 传输字节解码 /// 格式为:1+4+8+4+x+4+x+x /// </summary> /// <param name="data"></param> /// <param name="onDecode"></param> bool Decode(byte[] data, Action <RSocketMsg[], uint> onDecode) { uint offset = 0; try { if (data != null && data.Length >= offset + MIN) { var list = new List <RSocketMsg>(); while (data.Length >= offset + MIN) { var total = BitConverter.ToUInt32(data, (int)offset + 1); if (data.Length >= offset + total + 1) { var qm = new RSocketMsg((RSocketMsgType)data[offset]); offset += 5; qm.Total = total; qm.SequenceNumber = BitConverter.ToInt64(data, (int)offset); offset += 8; qm.SLen = BitConverter.ToUInt32(data, (int)offset); offset += 4; if (qm.SLen > 0) { var narr = new byte[qm.SLen]; Buffer.BlockCopy(data, (int)offset, narr, 0, narr.Length); qm.ServiceName = Encoding.UTF8.GetString(narr); } offset += qm.SLen; qm.MLen = BitConverter.ToUInt32(data, (int)offset); offset += 4; if (qm.MLen > 0) { var tarr = new byte[qm.MLen]; Buffer.BlockCopy(data, (int)offset, tarr, 0, tarr.Length); qm.MethodName = Encoding.UTF8.GetString(tarr); } offset += qm.MLen; var dlen = qm.Total - 8 - 4 - 4 - qm.SLen - 4 - qm.MLen; if (dlen > 0) { var darr = new byte[dlen]; Buffer.BlockCopy(data, (int)offset, darr, 0, (int)dlen); qm.Data = darr; offset += dlen; } list.Add(qm); } else { break; } } if (list.Count > 0) { onDecode?.Invoke(list.ToArray(), offset); return(true); } } } catch (Exception ex) { ConsoleHelper.WriteLine($"RCoder.Decode error:{ex.Message} stack:{ex.StackTrace} data:{data.Length} offset:{offset}"); } onDecode?.Invoke(null, 0); return(false); }
/// <summary> /// 发送数据 /// </summary> /// <param name="msg"></param> internal void BeginSend(RSocketMsg msg) { BeginSend(((RCoder)UserToken.Unpacker).Encode(msg)); }
/// <summary> /// 回复 /// </summary> /// <param name="userToken"></param> /// <param name="msg"></param> internal void Reply(IUserToken userToken, RSocketMsg msg) { var data = ((RUnpacker)userToken.Unpacker).Encode(msg); _server.SendAsync(userToken.ID, data); }
/// <summary> /// 发送数据 /// </summary> /// <param name="msg"></param> internal void Send(RSocketMsg msg) { var data = ((RCoder)_RContext.Unpacker).Encode(msg); _client.SendAsync(data); }
/// <summary> /// 发送数据 /// </summary> /// <param name="msg"></param> internal void SendBase(RSocketMsg msg) { var data = _rUnpacker.Encode(msg); _client.SendAsync(data); }
/// <summary> /// 回复 /// </summary> /// <param name="userToken"></param> /// <param name="msg"></param> internal void Reply(IUserToken userToken, RSocketMsg msg) { SendAsync(userToken, ((RCoder)userToken.Unpacker).Encode(msg)); }
internal void Send(RSocketMsg msg) { var data = ((RCoder)UserToken.Coder).Encode(msg); SendAsync(data); }