Exemplo n.º 1
0
        private void FinalizeTransfer(string trId, FileTransferState newState)
        {
            TransferItem finalizingTransfer =
                incoming.GetTransferItem(trId) == null?outgoing.GetTransferItem(trId) : incoming.GetTransferItem(trId);

            FinalizeTransfer(finalizingTransfer, newState);
        }
Exemplo n.º 2
0
        // DFT:TransferID:Data
        private void ProcessIncomingDataFrame(string commandText)
        {
            byte[]       buf;
            string[]     commandParts = commandText.Split(TRANSFERS_COMMAND_SEPARATOR.ToCharArray());
            TransferItem fileTransfer = incoming.GetTransferItem(commandParts[0]);

            if (fileTransfer == null)
            {
                StopTransfer(commandParts[0], FileTransferState.StoppedByError, false);
                return;
            }

            try
            {
                buf = Convert.FromBase64String(commandParts[1]);
                fileStreams[fileTransfer.TrId].Write(buf, 0, buf.Length);
                fileStreams[fileTransfer.TrId].Flush();
            }
            catch (Exception ex)
            {
                if (ex is ArgumentNullException || ex is FormatException || ex is IOException || ex is ObjectDisposedException)
                {
                    StopTransfer(fileTransfer.TrId, FileTransferState.StoppedByError, false);
                    return;
                }
                else
                {
                    throw;
                }
            }

            incoming.CurrentFile          = fileTransfer;
            fileTransfer.TransferedBytes += buf.Length;
            incoming.TransferedBytes     += buf.Length;
            SendAcknowledgement(fileTransfer.TrId);

            if (fileTransfer.TransferedBytes >= fileTransfer.FileSize)
            {
                fileStreams[fileTransfer.TrId].Close();
                if (incoming.CurrentIndex != incoming.MaxIndex)
                {
                    incoming.CurrentIndex++;
                }

                if (Helpers.GetMD5HashFromFile(fileTransfer.FilePath) != fileTransfer.FileMD5)
                {
                    StopTransfer(fileTransfer.TrId, FileTransferState.Corrupted, false);
                }
                else
                {
                    FinalizeTransfer(fileTransfer, fileTransfer.State = FileTransferState.SuccessfullyEnded);
                }
            }
        }
Exemplo n.º 3
0
        // RAC:TransferID
        private void StartOutgoingTransfer(string commandText, string receiverURI)
        {
            string[]     commandParts = commandText.Split(TRANSFERS_COMMAND_SEPARATOR.ToCharArray());
            TransferItem fileTransfer = outgoing.GetTransferItem(commandParts[0]);

            fileStreams.Add(fileTransfer.TrId, new FileStream(fileTransfer.FilePath, FileMode.Open, FileAccess.Read));
            fileTransfer.State        = FileTransferState.Accepted;
            fileTransfer.OtherSideURI = receiverURI;
            outgoing.TotalBytes      += fileTransfer.FileSize;
            if (outgoing.Files.FirstOrDefault(x => x.State == FileTransferState.Active) == null)
            {
                fileTransfer.State = FileTransferState.Active;
                ProcessContinueTransfer(fileTransfer.TrId);
            }
        }
Exemplo n.º 4
0
        // GDF:TransferID
        private void ProcessContinueTransfer(string trID)
        {
            int readBytes;

            byte[] buf;
            string base64Buf;

            TransferItem fileTransfer = outgoing.GetTransferItem(trID);

            if (fileTransfer == null)
            {
                StopTransfer(trID, FileTransferState.StoppedByError, true);
                return;
            }

            outgoing.TransferedBytes    += fileStreams[fileTransfer.TrId].Position - fileTransfer.TransferedBytes;
            fileTransfer.TransferedBytes = fileStreams[fileTransfer.TrId].Position;

            if (fileStreams[fileTransfer.TrId].Position < fileStreams[fileTransfer.TrId].Length)
            {
                buf = new byte[DATA_FRAME_SIZE];
                try
                {
                    readBytes = fileStreams[fileTransfer.TrId].Read(buf, 0, buf.Length);
                }
                catch (IOException)
                {
                    StopTransfer(fileTransfer.TrId, FileTransferState.StoppedByError, true);
                    SwitchToActiveTransfer();
                    return;
                }

                base64Buf = Convert.ToBase64String(buf, 0, readBytes);

                // DFT:TransferID:Data
                SendData(TransferCommands.DataFrameTansfer + TRANSFERS_COMMAND_SEPARATOR +
                         fileTransfer.TrId.ToString() + TRANSFERS_COMMAND_SEPARATOR + base64Buf);
                outgoing.CurrentFile = fileTransfer;
            }
            else
            {
                FinalizeTransfer(fileTransfer, FileTransferState.SuccessfullyEnded);
                SwitchToActiveTransfer();
            }
        }
Exemplo n.º 5
0
        private void SwitchToActiveTransfer()
        {
            //* If we already have an active transfer, do nothing.
            TransferItem tempItem = outgoing.Files.FirstOrDefault(x => x.State == FileTransferState.Active) as TransferItem;

            if (tempItem != null)
            {
                return;
            }

            tempItem = outgoing.Files.FirstOrDefault(x => x.State == FileTransferState.Accepted) as TransferItem;

            if (tempItem != null)
            {
                tempItem.State = FileTransferState.Active;
                outgoing.CurrentIndex++;
                ProcessContinueTransfer(tempItem.TrId);
            }
        }
