/// 需要实现DoMessage /// </summary> /// <typeparam name="T"></typeparam> /// <param name="message"></param> /// <param name="timeOut"></param> /// <returns></returns> public T SendMessageAnsy <T>(Message message, int timeOut = 30000) { if (string.IsNullOrEmpty(message.MessageHeader.TransactionID)) { throw new Exception("消息没有设置唯一的序号。无法进行同步。"); } string reqID = message.MessageHeader.TransactionID; using (AutoReSetEventResult autoResetEvent = new AutoReSetEventResult(reqID)) { watingEvents.Add(reqID, autoResetEvent); BuzException = null; SendMessage(message, null); //ThreadPool.QueueUserWorkItem(new WaitCallback(o => { SendMessage((Message)o); }), message); //new Func<Message, bool>(SendMessage).BeginInvoke(message, null, null); autoResetEvent.WaitOne(timeOut); //WaitHandle.WaitAny(new WaitHandle[] { autoResetEvent }, timeOut); watingEvents.Remove(reqID); if (BuzException != null) { BuzException.Data.Add("MessageType", message.MessageHeader.MessageType); BuzException.Data.Add("TransactionID", message.MessageHeader.TransactionID); throw BuzException; } if (autoResetEvent.IsTimeOut) { var ex = new TimeoutException(); ex.Data.Add("errorsender", "LJC.FrameWork.SocketApplication.SocketEasyUDP.Client.SessionClient"); ex.Data.Add("MessageType", message.MessageHeader.MessageType); ex.Data.Add("TransactionID", message.MessageHeader.TransactionID); if (SessionContext != null) { ex.Data.Add("SessionContext.IPAddress", this.SessionContext.IPAddress); ex.Data.Add("SessionContext.Port", this.SessionContext.Port); } if (message.MessageBuffer != null) { ex.Data.Add("MessageBuffer", Convert.ToBase64String(message.MessageBuffer)); } ex.Data.Add("resulttype", typeof(T).FullName); LogManager.LogHelper.Instance.Error("SendMessageAnsy", ex); throw ex; } else { T result = EntityBufCore.DeSerialize <T>((byte[])autoResetEvent.WaitResult); return(result); } } }
private UDPRevResultMessage QuestionBag(long bagid, IPEndPoint endpoint) { int trytimes = 0; Message question = new Message(MessageType.UDPQUERYBAG); question.SetMessageBody(new UDPRevResultMessage { BagId = bagid }); string key = string.Format("{0}:{1}:{2}", endpoint.Address.ToString(), endpoint.Port, bagid); var wait = new AutoReSetEventResult(string.Empty); _resetevent.Add(key, wait); try { while (true) { wait.IsTimeOut = true; wait.Reset(); try { SendMessageNoSure(question, endpoint); wait.WaitOne(100); if (!wait.IsTimeOut) { return((UDPRevResultMessage)wait.WaitResult); } else { throw new TimeoutException(); } } catch (TimeoutException ex) { trytimes++; if (trytimes >= 10) { throw ex; } } } } finally { _resetevent.Remove(key); } }