Beispiel #1
0
        /// <summary>发送消息。如果有响应,可在消息到达事件中获得。这里会实现大消息分包。</summary>
        /// <param name="message"></param>
        public override void Send(Message message)
        {
            // MaxMessageSize未设置时,先取一次Session,触发MaxMessageSize的设置
            if (MaxMessageSize <= 0 && GetSession() == null) throw new ArgumentNullException("会话为空!可能没有设置OnUpdate事件!");

            base.Send(message);
        }
        Boolean IAccessor.ReadComplete(IReader reader, Boolean success)
        {
            // 读取消息。对剩下的数据流,进行解压缩后,读取成为另一个消息
            using (var stream = new DeflateStream(reader.Stream, CompressionMode.Decompress, true))
            {
                Message = Read(stream);
            }

            return success;
        }
 /// <summary>发送消息。如果有响应,可在消息到达事件中获得。</summary>
 /// <param name="message"></param>
 public override void Send(Message message)
 {
     var client = Client;
     if (!_hasSetAsync)
     {
         client.UploadDataCompleted += new UploadDataCompletedEventHandler(client_UploadDataCompleted);
         client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(client_DownloadDataCompleted);
     }
     var data = message.GetStream().ReadBytes();
     if (data.Length < 128)
         client.DownloadDataAsync(new Uri(Uri.ToString() + "?" + DataHelper.ToHex(data)));
     else
         client.UploadDataAsync(Uri, data);
 }
Beispiel #4
0
        Boolean IAccessor.ReadComplete(IReader reader, Boolean success)
        {
            var ms = new MemoryStream();
            // 读取消息。对剩下的数据流,进行解压缩后,读取成为另一个消息
            using (var stream = new DeflateStream(reader.Stream, CompressionMode.Decompress, true))
            {
                //Message = Read(stream);
                // 必须全部复制到内存流,然后再读取,否则可能因为加密流不能读取位置和长度而导致消息读取失败
                stream.CopyTo(ms);
            }
            Message = Read(ms);

            return success;
        }
        /// <summary>发送并接收消息。主要用于应答式的请求和响应。</summary>
        /// <param name="message"></param>
        /// <param name="millisecondsTimeout">等待的毫秒数,或为 <see cref="F:System.Threading.Timeout.Infinite" /> (-1),表示无限期等待。默认0表示不等待</param>
        /// <returns></returns>
        public override Message SendAndReceive(Message message, int millisecondsTimeout = 0)
        {
            lock (this)
            {
                Byte[] rs = null;
                var data = message.GetStream().ReadBytes();
                if (data.Length < 128)
                    rs = Client.DownloadData(new Uri(Uri.ToString() + "?" + DataHelper.ToHex(data)));
                else
                    rs = Client.UploadData(Uri, data);
                if (rs == null || rs.Length < 1) return null;

                return Message.Read(new MemoryStream(rs));
            }
        }
Beispiel #6
0
 /// <summary>发送并接收消息。主要用于应答式的请求和响应。</summary>
 /// <remarks>如果内部实现是异步模型,则等待指定时间获取异步返回的第一条消息,该消息不再触发消息到达事件<see cref="OnReceived"/>。</remarks>
 /// <param name="message"></param>
 /// <param name="millisecondsTimeout">等待的毫秒数,或为 <see cref="F:System.Threading.Timeout.Infinite" /> (-1),表示无限期等待。默认0表示不等待</param>
 /// <returns></returns>
 public virtual Message SendAndReceive(Message message, Int32 millisecondsTimeout = 0)
 {
     message.Header.Channel = Channel;
     return Provider.SendAndReceive(message);
 }
Beispiel #7
0
 /// <summary>发送消息</summary>
 /// <param name="message"></param>
 public void Send(Message message)
 {
     message.Header.Channel = Channel;
     Provider.Send(message);
 }
Beispiel #8
0
        /// <summary>发送并接收消息。主要用于应答式的请求和响应。</summary>
        /// <remarks>如果内部实现是异步模型,则等待指定时间获取异步返回的第一条消息,该消息不再触发消息到达事件<see cref="OnReceived"/>。</remarks>
        /// <param name="message"></param>
        /// <param name="millisecondsTimeout">等待的毫秒数,或为 <see cref="F:System.Threading.Timeout.Infinite" /> (-1),表示无限期等待。默认0表示不等待</param>
        /// <returns></returns>
        public virtual Message SendAndReceive(Message message, Int32 millisecondsTimeout = 0)
        {
            Send(message);

            var _wait = new AutoResetEvent(true);
            _wait.Reset();

            Message msg = null;
            innerOnReceived += (s, e) => { msg = e.Message; _wait.Set(); };

            //if (!_wait.WaitOne(millisecondsTimeout, true)) return null;

            _wait.WaitOne(millisecondsTimeout, false);
            _wait.Close();

            return msg;
        }
