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; } }
public bool SendNextChunk(long position, int chunk_size) { if (_stream == null) { _stream = new FileStream(Path, FileMode.Open); } byte[] buffer = new byte[chunk_size]; long remaining = FileSize - position; if (remaining >= chunk_size) { if (_stream.Read(buffer, 0, chunk_size) == 0) { Kill(true); return(false); } var error = ToxErrorFileSendChunk.Ok; if (!Tox.FileSendChunk(FriendNumber, FileNumber, position, buffer, out error)) { _stream.Position -= chunk_size; Debug.WriteLine("Could not send data, rewinding the stream"); return(false); } Debug.WriteLine(string.Format("Data sent: {0} bytes", buffer.Length)); } /*else * { * buffer = new byte[remaining]; * * if (_stream.Read(buffer, 0, (int)remaining) == 0) * { * Kill(true); * return false; * } * * Tox.FileSendData(FriendNumber, FileNumber, buffer); * Debug.WriteLine(string.Format("Sent the last chunk of data: {0} bytes", buffer.Length)); * * Kill(true); * }*/ double value = (double)remaining / (double)FileSize; Progress = 100 - (int)(value * 100); return(true); }
public void TestToxFileTransfer() { byte[] fileData = new byte[0xBEEEF]; byte[] receivedData = new byte[0xBEEEF]; new Random().NextBytes(fileData); string fileName = "testing.dat"; bool fileReceived = false; _tox2.OnFileSendRequestReceived += (sender, args) => { if (fileName != args.FileName) { Assert.Fail("Filenames do not match"); } if (args.FileSize != fileData.Length) { Assert.Fail("File lengths do not match"); } var error2 = ToxErrorFileControl.Ok; bool result = _tox2.FileControl(args.FriendNumber, args.FileNumber, ToxFileControl.Resume); if (!result || error2 != ToxErrorFileControl.Ok) { Assert.Fail("Failed to send file control, error: {0}, result: {1}", error2, result); } }; var error = ToxErrorFileSend.Ok; var fileInfo = _tox1.FileSend(0, ToxFileKind.Data, fileData.Length, fileName, out error); if (error != ToxErrorFileSend.Ok) { Assert.Fail("Failed to send a file send request, error: {0}", error); } _tox1.OnFileChunkRequested += (sender, args) => { byte[] data = new byte[args.Length]; Array.Copy(fileData, args.Position, data, 0, args.Length); var error2 = ToxErrorFileSendChunk.Ok; bool result = _tox1.FileSendChunk(args.FriendNumber, args.FileNumber, args.Position, data, out error2); if (!result || error2 != ToxErrorFileSendChunk.Ok) { Assert.Fail("Failed to send chunk, error: {0}, result: {1}", error2, result); } }; _tox2.OnFileChunkReceived += (sender, args) => { if (args.Position == fileData.Length) { fileReceived = true; } else { Array.Copy(args.Data, 0, receivedData, args.Position, args.Data.Length); } }; while (!fileReceived) { DoIterate(); } if (!fileData.SequenceEqual(receivedData)) { Assert.Fail("Original data is not equal to the data we received"); } }