public static bool upload(string fileHash, NetworkStream serverStream, string peerName) { Configuration.writeLog("Upload module started."); bool successfulTransfer = false; byte[] byteSend = new byte[8096]; //byte[] byteSend = new byte[1450]; string filePath = ""; string fileName = ""; long fileSize = 0; TransferDetail tdCurrent = new TransferDetail(fileHash, 'u'); tdCurrent.peerName = peerName; //Get file path from hash. //TODO - Add error checking here.!! filePath = (string)Index.filehashes[fileHash]; FileStream fstFile; string[] temp; try { if (filePath == null) { Configuration.writeLog("upload : Incorrect args passed."); return false; } temp = filePath.Split('\\'); fileName = temp[temp.Length - 1]; //Start reading the file as binary fstFile = null; fstFile = new FileStream(filePath, FileMode.Open, FileAccess.Read); } //TODO - Put better exception handling catch (Exception e) { Configuration.writeLog("Upload module reported error : " + e.ToString()); sendMessage("404", serverStream); serverStream.Close(); return false; } BinaryReader binFile = new BinaryReader(fstFile); FileInfo fInfo = new FileInfo(filePath); Configuration.writeLog("Upload module : Started reading file from disk : " + filePath); Thread.Sleep(1000); fileName = fInfo.Name; fileSize = fInfo.Length; long bytesUploaded = 0; TimeSpan duration = new TimeSpan(); float tempTransferRate = 0; try { Configuration.writeLog("Upload module : Sending file details.."); sendMessage(fileName + "\n" + fileSize.ToString() + "\n", serverStream); tdCurrent.newTransfer(fileName, fileSize, "", ""); passiveWorker.ReportProgress(0, tdCurrent); Configuration.writeLog("Upload module : Sending the file " + fileName + "(" + fileSize + ")"); int bytesSize = 0; //Changes how fast the upload progress in the UI changes. int displayRefreshFactor = 10; int currentCycle = 0; DateTime startTime = DateTime.Now; DateTime currentTime; //Send the file. while ((bytesSize = fstFile.Read(byteSend, 0, byteSend.Length)) > 0) { //TODO - Remove the next line, it's for simulation only. //Thread.Sleep(30); currentCycle = currentCycle + 1; bytesUploaded = bytesUploaded + bytesSize; serverStream.Write(byteSend, 0, bytesSize); if (currentCycle == displayRefreshFactor) { tempTransferRate = (float)(1.0 * bytesUploaded / (1024 * duration.Seconds)); tdCurrent.updateTransferProgress(bytesUploaded, tempTransferRate, 0); passiveWorker.ReportProgress(0, tdCurrent); currentCycle = 0; } currentTime = DateTime.Now; duration = currentTime - startTime; } tdCurrent.updateTransferProgress(bytesUploaded, tempTransferRate, 0); tdCurrent.status = ("Uploaded in " + duration.Seconds.ToString() + " secs"); passiveWorker.ReportProgress(0, tdCurrent); Configuration.writeLog("Upload module : Sent file " + fileName); successfulTransfer = true; } catch (Exception e) { Configuration.writeLog("Upload module reported error : " + e.ToString()); tdCurrent.updateTransferProgress(bytesUploaded, tempTransferRate, 0); tdCurrent.status = ("Upload error."); passiveWorker.ReportProgress(0, tdCurrent); successfulTransfer = false; } finally { serverStream.Close(); fstFile.Close(); } return successfulTransfer; }
public static string download(TcpClient tcpClient, NetworkStream clientStream, string fileHash, string peerIP, string peerName) { //Downloads a file from peer. Returns the path of the downloaded file. FileStream strLocal = null; string fileName = ""; bool successfulTransfer = false; long fileSize = 0; TransferDetail tdCurrent = new TransferDetail(fileHash, 'd'); try { //Setting state of file to 'downloading' userDownloadState[fileHash] = 1; // For holding the number of bytes we are reading at one time from the stream int bytesSize = 0; byte[] downBuffer = new byte[4096]; //byte[] downBuffer = new byte[1300]; Configuration.writeLog("Download module : Getting file details.."); string instruction_params = receiveMessage(clientStream); //TODO - If 404, tell user better instead of silently failing if (instruction_params == "404") { Configuration.writeLog("Download module : Peer reported file not found."); return "ERROR"; } string[] instructions = instruction_params.Split('\n'); if(instructions.Length<2) { Configuration.writeLog("download : Incorrect params passed " + instruction_params); } fileName = instructions[0]; fileSize = Convert.ToInt64(instructions[1]); Configuration.writeLog("Download module : Beginning download of " + fileName + " (" + fileSize + ") bytes"); //Reporting addition to UI. tdCurrent.newTransfer(fileName, fileSize, peerIP, peerName); passiveWorker.ReportProgress(0, tdCurrent); long bytesUploaded = 0; int displayRefreshFactor = (int)fileSize/1000000; //Changes how fast the download progress in the UI changes. int currentCycle = 0; DateTime startTime = DateTime.Now; DateTime currentTime; TimeSpan duration = new TimeSpan(); float tempTransferRate = 0; strLocal = new FileStream(Configuration.downloadFolder + "\\" + fileName, FileMode.Create, FileAccess.Write, FileShare.ReadWrite); //Performing file transfer. while ((bytesSize = clientStream.Read(downBuffer, 0, downBuffer.Length)) > 0) { //In case user cancels download. if ((int)userDownloadState[fileHash] == 0) { tdCurrent.systemStatus = "CANCELED"; tdCurrent.status = "Canceled."; passiveWorker.ReportProgress(0, tdCurrent); return "ERROR"; } currentCycle = currentCycle + 1; bytesUploaded = bytesUploaded + bytesSize; strLocal.Write(downBuffer, 0, bytesSize); //TODO: Make this fixed update - every half a second or so, and not dependent on file //size. if (currentCycle == displayRefreshFactor) { tempTransferRate = (float)(1.0 * bytesUploaded / (1024 * duration.Seconds)); tdCurrent.updateTransferProgress(bytesUploaded, tempTransferRate, 0); passiveWorker.ReportProgress(0, tdCurrent); currentCycle = 0; } currentTime = DateTime.Now; duration = currentTime - startTime; } tdCurrent.updateTransferProgress(bytesUploaded, tempTransferRate, 0); tdCurrent.status=("Downloaded in " + duration.Seconds.ToString() + " secs"); passiveWorker.ReportProgress(0, tdCurrent); successfulTransfer = true; strLocal.Close(); } catch (Exception e) { successfulTransfer = false; Configuration.writeLog("Download module reported error : " + e.ToString()); tdCurrent.transferRate = 0; tdCurrent.status = "Download failed."; } finally { clientStream.Close(); } if (successfulTransfer) { Configuration.writeLog("Download module : Successfully downloaded " + fileName + "(" + fileSize + ")"); } return Configuration.downloadFolder + "\\" + fileName; }