Beispiel #9
0
        /// <summary>收到消息时调用该方法</summary>
        /// <param name="message">消息</param>
        /// <param name="remoteIdentity">远程标识</param>
        public virtual void Process(Message message, Object remoteIdentity = null)
        {
            if (message == null) return;

            // 检查消息范围
            if (Kinds != null && Array.IndexOf<MessageKind>(Kinds, message.Kind) < 0) return;

            if (message.Kind == MessageKind.Group && AutoJoinGroup)
            {
                message = JoinGroup(message as GroupMessage);
                // 如果为空,表明还没完成组合,直接返回
                if (message == null) return;
            }

            WriteLog("接收消息 {0}", message);

            // 为Receive准备的事件,只用一次
            EventHandler<MessageEventArgs> handler;
            do
            {
                handler = innerOnReceived;
            }
            while (handler != null && Interlocked.CompareExchange<EventHandler<MessageEventArgs>>(ref innerOnReceived, null, handler) != handler);

            if (handler != null) handler(this, new MessageEventArgs(message));

            if (OnReceived != null) OnReceived(this, new MessageEventArgs(message));

            // 记录已过期的,要删除
            var list = new List<WeakReference<IMessageProvider2>>();
            var cs = Consumers;
            foreach (var item in cs)
            {
                IMessageProvider2 mp;
                if (item.TryGetTarget(out mp) && mp != null)
                    mp.Process(message);
                else
                    list.Add(item);
            }

            if (list.Count > 0)
            {
                lock (cs)
                {
                    foreach (var item in list)
                    {
                        if (cs.Contains(item)) cs.Remove(item);
                    }
                }
            }

            // 记录已过期的,要删除
            var list2 = new List<WeakReference<MessageConsumer2>>();
            var cs2 = Consumers2;
            foreach (var item in cs2)
            {
                MessageConsumer2 mp;
                if (item.TryGetTarget(out mp) && mp != null)
                    mp.Process(message);
                else
                    list2.Add(item);
            }

            if (list2.Count > 0)
            {
                lock (cs2)
                {
                    foreach (var item in list2)
                    {
                        if (cs2.Contains(item)) cs2.Remove(item);
                    }
                }
            }
        }
Beispiel #10
0
 /// <summary>发送消息。如果有响应,可在消息到达事件<see cref="OnReceived"/>中获得。这里会实现大消息分包。</summary>
 /// <param name="message"></param>
 public virtual void Send(Message message)
 {
     var ms = message.GetStream();
     WriteLog("发送消息 [{0}] {1}", ms.Length, message);
     if (MaxMessageSize <= 0 || ms.Length < MaxMessageSize)
         OnSend(ms);
     else
     {
         var mg = new MessageGroup();
         mg.Split(ms, MaxMessageSize, message.Header);
         var count = 0;
         foreach (var item in mg)
         {
             if (item.Index == 1) count = item.Count;
             ms = item.GetStream();
             WriteLog("发送分组 Identity={0} {1}/{2} [{3}] [{4}]", item.Identity, item.Index, count, item.Data == null ? 0 : item.Data.Length, ms.Length);
             Debug.Assert(item.Index == count || ms.Length == MaxMessageSize, "分拆的组消息大小不合适!");
             OnSend(ms);
         }
     }
 }
Beispiel #11
0
        /// <summary>发送并接收</summary>
        /// <param name="message"></param>
        /// <param name="millisecondsTimeout"></param>
        /// <returns></returns>
        public override Message SendAndReceive(Message message, int millisecondsTimeout = 0)
        {
            var session = GetSession();
            if (session.UseReceiveAsync) return base.SendAndReceive(message, millisecondsTimeout);

            Send(message);
            var data = session.Receive();
            if (data == null || data.Length < 1) return null;

            var ms = new MemoryStream(data);
            return Message.Read(ms);
        }
 /// <summary>
 /// 处理消息
 /// </summary>
 /// <param name="message"></param>
 /// <param name="stream"></param>
 /// <returns></returns>
 public override Stream Process(Message message, Stream stream)
 {
     RemotingMessage rm = message as RemotingMessage;
     if (rm != null) ProcessRemoting(rm, stream);
     return stream;
 }
 /// <summary>已重载。不支持。</summary>
 /// <param name="message"></param>
 /// <param name="millisecondsTimeout"></param>
 /// <returns></returns>
 public override Message SendAndReceive(Message message, int millisecondsTimeout = 0)
 {
     throw new NotSupportedException();
 }
Beispiel #14
0
 /// <summary>
 /// 处理消息
 /// </summary>
 /// <param name="message">消息</param>
 /// <param name="stream">数据流,已经从里面读取消息实体</param>
 /// <returns></returns>
 public abstract Stream Process(Message message, Stream stream);
Beispiel #15
0
            public override Stream Process(Message message, Stream stream)
            {
                if (message is ExceptionMessage && Error != null) Error(this, new EventArgs<Message, Stream>(message, stream));
                if (message is NullMessage && Null != null) Null(this, new EventArgs<Message, Stream>(message, stream));

                return stream;
            }
Beispiel #16
0
            /// <summary>收到消息时调用该方法</summary>
            /// <param name="message"></param>
            public void Process(Message message)
            {
                if (message == null) return;

                // 检查消息范围
                if (!message.Header.UseHeader || message.Header.Channel != Channel) return;

                // 为Receive准备的事件,只用一次
                EventHandler<MessageEventArgs> handler;
                do
                {
                    handler = innerOnReceived;
                }
                while (handler != null && Interlocked.CompareExchange<EventHandler<MessageEventArgs>>(ref innerOnReceived, null, handler) != handler);

                if (handler != null) handler(this, new MessageEventArgs(message));

                if (OnReceived != null) OnReceived(this, new MessageEventArgs(message));
            }
Beispiel #17
0
 /// <summary>发送消息</summary>
 /// <param name="message"></param>
 public override void Send(Message message) { Parent.Send(message); }
Beispiel #18
0
 /// <summary>发送消息。如果有响应,可在消息到达事件<see cref="OnReceived"/>中获得。</summary>
 /// <param name="message"></param>
 public abstract void Send(Message message);
Beispiel #19
0
 /// <summary>实例化</summary>
 /// <param name="message"></param>
 public MessageEventArgs(Message message) { Message = message; }
 /// <summary>发送消息。如果有响应,可在消息到达事件中获得。</summary>
 /// <param name="message"></param>
 public override void Send(Message message)
 {
     var rs = Context.Response;
     rs.BinaryWrite(message.GetStream().ReadBytes());
 }