예제 #1
0
파일: WRQ.cs 프로젝트: pascal-canuel/TFTP
        public override void Thread()
        {
            FileStream fs;
            int        nOctetsLus = 0, nTimeOuts = 0, nErreurACK = 0;

            try
            {
                WRQrequest();
                m_socket.SendTo(m_tamponEnvoi, 2 + m_nomFichier.Length + 1 + 8 + 1, SocketFlags.None, m_PointDistant);

                if (!File.Exists(FullPath))
                {
                    Output.Text("File not found");
                    SendError(CodeErreur.FileNotFound);
                }
                else
                {
                    //WRQ

                    fs = File.Open(FullPath, FileMode.Open, FileAccess.Read, FileShare.Read);

                    Send();
                    do
                    {
                        nOctetsLus = fs.Read(m_tamponEnvoi, 4, 512);

                        do
                        {
                            if (m_lire = !m_socket.Poll(5000000, SelectMode.SelectRead))
                            {
                                nTimeOuts++;
                            }
                            else
                            {
                                m_socket.ReceiveFrom(m_tamponReception, ref m_PointDistant);
                                if (!Receive(m_tamponReception))
                                {
                                    m_lire = false;
                                    nErreurACK++;
                                }
                                else
                                {
                                    m_lire = true;

                                    m_socket.SendTo(m_tamponEnvoi, nOctetsLus + 4, SocketFlags.None, m_PointDistant);
                                    Send();

                                    if (nOctetsLus < 512)
                                    {
                                        if (m_lire = !m_socket.Poll(5000000, SelectMode.SelectRead))
                                        {
                                            nTimeOuts++;
                                        }
                                        else
                                        {
                                            m_socket.ReceiveFrom(m_tamponReception, ref m_PointDistant);
                                            if (!Receive(m_tamponReception))
                                            {
                                                m_lire = false;
                                                nErreurACK++;
                                            }
                                            else
                                            {
                                                m_lire = true;
                                            }
                                        }
                                    }
                                }
                            }
                        }while (m_lire == false && nTimeOuts < 10 && nErreurACK < 3);
                    }while (nOctetsLus == 512 && nTimeOuts < 10 && nErreurACK < 3);
                    Output.Text($"  WRQ closed from IP: {((IPEndPoint)PointDistant).Address} Port: {((IPEndPoint)PointDistant).Port} ---> {m_strFichier}");
                    fs.Close();
                    m_socket.Close();
                }
            }
            catch (SocketException se)
            {
                Output.Text(se.Message);
            }
        }