Exemple #1
0
        /// <summary>组合组消息</summary>
        /// <param name="message">消息</param>
        /// <param name="remoteIdentity">远程标识</param>
        /// <returns></returns>
        protected virtual Message JoinGroup(GroupMessage message, Object remoteIdentity = null)
        {
            var key = String.Format("{0}#{1}", remoteIdentity, message.Identity);

            MessageGroup mg = null;

            if (!groups.TryGetValue(key, out mg))
            {
                mg          = new MessageGroup();
                mg.Identity = message.Identity;
                groups.Add(key, mg);
            }

            // 加入到组,如果返回false,表示未收到所有消息
            if (!mg.Add(message))
            {
                WriteLog("接收分组 Identity={0} {1}/{2} [{3}] 已完成:{4}/{5}", message.Identity, message.Index, message.Count, message.Data == null ? 0 : message.Data.Length, mg.Count, mg.Total);

                return(null);
            }

            WriteLog("接收分组 Identity={0} {1}/{2} [{3}] 已完成:{4}/{5}", message.Identity, message.Index, message.Count, message.Data == null ? 0 : message.Data.Length, mg.Count, mg.Total);

            // 否则,表示收到所有消息
            groups.Remove(key);

            // 读取真正的消息
            return(mg.GetMessage());
        }
Exemple #2
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);
                }
            }
        }