Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net.Sockets;
- using System.Threading;
- using libtcod;
- using TS3QueryLib.Core;
- using TS3QueryLib.Core.Client;
- using TS3QueryLib.Core.Client.Entities;
- using TS3QueryLib.Core.Client.Notification.EventArgs;
- using TS3QueryLib.Core.Client.Responses;
- using TS3QueryLib.Core.Common;
- using TS3QueryLib.Core.Common.Responses;
- using TS3QueryLib.Core.Communication;
- namespace tsbot
- {
- class App
- {
- private AsyncTcpDispatcher AsyncQueryDispatcher;
- private SyncTcpDispatcher SyncQueryDispatcher;
- private TS3QueryLib.Core.Client.QueryRunner AsyncClientQueryRunner;
- private TS3QueryLib.Core.Server.QueryRunner AsyncServerQueryRunner;
- private QueryRunner SyncQueryRunner;
- private WhoAmIResponse me;
- public static Boolean CanISend = false;
- static void Main(string[] args)
- {
- new App().Start();
- }
- private void Connect()
- {
- try
- {
- Disconnect();
- Log("Creating Sync");
- SyncQueryDispatcher = new SyncTcpDispatcher("remote.ip", 13337);
- SyncQueryRunner = new QueryRunner(SyncQueryDispatcher);
- Log("Sync done");
- Log("Creating ASync");
- SyncQueryDispatcher = new SyncTcpDispatcher("remote.ip", 13337);
- AsyncQueryDispatcher.ReadyForSendingCommands += QueryDispatcher_ReadyForSendingCommands;
- AsyncQueryDispatcher.ServerClosedConnection += QueryDispatcher_ServerClosedConnection;
- AsyncQueryDispatcher.SocketError += QueryDispatcher_SocketError;
- AsyncQueryDispatcher.NotificationReceived += QueryDispatcher_NotificationReceived;
- AsyncQueryDispatcher.Connect();
- Log("ASync connected");
- }
- catch (Exception ex)
- {
- Log("Cannot connect", "fatal", ex.Message);
- Disconnect();
- }
- }
- private void QueryDispatcher_NotificationReceived(object sender, EventArgs<string> e)
- {
- Log(e.Value, "Notification");
- }
- private void QueryDispatcher_SocketError(object sender, SocketErrorEventArgs e)
- {
- // do not handle connection lost errors because they are already handled by QueryDispatcher_ServerClosedConnection
- if (e.SocketError == SocketError.ConnectionReset)
- return;
- // this event is raised when a socket exception has occured
- Log("Socket error", "fatal", e.SocketError.ToString());
- // force disconnect
- Connect();
- }
- private void QueryDispatcher_ServerClosedConnection(object sender, System.EventArgs e)
- {
- // this event is raised when the connection to the server is lost.
- Log("Connection to server closed/lost.", "Debug");
- // dispose
- Connect();
- }
- private void QueryDispatcher_ReadyForSendingCommands(object sender, System.EventArgs e)
- {
- Log("Ready for sending");
- // you can only run commands on the queryrunner when this event has been raised first!
- AsyncClientQueryRunner = new TS3QueryLib.Core.Client.QueryRunner(AsyncQueryDispatcher);
- AsyncClientQueryRunner.RegisterForNotifications(ClientNotifyRegisterEvent.Any);
- AsyncServerQueryRunner = new TS3QueryLib.Core.Server.QueryRunner(AsyncQueryDispatcher);
- AsyncServerQueryRunner.RegisterForNotifications(TS3QueryLib.Core.Server.Entities.ServerNotifyRegisterEvent.Server | TS3QueryLib.Core.Server.Entities.ServerNotifyRegisterEvent.Channel);
- Log("Logging in async");
- AsyncServerQueryRunner.Login("srv", "pass");
- AsyncServerQueryRunner.SelectVirtualServerById(1);
- Log("Async logged in");
- }
- public void Start()
- {
- Connect();
- while (true) ;
- }
- public static void Log(String msg, String type = null, String reason = null)
- {
- if(type != null)
- {
- Console.Write("[" + type.ToUpper() + "] ");
- }
- if(reason != null)
- {
- Console.WriteLine(msg + " Reason: " + reason);
- }
- else
- {
- Console.WriteLine(msg);
- }
- }
- public void Disconnect()
- {
- me = null;
- if (SyncQueryDispatcher != null)
- {
- SyncQueryDispatcher.Dispose();
- SyncQueryDispatcher = null;
- }
- if (SyncQueryRunner != null)
- {
- SyncQueryRunner.Dispose();
- SyncQueryRunner = null;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement