123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Text.RegularExpressions;
- namespace LYFZ.Network
- {
- /// <summary>
- /// 处理协议的ProtocalHandler处理类
- /// </summary>
- public class TCP_ProtocolHandler
- {
- private string partialProtocol; // 保存不完整的协议
- public TCP_ProtocolHandler()
- {
- partialProtocol = "";
- }
- public string[] GetProtocol(string input)
- {
- return GetProtocol(input, null);
- }
-
- /// <summary>
- /// 获得协议
- /// </summary>
- /// <param name="input"></param>
- /// <param name="outputList"></param>
- /// <returns></returns>
- private string[] GetProtocol(string input, List<string> outputList)
- {
- if (outputList == null)
- outputList = new List<string>();
- if (String.IsNullOrEmpty(input))
- return outputList.ToArray();
- if (!String.IsNullOrEmpty(partialProtocol))
- input = partialProtocol + input;
- string pattern = "(^<lyfz_protocol>.*?</lyfz_protocol>)";
- HPSocketCS.Extended.SystemFileLogs.WriteLogs("匹配协议:" + input);
- // 如果有匹配,说明已经找到了,是完整的协议
- if (Regex.IsMatch(input, pattern))
- {
- // 获取匹配的值
- string match = Regex.Match(input, pattern).Groups[0].Value;
- outputList.Add(match);
- partialProtocol = "";
- // 缩短input的长度
- input = input.Substring(match.Length);
- // 递归调用
- GetProtocol(input, outputList);
- }
- else
- {
- // 如果不匹配,说明协议的长度不够,
- // 那么先缓存,然后等待下一次请求
- partialProtocol = input;
- }
- return outputList.ToArray();
- }
- /// <summary>
- /// 获得协议
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- public string GetProtocolInfo(string input)
- {
- string pattern = "(^<lyfz_protocol>.*?</lyfz_protocol>)";
-
- // 如果有匹配,说明已经找到了,是完整的协议
- if (Regex.IsMatch(input, pattern))
- {
- // LYFZ.BLL.BLL_FileLogs.WriteMainLogs("匹配协议成功");
- // 获取匹配的值
- string match = Regex.Match(input, pattern).Groups[0].Value;
- return match;
- }
- else
- {
- // 如果不匹配,说明协议的长度不够,
- // 那么先缓存,然后等待下一次请求
- return "";
- }
- }
- }
- }
|