123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- using System;
- using System.Collections.Generic;
- //using System.Linq;
- using System.Text;
- using System.Xml;
- using LYFZ.Network.EnumerateLibrary;
- namespace LYFZ.Network
- {
- /// <summary>
- /// FileProtocol结构,用来为整个协议字符串提供强类型的访问,
- /// 注意这里覆盖了基类的ToString()方法,这样在客户端我们就不需要再手工去编写XML,只
- /// 要在结构值上调用ToString()就OK了,会方便很多。
- /// </summary>
- public struct TCP_FileProtocol
- {
- private readonly InformationType infoType;
- private readonly string content;
- private string _ExtraInfo;
- private string _ClientInfo;
-
- public TCP_FileProtocol(string content, string extraInfo="")
- {
- this.infoType = InformationType.Message;
- this.content = content;
- this._ExtraInfo = extraInfo;
- this._ClientInfo = "";
- this._ReceiveFileSize = 0;
- this.dataArray = null;
- this._ServerStartTimeStamp = LYFZ.Network.TCPNetworkServer.TCP_NetworkServer.ServerStartTimeStamp;
- }
- /// <summary>
- /// 初始化
- /// </summary>
- /// <param name="type">TCP数据传输类型</param>
- /// <param name="content">传送的文字信息内容</param>
- /// <param name="mode">FileRequestMode枚举 标记是发送还是接收</param>
- /// <param name="port">端口</param>
- /// <param name="fileName">文件名</param>
- /// <param name="clientInfo">客户端连接信息</param>
- public TCP_FileProtocol(InformationType type, string content, string extraInfo = "", string clientInfo="")
- {
- this.infoType = type;
- this.content = content;
- this._ExtraInfo = extraInfo;
- this._ClientInfo = clientInfo;
- this._ReceiveFileSize = 0;
- this.dataArray = null;
- this._ServerStartTimeStamp = LYFZ.Network.TCPNetworkServer.TCP_NetworkServer.ServerStartTimeStamp;
- }
- public string ClientInfo
- {
- get { return _ClientInfo; }
- set { _ClientInfo = value; }
- }
- /// <summary>
- /// TCP数据传输类型 标记是文本信息传输 或 是文件类型传输
- /// </summary>
- public InformationType InfoType
- {
- get { return infoType; }
- }
-
- /// <summary>
- /// 文件名
- /// </summary>
- public string ExtraInfo
- {
- get { return _ExtraInfo; }
- set { _ExtraInfo = value; }
- }
- /// <summary>
- /// 传送的文字信息内容
- /// </summary>
- public string Content
- {
- get { return content; }
- }
- long _ReceiveFileSize;
- public long ReceiveFileSize
- {
- get { return _ReceiveFileSize; }
- set { _ReceiveFileSize = value; }
- }
- string _ServerStartTimeStamp;
- /// <summary>
- /// 服务器启动时间标识
- /// </summary>
- public string ServerStartTimeStamp
- {
- get { return _ServerStartTimeStamp; }
- set { _ServerStartTimeStamp = value; }
- }
- byte[] dataArray;
- /// <summary>
- /// 数据传输时的二进制数组
- /// </summary>
- public byte[] DataArray
- {
- get { return dataArray; }
- set { dataArray = value; }
- }
- /// <summary>
- /// 获取协议XML字符串
- /// </summary>
- /// <returns></returns>
- public override string ToString()
- {
-
- StringBuilder strXML = new StringBuilder();
- string thisInfoType = InfoType.ToString();
- if (InfoType == InformationType.Message)
- {
- thisInfoType = "msg";
- }
- else if (InfoType == InformationType.File)
- {
- thisInfoType = "file";
- }
- string encodeContent = Content;
- string encodeExtraInfo = ExtraInfo;
-
- encodeContent = LYFZ.WinAPI.SDKSecurity.Encode(Content);
- encodeExtraInfo = LYFZ.WinAPI.SDKSecurity.Encode(ExtraInfo);
-
- strXML.Append("<lyfz_protocol>");
- strXML.Append(String.Format("<information Type=\"{0}\" ExtraInfo=\"{1}\" ReceiveFileSize=\"{2}\" ClientInfo=\"{3}\" ServerStartTimeStamp=\"{4}\"><![CDATA[{5}]]></information>", thisInfoType, encodeExtraInfo, ReceiveFileSize, ClientInfo, this.ServerStartTimeStamp, encodeContent));
- strXML.Append("</lyfz_protocol>");
- return strXML.ToString();
- }
- }
- }
|