Ejemplo n.º 1
0
        public void SendFile()
        {
            int              bytesRead = 0;
            long             len       = new FileInfo(_filePath).Length;
            DtmFileInfoSruct flHdr     = new DtmFileInfoSruct(_filePath, len, 0);
            int              ckSize    = _clientSocket.SendBufferSize - (flHdr.GetHeaderSize() + DtmPacketStruct.GetHeaderSize());

            byte[] inputBuffer = new byte[ckSize];

            try
            {
                using (FileStream inStream = new FileStream(_filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    // loop through file
                    while ((bytesRead = inStream.Read(inputBuffer, 0, ckSize)) > 0)
                    {
                        // wrap in a file info; option flag is used for payload length
                        flHdr.OptionsFlag = bytesRead;
                        byte[] hdrArr = flHdr.ToBytes();

                        // add data
                        if (bytesRead == ckSize)
                        {
                            hdrArr = ArrayUtils.Concat(hdrArr, inputBuffer);
                        }
                        else
                        {
                            hdrArr = ArrayUtils.Concat(hdrArr, ArrayUtils.GetRange(inputBuffer, 0, bytesRead));
                        }

                        // encrypt the header and data
                        byte[] enc = SymmetricTransform(_fileSymProcessor, hdrArr);
                        // send to the remote host
                        Transmit(DtmPacketFlags.Transfer, (short)DtmTransferFlags.DataChunk, _fileId, new MemoryStream(enc));
                        // increment counter
                        _bytesSent += bytesRead;

                        // progress
                        if (ProgressPercent != null)
                        {
                            double progress = 100.0 * (double)_bytesSent / inStream.Length;
                            ProgressPercent(this, new System.ComponentModel.ProgressChangedEventArgs((int)progress, (object)inStream.Length));
                        }
                    }
                }
                // notify app
                if (FileTransferred != null)
                {
                    FileTransferred(this, new DtmPacketArgs((short)DtmTransferFlags.Sent, _fileId));
                }
            }
            catch (Exception ex)
            {
                throw new CryptoFileTransferException("DtmFileTransfer:SendFile", "The file transfer did not complete!", ex);
            }
            finally
            {
                // flush
                SendFlush();
            }
        }
Ejemplo n.º 2
0
        private void Receive(Stream PacketStream)
        {
            // get the packet header
            DtmPacketStruct prcPacket = new DtmPacketStruct(PacketStream);

            // read the packet
            byte[] enc = new byte[prcPacket.PayloadLength];
            // get the encrypted data
            PacketStream.Read(enc, 0, enc.Length);
            // decrypt it using file crypto processor
            byte[] dec = SymmetricTransform(_fileSymProcessor, enc);
            // get file info header
            DtmFileInfoSruct pktFi = new DtmFileInfoSruct(dec);
            // store file name and size
            string fileName  = pktFi.FileName;
            long   fileSize  = pktFi.FileSize;
            long   streamLen = 0;

            try
            {
                using (FileStream outStream = new FileStream(_tempPath, FileMode.Append, FileAccess.Write, FileShare.Read))
                {
                    // calculate offsets
                    int hdrSize = pktFi.GetHeaderSize();
                    int len     = dec.Length - hdrSize;
                    // write to file
                    outStream.Write(ArrayUtils.GetRange(dec, hdrSize, len), 0, len);
                    // store length
                    streamLen = outStream.Length;

                    // progress
                    if (ProgressPercent != null)
                    {
                        double progress = 100.0 * (double)pktFi.OptionsFlag / fileSize;
                        ProgressPercent(this, new System.ComponentModel.ProgressChangedEventArgs((int)progress, (object)fileSize));
                    }
                }

                // transfer completed
                if (streamLen == fileSize)
                {
                    // reset attributes
                    File.SetAttributes(_tempPath, File.GetAttributes(_tempPath) & ~FileAttributes.Hidden);
                    // rename the file
                    File.Move(_tempPath, VTDev.Libraries.CEXEngine.Tools.FileTools.GetUniqueName(_filePath));

                    // notify app
                    if (FileTransferred != null)
                    {
                        FileTransferred(this, new DtmPacketArgs((short)DtmTransferFlags.Received, prcPacket.OptionFlag));
                    }

                    // flush and close
                    ReceiveClose();
                }
            }
            catch (Exception ex)
            {
                throw new CryptoFileTransferException("DtmFileTransfer:Receive", "The file transfer did not complete!", ex);
            }
        }