示例#1
0
        protected override void HandleChunks(string receivedString, Action <string> receivedStringAction)
        {
            var msgSet = ServerMsgSet.FromStringAndFraming(receivedString, framing);

            if (msgSet == null)
            {
                receivedStringAction($"Error handling string {receivedString}");
                return;
            }

            for (var i = 0; i < msgSet.FullMessages; i++)
            {
                var msg = msgSet[i];
                receivedStringAction(msgSet.IsValid(msg, framing) ? msg : $"Error handling string {receivedString}");
            }

            Buffer.SetLength(0);

            if (!msgSet.LastIsPartial)
            {
                return;
            }

            var lastMessage = msgSet.LastMessageBytes;

            Buffer.Write(lastMessage, 0, lastMessage.Length);
        }
        private static ServerMsgSet FromStringNonTransparent(string s, ServerMsgSet serverMsgSet)
        {
            const char lineFeed = '\n';
            serverMsgSet.messages = s.Split(lineFeed);

            var lastChar = s[s.Length - 1];
            serverMsgSet.LastIsPartial = lastChar == lineFeed;

            return serverMsgSet;
        }
示例#3
0
        private static ServerMsgSet FromStringNonTransparent(string s, ServerMsgSet serverMsgSet)
        {
            const char lineFeed = '\n';

            serverMsgSet.messages = s.Split(lineFeed);

            var lastChar = s[s.Length - 1];

            serverMsgSet.LastIsPartial = lastChar == lineFeed;

            return(serverMsgSet);
        }
        private static ServerMsgSet FromStringOctetCounting(string s, ServerMsgSet serverMsgSet)
        {
            var matches = Regex
                .Split(s, "(\\d{1,11} <\\d{1,3}>)")
                .Where(x => !string.IsNullOrEmpty(x))
                .Select((value, index) => new { value, index })
                .ToArray();
            serverMsgSet.messages = matches
                .Where(x => x.index % 2 == 0)
                .Select(x => x.value + (matches.Length <= x.index + 1 ? string.Empty : matches[x.index + 1].value))
                .ToArray();

            serverMsgSet.LastIsPartial = !serverMsgSet.IsValid(serverMsgSet.LastMessage, FramingMethod.OctetCounting);

            return serverMsgSet;
        }
示例#5
0
        private static ServerMsgSet FromStringOctetCounting(string s, ServerMsgSet serverMsgSet)
        {
            var matches = Regex
                          .Split(s, "(\\d{1,11} <\\d{1,3}>)")
                          .Where(x => !string.IsNullOrEmpty(x))
                          .Select((value, index) => new { value, index })
                          .ToArray();

            serverMsgSet.messages = matches
                                    .Where(x => x.index % 2 == 0)
                                    .Select(x => x.value + (matches.Length <= x.index + 1 ? string.Empty : matches[x.index + 1].value))
                                    .ToArray();

            serverMsgSet.LastIsPartial = !serverMsgSet.IsValid(serverMsgSet.LastMessage, FramingMethod.OctetCounting);

            return(serverMsgSet);
        }
 public static ServerMsgSet FromStringAndFraming(string s, FramingMethod? framing)
 {
     var msgSet = new ServerMsgSet();
     switch (framing)
     {
         case FramingMethod.NonTransparent:
         {
             return FromStringNonTransparent(s, msgSet);
         }
         case FramingMethod.OctetCounting:
         {
             return FromStringOctetCounting(s, msgSet);
         }
         default:
         {
             return null;
         }
     }
 }
示例#7
0
        public static ServerMsgSet FromStringAndFraming(string s, FramingMethod?framing)
        {
            var msgSet = new ServerMsgSet();

            switch (framing)
            {
            case FramingMethod.NonTransparent:
            {
                return(FromStringNonTransparent(s, msgSet));
            }

            case FramingMethod.OctetCounting:
            {
                return(FromStringOctetCounting(s, msgSet));
            }

            default:
            {
                return(null);
            }
            }
        }