Exemple #1
0
        private void Tox_OnFileChunkRequested(object sender, ToxEventArgs.FileRequestChunkEventArgs e)
        {
            var    transfer = FindTransfer(e.FriendNumber, e.FileNumber);
            Stream stream   = null;

            if (transfer == null || !_transfers.TryGetValue(transfer, out stream))
            {
                Debugging.Write("Chunk requested for a transfer we don't know about, ignoring");
                return;
            }

            if (e.Length == 0)
            {
                Debugging.Write("Transfer finished");

                //time to clean up
                RemoveTransfer(transfer, false);

                //let's be nice and send an empty chunk to let our friend know we're done sending the file
                _tox.FileSendChunk(e.FriendNumber, e.FileNumber, e.Position, new byte[0]);
                return;
            }

            if (stream.Position != e.Position)
            {
                Debugging.Write("Position doesn't equal ours, rewinding");

                try { stream.Seek(e.Position, SeekOrigin.Begin); }
                catch (Exception ex)
                {
                    Debugging.Write("Error while seeking in stream, exception: " + ex.ToString());
                    return; //TODO: should probably cancel the transfer here
                }

                transfer.TransferredBytes = e.Position;
            }

            byte[] buffer = new byte[e.Length];

            try { stream.Read(buffer, 0, buffer.Length); }
            catch (Exception ex)
            {
                Debugging.Write("Error while reading from stream, exception: " + ex.ToString());
                return; //TODO: should probably cancel the transfer here
            }

            var error = ToxErrorFileSendChunk.Ok;

            if (!_tox.FileSendChunk(e.FriendNumber, e.FileNumber, e.Position, buffer, out error))
            {
                Debugging.Write("Failed to send chunk: " + error);
            }
            else
            {
                transfer.TransferredBytes += e.Length;
            }
        }
Exemple #2
0
            public byte[] GetNextChunk(ToxEventArgs.FileRequestChunkEventArgs e)
            {
                lock (_stream)
                {
                    if (_stream.Position != e.Position)
                    {
                        _stream.Seek(e.Position, SeekOrigin.Begin);
                    }

                    var chunk = new byte[e.Length];
                    _stream.Read(chunk, 0, e.Length);

                    return(chunk);
                }
            }
Exemple #3
0
        private void FileChunkRequestedHandler(object sender, ToxEventArgs.FileRequestChunkEventArgs e)
        {
            if (IsPlaceholder || !IsThisTransfer(e))
            {
                return;
            }

            var chunk = GetNextChunk(e);
            var successfulChunkSend = _toxModel.FileSendChunk(e.FriendNumber, e.FileNumber, e.Position, chunk);

            if (successfulChunkSend)
            {
                if (IsFinished)
                {
                    _fileTransferResumer.RemoveTransfer(_friendNumber, _fileNumber);
                    State = FileTransferState.Finished;
                }
            }
        }
Exemple #4
0
        private void FileChunkRequestedHandler(object sender, ToxEventArgs.FileRequestChunkEventArgs e)
        {
            var transferId = new TransferId(e.FriendNumber, e.FileNumber);

            if (IsTransferFinished(transferId))
            {
                return;
            }

            var currentTransfer = _transfers[transferId];

            var chunk = currentTransfer.GetNextChunk(e);
            var successfulChunkSend = _toxModel.FileSendChunk(e.FriendNumber, e.FileNumber, e.Position, chunk);

            if (successfulChunkSend)
            {
                if (currentTransfer.IsFinished())
                {
                    HandleFinishedUpload(transferId);
                }
            }
        }
Exemple #5
0
 private void FileChunkRequestedHandler(object sender, ToxEventArgs.FileRequestChunkEventArgs e)
 {
     FileChunkRequested?.Invoke(this, e);
 }
Exemple #6
0
 private void OnToxFileChunkRequested(object sender, ToxEventArgs.FileRequestChunkEventArgs e)
 {
     this.Tox.FileControl(e.FriendNumber, e.FileNumber, ToxFileControl.Cancel);
     Logger.Log(LogLevel.Warning, "ToxFileChunkRequested not supported, canceled");
 }