示例#1
0
        public Basic()
        {
            ToxOptions options = new ToxOptions(true, false);

            tox = new Tox(options);
            tox.OnFriendRequest += tox_OnFriendRequest;
            tox.OnFriendMessage += tox_OnFriendMessage;

            foreach (ToxNode node in Nodes)
            {
                tox.BootstrapFromNode(node);
            }

            tox.Name          = "SharpTox";
            tox.StatusMessage = "Testing SharpTox";

            tox.Start();

            string id = tox.Id.ToString();

            Console.WriteLine("ID: {0}", id);

            Console.ReadKey();
            tox.Dispose();
        }
示例#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();
        }
示例#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();
            }
        }
示例#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();
        }
示例#5
0
        public NewUserProfileViewModel(string directory)
        {
            this.WhenAnyValue(x => x.Password)
            .Select(x => !string.IsNullOrEmpty(x))
            .ToPropertyEx(this, x => x.Encrypted);

            this.Login = ReactiveCommand.Create(() =>
            {
                var filePath = Path.Combine(directory, this.Name + ".tox");
                var tox      = new Tox(ToxOptions.Default());
                if (this.Encrypted)
                {
                    var data = tox.GetData().Bytes;
                    ToxEncryption.Encrypt(data, this.Password, out _);
                    var toxdata = ToxData.FromBytes(data);
                    tox.Dispose();
                    toxdata.Save(filePath);

                    tox = new Tox(ToxOptions.Default(), toxdata, this.Password);
                }

                return(new ToxSession(tox, filePath));
            }, this.WhenAnyValue(x => x.Name)
                                                .Select(fileName =>
            {
                if (string.IsNullOrWhiteSpace(fileName))
                {
                    return(false);
                }

                var filePath = Path.Combine(directory, fileName + ".tox");
                if (File.Exists(filePath))
                {
                    return(false);
                }

                if ((from i in Path.GetInvalidFileNameChars()
                     from c in fileName
                     select i == c).Any(x => x))
                {
                    return(false);
                }

                return(true);
            }));
        }
示例#6
0
        public Skynet()
        {
            // init tox client
            ToxOptions options = new ToxOptions(true, true);

            tox = new Tox(options);
            tox.OnFriendRequestReceived         += tox_OnFriendRequestReceived;
            tox.OnFriendMessageReceived         += tox_OnFriendMessageReceived;
            tox.OnFriendConnectionStatusChanged += tox_OnFriendConnectionStatusChanged;

            foreach (ToxNode node in Nodes)
            {
                tox.Bootstrap(node);
            }

            tox.Name          = "Skynet";
            tox.StatusMessage = "Running Skynet";
            tox.Start();

            string id = tox.Id.ToString();

            Console.WriteLine("ID: {0}", id);

            // Log tox online status
            Task.Run(() => {
                while (true)
                {
                    Thread.Sleep(200);
                    if (tox.IsConnected)
                    {
                        Console.WriteLine("From Server " + httpPort + ":" + "tox is connected.");
                        break;
                    }
                }
            });

            // start http server
            httpPort = Utils.Utils.FreeTcpPort();
            string baseUrl = "http://localhost:" + httpPort + "/";

            WebApp.Start <StartUp>(url: baseUrl);
            Console.WriteLine("Server listening on " + httpPort);

            allInstance.Add(this);
        }
示例#7
0
        public ExistingUserProfileViewModel(string filePath, ToxData data)
        {
            this.FilePath = filePath ?? throw new ArgumentNullException(nameof(filePath));
            this.Data     = data ?? throw new ArgumentNullException(nameof(data));

            this.Login = ReactiveCommand.Create(() =>
            {
                Tox tox = null;
                if (this.Encrypted)
                {
                    tox = new Tox(ToxOptions.Default(), this.Data, this.Password);
                }
                else
                {
                    tox = new Tox(ToxOptions.Default(), this.Data);
                }

                return(new ToxSession(tox, filePath));
            });
        }
