示例#1
0
        public static SendFile CreateFromString(String strPath)
        {
            if (!File.Exists(strPath))
            {
                return(null);
            }

            SendFile res = new SendFile()
            {
                FullPath = strPath,
                FileName = Path.GetFileName(strPath),
                FileType = MimeMapping.GetMimeMapping(Path.GetFileName(strPath)),
                FileID   = --s_nFileIDCounter,
                Status   = EStatus.WAITING
            };

            try
            {
                using (File.OpenRead(strPath)) { }
                res.FileSize = new FileInfo(strPath).Length;
            }
            catch (Exception e)
            {
                Log.w(TAG, "Unable to open file " + res.FileName + " - " + e.Message, true);
                return(null);
            }
            if (res.FileSize <= 0)
            {
                Log.w(TAG, res.FileName + " is empty, skipping upload", true);
                return(null);
            }
            return(res);
        }
示例#2
0
        private void StartNextFile(BbRemoteServer server)
        {
            SendFile next = m_liSendFiles.Find(x => x.Status == SendFile.EStatus.WAITING);

            if (next != null)
            {
                next.Status = SendFile.EStatus.REQUESTED;
                server.SendFileStart(next.FileName, next.FileType, next.FileID, (ulong)next.FileSize);
                Log.s(TAG, $"Waiting for remote client to accept file: {next.FileName}");
            }
        }
示例#3
0
        public async void onSendFileEvent(object sender, SendFileEventArgs eventArgs)
        {
            SendFile file = m_liSendFiles.Find(x => x.FileID == eventArgs.FileID);

            if (file != null)
            {
                if (eventArgs.FileEvent == SendFileEventArgs.EFileEvent.CANCELFILE)
                {
                    if (String.IsNullOrWhiteSpace(eventArgs.ErrorMessage))
                    {
                        Log.w(TAG, "Remote device requested to cancel upload of file " + file.FileName, true);
                    }
                    else
                    {
                        Log.w(TAG, "Remote device cancelled upload of file " + file.FileName + " because of an error: " + eventArgs.ErrorMessage, true);
                    }

                    file.Cancel();
                    m_liSendFiles.Remove(file);
                }
                else if (eventArgs.FileEvent == SendFileEventArgs.EFileEvent.ACCEPT && file.Status == SendFile.EStatus.REQUESTED)
                {
                    file.Status = SendFile.EStatus.ACCEPTED;
                    try
                    {
                        await file.UploadFile((BbRemoteServer)sender);
                    }
                    catch (OperationCanceledException) { } // thats fine, nothing else to do
                    catch (Exception e)
                    {
                        if (file.Status == SendFile.EStatus.TRANSFERRING)
                        {
                            Log.e(TAG, "Error while sending file " + file.FileName + " - " + e.Message, true);
                            ((BbRemoteServer)sender).SendFileCancel(file.FileID, true);
                            file.Cancel();
                        }
                    }
                    m_liSendFiles.Remove(file);
                    StartNextFile((BbRemoteServer)sender);
                    return;
                }
            }
            else
            {
                Log.w(TAG, "Received file command for unknown FileID " + eventArgs.FileID);
            }
        }
示例#4
0
        public void SendFiles(String[] aFiles, BbRemoteServer server)
        {
            int nActiveFiles = m_liSendFiles.Count(x => x.Status != SendFile.EStatus.WAITING);

            foreach (String file in aFiles)
            {
                if (!String.IsNullOrEmpty(file))
                {
                    SendFile newFile = SendFile.CreateFromString(file);
                    if (newFile != null)
                    {
                        m_liSendFiles.Add(newFile);
                    }
                }
            }
            if (nActiveFiles == 0)
            {
                StartNextFile(server);
            }
        }