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); }
/// <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 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); }
/// <summary> /// 异步下载 /// </summary> public void AsyncExecuteDownLoad() { var p = ProtocolMgmt.InitProtocolHeader(2, null, null); var state = client.GetState(); if (state == null) { throw new ArgumentNullException("Socket null"); } ThreadPool.QueueUserWorkItem(new WaitCallback((obj) => { base.ExecuteProtocolCommand(p, 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); }
/// SyncExecuteProtocolCommand /// </summary> /// <param name="ar"></param> public virtual void SyncExecuteProtocolCommand(IAsyncResult ar) { SocketState state = (SocketState)ar.AsyncState; try { System.Net.Sockets.Socket handler = state.workSocket; int bytesRead = handler.EndReceive(ar); Mark(string.Format("解析协议数据头")); if (bytesRead > 0) { var protocol = ProtocolMgmt.InitProtocolHeader(state.buffer); ThreadPool.QueueUserWorkItem(new WaitCallback((obj) => { Mark(string.Format("执行协议编号:{0}", protocol.Command)); InvokeExecuteProtocolCommand(protocol, state); })); } } catch (System.Net.Sockets.SocketException SktEx) { WriteLog(SktEx, SktEx.ErrorCode.ToString()); SetConnection(false, state); } catch (System.IO.IOException IoEx) { WriteLog(IoEx, IoEx.Message); SetConnection(false, state); } catch (Exception Ex) { WriteLog(Ex, Ex.Message); SetConnection(false, 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); }