TCP_FileProtocol.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System;
  2. using System.Collections.Generic;
  3. //using System.Linq;
  4. using System.Text;
  5. using System.Xml;
  6. using LYFZ.Network.EnumerateLibrary;
  7. namespace LYFZ.Network
  8. {
  9. /// <summary>
  10. /// FileProtocol结构,用来为整个协议字符串提供强类型的访问,
  11. /// 注意这里覆盖了基类的ToString()方法,这样在客户端我们就不需要再手工去编写XML,只
  12. /// 要在结构值上调用ToString()就OK了,会方便很多。
  13. /// </summary>
  14. public struct TCP_FileProtocol
  15. {
  16. private readonly InformationType infoType;
  17. private readonly string content;
  18. private string _ExtraInfo;
  19. private string _ClientInfo;
  20. public TCP_FileProtocol(string content, string extraInfo="")
  21. {
  22. this.infoType = InformationType.Message;
  23. this.content = content;
  24. this._ExtraInfo = extraInfo;
  25. this._ClientInfo = "";
  26. this._ReceiveFileSize = 0;
  27. this.dataArray = null;
  28. this._ServerStartTimeStamp = LYFZ.Network.TCPNetworkServer.TCP_NetworkServer.ServerStartTimeStamp;
  29. }
  30. /// <summary>
  31. /// 初始化
  32. /// </summary>
  33. /// <param name="type">TCP数据传输类型</param>
  34. /// <param name="content">传送的文字信息内容</param>
  35. /// <param name="mode">FileRequestMode枚举 标记是发送还是接收</param>
  36. /// <param name="port">端口</param>
  37. /// <param name="fileName">文件名</param>
  38. /// <param name="clientInfo">客户端连接信息</param>
  39. public TCP_FileProtocol(InformationType type, string content, string extraInfo = "", string clientInfo="")
  40. {
  41. this.infoType = type;
  42. this.content = content;
  43. this._ExtraInfo = extraInfo;
  44. this._ClientInfo = clientInfo;
  45. this._ReceiveFileSize = 0;
  46. this.dataArray = null;
  47. this._ServerStartTimeStamp = LYFZ.Network.TCPNetworkServer.TCP_NetworkServer.ServerStartTimeStamp;
  48. }
  49. public string ClientInfo
  50. {
  51. get { return _ClientInfo; }
  52. set { _ClientInfo = value; }
  53. }
  54. /// <summary>
  55. /// TCP数据传输类型 标记是文本信息传输 或 是文件类型传输
  56. /// </summary>
  57. public InformationType InfoType
  58. {
  59. get { return infoType; }
  60. }
  61. /// <summary>
  62. /// 文件名
  63. /// </summary>
  64. public string ExtraInfo
  65. {
  66. get { return _ExtraInfo; }
  67. set { _ExtraInfo = value; }
  68. }
  69. /// <summary>
  70. /// 传送的文字信息内容
  71. /// </summary>
  72. public string Content
  73. {
  74. get { return content; }
  75. }
  76. long _ReceiveFileSize;
  77. public long ReceiveFileSize
  78. {
  79. get { return _ReceiveFileSize; }
  80. set { _ReceiveFileSize = value; }
  81. }
  82. string _ServerStartTimeStamp;
  83. /// <summary>
  84. /// 服务器启动时间标识
  85. /// </summary>
  86. public string ServerStartTimeStamp
  87. {
  88. get { return _ServerStartTimeStamp; }
  89. set { _ServerStartTimeStamp = value; }
  90. }
  91. byte[] dataArray;
  92. /// <summary>
  93. /// 数据传输时的二进制数组
  94. /// </summary>
  95. public byte[] DataArray
  96. {
  97. get { return dataArray; }
  98. set { dataArray = value; }
  99. }
  100. /// <summary>
  101. /// 获取协议XML字符串
  102. /// </summary>
  103. /// <returns></returns>
  104. public override string ToString()
  105. {
  106. StringBuilder strXML = new StringBuilder();
  107. string thisInfoType = InfoType.ToString();
  108. if (InfoType == InformationType.Message)
  109. {
  110. thisInfoType = "msg";
  111. }
  112. else if (InfoType == InformationType.File)
  113. {
  114. thisInfoType = "file";
  115. }
  116. string encodeContent = Content;
  117. string encodeExtraInfo = ExtraInfo;
  118. encodeContent = LYFZ.WinAPI.SDKSecurity.Encode(Content);
  119. encodeExtraInfo = LYFZ.WinAPI.SDKSecurity.Encode(ExtraInfo);
  120. strXML.Append("<lyfz_protocol>");
  121. 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));
  122. strXML.Append("</lyfz_protocol>");
  123. return strXML.ToString();
  124. }
  125. }
  126. }