Ejemplo n.º 1
0
        public void TestToxLosslessPacket()
        {
            int receivedPackets = 0;

            byte[] data = new byte[ToxConstants.MaxCustomPacketSize];
            new Random().NextBytes(data);
            data[0] = 170;

            _tox2.OnFriendLosslessPacketReceived += (sender, args) =>
            {
                if (args.Data.Length != data.Length || data[0] != args.Data[0])
                {
                    Assert.Fail("Packet doesn't have the same length/identifier");
                }
                else if (!data.SequenceEqual(args.Data))
                {
                    Assert.Fail("Packet contents don't match");
                }

                receivedPackets++;
            };

            var  error  = ToxErrorFriendCustomPacket.Ok;
            bool result = _tox1.FriendSendLosslessPacket(0, data, out error);

            if (!result || error != ToxErrorFriendCustomPacket.Ok)
            {
                Assert.Fail("Failed to send lossless packet to friend, error: {0}, result: {1}", error, result);
            }

            while (receivedPackets != 1)
            {
                DoIterate();
            }
        }
Ejemplo n.º 2
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.º 3
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);
            }
        }