Пример #1
0
        override public bool pkt_proc(byte[] buf)
        {
            if (buf == null)
            {
                pktbuf = new pkt_rdq(dirname).pack();
                uc.Send(pktbuf, pktbuf.Length, r);
                return(true);
            }
            if (blkno == 0)
            {
                blkno++;
            }
            pkt_data pkt = new pkt_data();

            if (!pkt.parse(buf))
            {
                return(false);
            }
            if (pkt.blkno != (blkno & 0xffff))
            {
                return(true);  // ignore expired data?
            }
            filesize += pkt.data.Length;
            if (pkt.data.Length > 0)
            {
                dirinfo = Encoding.Default.GetString(pkt.data);
            }
            pktbuf = new pkt_ack(blkno++).pack();
            uc.Send(pktbuf, pktbuf.Length, r);
            return(false);
        }
Пример #2
0
 override public bool pkt_proc(byte[] buf)
 {
     if (blkno == 0)
     {
         pkt_wrq pkt = new pkt_wrq();
         if (!pkt.parse(buf))
         {
             return(false);
         }
         update_param(pkt.timeout * 1000 / Math.Max(idic["maxretry"], 1), pkt.blksize);
         if (File.Exists(sdic["cwd"] + pkt.filename) && !bdic["override"])
         {
             pktbuf = new pkt_err(Errcodes.FileAlreadyExists, pkt.filename).pack();
             uc.Send(pktbuf, pktbuf.Length, r);
             filename = pkt.filename; // set filename for log
             return(false);
         }
         write_file(pkt.filename);
         if (pkt.has_opt())
         {
             pkt.op = Opcodes.OAck;
             pktbuf = pkt.pack();
             blkno++; // wait for data blkno=1
         }
         else
         {
             pktbuf = new pkt_ack(blkno++).pack();
         }
     }
     else
     {
         pkt_data pkt = new pkt_data();
         if (!pkt.parse(buf))
         {
             return(false);
         }
         if (pkt.blkno != (blkno & 0xffff))
         {
             return(true);                                // ignore expired data?
         }
         filesize += pkt.data.Length;
         if (pkt.data.Length > 0)
         {
             while (q.produce(pkt.data) == 0)
             {
                 ;                              // infinit produce this data
             }
         }
         pktbuf = new pkt_ack(blkno++).pack();
         if (pkt.data.Length < idic["blksize"]) // stop
         {
             maxblkno = --blkno;
             q.stop();
             uc.Send(pktbuf, pktbuf.Length, r);
             return(false);
         }
     }
     curretry = 0;   // reset retry cnt
     return(uc.Send(pktbuf, pktbuf.Length, r) == pktbuf.Length);
 }
Пример #3
0
 override public bool pkt_proc(byte[] buf)
 {
     if (buf == null)
     {
         pktbuf = new pkt_rrq(remoteFile, tftpmode.ToString(), idic["timeout"] * idic["maxretry"] / 1000, idic["blksize"]).pack();
     }
     else if (blkno == 0 && (Opcodes)buf[1] == Opcodes.OAck) // oack
     {
         pkt_oack pkt = new pkt_oack();
         if (!pkt.parse(buf))
         {
             return(false);
         }
         update_param(pkt.timeout * 1000 / Math.Max(idic["maxretry"], 1), pkt.blksize);
         write_file(filename);
         pktbuf = new pkt_ack(blkno++).pack();
     }
     else
     {
         if (blkno == 0) // data blkno start with 1
         {
             write_file(filename);
             blkno++;
         }
         pkt_data pkt = new pkt_data();
         if (!pkt.parse(buf))
         {
             return(false);
         }
         if (pkt.blkno != (blkno & 0xffff))
         {
             return(true);                                // ignore expired data?
         }
         filesize += pkt.data.Length;
         if (pkt.data.Length > 0)
         {
             while (q.produce(pkt.data) == 0)
             {
                 ;                              // infinit produce this data
             }
         }
         pktbuf = new pkt_ack(blkno++).pack();
         if (pkt.data.Length < idic["blksize"]) // stop
         {
             maxblkno = --blkno;
             q.stop();
             uc.Send(pktbuf, pktbuf.Length, r);
             return(false);
         }
     }
     curretry = 0;   // reset retry cnt
     return(uc.Send(pktbuf, pktbuf.Length, r) == pktbuf.Length);
 }
Пример #4
0
 override public bool pkt_proc(byte[] buf)
 {
     byte[] data;
     if (buf == null)
     {
         pktbuf = new pkt_wrq(remoteFile, tftpmode.ToString(), idic["timeout"] * idic["maxretry"] / 1000, idic["blksize"]).pack();
     }
     else if (blkno == 0 && (Opcodes)buf[1] == Opcodes.OAck) // oack
     {
         pkt_oack pkt = new pkt_oack();
         if (!pkt.parse(buf))
         {
             return(false);
         }
         update_param(pkt.timeout * 1000 / Math.Max(idic["maxretry"], 1), pkt.blksize);
         read_file(filename);
         data   = q.consume(); //while ((data = q.consume()) == null) ; // may be infinite wait for a new msg here?
         pktbuf = new pkt_data(++blkno, data).pack();
     }
     else
     {
         if (blkno == 0)
         {
             read_file(filename);
         }
         pkt_ack pkt = new pkt_ack();
         if (!pkt.parse(buf))
         {
             return(false);
         }
         if (pkt.blkno != (blkno & 0xffff))
         {
             return(true);                   // ignore expired ack?
         }
         if (blkno == maxblkno && blkno > 0) // over
         {
             return(false);
         }
         if ((data = q.consume()) == null)
         {
             data = new byte[0]; // srv send last empty block; client write last block
         }
         pktbuf = new pkt_data(++blkno, data).pack();
     }
     curretry = 0;   // reset retry cnt
     return(uc.Send(pktbuf, pktbuf.Length, r) == pktbuf.Length);
 }
Пример #5
0
 override public bool pkt_proc(byte[] buf)
 {
     byte[] data;
     if (blkno == 0)
     {
         Opcodes op = (Opcodes)buf[1];
         if (op == Opcodes.Read)
         {
             pkt_rrq pkt = new pkt_rrq();
             if (!pkt.parse(buf))
             {
                 return(false);
             }
             update_param(pkt.timeout * 1000 / Math.Max(idic["maxretry"], 1), pkt.blksize);
             if (!File.Exists(sdic["cwd"] + pkt.filename))
             {
                 pktbuf = new pkt_err(Errcodes.FileNotFound, pkt.filename).pack();
                 uc.Send(pktbuf, pktbuf.Length, r);
                 filename = pkt.filename; // set filename for log
                 return(false);
             }
             read_file(pkt.filename);
             if (pkt.has_opt())
             {
                 pkt.op = Opcodes.OAck;
                 pktbuf = pkt.pack(); // wait for ack blkno=0
             }
             else
             {
                 data   = q.consume(); //while ((data = q.consume()) == null) ; // may be infinite wait for a new msg here?
                 pktbuf = new pkt_data(++blkno, data).pack();
             }
         }
         else // ack
         {
             pkt_ack pkt = new pkt_ack();
             if (!pkt.parse(buf))
             {
                 return(false);
             }
             if (pkt.blkno != (blkno & 0xffff))
             {
                 return(true);     // ignore expired ack?
             }
             data   = q.consume(); //while ((data = q.consume()) == null) ; // may be infinite wait for a new msg here?
             pktbuf = new pkt_data(++blkno, data).pack();
         }
     }
     else
     {
         pkt_ack pkt = new pkt_ack();
         if (!pkt.parse(buf))
         {
             return(false);
         }
         if (pkt.blkno != (blkno & 0xffff))
         {
             return(true);                   // ignore expired ack?
         }
         if (blkno == maxblkno && blkno > 0) // over
         {
             return(false);
         }
         if ((data = q.consume()) == null)
         {
             data = new byte[0]; // srv send last empty block; client write last block
         }
         pktbuf = new pkt_data(++blkno, data).pack();
     }
     curretry = 0;   // reset retry cnt
     return(uc.Send(pktbuf, pktbuf.Length, r) == pktbuf.Length);
 }