/// <summary> /// /// </summary> /// <param name="protocol"></param> /// <param name="state"></param> /// <param name="sobj"></param> /// <returns></returns> public SocketState Execute(CommunicateProtocol protocol, SocketState state, IHSSocket sobj) { state.IsReceiveThreadAlive = false; // Check File if (!FileHelper.IsFileExist(profile.UpLoadPath + profile.UpLoadFName)) { var p = ProtocolMgmt.InitProtocolHeader(998, System.Text.Encoding.UTF8.GetBytes("服务端文件不存在"), null); ProtocolMgmt.SendProtocol(state, p, sobj); state.OutPutMsg = string.Format("Command 1002 :服务端文件不存在"); return(state); } if (!FileHelper.CanRead(profile.UpLoadPath + profile.UpLoadFName)) { var p = ProtocolMgmt.InitProtocolHeader(998, System.Text.Encoding.UTF8.GetBytes("文件已被打开或占用,请稍后重试"), null); ProtocolMgmt.SendProtocol(state, p, sobj); state.OutPutMsg = string.Format("Command 1002 :文件已被打开或占用"); return(state); } FileInfo fi = new FileInfo(profile.UpLoadPath + profile.UpLoadFName); using (FileStream fs = new FileStream(profile.UpLoadPath + profile.UpLoadFName, FileMode.Open)) { var p = ProtocolMgmt.InitProtocolHeader(2002, System.Text.Encoding.UTF8.GetBytes(fi.Name), fs); ProtocolMgmt.SendProtocol(state, p, sobj); state.OutPutMsg = string.Format("Command 1002 :发送文件 {0} 成功。", fi.FullName); } return(state); }
public SocketState Execute(CommunicateProtocol protocol, SocketState state, IHSSocket sobj) { state.IsReceiveThreadAlive = false; // 接收完数据后立即解锁 if (protocol == null) { throw new ArgumentNullException("Protocol"); } var fName = "Default.dat"; try { byte[] body_stream1 = new byte[protocol.Stream1Len]; if (protocol.Stream1Len > 0) { body_stream1 = ProtocolMgmt.ReceiveBytes(state, protocol.Stream1Len, sobj); fName = System.Text.Encoding.UTF8.GetString(body_stream1, 0, protocol.Stream1Len).Trim('\0'); } // Create Folder FileHelper.CreateFolder(profile.DownLoadPath); if (protocol.Stream2Len > 0) { using (FileStream fs = new FileStream(profile.DownLoadPath + fName, FileMode.Create, FileAccess.Write, FileShare.ReadWrite, 5)) { foreach (var bytes in ProtocolMgmt.ReceiveProtocolStream(state, protocol, sobj)) { fs.Write(bytes, 0, bytes.Length); } } } } catch (System.Net.Sockets.SocketException SktEx) { FileHelper.DeleteFile(profile.DownLoadPath + fName); state.OutPutMsg = string.Format("Command 2002 :客户端连接异常,Socket Error Code : {0}", SktEx.ErrorCode); throw new Exception(state.OutPutMsg); } catch (Exception Ex) { FileHelper.DeleteFile(profile.DownLoadPath + fName); var errorACK = ProtocolMgmt.InitProtocolHeader(998, System.Text.Encoding.UTF8.GetBytes(Ex.Message), null); ProtocolMgmt.SendProtocol(state, errorACK, sobj); state.OutPutMsg = string.Format("Command 2002 :接收文件失败,{0}", Ex.Message); throw new Exception(state.OutPutMsg); } var pACK = ProtocolMgmt.InitProtocolHeader(999, System.Text.Encoding.UTF8.GetBytes("OK"), null); ProtocolMgmt.SendProtocol(state, pACK, sobj); state.OutPutMsg = string.Format("Command 2002 :接收文件{0}", "成功"); return(state); }
public SocketState Execute(CommunicateProtocol protocol, SocketState state, IHSSocket client) { if (protocol == null) { throw new ArgumentNullException("Protocol"); } var p = ProtocolMgmt.InitProtocolHeader(1002, null, null); ProtocolMgmt.SendProtocol(state, p, client); state.OutPutMsg = string.Format("Command 2 :向服务端请求下载文件"); return(state); }
public SocketState Execute(CommunicateProtocol protocol, SocketState state, IHSSocket sobj) { if (protocol == null) { throw new ArgumentNullException("Protocol"); } var p = ProtocolMgmt.InitProtocolHeader(1001, null, null); ProtocolMgmt.SendProtocol(state, p, sobj); state.OutPutMsg = string.Format("Command 1 :请求发送本地文件 {0}。", profile.UpLoadPath + profile.UpLoadFName); return(state); }
public SocketState Execute(CommunicateProtocol protocol, SocketState state, IHSSocket sobj) { state.IsReceiveThreadAlive = false; // 接收完数据后立即解锁 if (protocol == null) { throw new ArgumentNullException("Protocol"); } string Msg = string.Empty; if (protocol.Stream1Len > 0) { byte[] stream1 = ProtocolMgmt.ReceiveBytes(state, protocol.Stream1Len, sobj); Msg = string.Format("Command 998 :失败信息 {0}", System.Text.Encoding.UTF8.GetString(stream1, 0, protocol.Stream1Len).Trim('\0')); } state.OutPutMsg = string.Format("Command 998 :失败信息 {0}", Msg); return(state); }
public SocketState Execute(CommunicateProtocol protocol, SocketState state, IHSSocket sobj) { state.IsReceiveThreadAlive = false; if (protocol == null) { throw new ArgumentNullException("Protocol"); } string fullName = profile.UpLoadPath + profile.UpLoadFName; if (!FileHelper.IsFileExist(fullName)) { state.OutPutMsg = string.Format("Command 2001 :发送本地文件失败,找不到所需文件{0}{1}", profile.UpLoadPath, profile.UpLoadFName); return(state); } if (!FileHelper.CanRead(fullName)) { state.OutPutMsg = string.Format("Command 2001 :读取本地文件失败 {0}{1}", profile.UpLoadPath, profile.UpLoadFName); return(state); } FileInfo fi = new FileInfo(fullName); try { using (FileStream fs = new FileStream(fi.FullName, FileMode.Open)) { var p = ProtocolMgmt.InitProtocolHeader(3001, System.Text.Encoding.UTF8.GetBytes(profile.PID + fi.Name), fs); ProtocolMgmt.SendProtocol(state, p, sobj); state.OutPutMsg = string.Format("Command 2001 :发送本地文件 {0} 成功。", fullName); } } catch (Exception Ex) { state.OutPutMsg = string.Format("Command 2001 :读取本地文件失败 {0}", Ex.Message); } return(state); }
/// <summary> /// Execute /// </summary> /// <param name="protocol"></param> /// <param name="state"></param> /// <returns></returns> public SocketState Execute(CommunicateProtocol protocol, SocketState state, IHSSocket sobj) { state.IsReceiveThreadAlive = false; if (protocol == null) { throw new ArgumentNullException("Protocol"); } //var pdaId = string.Empty; //if (protocol.Stream1Len > 0) //{ // body_stream1 = ProtocolFactory.ReceiveBytes(state, protocol.Stream1Len, sobj); // pdaId = System.Text.Encoding.UTF8.GetString(body_stream1, 0, protocol.Stream1Len).Trim('\0'); //} //if (string.IsNullOrEmpty(pdaId)) //{ // var pACK = ProtocolFactory.InitProtocolHeader(998, Encoding.UTF8.GetBytes("解析PDA编号失败"), null); // ProtocolFactory.SendProtocol(state, pACK, sobj); // state.OutPutMsg = string.Format("Command 1001 :接收客户端上传请求{0}", "失败"); //} //else //{ // var pACK = ProtocolFactory.InitProtocolHeader(2001, Encoding.UTF8.GetBytes("OK"), null); // ProtocolFactory.SendProtocol(state, pACK, sobj); // state.OutPutMsg = string.Format("Command 1001 :接收客户端上传请求{0}", "成功"); //} var pACK = ProtocolMgmt.InitProtocolHeader(2001, System.Text.Encoding.UTF8.GetBytes("OK"), null); ProtocolMgmt.SendProtocol(state, pACK, sobj); state.OutPutMsg = string.Format("Command 1001 :接收客户端上传请求{0}", "成功"); return(state); }
public SocketState Execute(CommunicateProtocol protocol, SocketState state, IHSSocket sobj) { state.IsReceiveThreadAlive = false; // 接收完数据后立即解锁 state.OutPutMsg = string.Format("Command 0 :命令已弃用"); return(state); }
/// <summary> /// ReceiveProtocolStream /// </summary> /// <param name="state"></param> /// <param name="protocol"></param> /// <param name="sobj"></param> /// <returns></returns> public static IEnumerable <byte[]> ReceiveProtocolStream(SocketState state, CommunicateProtocol protocol, IHSSocket sobj) { if (protocol == null) { throw new Exception("Protocol should not be null."); } if (state == null) { throw new Exception("StateClient should not be null."); } if (sobj == null) { throw new Exception("client should not be null."); } int readIndex = 0; int fileLen = protocol.Stream2Len; while (readIndex < fileLen) { int bufferLen = GlobalConfig.Protocol_Buffer_Len; int readLen = fileLen - readIndex >= bufferLen ? bufferLen : fileLen - readIndex; var cache = sobj.SyncBufferReceive(state, readLen); readIndex += cache.Length; yield return(cache); } }
/// <summary> /// ReceiveBytes /// </summary> /// <param name="state"></param> /// <param name="length"></param> /// <param name="sobj"></param> /// <returns></returns> public static byte[] ReceiveBytes(SocketState state, int length, IHSSocket sobj) { return(sobj.SyncBufferReceive(state, length)); }
/// <summary> /// SendProtocol /// </summary> /// <param name="state"></param> /// <param name="protocol"></param> /// <param name="sobj"></param> public static void SendProtocol(SocketState state, CommunicateProtocol protocol, IHSSocket sobj) { if (protocol == null) { throw new Exception("Protocol should not be null."); } if (state == null) { throw new Exception("StateClient should not be null."); } if (sobj == null) { throw new Exception("client should not be null."); } sobj.SyncBufferSend(state, protocol.GetHeaderBytes()); if (protocol.GetStream1().Length > 0) { sobj.SyncBufferSend(state, protocol.GetStream1()); } if (protocol.GetStream2() != null) { var fs = protocol.GetStream2() as Stream; if (fs != null) { fs.Seek(0, SeekOrigin.Begin); int rIndex = 0; int fileLen = (int)fs.Length; while (rIndex < fs.Length) { byte[] cache = new byte[GlobalConfig.Protocol_Buffer_Len]; int len = fs.Read(cache, 0, fileLen - rIndex >= cache.Length ? cache.Length : fileLen - rIndex); if (len < cache.Length) { byte[] bytes = new byte[len]; Array.Copy(cache, 0, bytes, 0, bytes.Length); sobj.SyncBufferSend(state, bytes); } else { sobj.SyncBufferSend(state, cache); } rIndex += len; } } } }