Ejemplo n.º 1
0
        public bool sendMsg(ToxId toxid, string msg)
        {
            // check if this message is send to itself
            if (toxid.ToString() == tox.Id.ToString())
            {
                return(false); // this is not allowed
            }
            // wait toxcore online
            int maxOnlineWaitTime = 20000; // 20s
            int onlineWaitCount   = 0;

            while (!tox.IsConnected)
            {
                Thread.Sleep(10);
                onlineWaitCount += 10;
                if (onlineWaitCount > maxOnlineWaitTime)
                {
                    return(false);
                }
            }

            ToxKey toxkey    = toxid.PublicKey;
            int    friendNum = tox.GetFriendByPublicKey(toxkey);

            if (friendNum == -1)
            {
                int res = tox.AddFriend(toxid, "add friend");
                if (res != (int)ToxErrorFriendAdd.Ok)
                {
                    return(false);
                }
                friendNum = tox.GetFriendByPublicKey(toxkey);
            }
            int waitCount = 0;
            int maxCount  = 500;

            if (connectedList.IndexOf(toxkey.GetString()) == -1)
            {
                maxCount = 200 * 1000; // first time wait for 200s
            }
            while (tox.GetFriendConnectionStatus(friendNum) == ToxConnectionStatus.None && waitCount < maxCount)
            {
                if (waitCount % 1000 == 0)
                {
                    Console.WriteLine("target is offline." + waitCount / 1000);
                }
                waitCount += 10;
                Thread.Sleep(10);
            }
            if (waitCount == maxCount)
            {
                Console.WriteLine("Connect Failed");
                connectedList.Remove(toxkey.GetString());
                return(false);
            }
            connectedList.Add(toxkey.GetString());
            int msgRes = tox.SendMessage(friendNum, msg, ToxMessageType.Message);

            return(msgRes > 0);
        }
Ejemplo n.º 2
0
        public void TestToxAvCallAndAnswer()
        {
            var options = new ToxOptions(true, true);
            var tox1    = new Tox(options);
            var tox2    = new Tox(options);

            var toxAv1 = new ToxAv(tox1);
            var toxAv2 = new ToxAv(tox2);

            bool testFinished = false;

            Task.Run(async() =>
            {
                while (!testFinished)
                {
                    int time1 = tox1.Iterate();
                    int time2 = tox2.Iterate();

                    await Task.Delay(Math.Min(time1, time2));
                }
            });

            tox1.AddFriend(tox2.Id, "hey");
            tox2.AddFriend(tox1.Id, "hey");

            while (tox1.GetFriendConnectionStatus(0) == ToxConnectionStatus.None)
            {
                Thread.Sleep(10);
            }

            bool answered = false;

            toxAv1.Call(0, 48, 30000);

            toxAv2.OnCallRequestReceived += (sender, e) =>
            {
                var  error2  = ToxAvErrorAnswer.Ok;
                bool result2 = toxAv2.Answer(e.FriendNumber, 48, 30000, out error2);
            };

            toxAv1.OnCallStateChanged += (sender, e) =>
            {
                answered = true;
            };

            while (!answered)
            {
                Thread.Sleep(10);
            }

            testFinished = true;
            toxAv1.Dispose();
            toxAv2.Dispose();
            tox1.Dispose();
            tox2.Dispose();
        }
Ejemplo n.º 3
0
        public void Init()
        {
            var options = new ToxOptions(true, true);

            _tox1 = new Tox(options);
            _tox2 = new Tox(options);

            _tox1.AddFriend(_tox2.Id, "hey");
            _tox2.AddFriend(_tox1.Id, "hey");

            while (_tox1.GetFriendConnectionStatus(0) == ToxConnectionStatus.None)
            {
                DoIterate();
            }
        }
