Esempio n. 1
0
        /// <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);
        }
Esempio n. 2
0
        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>
 public override void InvokeExecuteProtocolCommand(CommunicateProtocol protocol, SocketState state)
 {
     if (ExecuteProtocolCommand != null)
     {
         ExecuteProtocolCommand(protocol, state);
     }
 }
Esempio n. 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="func"></param>
        /// <param name="p"></param>
        /// <param name="s"></param>
        protected override void CommandWrapper(ICommandFunc func, CommunicateProtocol p, SocketState s)
        {
            try
            {
                func.profile = this.GetProfile(Settings.Default);
                SocketState state = func.Execute(p, s, client);

                OnSocketEventRaised(this, new EventArgCollection.SocketEventArgs()
                {
                    Msg = state.OutPutMsg
                });
            }
            catch (Exception Ex)
            {
                OnErrorRaised(this, new EventArgCollection.SocketErrorEventArgs()
                {
                    Exp = Ex
                });
            }
            finally
            {
                client.ContinueAsyncReceive(s);
                GC.WaitForFullGCComplete();
                GC.Collect();
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="func"></param>
        /// <param name="p"></param>
        /// <param name="s"></param>
        protected override void CommandWrapper(ICommandFunc func, CommunicateProtocol p, SocketState s)
        {
            try
            {
                func.profile = this.GetProfile(Settings.Default);
                SocketState state = func.Execute(p, s, listener);

                if (this.window != null)
                {
                    this.window.MessageCallBackMethod(state.OutPutMsg, null);
                }
            }
            catch (Exception Ex)
            {
                Logger.Write(LogType.Error, Ex.Message, Ex);

                if (this.window != null)
                {
                    this.window.MessageCallBackMethod(Ex.ToString(), null);
                }
            }
            finally
            {
                listener.ContinueAsyncReceive(s);
                GC.WaitForFullGCComplete();
                GC.Collect();
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="p"></param>
        /// <param name="s"></param>
        protected void ExecuteProtocolCommand(CommunicateProtocol p, SocketState s)
        {
            if (p == null)
            {
                throw new ArgumentNullException("CommunicateProtocol is null");
            }
            switch (p.Command)
            {
            case 0: CommandWrapper(((ICommandFunc) new Command0()), p, s); break;

            case 1: CommandWrapper(((ICommandFunc) new Command1()), p, s); break;

            case 2: CommandWrapper(((ICommandFunc) new Command2()), p, s); break;

            case 998: CommandWrapper(((ICommandFunc) new Command998()), p, s); break;

            case 999: CommandWrapper(((ICommandFunc) new Command999()), p, s); break;

            //
            case 1001: CommandWrapper(((ICommandFunc) new Command1001()), p, s); break;

            case 1002: CommandWrapper(((ICommandFunc) new Command1002()), p, s); break;

            //
            case 2001: CommandWrapper(((ICommandFunc) new Command2001()), p, s); break;

            case 2002: CommandWrapper(((ICommandFunc) new Command2002()), p, s); break;

            //
            case 3001: CommandWrapper(((ICommandFunc) new Command3001()), p, s); break;

            default: throw new Exception("Protocol type does not exist.");
            }
        }
Esempio n. 7
0
        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);
        }
Esempio n. 8
0
        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);
        }
Esempio n. 10
0
        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>
        /// AsyncReceivedCallback
        /// </summary>
        /// <param name="ar"></param>
        public void AsyncReceivedCallback(IAsyncResult ar)
        {
            SocketState state = (SocketState)ar.AsyncState;

            try
            {
                System.Net.Sockets.Socket client = state.workSocket;
                int bytesRead = client.EndReceive(ar);
                if (bytesRead > 0)
                {
                    Mark(string.Format("分析协议数据包"));
                    byte[] header_flag = new byte[2];
                    Array.Copy(state.buffer, 0, header_flag, 0, 2);
                    if (CommunicateProtocol.CheckFlag(header_flag))
                    {
                        client.BeginReceive(state.buffer, 0, 12, 0, new AsyncCallback(SyncExecuteProtocolCommand), state);
                    }
                    else
                    {
                        client.BeginReceive(state.buffer, 0, 2, 0, new AsyncCallback(AsyncReceivedCallback), state);
                    }
                }
                else
                {
                    WriteLog(null, "客户端" + IPAddress.Parse(((IPEndPoint)state.workSocket.RemoteEndPoint).Address.ToString()) + "连接异常");
                    SetConnection(false, state);
                }
            }
            catch (System.Net.Sockets.SocketException sktEx)
            {
                WriteLog(sktEx, sktEx.ErrorCode.ToString());
                SetConnection(false, state);
            }
            catch (Exception Ex)
            {
                WriteLog(Ex, Ex.Message);
                SetConnection(false, state);
            }
        }
Esempio n. 12
0
        /// <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 abstract void InvokeExecuteProtocolCommand(CommunicateProtocol protocol, SocketState state);
 /// <summary>
 ///
 /// </summary>
 /// <param name="func"></param>
 /// <param name="p"></param>
 /// <param name="s"></param>
 protected abstract void CommandWrapper(ICommandFunc func, CommunicateProtocol p, SocketState s);
Esempio n. 15
0
 public SocketState Execute(CommunicateProtocol protocol, SocketState state, IHSSocket sobj)
 {
     state.IsReceiveThreadAlive = false; // 接收完数据后立即解锁
     state.OutPutMsg            = string.Format("Command 0 :命令已弃用");
     return(state);
 }