示例#1
0
        /// <summary>
        /// 发送前的准备
        /// </summary>
        /// <param name="msg"></param>
        public void PrepareMsg(T msg)
        {
            MsgSync <T> sync = new MsgSync <T>
            {
                SendMsg = msg
            };

            lock (m_locker)
            {
                m_ackSyncs.Add(msg, sync);
            }
        }
示例#2
0
        /// <summary>
        /// 接收到消息
        /// </summary>
        /// <param name="msg"></param>
        public void ReceiveMsg(T msg)
        {
            MsgSync <T> sync = null;

            lock (m_locker)
            {
                sync = m_ackSyncs.Values.FirstOrDefault(o => m_msgComparer(o.SendMsg, msg));
            }

            if (sync != null)
            {
                sync.ReceiveMsg = msg;
                sync.SyncLocker.Set();
            }
        }
示例#3
0
        /// <summary>
        /// 等待消息回复
        /// </summary>
        /// <typeparam name="TResponse">回复的消息类型</typeparam>
        /// <param name="msg">发送的消息</param>
        /// <param name="timeout">超时时间</param>
        /// <returns></returns>
        public TResponse WaitMsg <TResponse>(T msg, int timeout = 5000) where TResponse : class, T
        {
            MsgSync <T> sync = null;

            lock (m_locker)
            {
                if (m_ackSyncs.ContainsKey(msg))
                {
                    sync = m_ackSyncs[msg];
                }
            }

            if (sync == null)
            {
                return(null);
            }

            sync.SyncLocker.WaitOne(timeout);
            return(sync.ReceiveMsg as TResponse);
        }