private void HandleFileUpload(NetworkStream networkStream) { var header_len_bytes = new byte[sizeof(int)]; networkStream.Read(header_len_bytes, 0, header_len_bytes.Length); var header_len = BitConverter.ToInt32(header_len_bytes, 0); var header_bytes = new byte[header_len]; networkStream.Read(header_bytes, 0, header_bytes.Length); var header = (ClientUploadFileHeader)SerializationMethods.Deserialize(header_bytes); var session = new FileUploadSession(FileDB, header.Filename); var totalBytesRead = 0; while (totalBytesRead < header.FileSize) { var data = new byte[BlockSize]; var bytesRead = networkStream.Read(data, 0, data.Length); session.WriteToFile(data, bytesRead); totalBytesRead += bytesRead; } session.TerminateSession(); FileDB.FileEntries.Add(new FileEntry { Name = header.Filename, Size = header.FileSize }); var response_buffer = BitConverter.GetBytes((int)ResponeType.Success); networkStream.Write(response_buffer, 0, sizeof(int)); NewFileUploaded?.Invoke(this, new NewFileUploadedEventArgs { FileName = header.Filename, FileSize = header.FileSize }); }
private static ClientDownloadFileHeader GetClientDownloadFileHeader(NetworkStream networkStream) { var headerLengthBytes = new byte[sizeof(int)]; networkStream.Read(headerLengthBytes, 0, headerLengthBytes.Length); var headerLength = BitConverter.ToInt32(headerLengthBytes, 0); var headerBytes = new byte[headerLength]; networkStream.Read(headerBytes, 0, headerBytes.Length); var header = (ClientDownloadFileHeader)SerializationMethods.Deserialize(headerBytes); return(header); }
// Update the list of files displayed on the Download Panel private void UpdateFileList() { try { var clientSocket = new TcpClient(ServerIPAddress, ServerPortNumber); var networkStream = clientSocket.GetStream(); var data = BitConverter.GetBytes((int)RequestType.GetList); networkStream.Write(data, 0, data.Length); var response = new byte[sizeof(int)]; networkStream.Read(response, 0, response.Length); var data_length = BitConverter.ToInt32(response, 0); data = new byte[data_length]; networkStream.Read(data, 0, data.Length); var entries = (List <FileEntry>)SerializationMethods.Deserialize(data); foreach (var entry in entries) { AddOnUI(FilesCollection, entry); } } catch (SocketException) { MessageBox.Show("Could not connect to the server, please try again", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
// Download a file from the shared drive private void DownloadFile(string fileToDownload, IProgress <ProgressModel> progress) { NetworkStream networkStream = null; FileStream fileStream = null; try { var report = new ProgressModel() { PctCompleted = 0, Filename = fileToDownload }; var socket = new TcpClient(ServerIPAddress, ServerPortNumber); networkStream = socket.GetStream(); // Send request var request_bytes = BitConverter.GetBytes((int)RequestType.DownloadFile); networkStream.Write(request_bytes, 0, request_bytes.Length); // Create and send request header var clientHeader = new ClientDownloadFileHeader() { Filename = fileToDownload }; var clientHeaderBytes = SerializationMethods.Serialize(clientHeader); var clientHeaderLengthBytes = BitConverter.GetBytes(clientHeaderBytes.Length); networkStream.Write(clientHeaderLengthBytes, 0, clientHeaderLengthBytes.Length); networkStream.Write(clientHeaderBytes, 0, clientHeaderBytes.Length); // Check server response var responseBytes = new byte[sizeof(int)]; networkStream.Read(responseBytes, 0, responseBytes.Length); var response = BitConverter.ToInt32(responseBytes, 0); if (response == (int)ResponeType.Failure) { throw new FileDownloadException(); } // Receive server header var serverHeaderSizeBytes = new byte[sizeof(int)]; networkStream.Read(serverHeaderSizeBytes, 0, serverHeaderSizeBytes.Length); var serverHeaderBytes = new byte[BitConverter.ToInt32(serverHeaderSizeBytes, 0)]; networkStream.Read(serverHeaderBytes, 0, serverHeaderBytes.Length); var serverHeader = (ServerDownloadFileHeader)SerializationMethods.Deserialize(serverHeaderBytes); var receivedBlockBuffer = new byte[BlockSize]; fileStream = File.OpenWrite(DownloadDirectory + fileToDownload); int totalBytesRead = 0; var fileSize = serverHeader.FileSize; while (totalBytesRead < fileSize) { var buffer = new byte[BlockSize]; var bytesRead = networkStream.Read(buffer, 0, buffer.Length); totalBytesRead += bytesRead; fileStream.Write(buffer, 0, bytesRead); var bytesReadRatio = (double)totalBytesRead / serverHeader.FileSize; report.PctCompleted = (int)(bytesReadRatio * 100); progress.Report(report); } } catch (FileDownloadException) { MessageBox.Show("The requested file cannot be downloaded at this time"); } catch (Exception e) { MessageBox.Show(e.Message); } finally { if (fileStream != null) { fileStream.Close(); } networkStream.Close(); } }