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); }
public static ServerMsgSet FromStringAndFraming(string s, int?framing) { var msgSet = new ServerMsgSet(); if (framing == TcpState.NonTransparentHashCode) { return(FromStringNonTransparent(s, msgSet)); } if (framing == TcpState.OctetCountingHashCode) { return(FromStringOctetCounting(s, msgSet)); } return(null); }
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, TcpState.OctetCountingHashCode); return(serverMsgSet); }