ClassSoftUpdate.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net.Sockets;
  6. using System.Net;
  7. using System.IO;
  8. using System.Threading;
  9. using HslCommunication.Core;
  10. using HslCommunication.Core.Net;
  11. namespace HslCommunication.Enthernet
  12. {
  13. /// <summary>
  14. /// 用于服务器支持软件全自动更新升级的类
  15. /// </summary>
  16. public sealed class NetSoftUpdateServer : NetworkServerBase
  17. {
  18. #region Constructor
  19. /// <summary>
  20. /// 实例化一个对象
  21. /// </summary>
  22. public NetSoftUpdateServer()
  23. {
  24. }
  25. #endregion
  26. private string m_FilePath = @"C:\HslCommunication";
  27. /// <summary>
  28. /// 系统升级时客户端所在的目录,默认为C:\HslCommunication
  29. /// </summary>
  30. public string FileUpdatePath
  31. {
  32. get { return m_FilePath; }
  33. set { m_FilePath = value; }
  34. }
  35. /// <summary>
  36. /// 系统的登录方法
  37. /// </summary>
  38. /// <param name="obj"></param>
  39. protected override void ThreadPoolLogin(object obj)
  40. {
  41. Socket socket = obj as Socket;
  42. try
  43. {
  44. OperateResult<byte[]> receive = Receive( socket, 4 );
  45. if (!receive.IsSuccess) return;
  46. byte[] ReceiveByte = receive.Content;
  47. int Protocol = BitConverter.ToInt32(ReceiveByte, 0);
  48. if (Protocol == 0x1001 || Protocol == 0x1002)
  49. {
  50. // 安装系统和更新系统
  51. if (Protocol == 0x1001)
  52. {
  53. LogNet?.WriteInfo(ToString(), StringResources.SystemInstallOperater + ((IPEndPoint)socket.RemoteEndPoint).Address.ToString());
  54. }
  55. else
  56. {
  57. LogNet?.WriteInfo( ToString( ), StringResources.SystemUpdateOperater + ((IPEndPoint)socket.RemoteEndPoint).Address.ToString());
  58. }
  59. if (Directory.Exists(FileUpdatePath))
  60. {
  61. string[] files = Directory.GetFiles(FileUpdatePath);
  62. List<string> Files = new List<string>(files);
  63. for (int i = Files.Count - 1; i >= 0; i--)
  64. {
  65. FileInfo finfo = new FileInfo(Files[i]);
  66. if (finfo.Length > 200000000)
  67. {
  68. Files.RemoveAt(i);
  69. }
  70. if (Protocol == 0x1002)
  71. {
  72. if (finfo.Name == "软件自动更新.exe")
  73. {
  74. Files.RemoveAt(i);
  75. }
  76. }
  77. }
  78. files = Files.ToArray();
  79. socket.BeginReceive(new byte[4], 0, 4, SocketFlags.None, new AsyncCallback(ReceiveCallBack), socket);
  80. socket.Send(BitConverter.GetBytes(files.Length));
  81. for (int i = 0; i < files.Length; i++)
  82. {
  83. // 传送数据包含了本次数据大小,文件数据大小,文件名(带后缀)
  84. FileInfo finfo = new FileInfo(files[i]);
  85. string fileName = finfo.Name;
  86. byte[] ByteName = Encoding.Unicode.GetBytes(fileName);
  87. int First = 4 + 4 + ByteName.Length;
  88. byte[] FirstSend = new byte[First];
  89. FileStream fs = new FileStream(files[i], FileMode.Open, FileAccess.Read);
  90. Array.Copy(BitConverter.GetBytes(First), 0, FirstSend, 0, 4);
  91. Array.Copy(BitConverter.GetBytes((int)fs.Length), 0, FirstSend, 4, 4);
  92. Array.Copy(ByteName, 0, FirstSend, 8, ByteName.Length);
  93. socket.Send(FirstSend);
  94. Thread.Sleep(10);
  95. byte[] buffer = new byte[4096];
  96. int sended = 0;
  97. while (sended < fs.Length)
  98. {
  99. int count = fs.Read(buffer, 0, 4096);
  100. socket.Send(buffer, 0, count, SocketFlags.None);
  101. sended += count;
  102. }
  103. fs.Close();
  104. fs.Dispose();
  105. Thread.Sleep(20);
  106. }
  107. }
  108. }
  109. else
  110. {
  111. // 兼容原先版本的更新,新的验证方式无需理会
  112. socket.Send(BitConverter.GetBytes(10000f));
  113. Thread.Sleep(20);
  114. socket?.Close();
  115. }
  116. }
  117. catch (Exception ex)
  118. {
  119. Thread.Sleep(20);
  120. socket?.Close();
  121. LogNet?.WriteException( ToString( ), StringResources.FileSendClientFailed, ex);
  122. }
  123. }
  124. private void ReceiveCallBack(IAsyncResult ir)
  125. {
  126. if (ir.AsyncState is Socket)
  127. {
  128. Socket socket = ir.AsyncState as Socket;
  129. try
  130. {
  131. socket.EndReceive(ir);
  132. }
  133. catch(Exception ex)
  134. {
  135. LogNet?.WriteException( ToString( ), ex);
  136. }
  137. finally
  138. {
  139. socket?.Close();
  140. socket = null;
  141. }
  142. }
  143. }
  144. #region Object Override
  145. /// <summary>
  146. /// 获取本对象的字符串表示形式
  147. /// </summary>
  148. /// <returns></returns>
  149. public override string ToString( )
  150. {
  151. return "NetSoftUpdateServer";
  152. }
  153. #endregion
  154. }
  155. }