Пример #1
0
        static public FilePiece parse_packet(byte[] packet)
        {
            try
            {
                if (packet.Length < header_size)
                { // paket je prilis kratky
                    return(null);
                }
                Int32 data_length = packet.Length - 10;
                if (data_length < 0)
                { // subor bez dat
                    return(null);
                }

                Int16 status          = System.BitConverter.ToInt16(packet, 0);
                Int64 position_number = System.BitConverter.ToInt64(packet, 2);

                byte[] data = new byte[data_length];

                Buffer.BlockCopy(packet, 10, data, 0, data_length);

                FilePiece piece = new FilePiece(status, position_number, data);

                return(piece);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error Piece: " + ex.Message);
                return(new FilePiece(1, 0, new byte[0]));
            }
        }
Пример #2
0
 public bool SendingTCP(string SendingFile)
 {
     try
     {
         client    = new TcpClient(ipAddress, PORT + 1000);
         netstream = client.GetStream();
         FileStreamer file_stream = new FileStreamer(SendingFile);
         FileInfo     f           = new FileInfo(SendingFile);
         file_stream = new FileStreamer(SendingFile);
         byte[]    bytes = new byte[FilePiece.data_size];
         FilePiece fp    = file_stream.GetNextPiece();
         if (fp != null)
         {
             bytes = fp.data;
         }
         while (fp != null && bytes != null)
         {
             netstream.Write(bytes, 0, (int)bytes.Length);
             fp = file_stream.GetNextPiece();
             if (fp != null)
             {
                 bytes = fp.data;
             }
         }
         netstream.Write(new byte[0], 0, 0);
         netstream.Close();
         client.Close();
         return(true);
     }
     catch
     {
         return(false);
     }
 }
Пример #3
0
        public FilePiece GetNextPiece()
        {
            byte[]    b     = GetNextChunk();
            FilePiece piece = new FilePiece(1, 2, b);;

            try
            {
                if (b != null)
                {
                    double preresult = fs.Position / FilePiece.data_size;
                    if (fs.Position % b.Length != 0)
                    {
                        preresult = preresult + 0.5;
                    }
                    double result = Math.Round(Convert.ToDouble(preresult - 1), MidpointRounding.AwayFromZero);


                    piece = new FilePiece(1, Convert.ToInt64(result), b);
                }
                else
                {
                    piece = null;
                }
                return(piece);
            }

            catch (Exception ex)
            {
                MessageBox.Show("Error File Streamer: " + ex.Message);
                return(piece);
            }
        }