protected void HandleResponse <R>(FramedClient client, long reqId, Task <R> actorResponse, IReplyMessage <R> msgToSend) { if (actorResponse == null) { Send(client, reqId, msgToSend); } else { actorResponse.ContinueWith(t => { try { msgToSend.SetResult(t.Result); } catch (AggregateException exc) { msgToSend.SetError(exc.InnerException.Message); } catch (Exception exc) { msgToSend.SetError(exc.Message); } Send(client, reqId, msgToSend); }); } }
private unsafe void Send <R>(FramedClient client, long requestId, IReplyMessage <R> packet) { using (var ms = new MemoryStream()) { ms.SetLength(12); ms.Position = 12; serializer.Serialize(packet, ms); ms.Position = 0; var buffer = ms.GetBuffer(); fixed(byte *buf = buffer) { *(ActorProtocolFlags *)buf = ActorProtocolFlags.RequestReponse; *(long *)(buf + 4) = requestId; } client.SendPacket(new ArraySegment <byte>(buffer, 0, (int)ms.Length)); } }
public IReplyMessage Reply(IIncomeMessage msg) { if (index >= handlerList.Count) { index = 0; return(null); } var current = handlerList[index]; index++; IReplyMessage reply = null; if (current.Predict(msg)) { reply = current.Reply(msg); } if (reply == null) { return(Reply(msg)); } index = 0; return(reply); }
/// <summary> /// 消息处理流程 /// </summary> /// <param name="msgBag"></param> /// <returns></returns> protected virtual string ProcessMessageReceive(Dictionary <string, string> msgBag) { IReplyMessage reply = null; IIncomeMessage msg = null; if (!msgBag.ContainsKey("MsgType")) { throw new WechatApiException(-3, "received unexpected message type."); } try { switch (msgBag["MsgType"]) { case "text": { msg = MessageHelper.Create <TextMessage>(msgBag); reply = OnReceiveTextMessage(msg as TextMessage); break; } case "image": { msg = MessageHelper.Create <ImageMessage>(msgBag); reply = OnReceiveImageMessage(msg as ImageMessage); break; } case "voice": { msg = MessageHelper.Create <VoiceMessage>(msgBag); reply = OnReceiveVoiceMessage(msg as VoiceMessage); break; } case "video": { msg = MessageHelper.Create <VideoMessage>(msgBag); reply = OnReceiveVideoMessage(msg as VideoMessage); break; } //case "location": // { // msg = MessageHelper.Create<LocationMessage>(msgBag); // reply = OnReceiveLocationMessage(msg as LocationMessage); // break; // } case "link": { msg = MessageHelper.Create <LinkMessage>(msgBag); reply = OnReceiveLinkMessage(msg as LinkMessage); break; } case "event": { var eventType = msgBag["Event"].ToLower(); switch (eventType) { case "click": case "view": msg = MessageHelper.Create <MenuClickEventMessage>(msgBag); reply = OnReceiveMenuClickMessage(msg as MenuClickEventMessage); break; case "subscribe": msg = MessageHelper.Create <SubscribeEventMessage>(msgBag); reply = OnReceiveSubscribeMessage(msg as SubscribeEventMessage); break; case "unsubscribe": msg = MessageHelper.Create <UnSubscribeEventMessage>(msgBag); reply = OnReceiveUnSubscribeMessage(msg as UnSubscribeEventMessage); break; case "scan": msg = MessageHelper.Create <QrScanEventMessage>(msgBag); reply = OnReceiveQrScanMessage(msg as QrScanEventMessage); break; case "location": msg = MessageHelper.Create <LocationEventMessage>(msgBag); reply = OnReceiveLocationMessage(msg as LocationEventMessage); break; default: msg = MessageHelper.Create <EventMessage>(msgBag); reply = OnReceiveEventMessage(msg as EventMessage); break; } break; } } if (reply == null && msg != null && OnReceiveMessage != null) { reply = OnReceiveMessage(msg); } if (reply != null) { switch (reply.MsgType) { case "text": { return(MessageHelper.TransReply(reply as ReplyTextMessage)); } case "music": { return(MessageHelper.TransReply(reply as ReplyMusicMessage)); } case "news": { return(MessageHelper.TransReply(reply as ReplyNewsMessage)); } } } return(string.Empty); } catch (Exception ex) { if (OnProcessMessageError != null) { OnProcessMessageError(msg, ex); } return(string.Empty); } }