示例#8
0
        static void Main(string[] args)
        {
            using (IToxOptions options = ToxOptions.Default())
                using (ITox tox = options.Create())
                {
                    tox.OnFriendRequestReceived   += OnFriendRequestReceived;
                    tox.OnFriendMessageReceived   += OnFriendMessageReceived;
                    tox.OnConnectionStatusChanged += Tox_OnConnectionStatusChanged;

                    foreach (ToxNode node in Nodes)
                    {
                        tox.Bootstrap(node, out _);
                    }

                    tox.Name          = "SharpTox";
                    tox.StatusMessage = "Testing SharpTox";

                    using (ToxLoop.Start(tox))
                    {
                        Console.WriteLine($"ID: {tox.Id}");
                        Console.ReadKey();
                    }

                    void OnFriendMessageReceived(object sender, ToxEventArgs.FriendMessageEventArgs e)
                    {
                        //get the name associated with the friendnumber
                        string name = tox.GetFriendName(e.FriendNumber, out _);

                        //print the message to the console
                        Console.WriteLine("<{0}> {1}", name, e.Message);
                    }

                    void OnFriendRequestReceived(object sender, ToxEventArgs.FriendRequestEventArgs e)
                    {
                        //automatically accept every friend request we receive
                        tox.AddFriendNoRequest(e.PublicKey, out _);
                    }
                }
        }
示例#9
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();
            }
        }
示例#10
0
        public void TestToxProxySocks5()
        {
            var options = new ToxOptions(true, ToxProxyType.Socks5, "127.0.0.1", 9050);
            var tox     = new Tox(options);
            var error   = ToxErrorBootstrap.Ok;

            foreach (var node in Globals.TcpRelays)
            {
                bool result = tox.AddTcpRelay(node, out error);
                if (!result || error != ToxErrorBootstrap.Ok)
                {
                    Assert.Fail("Failed to bootstrap, error: {0}, result: {1}", error, result);
                }
            }

            tox.Start();
            while (!tox.IsConnected)
            {
                Thread.Sleep(10);
            }

            Console.WriteLine("Tox connected!");
            tox.Dispose();
        }
示例#11
0
        public Skynet(string filename = "")
        {
            // init tox client
            ToxOptions options = new ToxOptions(true, true);

            if (filename != "")
            {
                tox = new Tox(options, ToxData.FromDisk(filename));
            }
            else
            {
                tox = new Tox(options);
            }

            tox.OnFriendRequestReceived         += tox_OnFriendRequestReceived;
            tox.OnFriendLosslessPacketReceived  += tox_OnFriendLosslessPacketReceived;
            tox.OnFriendConnectionStatusChanged += tox_OnFriendConnectionStatusChanged;

            foreach (ToxNode node in Nodes)
            {
                tox.Bootstrap(node);
            }

            tox.Name          = "Skynet";
            tox.StatusMessage = "Running Skynet";
            tox.Start();

            string id = tox.Id.ToString();

            Console.WriteLine("ID: {0}", id);
            Utils.Utils.Log("ID: " + id, true);

            // Log tox online status
            Task.Factory.StartNew(async() =>
            {
                var offLineCount = 0;
                while (true)
                {
                    Thread.Sleep(2000);
                    if (tox.IsConnected)
                    {
                        Console.WriteLine("From Server " + httpPort + ":" + "tox is connected.");
                        Utils.Utils.Log("From Server " + httpPort + ":" + "tox is connected.", true);
                        Utils.Utils.WriteNodeInfo(tox.Id.ToString(), true);
                        offLineCount = 0;
                        break;
                    }
                    else
                    {
                        Utils.Utils.Log("Event: tox is offline", true);
                        offLineCount++;
                    }
                    if (offLineCount > 10)
                    {
                        // start a new tox node
                        offLineCount = 0;
                        tox.Stop();
                        options = new ToxOptions(true, true);
                        if (filename != "")
                        {
                            tox = new Tox(options, ToxData.FromDisk(filename));
                        }
                        else
                        {
                            tox = new Tox(options);
                        }

                        tox.OnFriendRequestReceived         += tox_OnFriendRequestReceived;
                        tox.OnFriendLosslessPacketReceived  += tox_OnFriendLosslessPacketReceived;
                        tox.OnFriendConnectionStatusChanged += tox_OnFriendConnectionStatusChanged;

                        foreach (ToxNode node in Nodes)
                        {
                            tox.Bootstrap(node);
                        }

                        tox.Name          = "Skynet";
                        tox.StatusMessage = "Running Skynet";
                        tox.Start();

                        id = tox.Id.ToString();
                        Console.WriteLine("ID: {0}", id);
                        Console.WriteLine("Start a new Tox node");
                        Utils.Utils.Log("ID: " + id, true);
                    }
                }

                bool onlineStatus = true;
                while (true)
                {
                    // start queue process
                    while (tox.IsConnected)
                    {
                        if (!onlineStatus)
                        {
                            onlineStatus = true;
                            Utils.Utils.Log("Event: tox is online");
                        }
                        processFriendMessage();
                    }
                    Utils.Utils.Log("Event: tox is offline", true);
                    onlineStatus = false;
                    Thread.Sleep(1000);
                }
            }, TaskCreationOptions.LongRunning).ForgetOrThrow();

            // start http server
            httpPort = Utils.Utils.FreeTcpPort();
            string baseUrl = "http://localhost:" + httpPort + "/";

            //WebApp.Start<StartUp> (url: baseUrl);
            Console.WriteLine("Server listening on " + httpPort);
            Utils.Utils.Log("Server listening on " + httpPort, true);
            allInstance.Add(this);
        }
