Пример #1
0
        /// <summary>
        /// Uploads a file to the service and sends signal for service to send the file to a different client.
        /// </summary>
        public void UploadFile()
        {
            if (_receiver.Name!=null&&_viewModel.Name == _receiver.Name)
            {
                MessageBoxResult result = MessageBox.Show("Select a file receiver.");
                return;
            }
            _viewModel.FileWindowModel.DoneLabel = "Uploading to service";
            Thread thread = new Thread(() =>
            {
                string[] address = _viewModel.ConnectionIP.Split(':');
                string fileServicePath = "/WPFHost/http";
                var fileEndAdd = new EndpointAddress("http://" + address[0] + ":" + (int.Parse(address[1]) + 1111) + fileServicePath);
                var fileBinding = new BasicHttpBinding("httpBinding");
                _fileChannel = new FileTransferWcfClient(fileBinding, fileEndAdd);
                try
                {
                    _fileChannel.Open();
                    _fileChannel.SetProxy(_fileChannel.ChannelFactory.CreateChannel());
                    // get some info about the input file
                    FileInfo fileInfo = new FileInfo(_viewModel.FileWindowModel.SelectedFile);
                    _viewModel.FileWindowModel.ProgressMax = fileInfo.Length;
                    // open input stream
                    using (FileStream stream = new FileStream(_viewModel.FileWindowModel.SelectedFile, FileMode.Open, FileAccess.Read))
                    {
                        using (StreamWithProgress uploadStreamWithProgress = new StreamWithProgress(stream))
                        {
                            uploadStreamWithProgress.ProgressChanged += uploadStreamWithProgress_ProgressChanged;

                            // upload file
                            _fileChannel.UploadFile(fileInfo.Name, fileInfo.Length, uploadStreamWithProgress);
                        }
                    }
                    // close service client
                    _fileChannel.Close();
                    _channel.SendFileToClientAsync(_localClient,_receiver, fileInfo.Name);
                    _viewModel.FileWindowModel.DoneLabel = "Sending to client";
                }
                catch (Exception ex)
                {
                    _viewModel.Name = ex.ToString();
                }
            });
            thread.Start();
        }
Пример #2
0
        /// <summary>
        /// Opens progress windows, opens http channel, sends request for file download, saves file and tidies up.
        /// </summary>
        /// <param name="client">Client that sent the file.</param>
        /// <param name="filename">File that will be downloaded from service.</param>
        public void PrepareForFile(SharedComponents.Client client, string filename)
        {
            _viewModel.OpenProgressWindow();
            _viewModel.FileWindowModel.FileNameLabel = filename;
            Thread thread = new Thread(() =>
            {
                string[] address = _viewModel.ConnectionIP.Split(':');
                string fileServicePath = "/WPFHost/http";
                var fileEndAdd = new EndpointAddress("http://" + address[0] + ":" + (int.Parse(address[1]) + 1111) + fileServicePath);
                var fileBinding = new BasicHttpBinding("httpBinding");
                _fileChannel = new FileTransferWcfClient(fileBinding, fileEndAdd);
                try
                {
                    // start service client
                    _fileChannel.Open();
                    _fileChannel.SetProxy(_fileChannel.ChannelFactory.CreateChannel());
                    // kill target file, if already exists
                    // get stream from server

                    Stream inputStream;
                    long length = _fileChannel.DownloadFile(ref filename, out inputStream);
                    string filePath = Path.Combine("Download", filename);
                    // write server stream to disk
                    using (FileStream writeStream = new FileStream(filePath, FileMode.CreateNew, FileAccess.Write))
                    {
                        int chunkSize = 2048;
                        byte[] buffer = new byte[chunkSize];
                        _viewModel.FileWindowModel.ProgressMax = length;
                        do
                        {
                            // read bytes from input stream
                            int bytesRead = inputStream.Read(buffer, 0, chunkSize);
                            if (bytesRead == 0) break;

                            // write bytes to output stream
                            writeStream.Write(buffer, 0, bytesRead);

                            // report progress from time to time
                            _viewModel.FileWindowModel.Progress = writeStream.Position;
                        } while (true);

                        writeStream.Close();
                        Thread threadClose = new Thread(() =>
                        {
                            _viewModel.FileWindowModel.DoneLabel = "Done";
                           // _viewModel.CloseProgressWindow();
                        });
                        threadClose.SetApartmentState(ApartmentState.STA);
                        threadClose.Start();
                    }

                    // close service client
                    //inputStream.Close();
                    _fileChannel.Close();
                    _channel.DeleteFileAsync(client, Path.Combine("Upload", filename));
                }
                catch (Exception ex)
                {
                    _viewModel.Name = ex.ToString();
                }
            });
            thread.Start();
        }