public void Init() { if (!File.Exists(DataPath)) { Tox = new Tox(ToxOptions.Default); Tox.GetData().Save(DataPath); } else { Tox = new Tox(ToxOptions.Default, ToxData.FromDisk(DataPath)); } foreach (var node in Nodes) { Tox.Bootstrap(node); } BindEvents(); PopulateList(); Tox.Start(); Tox.StatusMessage = "Toxing on Detox"; }
private bool Bootstrap(ToxConfigNode node) { var toxNode = new ToxNode(node.Address, node.Port, new ToxKey(ToxKeyType.Public, node.PublicKey)); var error = ToxErrorBootstrap.Ok; bool success = _tox.Bootstrap(toxNode, out error); if (success) { Debugging.Write(string.Format("Bootstrapped off of {0}:{1}", node.Address, node.Port)); } else { Debugging.Write(string.Format("Could not bootstrap off of {0}:{1}, error: {2}", node.Address, node.Port, error)); } //even if adding the tcp relay fails for some reason (while it shouldn't...), we'll consider this successful. if (_tox.AddTcpRelay(toxNode, out error)) { Debugging.Write(string.Format("Added TCP relay {0}:{1}", node.Address, node.Port)); } else { Debugging.Write(string.Format("Could not add TCP relay {0}:{1}, error: {2}", node.Address, node.Port, error)); } return(success); }
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); }
public void TestToxBootstrapAndConnect() { var tox = new Tox(_options); var error = ToxErrorBootstrap.Ok; foreach (var node in Globals.Nodes) { bool result = tox.Bootstrap(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(); }
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); }
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); }