Ejemplo n.º 4
0
        public void TestToxFriendRequest()
        {
            var    options      = new ToxOptions(true, true);
            var    tox1         = new Tox(options);
            var    tox2         = new Tox(options);
            var    error        = ToxErrorFriendAdd.Ok;
            string message      = "Hey, this is a test friend request.";
            bool   testFinished = false;

            tox1.AddFriend(tox2.Id, message, out error);
            if (error != ToxErrorFriendAdd.Ok)
            {
                Assert.Fail("Failed to add friend: {0}", error);
            }

            tox2.OnFriendRequestReceived += (sender, args) =>
            {
                if (args.Message != message)
                {
                    Assert.Fail("Message received in the friend request is not the same as the one that was sent");
                }

                tox2.AddFriendNoRequest(args.PublicKey, out error);
                if (error != ToxErrorFriendAdd.Ok)
                {
                    Assert.Fail("Failed to add friend (no request): {0}", error);
                }

                if (!tox2.FriendExists(0))
                {
                    Assert.Fail("Friend doesn't exist according to core");
                }

                testFinished = true;
            };

            while (!testFinished && tox1.GetFriendConnectionStatus(0) == ToxConnectionStatus.None)
            {
                int time1 = tox1.Iterate();
                int time2 = tox2.Iterate();

                Thread.Sleep(Math.Min(time1, time2));
            }

            tox1.Dispose();
            tox2.Dispose();
        }
Ejemplo n.º 5
0
        public void Init()
        {
            var options = new ToxOptions(true, true);

            _tox1 = new Tox(options);
            _tox2 = new Tox(options);

            _toxAv1 = new ToxAv(_tox1);
            _toxAv2 = new ToxAv(_tox2);

            _tox1.AddFriend(_tox2.Id, "hey");
            _tox2.AddFriend(_tox1.Id, "hey");

            while (_tox1.GetFriendConnectionStatus(0) == ToxConnectionStatus.None)
            {
                DoIterate();
            }

            bool answered = false;

            _toxAv1.Call(0, 48, 3000);

            _toxAv2.OnCallRequestReceived += (sender, e) =>
            {
                var  error2  = ToxAvErrorAnswer.Ok;
                bool result2 = _toxAv2.Answer(e.FriendNumber, 48, 3000, out error2);
            };

            _toxAv1.OnCallStateChanged += (sender, e) =>
            {
                answered = true;
            };

            while (!answered)
            {
                DoIterate();
            }
        }
Ejemplo n.º 6
0
        public bool sendMsg(ToxId toxid, byte[] msg, int timeout = 20)
        {
            try
            {
                lock (sendLock)
                {
                    // check if this message is send to itself
                    if (toxid.ToString() == tox.Id.ToString())
                    {
                        return(false); // this is not allowed
                    }

                    // wait toxcore online
                    int maxOnlineWaitTime = 200000; // 200s
                    int onlineWaitCount   = 0;
                    while (!tox.IsConnected)
                    {
                        Thread.Sleep(10);
                        onlineWaitCount += 10;
                        if (onlineWaitCount > maxOnlineWaitTime)
                        {
                            return(false);
                        }
                    }

                    ToxKey toxkey    = toxid.PublicKey;
                    int    friendNum = tox.GetFriendByPublicKey(toxkey);
                    if (friendNum == -1)
                    {
                        int res = tox.AddFriend(toxid, "add friend");
                        if (res != (int)ToxErrorFriendAdd.Ok)
                        {
                            return(false);
                        }
                        friendNum = tox.GetFriendByPublicKey(toxkey);
                    }

                    if (tox.GetFriendConnectionStatus(friendNum) == ToxConnectionStatus.None)
                    {
                        return(false);
                    }

                    var mesError = new ToxErrorFriendCustomPacket();
                    // retry send message
                    int retryCount = 0;
                    while (retryCount < 60)
                    {
                        byte[] msgToSend = new byte[msg.Length + 1];
                        msgToSend[0] = 170; // The first byte must be in the range 160-191.
                        msg.CopyTo(msgToSend, 1);
                        bool msgRes = tox.FriendSendLosslessPacket(friendNum, msgToSend, out mesError);
                        if (msgRes)
                        {
                            break;
                        }

                        Utils.Utils.Log("Event: " + mesError, true);
                        Console.WriteLine(Utils.Utils.UnixTimeNow() + " Event: " + mesError);
                        if (mesError == ToxErrorFriendCustomPacket.SendQ)
                        {
                            Thread.Sleep(20);
                            continue;
                        }
                        retryCount++;
                        Thread.Sleep(100);
                    }
                    if (retryCount == 60)
                    {
                        tox.DeleteFriend(friendNum);
                        return(false);
                    }

                    return(true);
                }
            }
            catch (Exception e)
            {
                Utils.Utils.Log(e.StackTrace, true);
                return(false);
            }
        }
