Пример #1
0
        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);
        }
Пример #2
0
 public bool FileHasMoreParts(FileTransReq fileTrans)
 {
     if (fileTrans.curFilePartNum < fileTrans.TotalPartNum)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Пример #3
0
        private async Task <FileReceiveReq> CreateFileReceiveReq(FileReqMeta fileRequest, Metadata mMetadata)
        {
            //open stream for all files being received
            List <FileTransReq> fileTrans = new List <FileTransReq>();

            foreach (FileMeta file in fileRequest.Files)
            {
                FileTransReq fileTran = await SetupTransmitionForNewFile(file, fileRequest.BufferSize);

                fileTrans.Add(fileTran);
            }

            FileReceiveReq fileReceivedReq = new FileReceiveReq(fileTrans, mMetadata.SourceIp);

            return(fileReceivedReq);
        }
Пример #4
0
        public async Task WriteFilePartToFile(FilePartObj receivedFilePart)
        {
            FileTransReq fileTrans = GetFileTransReqFromFileMeta(receivedFilePart.FileMetadata);

            //validate packet recieved
            bool isValid = ValidFilePart(receivedFilePart);

            if (!isValid)
            {
                throw new FileTransitionError("file part received is not valided for this peer.");
            }

            //valid file part
            byte[] buffer = receivedFilePart.FileData;
            await fileTrans.WriteBytes(buffer);
        }
Пример #5
0
        //called when a file part is received
        private async Task ProcessFilePart(FilePartObj filePart, Metadata metadata)
        {
            //check if file part is valid
            if (filePart == null)
            {
                throw new Exception("filePart has not been set.");
            }

            //find file recieve request
            FileReceiveReq fileReceived = GetReceivedFileReqFromMeta(metadata);
            FileTransReq   fileTrans    = fileReceived.GetFileTransReqFromFileMeta(filePart.FileMetadata);

            //dump file data to disk
            await fileReceived.WriteFilePartToFile(filePart);

            //log incoming file
            FileProgUpdate?.Invoke(this, new FileTransferEventArgs(fileTrans, TransDirrection.receiving));
        }
Пример #6
0
        public async Task <FilePartObj> GetNextFilePart(FileTransReq mCurFileTrans)
        {
            //set buffer lengths
            int bufferLen = (int)Math.Min(mCurFileTrans.FileDetails.FileSize - mCurFileTrans.BytesProcessed, this.bufferSize);

            if (bufferLen <= 0)
            {
                //nothing more to be sent
                return(null);
            }
            FileMeta fileMetadata = mCurFileTrans.FileDetails;

            byte[] fileData = await mCurFileTrans.ReadBytes(bufferLen);


            //populate File part object
            FilePartObj filePart = new FilePartObj(fileMetadata, fileData, mCurFileTrans.curFilePartNum, mCurFileTrans.TotalPartNum);

            return(filePart);
        }
Пример #7
0
        private async Task <FileTransReq> SetupTransmitionForNewFile(FileMeta fileDetails, int bufferSize)
        {
            //create a folder to store the file
            IFolder root = await fileSystem.GetFolderFromPathAsync("./");

            if (await root.CheckExistsAsync(DefaultFilePath) == ExistenceCheckResult.NotFound)
            {
                //create folder
                await root.CreateFolderAsync(DefaultFilePath, CreationCollisionOption.FailIfExists);
            }
            IFolder tempFolder = await fileSystem.GetFolderFromPathAsync(DefaultFilePath);

            //create empty file stream
            IFile newFile = await tempFolder.CreateFileAsync(fileDetails.FileName, CreationCollisionOption.ReplaceExisting);

            Stream fileStream = await newFile.OpenAsync(PCLStorage.FileAccess.ReadAndWrite);

            //return file trans. object
            FileTransReq fileTrans = new FileTransReq(fileDetails, fileStream, bufferSize);

            return(fileTrans);
        }