Beispiel #1
0
        public FriendListViewModel(Tox tox)
        {
            var friends = new SourceList <uint>();

            foreach (var friend in tox.Friends)
            {
                friends.Add(friend);
                tox.AddFriendNoRequest(tox.GetFriendPublicKey(friend, out _), out _);
            }

            this.Add = ReactiveCommand.Create <uint>(friendNumber =>
            {
                friends.Add(friendNumber);
            });

            ReadOnlyObservableCollection <FriendListEntryViewModel> entries;

            friends.Connect()
            .Transform(number => new FriendListEntryViewModel(tox, number))
            .Bind(out entries)
            .Subscribe();

            this.Friends = entries;

            this.Friends.ToObservableChangeSet()
            .MergeMany(x => x.Remove)
            .Subscribe(number =>
            {
                if (tox.DeleteFriend(number, out _))
                {
                    friends.Remove(number);
                }
            });
        }
Beispiel #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);
            }
        }