Advertisement
Guest User

Untitled

a guest
Sep 29th, 2014
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.08 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net.Sockets;
  6. using System.Threading;
  7.  
  8. using libtcod;
  9.  
  10. using TS3QueryLib.Core;
  11. using TS3QueryLib.Core.Client;
  12. using TS3QueryLib.Core.Client.Entities;
  13. using TS3QueryLib.Core.Client.Notification.EventArgs;
  14. using TS3QueryLib.Core.Client.Responses;
  15. using TS3QueryLib.Core.Common;
  16. using TS3QueryLib.Core.Common.Responses;
  17. using TS3QueryLib.Core.Communication;
  18.  
  19. namespace tsbot
  20. {
  21.     class App
  22.     {
  23.         private AsyncTcpDispatcher AsyncQueryDispatcher;
  24.         private SyncTcpDispatcher SyncQueryDispatcher;
  25.         private TS3QueryLib.Core.Client.QueryRunner AsyncClientQueryRunner;
  26.         private TS3QueryLib.Core.Server.QueryRunner AsyncServerQueryRunner;
  27.         private QueryRunner SyncQueryRunner;
  28.         private WhoAmIResponse me;
  29.  
  30.  
  31.         public static Boolean CanISend = false;
  32.         static void Main(string[] args)
  33.         {
  34.             new App().Start();
  35.         }
  36.  
  37.         private void Connect()
  38.         {
  39.             try
  40.             {
  41.                 Disconnect();
  42.  
  43.                 Log("Creating Sync");
  44.  
  45.                 SyncQueryDispatcher = new SyncTcpDispatcher("remote.ip", 13337);
  46.                 SyncQueryRunner = new QueryRunner(SyncQueryDispatcher);
  47.  
  48.                 Log("Sync done");
  49.  
  50.                 Log("Creating ASync");
  51.  
  52.                 SyncQueryDispatcher = new SyncTcpDispatcher("remote.ip", 13337);
  53.                 AsyncQueryDispatcher.ReadyForSendingCommands += QueryDispatcher_ReadyForSendingCommands;
  54.                 AsyncQueryDispatcher.ServerClosedConnection += QueryDispatcher_ServerClosedConnection;
  55.                 AsyncQueryDispatcher.SocketError += QueryDispatcher_SocketError;
  56.                 AsyncQueryDispatcher.NotificationReceived += QueryDispatcher_NotificationReceived;
  57.                 AsyncQueryDispatcher.Connect();
  58.  
  59.                 Log("ASync connected");
  60.             }
  61.             catch (Exception ex)
  62.             {
  63.                 Log("Cannot connect", "fatal", ex.Message);
  64.                 Disconnect();
  65.             }
  66.         }
  67.  
  68.         private void QueryDispatcher_NotificationReceived(object sender, EventArgs<string> e)
  69.         {
  70.             Log(e.Value, "Notification");
  71.         }
  72.  
  73.         private void QueryDispatcher_SocketError(object sender, SocketErrorEventArgs e)
  74.         {
  75.             // do not handle connection lost errors because they are already handled by QueryDispatcher_ServerClosedConnection
  76.             if (e.SocketError == SocketError.ConnectionReset)
  77.                 return;
  78.  
  79.             // this event is raised when a socket exception has occured
  80.             Log("Socket error", "fatal", e.SocketError.ToString());
  81.  
  82.             // force disconnect
  83.             Connect();
  84.         }
  85.  
  86.         private void QueryDispatcher_ServerClosedConnection(object sender, System.EventArgs e)
  87.         {
  88.             // this event is raised when the connection to the server is lost.
  89.             Log("Connection to server closed/lost.", "Debug");
  90.  
  91.             // dispose
  92.             Connect();
  93.         }
  94.  
  95.         private void QueryDispatcher_ReadyForSendingCommands(object sender, System.EventArgs e)
  96.         {
  97.             Log("Ready for sending");
  98.             // you can only run commands on the queryrunner when this event has been raised first!
  99.             AsyncClientQueryRunner = new TS3QueryLib.Core.Client.QueryRunner(AsyncQueryDispatcher);
  100.             AsyncClientQueryRunner.RegisterForNotifications(ClientNotifyRegisterEvent.Any);
  101.  
  102.             AsyncServerQueryRunner = new TS3QueryLib.Core.Server.QueryRunner(AsyncQueryDispatcher);
  103.             AsyncServerQueryRunner.RegisterForNotifications(TS3QueryLib.Core.Server.Entities.ServerNotifyRegisterEvent.Server | TS3QueryLib.Core.Server.Entities.ServerNotifyRegisterEvent.Channel);
  104.  
  105.             Log("Logging in async");
  106.             AsyncServerQueryRunner.Login("srv", "pass");
  107.             AsyncServerQueryRunner.SelectVirtualServerById(1);
  108.             Log("Async logged in");
  109.         }
  110.  
  111.         public void Start()
  112.         {
  113.             Connect();
  114.  
  115.             while (true) ;
  116.         }
  117.  
  118.         public static void Log(String msg, String type = null, String reason = null)
  119.         {
  120.             if(type != null)
  121.             {
  122.                 Console.Write("[" + type.ToUpper() + "] ");
  123.             }
  124.  
  125.             if(reason != null)
  126.             {
  127.                 Console.WriteLine(msg + " Reason: " + reason);
  128.             }
  129.             else
  130.             {
  131.                 Console.WriteLine(msg);
  132.             }
  133.         }
  134.  
  135.         public void Disconnect()
  136.         {
  137.             me = null;
  138.  
  139.             if (SyncQueryDispatcher != null)
  140.             {
  141.                 SyncQueryDispatcher.Dispose();
  142.                 SyncQueryDispatcher = null;
  143.             }
  144.  
  145.             if (SyncQueryRunner != null)
  146.             {
  147.                 SyncQueryRunner.Dispose();
  148.                 SyncQueryRunner = null;
  149.             }
  150.         }
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement