首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 網路程式設計:非同步傳輸字串 (3) — 協議解析器 RequestHandler 完整實作與測試

     編輯:關於C語言

協議解析核心邏輯

GetActualString 接收符合協議格式的字串,回傳字串陣列。若發生多個請求合併,會全部拆解回傳。類別內建靜態 Test() 與輔助 PrintOutput() 方便驗證(length=13 為預先計算)。

public class RequestHandler {
    private string temp = string.Empty;

    public string[] GetActualString(string input) {
        return GetActualString(input, null);
    }

    private string[] GetActualString(string input, List<string> outputList) {
        if (outputList == null) outputList = new List<string>();
        if (!String.IsNullOrEmpty(temp)) input = temp + input;

        string output = "";
        string pattern = @"(?<=^\[length=)(\d+)(?=\])";
        int length;

        if (Regex.IsMatch(input, pattern)) {
            Match m = Regex.Match(input, pattern);
            length = Convert.ToInt32(m.Groups[0].Value);
            int startIndex = input.IndexOf(']') + 1;
            output = input.Substring(startIndex);

            if (output.Length == length) {
                outputList.Add(output);
                temp = "";
            } else if (output.Length < length) {
                temp = input; // 不完整,緩存等待下次
            } else { // output.Length > length
                output = output.Substring(0, length);
                outputList.Add(output);
                temp = "";
                input = input.Substring(startIndex + length);
                GetActualString(input, outputList); // 遞迴處理剩餘
            }
        } else { // 中括號不完整
            temp = input;
        }
        return outputList.ToArray();
    }

    // 測試方法
    public static void Test() {
        RequestHandler handler = new RequestHandler();
        string input;

        // 情況 1:單一完整訊息
        input = "[length=13]明天中秋,祝大家節日快樂!";
        handler.PrintOutput(input);

        // 情況 2:兩條完整訊息一次發送
        input = "明天中秋,祝大家節日快樂!";
        input = String.Format("[length=13]{0}[length=13]{0}", input);
        handler.PrintOutput(input);

        // 情況 3A:分兩次不完整發送
        input = "[length=13]明天中秋,祝大家節日快樂![length=13]明天中秋";
        handler.PrintOutput(input);
        input = ",祝大家節日快樂!";
        handler.PrintOutput(input);

        // 情況 3B:另一種分割方式
        input = "[length=13]明天中秋,祝大家";
        handler.PrintOutput(input);
        input = "節日快樂![length=13]明天中秋,祝大家節日快樂!";
        handler.PrintOutput(input);

        // 情況 4:中繼資料不完整
        input = "[leng";
        handler.PrintOutput(input);
        input = "th=13]明天中秋,祝大家節日快樂!";
        handler.PrintOutput(input);
    }

    private void PrintOutput(string input) {
        Console.WriteLine(input);
        string[] outputArray = GetActualString(input);
        foreach (string output in outputArray) Console.WriteLine(output);
        Console.WriteLine();
    }
}

測試輸出驗證

輸出證實解析器能正確處理:完整訊息、合併訊息、分割訊息、中繼資料斷裂等四大情境。此機制可輕鬆整合進先前程式解決「訊息邊界」問題(原始碼附修正版)。接著回到主線,看如何用非同步方式完成上一篇程式。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved