예제 #1
0
        private static bool MergeMsg(Message msg, ref Message newmsg)
        {
            MsmqLable label = null;

            if (string.IsNullOrWhiteSpace(msg.Label) || (label = msg.Label.JsonToEntity <MsmqLable>()).Split <= 1)
            {
                newmsg = msg;
                return(true);
            }

            SortedDictionary <int, Message> dic = null;

            if (TempMergeMsgDic.TryGetValue(label.MergeId, out dic))
            {
                dic.Add(label.SplitNo, msg);
                if (dic.Count == label.Split)
                {
                    var msgbody = string.Join(string.Empty, dic.Select(p => p.Value.Body.ToString()));
                    //LogManager.LogHelper.Instance.Debug(msgbody);

                    newmsg = new Message(msgbody);

                    TempMergeMsgDic.TryRemove(label.MergeId, out dic);
                    return(true);
                }
            }
            else
            {
                dic = new SortedDictionary <int, Message>();
                dic.Add(label.SplitNo, msg);
                TempMergeMsgDic.TryAdd(label.MergeId, dic);
            }

            return(false);
        }
예제 #2
0
        public static IEnumerable <Message> SplitMessage(string content, string labletxt = "")
        {
            if (content == null || content.Length <= MaxMsgSize)
            {
                if (string.IsNullOrWhiteSpace(labletxt))
                {
                    yield return(new Message(content));
                }
                else
                {
                    var msg = new Message(content);
                    msg.Label = new MsmqLable {
                        Lable = labletxt
                    }.ToJson();
                    yield return(msg);
                }
                yield break;
            }

            MsmqLable lable = new MsmqLable();

            lable.MergeId = Guid.NewGuid().ToString();
            lable.MsgSize = content.Length;
            lable.Split   = (int)Math.Ceiling(content.Length * 1.0 / MaxMsgSize);
            int startindex = 0;

            while (lable.SplitNo <= lable.Split)
            {
                var len = Math.Min(content.Length - startindex, MaxMsgSize);
                if (len == 0)
                {
                    break;
                }

                var substring = content.Substring(startindex, len);
                lable.SplitNo++;

                yield return(new Message
                {
                    Body = substring,
                    Label = lable.ToJson()
                });

                if (len < MaxMsgSize)
                {
                    break;
                }

                startindex += len;
            }
        }