示例#12
0
        public void SwitchTo(ProfileInfo profile)
        {
            if (profile == null)
            {
                throw new ArgumentNullException("profile");
            }

            ToxOptions options = ToxOptions.Default;

            options.Ipv6Enabled = Config.Instance.EnableIpv6;
            options.UdpEnabled  = Config.Instance.EnableUdp;

            ToxData data = ToxData.FromDisk(profile.Path);

            if (data == null)
            {
                throw new Exception("Could not load profile.");
            }

            Tox   tox   = new Tox(options, data);
            ToxAv toxAv = new ToxAv(tox);

            try
            {
                this.InitManagers(tox, toxAv);
            }
            catch
            {
                toxAv.Dispose();
                tox.Dispose();
                throw;
            }

            if (this.Tox != null)
            {
                this.Tox.Dispose();
                this.Tox = null;
            }

            if (this.ToxAv != null)
            {
                this.ToxAv.Dispose();
                this.ToxAv = null;
            }

            this.Tox   = tox;
            this.ToxAv = toxAv;

            this.Tox.OnFriendRequestReceived         += this.OnToxFriendRequestReceived;
            this.Tox.OnFriendMessageReceived         += this.OnToxFriendMessageReceived;
            this.Tox.OnFriendNameChanged             += this.OnToxFriendNameChanged;
            this.Tox.OnFriendStatusMessageChanged    += this.OnToxFriendStatusMessageChanged;
            this.Tox.OnFriendStatusChanged           += this.OnToxFriendStatusChanged;
            this.Tox.OnFriendTypingChanged           += this.OnToxFriendTypingChanged;
            this.Tox.OnFriendConnectionStatusChanged += this.OnToxFriendConnectionStatusChanged;
            this.Tox.OnReadReceiptReceived           += this.OnToxReadReceiptReceived;

            this.Tox.OnFriendLossyPacketReceived    += this.OnToxFriendLossyPacketReceived;
            this.Tox.OnFriendLosslessPacketReceived += this.OnToxFriendLosslessPacketReceived;

            this.Tox.OnGroupInvite         += this.OnToxGroupInvite;
            this.Tox.OnGroupAction         += this.OnToxGroupAction;
            this.Tox.OnGroupMessage        += this.OnToxGroupMessage;
            this.Tox.OnGroupNamelistChange += this.OnToxGroupNamelistChange;
            this.Tox.OnGroupTitleChanged   += this.OnToxGroupTitleChanged;

            this.Tox.OnFileSendRequestReceived += this.OnToxFileSendRequestReceived;
            this.Tox.OnFileControlReceived     += this.OnToxFileControlReceived;
            this.Tox.OnFileChunkReceived       += this.OnToxFileChunkReceived;
            this.Tox.OnFileChunkRequested      += this.OnToxFileChunkRequested;

            this.CurrentProfile = profile;
        }
