Esempio n. 1
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);
 }
Esempio n. 2
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);
 }