Пример #1
0
        public void UploadFile(FileTransferMessage msg)
        {
            Stopwatch timer = new Stopwatch();

            timer.Start();
            string filename;

            switch (msg.Type)
            {
            case FileTransferMessage.FileType.Library:
                filename = Path.Combine(FilesPath, msg.Filename);
                break;

            case FileTransferMessage.FileType.Result:
                filename = Path.Combine(ResultsPath, msg.Filename);
                break;

            case FileTransferMessage.FileType.ConnectionTest:
                filename = Path.Combine(Directory.GetCurrentDirectory(), msg.Filename);
                break;

            default:
                return;
            }

            int BlockSize = 1024; byte[] block = new byte[BlockSize];

            if (!Directory.Exists(FilesPath))
            {
                Directory.CreateDirectory(FilesPath);
            }
            if (!Directory.Exists(ResultsPath))
            {
                Directory.CreateDirectory(ResultsPath);
            }
            using (FileStream outputStream = new FileStream(filename, FileMode.Create)) {
                int bytesRead;
                while (true)
                {
                    bytesRead = msg.TransferStream.Read(block, 0, BlockSize);
                    if (bytesRead > 0)
                    {
                        outputStream.Write(block, 0, bytesRead);
                    }
                    else
                    {
                        break;
                    }
                }
            }
            timer.Stop();
        }
Пример #2
0
        // streaming service methods region
        #region
        /// <summary> Sends file to host using StreamChannel</summary>
        /// <param name="FilePath">Specifies the path of the file to send</param>
        public void UploadFile(string FilePath, FileTransferMessage.FileType Type
                               , string ChannelName)
        {
            if (StreamingChannels[ChannelName] == null)
            {
                Console.Write("\n Unable to find the specifies channel \"{0}\"", ChannelName);
                return;
            }
            string Filename = Path.GetFileName(FilePath);

            using (var inputStream = new FileStream(FilePath, FileMode.Open)) {
                // Prepare file streaming message
                FileTransferMessage msg = new FileTransferMessage();
                msg.Type           = Type;
                msg.Filename       = Filename;
                msg.TransferStream = inputStream;
                // Send the message
                StreamingChannels[ChannelName].UploadFile(msg);
            }
        }
Пример #3
0
        public void upLoadFile(FileTransferMessage msg)
        {
            int totalBytes = 0;

            hrt.Start();
            filename = msg.filename;
            savePath = msg.savePath;
            string rfilename = Path.Combine(savePath, filename);

            if (!Directory.Exists(savePath))
            {
                Directory.CreateDirectory(savePath);
            }
            using (var outputStream = new FileStream(rfilename, FileMode.Create))
            {
                while (true)
                {
                    int bytesRead = msg.transferStream.Read(block, 0, BlockSize);
                    totalBytes += bytesRead;
                    if (bytesRead > 0)
                    {
                        outputStream.Write(block, 0, bytesRead);
                    }
                    else
                    {
                        break;
                    }
                }
            }
            hrt.Stop();


            Console.Write(
                "\n  Received file \"{0}\" of {1} bytes in {2} microsec.",
                filename, totalBytes, hrt.ElapsedMicroseconds
                );
        }