public void TestUInt64ToString() { var flag1NativeToString = Flag1.ToString(); var flag2NativeToString = Flag2.ToString(); Flag1.AsString().ShouldBeEquivalentTo(Flag1StringValue); Flag2.AsString().ShouldBeEquivalentTo(flag2NativeToString); AllFlags.AsString().ShouldBeEquivalentTo(AllFlags.ToString()); (Flag2 | Flag10).ShouldBeEquivalentTo(Flag2Flag10StringValue); AllFlags.AsString().ShouldBeEquivalentTo(FlagsAllStringValue); }
private void HandleResponse(string response) { void HandleCommand(int window, int currentWindow, Flag1 flag1, Flag2 flag2, int?windowConfig, string text) { if (flag1.HasFlag(Flag1.TxWindowDimensions)) { Debug.Assert(windowConfig.HasValue); SetWindowConfig(((WindowConfig[])Enum.GetValues(typeof(WindowConfig)))[windowConfig ?? 0]); } SetCurrentWindow(currentWindow - 1); Console.WriteLine($"Window Number: {window}, CurrentWindow: {currentWindow}, Flag1: {flag1}, Flag2: {flag2}"); if (flag1.HasFlag(Flag1.TxSpecialMessage) && text != null) { Console.WriteLine($"Special message: {text}"); ErrorBuffer.BufferText = text.Replace("\r", ""); } else if (!flag1.HasFlag(Flag1.TxBinaryData) && text != null) { if (window <= Buffers.Length) { Buffers[window - 1].BufferText = text.Replace("\r", ""); } else { Console.WriteLine($"Fress talking to nonexistent window {window}. Contents: {text}"); } } } bool ParseCommand() { if (_responseBuffer.Length == 0) { return(false); } Match residueMatch = _residueRegex.Match(_responseBuffer); if (residueMatch.Success) { Console.WriteLine("Non-FRESS output: "); Console.WriteLine(residueMatch.Groups["junk"].Captures[0].Value); if (residueMatch.Groups["data"].Captures.Count > 0) { _responseBuffer = residueMatch.Groups["data"].Captures[0].Value; } else { _responseBuffer = ""; } } Match commandMatch = _windowCommand.Match(_responseBuffer); if (commandMatch.Success) { int window = commandMatch.Groups["winNum"].Captures[0].Value[0] - '0'; int currentWindow = commandMatch.Groups["curWinNum"].Captures[0].Value[0] - '0'; Flag1 flag1 = (Flag1)commandMatch.Groups["flag1"].Captures[0].Value[0] - '0'; Flag2 flag2 = (Flag2)commandMatch.Groups["flag2"].Captures[0].Value[0] - '0'; int? windowConfig = null; Group windowConfigMatch = commandMatch.Groups["op1"]; if (windowConfigMatch.Success) { windowConfig = windowConfigMatch.Captures[0].Value[0] - '0'; } HandleCommand(window, currentWindow, flag1, flag2, windowConfig, null); _responseBuffer = _responseBuffer.Substring(commandMatch.Index + commandMatch.Length - 1); //Exclude extra matched \ return(true); } Match commandTextMatch = _commandWithTextRegex.Match(_responseBuffer); if (commandTextMatch.Success) { int window = commandTextMatch.Groups["winNum"].Captures[0].Value[0] - '0'; int currentWindow = commandTextMatch.Groups["curWinNum"].Captures[0].Value[0] - '0'; Flag1 flag1 = (Flag1)commandTextMatch.Groups["flag1"].Captures[0].Value[0] - '0'; Flag2 flag2 = (Flag2)commandTextMatch.Groups["flag2"].Captures[0].Value[0] - '0'; string text = ""; if (commandTextMatch.Groups["line"].Success) { foreach (Capture cap in commandTextMatch.Groups["line"].Captures) { string line = cap.Value; //trim delimiters from matched string text = text + line.Substring(0, line.Length); } } HandleCommand(window, currentWindow, flag1, flag2, null, text); _responseBuffer = _responseBuffer.Substring(commandTextMatch.Index + commandTextMatch.Length); return(true); } Match specialCommandMatch = _specialCommandRegex.Match(_responseBuffer); if (specialCommandMatch.Success) { int window = specialCommandMatch.Groups["winNum"].Captures[0].Value[0] - '0'; int currentWindow = specialCommandMatch.Groups["curWinNum"].Captures[0].Value[0] - '0'; Flag1 flag1 = (Flag1)specialCommandMatch.Groups["flag1"].Captures[0].Value[0] - '0'; Flag2 flag2 = (Flag2)specialCommandMatch.Groups["flag2"].Captures[0].Value[0] - '0'; if (flag1.HasFlag(Flag1.TxSpecialMessage)) { string text = specialCommandMatch.Groups["data"].Captures[0].Value; HandleCommand(window, currentWindow, flag1, flag2, null, text); _responseBuffer = _responseBuffer.Substring(specialCommandMatch.Index + specialCommandMatch.Length); return(true); } } Match bad = _badcommand.Match(_responseBuffer); if (bad.Success && bad.Length != 0) { if (bad.Groups["junk"].Success) { Console.WriteLine("Bad FRESS Protocol or Non-FRESS output: "); Console.WriteLine(bad.Groups["junk"].Captures[0].Value); } if (bad.Groups["data"].Success) { _responseBuffer = bad.Groups["data"].Captures[0].Value; } Console.WriteLine("Remaining to process: "); Console.WriteLine(_responseBuffer); } return(false); } _responseBuffer += response; while (ParseCommand()) { ; } }