Example #1
0
        /// <summary>
        /// 发送网络命令包
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        ///
        public bool SendData(byte[] command)
        {
            try
            {
                if (Dstream != null)
                {
                    if (Dstream.CanWrite)
                    {
                        Dstream.Write(command, 0, command.Length);
                        NetCmdFile.OpenFile(MainForm.pMainForm.NetCmdDataPathInfo);
                        NetCmdFile.BinaryWrite(command);
                        NetCmdFile.close();
                        return(true);
                    }
                    return(false);
                }
                return(false);
            }
            catch (Exception MyEx)
            {
                SendStatusLabel(MyEx.Message);

                AddtoBox(Color.Black, MyEx.Message + ":" + MyEx.StackTrace + "\r\n");
                return(false);
            }
        }
Example #2
0
        private void DLoadDataWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            networkdata      data   = (networkdata)e.Argument;

            try
            {
                filereader = new BinaryReader(File.Open(data.FileNamePath, FileMode.Open));
                FileInfo d          = new FileInfo(data.FileNamePath);
                long     filelength = d.Length;
                if (ACPacketHandle == null)
                {
                    ACPacketHandle = new EventWaitHandle(false, EventResetMode.AutoReset);
                }
                if (Dclient.Connected)
                {
                    byte[] mySendBuffer = new byte[1028];
                    Int64  SendBytes    = 0;
                    if (Dstream.CanWrite)
                    {
                        int  numberOfBytesRead = 0;
                        uint head = 0xDADA;
                        Buffer.BlockCopy(BitConverter.GetBytes(head), 0, mySendBuffer, 0, 2);
                        while (((numberOfBytesRead = filereader.Read(mySendBuffer, 4, 1024)) != 0) && (!worker.CancellationPending))
                        {
                            Buffer.BlockCopy(BitConverter.GetBytes(numberOfBytesRead), 0, mySendBuffer, 2, 2);
                            Dstream.Write(mySendBuffer, 0, numberOfBytesRead + 4);//
                            SendBytes        += numberOfBytesRead;
                            numberOfBytesRead = 0;
                            worker.ReportProgress((int)((double)SendBytes * 100 / filelength));
                            if (!ACPacketHandle.WaitOne(10000))//等待信号超时
                            {
                                Exception MyEx = new Exception("接收应答数据超时!");
                                throw MyEx;
                            }
                            if (worker.CancellationPending)
                            {
                                e.Cancel = true;
                                break;
                            }
                        }
                        if (worker.CancellationPending)
                        {
                            e.Cancel = true;
                        }
                        filereader.BaseStream.Seek(0, SeekOrigin.Begin);//回到文件头
                        byte[] totalb = filereader.ReadBytes((int)filereader.BaseStream.Length);
                        ushort crc    = CRCHelper.CRC16byte(totalb);
                        filereader.Close();
                        head = 0xEDED;
                        Buffer.BlockCopy(BitConverter.GetBytes(head), 0, mySendBuffer, 0, 2);
                        Buffer.BlockCopy(BitConverter.GetBytes(crc), 0, mySendBuffer, 2, 2);
                        Dstream.Write(mySendBuffer, 0, 4);
                    }
                }
            }
            catch (Exception MyEx)
            {
                MessageBox.Show(MyEx.Message);
                AddtoBox(Color.Black, MyEx.Message + ":" + MyEx.StackTrace.ToString() + "\r\n/>");
                filereader.Close();
            }
        }