123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- using Cksoft.Unity;
- using DllEapEntity;
- using DllEapEntity.Dtos;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading;
- namespace DllSocketFile
- {
- public class SocketFile
- {
- public BusinessFile UploadFile(Machine mac, string filename, ref string errorinfo)
- {
- Socket filesocket = null;
- try
- {
- //ploger.LogError($"开始连接机台文件服务......");
- filesocket = ConnServer(mac.IPAddress);
- if (filesocket == null)
- return null;
- byte[] datas = GetFileDatas(filename, "", 1);
- //ploger.LogError($"开始发送请求上传文件指令,文件名称={filename}");
- filesocket.Send(datas);
- int alllen = 0;
- //ploger.LogError($"等待机台返回文件处理结果,文件名称={filename}");
- datas = ReadData(filesocket, ref alllen, ref errorinfo);
- if (datas == null)
- {
- //ploger.LogError($"等待机台返回文件处理结果发生错误:{errorinfo},文件名称={filename}");
- return null;
- }
- byte[] tempstr = new byte[alllen - 12];
- Array.Copy(datas, 12, tempstr, 0, tempstr.Length);
- byte[] statusdatas = new byte[4];
- Array.Copy(datas, 8, statusdatas, 0, 4);
- //Array.Reverse(statusdatas);
- int status = BitConverter.ToInt32(statusdatas, 0);
- if (status != 1)
- {
- errorinfo = Encoding.UTF8.GetString(tempstr);
- return null;
- }
- return EntityHelper.DeSerializeBytes<BusinessFile>(tempstr);
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- //ploger.LogError($"UploadFile 函数异常:{errorinfo},文件名称={filename}");
- return null;
- }
- finally
- {
- if (filesocket != null)
- filesocket.Close();
- }
- }
- public int DownloadFile(Machine mac, string filename, string filepath, ref string errorinfo)
- {
- Socket filesocket = null;
- try
- {
- filesocket = ConnServer(mac.IPAddress);
- if (filesocket == null)
- return -1;
- byte[] datas = GetFileDatas(filename, filepath, 2);
- filesocket.Send(datas);
- int alllen = 0;
- datas = ReadData(filesocket, ref alllen, ref errorinfo);
- if (datas == null)
- return -1;
- byte[] tempstr = new byte[alllen - 12];
- Array.Copy(datas, 12, tempstr, 0, tempstr.Length);
- byte[] statusdatas = new byte[4];
- Array.Copy(datas, 8, statusdatas, 0, 4);
- //Array.Reverse(statusdatas);
- int status = BitConverter.ToInt32(statusdatas, 0);
- if (status != 1)
- {
- errorinfo = Encoding.UTF8.GetString(tempstr);
- return -1;
- }
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- finally
- {
- if (filesocket != null)
- filesocket.Close();
- }
- }
- public int DownloadFileAdt(Machine mac, string filename, string filepath, ref string errorinfo)
- {
- Socket filesocket = null;
- try
- {
- filesocket = ConnServer(mac.IPAddress);
- if (filesocket == null)
- return -1;
- byte[] datas = GetFileDatas(filename, filepath, 2);
- filesocket.Send(datas);
- int alllen = 0;
- datas = ReadData(filesocket, ref alllen, ref errorinfo);
- if (datas == null)
- return -1;
- byte[] tempstr = new byte[alllen - 12];
- Array.Copy(datas, 12, tempstr, 0, tempstr.Length);
- byte[] statusdatas = new byte[4];
- Array.Copy(datas, 8, statusdatas, 0, 4);
- //Array.Reverse(statusdatas);
- int status = BitConverter.ToInt32(statusdatas, 0);
- errorinfo = Encoding.UTF8.GetString(tempstr);
- return status;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- finally
- {
- if (filesocket != null)
- filesocket.Close();
- }
- }
- private Socket ConnServer(string ipaddress)
- {
- IPAddress ip = IPAddress.Parse(ipaddress);//服务端所在IP
- IPEndPoint ipEnd = new IPEndPoint(ip, 5888);//服务端所监听的接口
- Socket CurrSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//初始化一个Socket对象
- CurrSocket.Connect(ipEnd);
- //此语句只支持windows系统,不支持linux系统
- //CurrSocket.IOControl(IOControlCode.KeepAliveValues, GetKeepAliveData(), null);
- ////处理来自机台的包,设置接受超时时间为60秒
- CurrSocket.ReceiveTimeout = 1000 * 150;
- CurrSocket.SendTimeout = 1000 * 150;
- return CurrSocket;
- }
- private byte[] GetFileDatas(string filename, string filepath, Int32 mode)
- {
- Dictionary<string, string> dic = new Dictionary<string, string>();
- dic.Add("filename", filename);
- dic.Add("filepath", filepath);
- //dic.Add("TRSFilePath", filepath);
- byte[] filenamebytes = EntityHelper.SerializeBytes<Dictionary<string, string>>(dic);
- Int32 len = filenamebytes.Length + 8;
- byte[] alldatas = BitConverter.GetBytes(len);
- return alldatas.Concat(BitConverter.GetBytes(mode)).Concat(filenamebytes).ToArray();
- }
- //socket读取数据
- private byte[] ReadData(Socket CurrSocket, ref int alllen, ref string errorinfo)
- {
- byte[] allbuffs = null;
- try
- {
- byte[] tempbuff = new byte[4];
- byte[] bytesbuff = new byte[4];
- int result = 0;
- int levlen = 4;
- int zerocount = 0;
- while (levlen > 0)
- {
- //说明前面4位还没接受完,需要继续接受
- result = CurrSocket.Receive(tempbuff, levlen, SocketFlags.None);//先读取4位块长度字节
- if (result == 0 && zerocount <= 20)
- {
- zerocount++;
- //如果返回数据长度为0,则休眠1秒钟,然后继续读取
- Thread.Sleep(1000);
- continue;
- }
- if (result <= 0)
- {
- errorinfo = $"未能从机台上读取数据,可能机台已经断开了,接受长度={result} ";
- return null;
- }
- if (result < levlen)
- {
- //添加测试代码
- //int a = 10;
- }
- if (result > 0)
- {
- Array.Copy(tempbuff, 0, bytesbuff, 4 - levlen, result);
- }
- levlen = levlen - result;
- }
- //Array.Reverse(bytesbuff);
- int len = BitConverter.ToInt32(bytesbuff, 0);
- alllen = len;
- len = len - 4;//数据长度
- if (len > 10)
- {
- int a = 10;
- }
- byte[] buffs = new byte[len];
- tempbuff = new byte[len];
- levlen = len;
- while (levlen > 0)
- {
- //说明还没接受完,需要继续接受
- result = CurrSocket.Receive(tempbuff, levlen, SocketFlags.None);
- if (result < levlen)
- {
- //添加测试代码
- //int a = 10;
- }
- if (result > 0)
- {
- Array.Copy(tempbuff, 0, buffs, len - levlen, result);
- }
- levlen = levlen - result;
- }
- //result =mysocket.Receive(buffs,len,SocketFlags.None);
- //Array.Reverse(bytesbuff);
- allbuffs = new byte[len + 4];
- Array.Copy(bytesbuff, allbuffs, 4);
- Array.Copy(buffs, 0, allbuffs, 4, len);
- return allbuffs;
- }
- catch (SocketException ex)
- {
- errorinfo = "Socket接受数据发生异常,错误信息为:" + ex.SocketErrorCode.ToString();
- return null;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- errorinfo = "Socket接受数据发生异常,错误信息为:" + errorinfo;
- return null;
- }
- }
- public List<string> GetMacFileList(Machine mac, string filename, ref string errorinfo)
- {
- Socket filesocket = null;
- try
- {
- //ploger.LogError($"开始连接机台文件服务......");
- filesocket = ConnServer(mac.IPAddress);
- if (filesocket == null)
- return null;
- byte[] datas = GetFileDatas(filename, "", 3);
- //ploger.LogError($"开始发送请求上传文件指令,文件名称={filename}");
- filesocket.Send(datas);
- int alllen = 0;
- //ploger.LogError($"等待机台返回文件处理结果,文件名称={filename}");
- datas = ReadData(filesocket, ref alllen, ref errorinfo);
- if (datas == null)
- {
- //ploger.LogError($"等待机台返回文件处理结果发生错误:{errorinfo},文件名称={filename}");
- return null;
- }
- byte[] tempstr = new byte[alllen - 12];
- Array.Copy(datas, 12, tempstr, 0, tempstr.Length);
- byte[] statusdatas = new byte[4];
- Array.Copy(datas, 8, statusdatas, 0, 4);
- //Array.Reverse(statusdatas);
- int status = BitConverter.ToInt32(statusdatas, 0);
- if (status != 1)
- {
- errorinfo = Encoding.UTF8.GetString(tempstr);
- return null;
- }
- return EntityHelper.DeSerializeBytes<List<string>>(tempstr);
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- //ploger.LogError($"UploadFile 函数异常:{errorinfo},文件名称={filename}");
- return null;
- }
- finally
- {
- if (filesocket != null)
- filesocket.Close();
- }
- }
- /// <summary>
- /// 泰睿思文件列表接收
- /// </summary>
- /// <param name="mac"></param>
- /// <param name="fileName"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- public IEnumerable<OnsemiProgramDto> GetMacFileListForTRS(Machine mac, string filename, ref string errorinfo)
- {
- Socket filesocket = null;
- try
- {
- //ploger.LogError($"开始连接机台文件服务......");
- filesocket = ConnServer(mac.IPAddress);
- if (filesocket == null)
- return null;
- byte[] datas = GetFileDatas(filename, "", 3);
- //ploger.LogError($"开始发送请求上传文件指令,文件名称={filename}");
- filesocket.Send(datas);
- int alllen = 0;
- //ploger.LogError($"等待机台返回文件处理结果,文件名称={filename}");
- datas = ReadData(filesocket, ref alllen, ref errorinfo);
- if (datas == null)
- {
- //ploger.LogError($"等待机台返回文件处理结果发生错误:{errorinfo},文件名称={filename}");
- return null;
- }
- byte[] tempstr = new byte[alllen - 12];
- Array.Copy(datas, 12, tempstr, 0, tempstr.Length);
- byte[] statusdatas = new byte[4];
- Array.Copy(datas, 8, statusdatas, 0, 4);
- //Array.Reverse(statusdatas);
- int status = BitConverter.ToInt32(statusdatas, 0);
- if (status != 1)
- {
- errorinfo = Encoding.UTF8.GetString(tempstr);
- return null;
- }
- var str = Encoding.UTF8.GetString(tempstr);
- // var str = EntityHelper.DeSerializeBytes<string>(tempstr);
- return Json.ToList<OnsemiProgramDto>(str);
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- //ploger.LogError($"UploadFile 函数异常:{errorinfo},文件名称={filename}");
- return null;
- }
- finally
- {
- if (filesocket != null)
- filesocket.Close();
- }
- }
- public BusinessFile UploadForTRS(Machine mac, string filename, string filePath, ref string errorinfo)
- {
- Socket filesocket = null;
- try
- {
- //ploger.LogError($"开始连接机台文件服务......");
- filesocket = ConnServer(mac.IPAddress);
- if (filesocket == null)
- return null;
- byte[] datas = GetFileDatas(filename, filePath, 1);
- //ploger.LogError($"开始发送请求上传文件指令,文件名称={filename}");
- filesocket.Send(datas);
- int alllen = 0;
- //ploger.LogError($"等待机台返回文件处理结果,文件名称={filename}");
- datas = ReadData(filesocket, ref alllen, ref errorinfo);
- if (datas == null)
- {
- //ploger.LogError($"等待机台返回文件处理结果发生错误:{errorinfo},文件名称={filename}");
- return null;
- }
- byte[] tempstr = new byte[alllen - 12];
- Array.Copy(datas, 12, tempstr, 0, tempstr.Length);
- byte[] statusdatas = new byte[4];
- Array.Copy(datas, 8, statusdatas, 0, 4);
- //Array.Reverse(statusdatas);
- int status = BitConverter.ToInt32(statusdatas, 0);
- if (status != 1)
- {
- errorinfo = Encoding.UTF8.GetString(tempstr);
- return null;
- }
- return EntityHelper.DeSerializeBytes<BusinessFile>(tempstr);
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- //ploger.LogError($"UploadFile 函数异常:{errorinfo},文件名称={filename}");
- return null;
- }
- finally
- {
- if (filesocket != null)
- filesocket.Close();
- }
- }
- }
- }
|