Exemplo n.º 6
0
        private void FinalizeTransfer(TransferItem finalizingTransfer, FileTransferState newState)
        {
            if (finalizingTransfer == null)
            {
                return;
            }

            if (finalizingTransfer.State != newState)
            {
                finalizingTransfer.State = newState;
            }

            if (fileStreams.ContainsKey(finalizingTransfer.TrId) && fileStreams[finalizingTransfer.TrId] != null)
            {
                fileStreams[finalizingTransfer.TrId].Close();
            }

            fileStreams.Remove(finalizingTransfer.TrId);

            incoming.RemoveTransferItem(finalizingTransfer);
            outgoing.RemoveTransferItem(finalizingTransfer);

            if (incoming.Files.Count == 0)
            {
                incoming.Initialize();
            }

            if (outgoing.Files.Count == 0)
            {
                outgoing.Initialize();
            }

            List <ITransferItem> tempList = new List <ITransferItem>();

            tempList.Add(finalizingTransfer);
            OnTransferEnded(new TransfersManagerEventArgs(tempList, finalizingTransfer.OtherSideURI));
        }
Exemplo n.º 7
0
        // TRQ:TransfersNumber:TransferID:FileName:FileSize:MD5:...
        private void ProcessIncomingTransfer(string commandText, string fromURI)
        {
            int c;
            List <ITransferItem> requestedFiles = new List <ITransferItem>();

            string[] commandParts = commandText.Split(TRANSFERS_COMMAND_SEPARATOR.ToCharArray());

            for (c = 0; c < Int32.Parse(commandParts[0]); c++)
            {
                TransferItem newTransfer = new TransferItem(session);

                newTransfer.State             = FileTransferState.Requested;
                newTransfer.TrId              = commandParts[c * 4 + 1];
                newTransfer.FileName          = commandParts[c * 4 + 2];
                newTransfer.FileSize          = Int32.Parse(commandParts[c * 4 + 3]);
                newTransfer.FileMD5           = commandParts[c * 4 + 4];
                newTransfer.FilePath          = newTransfer.FileName;
                newTransfer.OtherSideURI      = fromURI;
                fileStreams[newTransfer.TrId] = null;
                incoming.AddTransferItem(newTransfer);
                requestedFiles.Add(newTransfer);
            }
            OnIncomingTransferRequest(new TransfersManagerEventArgs(requestedFiles, fromURI));
        }
Exemplo n.º 8
0
 public void RemoveTransferItem(TransferItem item)
 {
     files.Remove(item);
 }
Exemplo n.º 9
0
 public void AddTransferItem(TransferItem item)
 {
     files.Add(item);
     MaxIndex++;
 }
Exemplo n.º 10
0
        public void Send(string[] filesList)
        {
            string               tempMD5;
            FileInfo             fi;
            TransferItem         tempItem;
            List <ITransferItem> errorList = new List <ITransferItem>();
            List <ITransferItem> sentList  = new List <ITransferItem>();

            var    remoteReceiver = this.session.ParticipantLogs.FirstOrDefault(x => x.IsLocal == false);
            string otherSideUri   = "";

            if (remoteReceiver != null)
            {
                otherSideUri = remoteReceiver.Uri;
            }

            string bufRequestCommand = TransferCommands.TransferRequest + TRANSFERS_COMMAND_SEPARATOR +
                                       filesList.Length + TRANSFERS_COMMAND_SEPARATOR;

            foreach (string filePath in filesList)
            {
                tempItem              = new TransferItem(this.session);
                tempItem.TrId         = Guid.NewGuid().ToString();
                tempItem.FileName     = System.IO.Path.GetFileName(filePath);
                tempItem.FilePath     = filePath;
                tempItem.OtherSideURI = otherSideUri;
                outgoing.AddTransferItem(tempItem);

                if (!Helpers.CheckFileAvailability(filePath, false))
                {
                    errorList.Add(tempItem);
                    continue;
                }

                tempMD5           = Helpers.GetMD5HashFromFile(filePath);
                fi                = new FileInfo(filePath);
                tempItem.FileSize = fi.Length;
                tempItem.FileMD5  = tempMD5;

                bufRequestCommand += tempItem.TrId + TRANSFERS_COMMAND_SEPARATOR + tempItem.FileName +
                                     TRANSFERS_COMMAND_SEPARATOR + tempItem.FileSize + TRANSFERS_COMMAND_SEPARATOR +
                                     tempItem.FileMD5 + TRANSFERS_COMMAND_SEPARATOR;
                sentList.Add(tempItem);
            }

            if (errorList.Count > 0)
            {
                OnTransferError(new TransferErrorEventArgs(errorList, errorList.First().OtherSideURI, FileTransferState.ErrorDuringCreation, true));

                foreach (ITransferItem ErrorItem in errorList)
                {
                    FinalizeTransfer(ErrorItem.TrId, FileTransferState.ErrorDuringCreation);
                }
                outgoing.MaxIndex--;
            }

            if (errorList.Count == filesList.Count())
            {
                bufRequestCommand = "";
            }

            if (!string.IsNullOrEmpty(bufRequestCommand))
            {
                SendData(bufRequestCommand);

                if (OutgoingTransfer != null)
                {
                    OutgoingTransfer(this, new TransfersManagerEventArgs(sentList, otherSideUri));
                }
            }
        }