123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using LYFZ.Network.EnumerateLibrary;
- using System.Xml;
- namespace LYFZ.Network
- {
- /// <summary>
- /// ProtocolHelper辅助类
- ///这个类专用于将XML格式的协议映射为我们上面定义的强类型对象,这里我没有加入
- ///try/catch异常处理,因为协议对用户来说是不可见的,而且客户端应该总是发送正确的协议
- /// </summary>
- public class TCP_ProtocolHelperXML
- {
- private XmlNode fileNode;
- private XmlNode root;
- /// <summary>
- /// 初始化
- /// </summary>
- /// <param name="protocol">XML格式的协议字符串</param>
- public TCP_ProtocolHelperXML(string protocol)
- {
- if (protocol.Trim().Length > 1)
- {
- XmlDocument doc = new XmlDocument();
- doc.LoadXml(protocol);
- root = doc.DocumentElement;
- fileNode = root.SelectSingleNode("information");
- }
- }
-
- /// <summary>
- /// 获取TCP数据传送类型,是文字信息还是文件,此时的protocal一定为单条完整protocal
- /// </summary>
- /// <returns></returns>
- private InformationType GetInformationType()
- {
- string mode = fileNode.Attributes["Type"].Value;
- if (mode.ToLower() == "msg")
- {
- return InformationType.Message;
- }
- else if (mode.ToLower() == "file")
- {
- return InformationType.File;
- }
- else
- {
- return (InformationType)Enum.Parse(typeof(InformationType), mode);
- }
- }
-
- /// <summary>
- /// 获取单条协议包含的信息
- /// </summary>
- /// <returns></returns>
- public TCP_FileProtocol GetProtocol()
- {
- try
- {
- //Type=\"{0}\" ExtraInfo=\"{1}\" ReceiveFileSize=\"{2}\" ClientInfo=\"{3}\" ServerStartTimeStamp=\"{4}\"
- string ExtraInfo = "";
- long receiveFileSize = 0;
- InformationType infoType = GetInformationType();
- string content = "";
- ExtraInfo = LYFZ.WinAPI.SDKSecurity.Decode(fileNode.Attributes["ExtraInfo"].Value);
- try
- {
- receiveFileSize = Convert.ToInt64(fileNode.Attributes["ReceiveFileSize"].Value);
- }
- catch { }
- content = LYFZ.WinAPI.SDKSecurity.Decode(fileNode.InnerText.Trim());
- string clientInfo = "";
- string serverStartTimeStamp = "";
- try
- {
- clientInfo = fileNode.Attributes["ClientInfo"].Value;
- serverStartTimeStamp = fileNode.Attributes["ServerStartTimeStamp"].Value;
- }
- catch { }
- TCP_FileProtocol retProtocol= new TCP_FileProtocol(infoType, content, ExtraInfo, clientInfo);
- retProtocol.ServerStartTimeStamp = serverStartTimeStamp;
- retProtocol.ReceiveFileSize = receiveFileSize;
- return retProtocol;
- }
- catch { return new TCP_FileProtocol(); }
- }
- }
- }
|