Ejemplo n.º 7
0
 public static Status GetDetoxStatusByFriendNumber(int number, Tox Tox)
 {
     return(Utilities.ToxStatusToDetoxStatus(Tox.GetFriendStatus(number), Tox.GetFriendConnectionStatus(number)));
 }
Ejemplo n.º 8
0
        public bool sendMsg(ToxId toxid, byte[] msg)
        {
            lock (sendLock) {
                // check if this message is send to itself
                if (toxid.ToString() == tox.Id.ToString())
                {
                    return(false);                    // this is not allowed
                }

                // wait toxcore online
                int maxOnlineWaitTime = 20000;                 // 20s
                int onlineWaitCount   = 0;
                while (!tox.IsConnected)
                {
                    Thread.Sleep(10);
                    onlineWaitCount += 10;
                    if (onlineWaitCount > maxOnlineWaitTime)
                    {
                        return(false);
                    }
                }

                ToxKey toxkey    = toxid.PublicKey;
                int    friendNum = tox.GetFriendByPublicKey(toxkey);
                if (friendNum == -1)
                {
                    int res = tox.AddFriend(toxid, "add friend");
                    if (res != (int)ToxErrorFriendAdd.Ok)
                    {
                        return(false);
                    }
                    friendNum = tox.GetFriendByPublicKey(toxkey);
                }

                int waitCount = 0;
                int maxCount  = 500;
                if (connectedList.IndexOf(toxkey.GetString()) == -1)
                {
                    maxCount = 200 * 1000;                     // first time wait for 200s
                }
                while (tox.GetFriendConnectionStatus(friendNum) == ToxConnectionStatus.None && waitCount < maxCount)
                {
                    if (waitCount % 1000 == 0)
                    {
                        Utils.Utils.LogUtils("Event: target is offline " + waitCount / 1000);
                    }
                    waitCount += 10;
                    Thread.Sleep(10);
                }
                if (waitCount == maxCount)
                {
                    Utils.Utils.LogUtils("Event: Connect Failed");
                    connectedList.Remove(toxkey.GetString());
                    return(false);
                }
                if (connectedList.IndexOf(toxkey.GetString()) == -1)
                {
                    connectedList.Add(toxkey.GetString());
                }

                var mesError = new ToxErrorFriendCustomPacket();
                // retry send message
                int retryCount = 0;
                while (retryCount < 10)
                {
                    byte[] msgToSend = new byte[msg.Length + 1];
                    msgToSend [0] = 170;                     // The first byte must be in the range 160-191.
                    msg.CopyTo(msgToSend, 1);
                    bool msgRes = tox.FriendSendLosslessPacket(friendNum, msgToSend, out mesError);
                    if (msgRes)
                    {
                        break;
                    }

                    Utils.Utils.LogUtils("Event: " + mesError);
                    if (mesError == ToxErrorFriendCustomPacket.SendQ)
                    {
                        Thread.Sleep(10);
                        continue;
                    }
                    retryCount++;
                    Thread.Sleep(100);
                }
                if (retryCount == 10)
                {
                    return(false);
                }
                return(true);
            }
        }