예제 #1
0
        private void NodeReceiver_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            networkdata DType = (networkdata)e.Argument;

            try
            {
                if (Dclient.Connected)
                {
                    byte[] myReadBuffer = new byte[4100];
                    while ((Dstream.CanRead) && (!worker.CancellationPending))
                    {
                        Array.Clear(myReadBuffer, 0, 4100); //置零
                        int numberOfBytesRead = 0;
                        Dstream.Read(myReadBuffer, 0, 4);   //先读包头
                        UInt16 PacketLength = BitConverter.ToUInt16(myReadBuffer, 2);
                        UInt16 head         = BitConverter.ToUInt16(myReadBuffer, 0);
                        if (PacketLength > 4096)
                        {
                            continue;
                        }
                        if ((head != 0xABCD) && (head != 0xAD01) && (head != 0xAD02) && (head != 0xAD03) && (head != 0xAD04) &&
                            (head != 0xEDED) && (head != 0xEE01) && (head != 0xBB01) && (head != 0xACAC) && (head != 0x45FF))
                        {
                            continue;
                        }
                        // Incoming message may be larger than the buffer size.
                        do
                        {
                            int n = Dstream.Read(myReadBuffer, 4 + numberOfBytesRead, PacketLength - numberOfBytesRead);
                            numberOfBytesRead += n;
                        }while (numberOfBytesRead != PacketLength);
                        ParseNetworkPacket(myReadBuffer, PacketLength);
                    }
                    if (worker.CancellationPending)
                    {
                        e.Cancel = true;
                    }
                }
            }
            catch (SocketException MyEx)
            {
                e.Result = MyEx.ErrorCode;
                MainForm.ParseLock.ReleaseMutex();//ParseNetworkPacket中调用了lock
                AddtoBox(Color.Black, MyEx.Message + ":" + MyEx.StackTrace.ToString() + "\r\n/>");
                bConnect = false;
            }
            catch (IOException IOEx)
            {
                bConnect = false;
                AddtoBox(Color.Black, "网络连接关闭!\r\n/>");
                //SendStatusLabel("IO错误!网络连接关闭");
            }
        }
예제 #2
0
        private void NodeLinker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            if (e.Cancelled)
            {
                ConnNodeBtn.Text = "连接节点";
                string errstring = "操作已被取消!";
                SendStatusLabel(errstring);
                AddtoBox(Color.Black, errstring + "\r\n");
                bConnect = false;
                Tclient.Close();
                Dclient.Close();
            }
            else if (e.Result != null)
            {
                ConnNodeBtn.Text = "连接节点";
                string errstring = " 操作失败!" + "错误号:" + e.Result.ToString();
                SendStatusLabel(errstring);
                AddtoBox(Color.Black, errstring + "\r\n");
                bConnect = false;
                Tclient.Close();
                Dclient.Close();
            }
            else
            {
                bConnect         = true;
                ConnNodeBtn.Text = "断开连接";
                SendStatusLabel("已连接节点");
                AddtoBox(Color.Black, "已连接节点\r\n");
                if (Tclient.Connected)
                {
                    networkdata data = new networkdata();
                    data.DataType = 0;
                    CommAnsReceiver.RunWorkerAsync(data);
                }
                if (Dclient.Connected)
                {
                    networkdata data = new networkdata();
                    data.DataType = 1;
                    NodeReceiver.RunWorkerAsync(data);
                }
            }
        }
예제 #3
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();
            }
        }