private async Task <FileSentReq> CreateFileSendReq(string ipAddress, List <string> filePaths, int bufferSize) { List <FileTransReq> fileTransReqs = new List <FileTransReq>(); //create a fileTrans for each file foreach (string filePath in filePaths) { //TODO: handle folders as well as files //get file details IFile file = await fileSystem.GetFileFromPathAsync(filePath); //TODO: check if file is already open Stream fileStream; try { fileStream = await file.OpenAsync(PCLStorage.FileAccess.Read); } catch { //can't find file throw new FileNotFound("Can't access the file: " + filePath); } //store away file details FileTransReq fileTransReq = new FileTransReq(file, fileStream, bufferSize); fileTransReqs.Add(fileTransReq); } FileSentReq fileSendReq = new FileSentReq(fileTransReqs, bufferSize, ipAddress); return(fileSendReq); }
private async Task ProcessFileReqAck(ReqAck mFileReqAck, Metadata mMetadata) { //see if received accepted the file transmition request bool acceptedReq = mFileReqAck.AcceptedFile; FileSentReq fileSendReq = GetSendFileReqFromMeta(mMetadata); if (!acceptedReq) { //didn't accept request sendFileRequests.Remove(fileSendReq); return; } //start sending the file parts foreach (FileTransReq fileTrans in fileSendReq.FileTransReqs) { //for each file while (fileSendReq.FileHasMoreParts(fileTrans)) { //send all its parts FilePartObj filePart = await fileSendReq.GetNextFilePart(fileTrans); await ObjectManager.SendAsyncTCP(fileSendReq.targetIpAddress, filePart); //send update event FileProgUpdate?.Invoke(this, new FileTransferEventArgs(fileTrans, TransDirrection.sending)); } } }
//TODO: update description /// <summary> /// send file to the peer with the given IP address via a reliable TCP connection. /// Works by breaking down the file into blocks each of length <C>bufferSize</C>. Each block is /// then compressed and sent one by one to the other peer. /// /// Note: can only send one file request at a time. Wait until This function has triggered an /// file received event before continuing /// </summary> /// <param name="ipAddress">The IP address of the peer to send the message to</param> /// <param name="filePath">The path to the file you want to send</param> /// <param name="bufferSize"> /// Using a small buffer size will trigger <c>FileProgUpdate</c>> more but /// will also increase buffer overhead. Buffer size is also the max amount of memory /// a file will occupy in RAM. /// </param> /// <returns></returns> public async Task SendFileAsync(string ipAddress, List <string> filePaths, int bufferSize = 100 * 1024) { //TODO: check if there is an active sendFile request to the target ip address //create a file send request FileSentReq fileSentRequest = await CreateFileSendReq(ipAddress, filePaths, bufferSize); sendFileRequests.Add(fileSentRequest); //send file request metadata to receiver FileReqMeta fileMetadata = fileSentRequest.GenerateMetadataRequest(); await ObjectManager.SendAsyncTCP(ipAddress, fileMetadata); //receiver will then send back a acceptance message which is proccessed in ProcessAckMessage() }