SocketFile.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. using Cksoft.Unity;
  2. using DllEapEntity;
  3. using DllEapEntity.Dtos;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Net.Sockets;
  9. using System.Text;
  10. using System.Threading;
  11. namespace DllSocketFile
  12. {
  13. public class SocketFile
  14. {
  15. public BusinessFile UploadFile(Machine mac, string filename, ref string errorinfo)
  16. {
  17. Socket filesocket = null;
  18. try
  19. {
  20. //ploger.LogError($"开始连接机台文件服务......");
  21. filesocket = ConnServer(mac.IPAddress);
  22. if (filesocket == null)
  23. return null;
  24. byte[] datas = GetFileDatas(filename, "", 1);
  25. //ploger.LogError($"开始发送请求上传文件指令,文件名称={filename}");
  26. filesocket.Send(datas);
  27. int alllen = 0;
  28. //ploger.LogError($"等待机台返回文件处理结果,文件名称={filename}");
  29. datas = ReadData(filesocket, ref alllen, ref errorinfo);
  30. if (datas == null)
  31. {
  32. //ploger.LogError($"等待机台返回文件处理结果发生错误:{errorinfo},文件名称={filename}");
  33. return null;
  34. }
  35. byte[] tempstr = new byte[alllen - 12];
  36. Array.Copy(datas, 12, tempstr, 0, tempstr.Length);
  37. byte[] statusdatas = new byte[4];
  38. Array.Copy(datas, 8, statusdatas, 0, 4);
  39. //Array.Reverse(statusdatas);
  40. int status = BitConverter.ToInt32(statusdatas, 0);
  41. if (status != 1)
  42. {
  43. errorinfo = Encoding.UTF8.GetString(tempstr);
  44. return null;
  45. }
  46. return EntityHelper.DeSerializeBytes<BusinessFile>(tempstr);
  47. }
  48. catch (Exception ex)
  49. {
  50. errorinfo = ex.Message.ToString();
  51. //ploger.LogError($"UploadFile 函数异常:{errorinfo},文件名称={filename}");
  52. return null;
  53. }
  54. finally
  55. {
  56. if (filesocket != null)
  57. filesocket.Close();
  58. }
  59. }
  60. public int DownloadFile(Machine mac, string filename, string filepath, ref string errorinfo)
  61. {
  62. Socket filesocket = null;
  63. try
  64. {
  65. filesocket = ConnServer(mac.IPAddress);
  66. if (filesocket == null)
  67. return -1;
  68. byte[] datas = GetFileDatas(filename, filepath, 2);
  69. filesocket.Send(datas);
  70. int alllen = 0;
  71. datas = ReadData(filesocket, ref alllen, ref errorinfo);
  72. if (datas == null)
  73. return -1;
  74. byte[] tempstr = new byte[alllen - 12];
  75. Array.Copy(datas, 12, tempstr, 0, tempstr.Length);
  76. byte[] statusdatas = new byte[4];
  77. Array.Copy(datas, 8, statusdatas, 0, 4);
  78. //Array.Reverse(statusdatas);
  79. int status = BitConverter.ToInt32(statusdatas, 0);
  80. if (status != 1)
  81. {
  82. errorinfo = Encoding.UTF8.GetString(tempstr);
  83. return -1;
  84. }
  85. return 1;
  86. }
  87. catch (Exception ex)
  88. {
  89. errorinfo = ex.Message.ToString();
  90. return -1;
  91. }
  92. finally
  93. {
  94. if (filesocket != null)
  95. filesocket.Close();
  96. }
  97. }
  98. public int DownloadFileAdt(Machine mac, string filename, string filepath, ref string errorinfo)
  99. {
  100. Socket filesocket = null;
  101. try
  102. {
  103. filesocket = ConnServer(mac.IPAddress);
  104. if (filesocket == null)
  105. return -1;
  106. byte[] datas = GetFileDatas(filename, filepath, 2);
  107. filesocket.Send(datas);
  108. int alllen = 0;
  109. datas = ReadData(filesocket, ref alllen, ref errorinfo);
  110. if (datas == null)
  111. return -1;
  112. byte[] tempstr = new byte[alllen - 12];
  113. Array.Copy(datas, 12, tempstr, 0, tempstr.Length);
  114. byte[] statusdatas = new byte[4];
  115. Array.Copy(datas, 8, statusdatas, 0, 4);
  116. //Array.Reverse(statusdatas);
  117. int status = BitConverter.ToInt32(statusdatas, 0);
  118. errorinfo = Encoding.UTF8.GetString(tempstr);
  119. return status;
  120. }
  121. catch (Exception ex)
  122. {
  123. errorinfo = ex.Message.ToString();
  124. return -1;
  125. }
  126. finally
  127. {
  128. if (filesocket != null)
  129. filesocket.Close();
  130. }
  131. }
  132. private Socket ConnServer(string ipaddress)
  133. {
  134. IPAddress ip = IPAddress.Parse(ipaddress);//服务端所在IP
  135. IPEndPoint ipEnd = new IPEndPoint(ip, 5888);//服务端所监听的接口
  136. Socket CurrSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//初始化一个Socket对象
  137. CurrSocket.Connect(ipEnd);
  138. //此语句只支持windows系统,不支持linux系统
  139. //CurrSocket.IOControl(IOControlCode.KeepAliveValues, GetKeepAliveData(), null);
  140. ////处理来自机台的包,设置接受超时时间为60秒
  141. CurrSocket.ReceiveTimeout = 1000 * 150;
  142. CurrSocket.SendTimeout = 1000 * 150;
  143. return CurrSocket;
  144. }
  145. private byte[] GetFileDatas(string filename, string filepath, Int32 mode)
  146. {
  147. Dictionary<string, string> dic = new Dictionary<string, string>();
  148. dic.Add("filename", filename);
  149. dic.Add("filepath", filepath);
  150. //dic.Add("TRSFilePath", filepath);
  151. byte[] filenamebytes = EntityHelper.SerializeBytes<Dictionary<string, string>>(dic);
  152. Int32 len = filenamebytes.Length + 8;
  153. byte[] alldatas = BitConverter.GetBytes(len);
  154. return alldatas.Concat(BitConverter.GetBytes(mode)).Concat(filenamebytes).ToArray();
  155. }
  156. //socket读取数据
  157. private byte[] ReadData(Socket CurrSocket, ref int alllen, ref string errorinfo)
  158. {
  159. byte[] allbuffs = null;
  160. try
  161. {
  162. byte[] tempbuff = new byte[4];
  163. byte[] bytesbuff = new byte[4];
  164. int result = 0;
  165. int levlen = 4;
  166. int zerocount = 0;
  167. while (levlen > 0)
  168. {
  169. //说明前面4位还没接受完,需要继续接受
  170. result = CurrSocket.Receive(tempbuff, levlen, SocketFlags.None);//先读取4位块长度字节
  171. if (result == 0 && zerocount <= 20)
  172. {
  173. zerocount++;
  174. //如果返回数据长度为0,则休眠1秒钟,然后继续读取
  175. Thread.Sleep(1000);
  176. continue;
  177. }
  178. if (result <= 0)
  179. {
  180. errorinfo = $"未能从机台上读取数据,可能机台已经断开了,接受长度={result} ";
  181. return null;
  182. }
  183. if (result < levlen)
  184. {
  185. //添加测试代码
  186. //int a = 10;
  187. }
  188. if (result > 0)
  189. {
  190. Array.Copy(tempbuff, 0, bytesbuff, 4 - levlen, result);
  191. }
  192. levlen = levlen - result;
  193. }
  194. //Array.Reverse(bytesbuff);
  195. int len = BitConverter.ToInt32(bytesbuff, 0);
  196. alllen = len;
  197. len = len - 4;//数据长度
  198. if (len > 10)
  199. {
  200. int a = 10;
  201. }
  202. byte[] buffs = new byte[len];
  203. tempbuff = new byte[len];
  204. levlen = len;
  205. while (levlen > 0)
  206. {
  207. //说明还没接受完,需要继续接受
  208. result = CurrSocket.Receive(tempbuff, levlen, SocketFlags.None);
  209. if (result < levlen)
  210. {
  211. //添加测试代码
  212. //int a = 10;
  213. }
  214. if (result > 0)
  215. {
  216. Array.Copy(tempbuff, 0, buffs, len - levlen, result);
  217. }
  218. levlen = levlen - result;
  219. }
  220. //result =mysocket.Receive(buffs,len,SocketFlags.None);
  221. //Array.Reverse(bytesbuff);
  222. allbuffs = new byte[len + 4];
  223. Array.Copy(bytesbuff, allbuffs, 4);
  224. Array.Copy(buffs, 0, allbuffs, 4, len);
  225. return allbuffs;
  226. }
  227. catch (SocketException ex)
  228. {
  229. errorinfo = "Socket接受数据发生异常,错误信息为:" + ex.SocketErrorCode.ToString();
  230. return null;
  231. }
  232. catch (Exception ex)
  233. {
  234. errorinfo = ex.Message.ToString();
  235. errorinfo = "Socket接受数据发生异常,错误信息为:" + errorinfo;
  236. return null;
  237. }
  238. }
  239. public List<string> GetMacFileList(Machine mac, string filename, ref string errorinfo)
  240. {
  241. Socket filesocket = null;
  242. try
  243. {
  244. //ploger.LogError($"开始连接机台文件服务......");
  245. filesocket = ConnServer(mac.IPAddress);
  246. if (filesocket == null)
  247. return null;
  248. byte[] datas = GetFileDatas(filename, "", 3);
  249. //ploger.LogError($"开始发送请求上传文件指令,文件名称={filename}");
  250. filesocket.Send(datas);
  251. int alllen = 0;
  252. //ploger.LogError($"等待机台返回文件处理结果,文件名称={filename}");
  253. datas = ReadData(filesocket, ref alllen, ref errorinfo);
  254. if (datas == null)
  255. {
  256. //ploger.LogError($"等待机台返回文件处理结果发生错误:{errorinfo},文件名称={filename}");
  257. return null;
  258. }
  259. byte[] tempstr = new byte[alllen - 12];
  260. Array.Copy(datas, 12, tempstr, 0, tempstr.Length);
  261. byte[] statusdatas = new byte[4];
  262. Array.Copy(datas, 8, statusdatas, 0, 4);
  263. //Array.Reverse(statusdatas);
  264. int status = BitConverter.ToInt32(statusdatas, 0);
  265. if (status != 1)
  266. {
  267. errorinfo = Encoding.UTF8.GetString(tempstr);
  268. return null;
  269. }
  270. return EntityHelper.DeSerializeBytes<List<string>>(tempstr);
  271. }
  272. catch (Exception ex)
  273. {
  274. errorinfo = ex.Message.ToString();
  275. //ploger.LogError($"UploadFile 函数异常:{errorinfo},文件名称={filename}");
  276. return null;
  277. }
  278. finally
  279. {
  280. if (filesocket != null)
  281. filesocket.Close();
  282. }
  283. }
  284. /// <summary>
  285. /// 泰睿思文件列表接收
  286. /// </summary>
  287. /// <param name="mac"></param>
  288. /// <param name="fileName"></param>
  289. /// <param name="errorinfo"></param>
  290. /// <returns></returns>
  291. public IEnumerable<OnsemiProgramDto> GetMacFileListForTRS(Machine mac, string filename, ref string errorinfo)
  292. {
  293. Socket filesocket = null;
  294. try
  295. {
  296. //ploger.LogError($"开始连接机台文件服务......");
  297. filesocket = ConnServer(mac.IPAddress);
  298. if (filesocket == null)
  299. return null;
  300. byte[] datas = GetFileDatas(filename, "", 3);
  301. //ploger.LogError($"开始发送请求上传文件指令,文件名称={filename}");
  302. filesocket.Send(datas);
  303. int alllen = 0;
  304. //ploger.LogError($"等待机台返回文件处理结果,文件名称={filename}");
  305. datas = ReadData(filesocket, ref alllen, ref errorinfo);
  306. if (datas == null)
  307. {
  308. //ploger.LogError($"等待机台返回文件处理结果发生错误:{errorinfo},文件名称={filename}");
  309. return null;
  310. }
  311. byte[] tempstr = new byte[alllen - 12];
  312. Array.Copy(datas, 12, tempstr, 0, tempstr.Length);
  313. byte[] statusdatas = new byte[4];
  314. Array.Copy(datas, 8, statusdatas, 0, 4);
  315. //Array.Reverse(statusdatas);
  316. int status = BitConverter.ToInt32(statusdatas, 0);
  317. if (status != 1)
  318. {
  319. errorinfo = Encoding.UTF8.GetString(tempstr);
  320. return null;
  321. }
  322. var str = Encoding.UTF8.GetString(tempstr);
  323. // var str = EntityHelper.DeSerializeBytes<string>(tempstr);
  324. return Json.ToList<OnsemiProgramDto>(str);
  325. }
  326. catch (Exception ex)
  327. {
  328. errorinfo = ex.Message.ToString();
  329. //ploger.LogError($"UploadFile 函数异常:{errorinfo},文件名称={filename}");
  330. return null;
  331. }
  332. finally
  333. {
  334. if (filesocket != null)
  335. filesocket.Close();
  336. }
  337. }
  338. public BusinessFile UploadForTRS(Machine mac, string filename, string filePath, ref string errorinfo)
  339. {
  340. Socket filesocket = null;
  341. try
  342. {
  343. //ploger.LogError($"开始连接机台文件服务......");
  344. filesocket = ConnServer(mac.IPAddress);
  345. if (filesocket == null)
  346. return null;
  347. byte[] datas = GetFileDatas(filename, filePath, 1);
  348. //ploger.LogError($"开始发送请求上传文件指令,文件名称={filename}");
  349. filesocket.Send(datas);
  350. int alllen = 0;
  351. //ploger.LogError($"等待机台返回文件处理结果,文件名称={filename}");
  352. datas = ReadData(filesocket, ref alllen, ref errorinfo);
  353. if (datas == null)
  354. {
  355. //ploger.LogError($"等待机台返回文件处理结果发生错误:{errorinfo},文件名称={filename}");
  356. return null;
  357. }
  358. byte[] tempstr = new byte[alllen - 12];
  359. Array.Copy(datas, 12, tempstr, 0, tempstr.Length);
  360. byte[] statusdatas = new byte[4];
  361. Array.Copy(datas, 8, statusdatas, 0, 4);
  362. //Array.Reverse(statusdatas);
  363. int status = BitConverter.ToInt32(statusdatas, 0);
  364. if (status != 1)
  365. {
  366. errorinfo = Encoding.UTF8.GetString(tempstr);
  367. return null;
  368. }
  369. return EntityHelper.DeSerializeBytes<BusinessFile>(tempstr);
  370. }
  371. catch (Exception ex)
  372. {
  373. errorinfo = ex.Message.ToString();
  374. //ploger.LogError($"UploadFile 函数异常:{errorinfo},文件名称={filename}");
  375. return null;
  376. }
  377. finally
  378. {
  379. if (filesocket != null)
  380. filesocket.Close();
  381. }
  382. }
  383. }
  384. }