Esempio n. 1
0
 public void stop()
 {
     tox.Stop();
 }
Esempio n. 2
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);
        }
Esempio n. 3
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);
                        offLineCount = 0;
                        // send a online message to server
                        if (filename != "")
                        {
                            using (var client = new HttpClient())
                            {
                                ClientInfo minfo = new ClientInfo(tox.Id.ToString(), Utils.Utils.GetLocalIPAddress());
                                await client.PostAsJsonAsync("http://xiaoqiang.bwbot.org/v2/online", minfo);
                            }
                        }
                        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");
                            // send a online message to server
                            if (filename != "")
                            {
                                using (var client = new HttpClient())
                                {
                                    ClientInfo minfo = new ClientInfo(tox.Id.ToString(), Utils.Utils.GetLocalIPAddress());
                                    await client.PostAsJsonAsync("http://xiaoqiang.bwbot.org/v2/online", minfo);
                                }
                            }
                        }
                        Package processPack = null;
                        lock (reqQueueLock)
                        {
                            if (reqQueue.Count > 0)
                            {
                                processPack = reqQueue.Dequeue();
                            }
                        }
                        if (processPack != null)
                        {
                            newReqReceived(processPack);
                        }
                        else
                        {
                            Thread.Sleep(1);
                        }
                    }
                    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);
        }