/// <summary> /// 尝试以远程调用的方式处理消息 /// </summary> /// <param name="msg"></param> /// <param name="data"></param> /// <returns></returns> private static bool TryProcessCall(Message msg, RtpData data) { var requestId = msg.Header.GetValue <Guid>(FieldRtpCallRequestId, Guid.Empty); if (requestId == Guid.Empty) { return(false); } var error = msg.Header.GetValue <string>(FieldRtpCallErrorMessage, string.Empty); var isCompleted = msg.Header.GetValue <bool>(FieldRtpCallIsCompleted, false); var identity = RtpCallManager.GetIdentity(requestId); if (identity != null) { if (!string.IsNullOrEmpty(error)) { //错误处理 identity.Future.SetError(new ApplicationException(error)); } else { identity.Process(data, isCompleted); } if (isCompleted) { identity.Future.SetResult(true); RtpCallManager.ReturnIdentity(identity); } } return(true); }
/// <summary> /// 创建一个rtp能力可以识别的消息 /// </summary> /// <param name="eventName"></param> /// <param name="header"></param> /// <param name="content"></param> /// <returns></returns> private Message CreateResponseMessage(string eventName, DTObject header, byte[] content, string error, bool isCompleted) { var rtpData = new RtpData(this.Data.Participant, header, content); var data = RtpDataAnalyzer.Serialize(rtpData); var msg = RtpCapability.CreateCallMessage(this.Name, eventName, data, this.RequestId, (mh) => { if (!string.IsNullOrEmpty(this.Destination)) { mh.SetValue(MessageField.Destination, this.Destination); } else if (this.Destinations != null) { mh.SetValue(MessageField.Destinations, this.Destinations); } if (!string.IsNullOrEmpty(error)) { mh.SetValue(RtpCapability.FieldRtpCallErrorMessage, error); } mh.SetValue(RtpCapability.FieldRtpCallIsCompleted, isCompleted); }); msg.Origin = this.Data.Participant.Orgin; return(msg); }
private Future <bool> RaiseEvent(string eventName, DTObject header, byte[] body, Action <DTObject> setMessageHeader) { var rtpData = new RtpData(this.Channel.Participant, header, body); var data = RtpDataAnalyzer.Serialize(rtpData); Message msg = CreateMessage(eventName, data, setMessageHeader); return(this.Channel.Context.Client.Send(msg)); }
/// <summary> /// 远程调用,该调用非常强大,不仅可以同步或异步执行并且获取结果,更可以获取服务器端多次推送的结果 /// </summary> /// <param name="header"></param> /// <param name="body"></param> /// <param name="future"></param> private Future <bool> Call(string eventName, DTObject header, byte[] body, Action <RtpData, bool> process, Action <DTObject> setMessageHeader) { var identity = RtpCallManager.BorrowIdentity(); identity.Process = process; var rtpData = new RtpData(this.Channel.Participant, header, body); var data = RtpDataAnalyzer.Serialize(rtpData); identity.Future.Start(); Message msg = CreateCallMessage(this.Name, eventName, data, identity.RequestId, setMessageHeader); this.Channel.Context.Client.Send(msg); return(identity.Future); }
public static byte[] Serialize(RtpData data) { using (var temp = ByteBuffer.Borrow(data.Body.Length)) { var array = temp.Item; var participantData = DataAnalyzer.SerializeParticipant(data.Participant); var extensionssData = GetExtensionsData(data.Header); array.Write(participantData.Length); array.Write(participantData); array.Write(extensionssData.Length); array.Write(extensionssData); array.Write(data.Body.Length); array.Write(data.Body); return(array.ToArray()); } }
private static void RaiseLocalEvent(AnycastClient client, string origin, string eventName, RtpData data) { if (ReceivedRaise != null) { var ea = new ReceivedRaiseEventArgs(origin, eventName, data); ReceivedRaise(client, ea); } }
public ReceivedRaiseEventArgs(string origin, string eventName, RtpData data) { this.Origin = origin; this.EventName = eventName; this.Data = data; }