예제 #1
0
        public void Start()
        {
            IsStart    = true;
            reTryCount = 0;

            ReceiveStage = YmodemReceiveStage.WaitForFileInfo;
            SendStage    = YmodemSendStage.WaitReceiveRequestFileInfo;


            TransThread = new Thread(new ThreadStart(TransThreadHandler));
            TransThread.IsBackground = true;
            TransThread.Name         = "YmodemTransThread";
            TransThread.Start();
            if (ymodemInfo.TransMode == TransmitMode.Receive)
            {
                if (StartReceive != null)
                {
                    StartReceive(ymodemInfo, null);
                }
            }
        }
예제 #2
0
        void ReceiveHandler()
        {
            if (ReceiveStage == YmodemReceiveStage.WaitForFileInfo || ReceiveStage == YmodemReceiveStage.WaitForFirstPacket)
            {
                SendKEYC();
            }

            if (msgQueue.Count > 0)
            {
                YmodemMessage msg = msgQueue.Dequeue();
                if (msg != null)
                {
                    reTryCount = 0;

                    switch (msg.MessageType)
                    {
                    case YmodemMessageType.PACKET:

                        PacketEventArgs e = msg.Value as PacketEventArgs;
                        if (ReceiveStage == YmodemReceiveStage.WaitForFileInfo)
                        {
                            if (e.PacketNo == 0)
                            {
                                ReceiveStage = YmodemReceiveStage.WaitForFirstPacket;
                                SendACK();

                                if (ReceivedPacket != null)
                                {
                                    ReceivedPacket(ymodemInfo, new PacketEventArgs(e.PacketNo, e.Packet));
                                }
                            }
                            //else
                            //{
                            //    SendNAK();
                            //}
                        }

                        else if (ReceiveStage == YmodemReceiveStage.WaitForFirstPacket ||
                                 ReceiveStage == YmodemReceiveStage.PacketReceiving)
                        {
                            if (ReceiveStage == YmodemReceiveStage.WaitForFirstPacket)
                            {
                                ReceiveStage = YmodemReceiveStage.PacketReceiving;
                            }

                            SendACK();

                            if (ReceivedPacket != null)
                            {
                                ReceivedPacket(ymodemInfo, new PacketEventArgs(e.PacketNo, e.Packet));
                            }

                            // 通知发下一包
                            if (SendNextPacket != null)
                            {
                                SendNextPacket(ymodemInfo, null);
                            }
                        }


                        break;

                    case YmodemMessageType.PACKET_ERROR:
                        SendNAK();
                        // 通知重发
                        if (ReSendPacket != null)
                        {
                            ReSendPacket(ymodemInfo, null);
                        }
                        break;

                    case YmodemMessageType.EOT:
                        SendACK();
                        // 通知完成
                        if (EndOfTransmit != null)
                        {
                            EndOfTransmit(ymodemInfo, null);
                        }
                        break;

                    case YmodemMessageType.CAN:
                        SendACK();
                        // 通知中止
                        if (AbortTransmit != null)
                        {
                            AbortTransmit(ymodemInfo, null);
                        }
                        break;

                    default:
                        break;
                    }
                }
            }
            else
            {
                if (waitReceiveEvent.WaitOne(3000))
                {
                    waitReceiveEvent.Reset();
                }
                else
                {
                    reTryCount++;
                    if (reTryCount > RetryMax)
                    {
                        IsStart = false;
                        //通知接收超时
                        if (TransmitTimeOut != null)
                        {
                            TransmitTimeOut(ymodemInfo, null);
                        }
                    }
                }
            }
        }