Ejemplo n.º 1
0
        public void SendAvatar(int friendNumber, byte[] avatar)
        {
            //cancel any existing avatar file transfers with this friend first
            for (int i = _transfers.Count() - 1; i >= 0; i--)
            {
                //reverse loop to be able to delete entries while iterating through them, fun stuff
                var entry = _transfers.ElementAt(i);
                if (entry.Key.FriendNumber == friendNumber && entry.Key.Direction == FileTransferDirection.Outgoing)
                {
                    CancelTransfer(entry.Key);
                }
            }

            var         error = ToxErrorFileSend.Ok;
            ToxFileInfo fileInfo;

            if (avatar != null)
            {
                fileInfo = _tox.FileSend(friendNumber, ToxFileKind.Avatar, avatar.Length, "avatar.png", ToxTools.Hash(avatar), out error);
            }
            else
            {
                fileInfo = _tox.FileSend(friendNumber, ToxFileKind.Avatar, 0, "avatar.png");
            }

            if (error != ToxErrorFileSend.Ok)
            {
                Debugging.Write("Could not send file transfer request: " + error.ToString());
                return;
            }

            //no point in adding a 'dummy' transfer to our list
            if (avatar == null)
            {
                return;
            }

            var tr = new FileTransfer(fileInfo.Number, friendNumber, ToxFileKind.Avatar, FileTransferDirection.Outgoing);

            if (!_transfers.ContainsKey(tr))
            {
                _transfers.Add(tr, new MemoryStream(avatar));
            }
            else
            {
                Debugging.Write("Tried to add a filetransfer that's already in the list, panic!");
            }
        }
Ejemplo n.º 2
0
        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");
            }
        }