示例#13
0
        public Skynet(string filename = "")
        {
            // init tox client
            ToxOptions options = new ToxOptions(true, true);

            if (filename != "")
            {
                tox = new Tox(options, ToxData.FromDisk(filename));
            }
            else
            {
                tox = new Tox(options);
            }



            tox.OnFriendRequestReceived         += tox_OnFriendRequestReceived;
            tox.OnFriendLosslessPacketReceived  += tox_OnFriendLosslessPacketReceived;
            tox.OnFriendConnectionStatusChanged += tox_OnFriendConnectionStatusChanged;

            foreach (ToxNode node in Nodes)
            {
                tox.Bootstrap(node);
            }

            tox.Name          = "Skynet";
            tox.StatusMessage = "Running Skynet";
            tox.Start();

            string id = tox.Id.ToString();

            Console.WriteLine("ID: {0}", id);
            Utils.Utils.LogUtils("ID: " + id);

            // Log tox online status
            Task.Factory.StartNew(async() => {
                var offLineCount = 0;
                while (true)
                {
                    Thread.Sleep(2000);
                    if (tox.IsConnected)
                    {
                        Console.WriteLine("From Server " + httpPort + ":" + "tox is connected.");
                        Utils.Utils.LogUtils("From Server " + httpPort + ":" + "tox is connected.");
                        offLineCount = 0;
                        // send a online message to server
                        using (var client = new HttpClient()){
                            await client.PostAsJsonAsync("http://xiaoqiang.bwbot.org/online", tox.Id.ToString());
                        }
                        break;
                    }
                    else
                    {
                        Utils.Utils.LogUtils("Event: tox is offline");
                        offLineCount++;
                    }
                    if (offLineCount > 10)
                    {
                        // start a new tox node
                        offLineCount = 0;
                        tox.Dispose();
                        options = new ToxOptions(true, true);
                        if (filename != "")
                        {
                            tox = new Tox(options, ToxData.FromDisk(filename));
                        }
                        else
                        {
                            tox = new Tox(options);
                        }

                        tox.OnFriendRequestReceived         += tox_OnFriendRequestReceived;
                        tox.OnFriendLosslessPacketReceived  += tox_OnFriendLosslessPacketReceived;
                        tox.OnFriendConnectionStatusChanged += tox_OnFriendConnectionStatusChanged;

                        foreach (ToxNode node in Nodes)
                        {
                            tox.Bootstrap(node);
                        }

                        tox.Name          = "Skynet";
                        tox.StatusMessage = "Running Skynet";
                        tox.Start();

                        id = tox.Id.ToString();
                        Console.WriteLine("ID: {0}", id);
                        Utils.Utils.LogUtils("ID: " + id);
                    }
                }

                while (true)
                {
                    // start queue process
                    while (tox.IsConnected)
                    {
                        Package processPack = null;
                        lock (reqQueueLock) {
                            if (reqQueue.Count > 0)
                            {
                                processPack = reqQueue.Dequeue();
                            }
                        }
                        if (processPack != null)
                        {
                            newReqReceived(processPack);
                        }
                        else
                        {
                            Thread.Sleep(1);
                        }
                    }
                    Utils.Utils.LogUtils("Event: tox is offline");
                    Thread.Sleep(1000);
                }
            }, TaskCreationOptions.LongRunning).ForgetOrThrow();



            // start http server
            httpPort = Utils.Utils.FreeTcpPort();
            string baseUrl = "http://localhost:" + httpPort + "/";

            WebApp.Start <StartUp> (url: baseUrl);
            Console.WriteLine("Server listening on " + httpPort);
            Utils.Utils.LogUtils("Server listening on " + httpPort);
            allInstance.Add(this);
        }
示例#14
0
 public ExtendedTox(ToxOptions options)
     : base(options)
 {
 }
示例#15
0
 public ExtendedTox(ToxOptions options, ToxData data) :
     base(options, data)
 {
 }