Advertisement
Joker119

Untitled

Dec 11th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.60 KB | None | 0 0
  1.     public class ProcessSTT
  2.     {
  3.         private static ConcurrentDictionary<int, TcpClient> bag = new ConcurrentDictionary<int, TcpClient>();
  4.         private static ConcurrentDictionary<int, int> heartbeats = new ConcurrentDictionary<int, int>();
  5.  
  6.         public static void SendData(string data, int port, ulong channel = 0)
  7.         {
  8.             try
  9.             {
  10.                 BinaryFormatter formatter = new BinaryFormatter();
  11.                 SerializedData.SerializedData serializedData =
  12.                     new SerializedData.SerializedData { Data = data, Port = port, Channel = channel };
  13.                 //Console.WriteLine($"Sending {serializedData.Data}");
  14.                 if (!bag.ContainsKey(port))
  15.                 {
  16.                     Console.WriteLine($"STT: Bag does not contain {port}");
  17.                     return;
  18.                 }
  19.  
  20.                 if (bag[port] != null && bag[port].Connected)
  21.                     formatter.Serialize(bag[port].GetStream(), serializedData);
  22.                 else
  23.                 {
  24.                     Console.WriteLine($"Error - Bag {port} is null or not connected.");
  25.                     if (bag.TryRemove(port, out TcpClient client))
  26.                         client.Dispose();
  27.                 }
  28.             }
  29.             catch (IOException s)
  30.             {
  31.                 Console.WriteLine($"STT: Socket exception, removing..");
  32.                 KeyValuePair<int, TcpClient> thingything = default;
  33.                 foreach (var thing in bag)
  34.                     if (thing.Key == port)
  35.                         thingything = thing;
  36.  
  37.                 if (bag.TryRemove(thingything.Key, out TcpClient _client))
  38.                 {
  39.                     _client.Close();
  40.                 }
  41.             }
  42.             catch (Exception e)
  43.             {
  44.                 Console.WriteLine(e);
  45.             }
  46.         }
  47.  
  48.         private static List<TcpListener> listener = new List<TcpListener>();
  49.         public static void Init()
  50.         {
  51.             for (int i = 1; i < 8; i++)
  52.             {
  53.                 TcpListener list = new TcpListener(IPAddress.Loopback, 11900 + i);
  54.                 Console.WriteLine($"STT: Listener started for port {11900 + i}");
  55.                 listener.Add(list);
  56.                 list.Start();
  57.                 ThreadPool.QueueUserWorkItem(ListenForConn, list);
  58.             }
  59.         }
  60.  
  61.         public static async Task Heartbeat(int port)
  62.         {
  63.             await Task.Delay(10000);
  64.             for (;;)
  65.             {
  66.                 Console.WriteLine("STT: Starting Heartbeat");
  67.                 if (heartbeats[port] > 3)
  68.                 {
  69.                     Console.WriteLine($"STT: Removing {port} due to heartbeat timeout.");
  70.                     if (bag.TryRemove(port, out TcpClient client))
  71.                         client.Close();
  72.                     heartbeats.TryRemove(port, out int _);
  73.                     return;
  74.                 }
  75.  
  76.                 heartbeats[port]++;
  77.  
  78.                 Console.WriteLine($"STT: Sending heartbeat to: {port}");
  79.                 if (!bag[port].Connected)
  80.                 {
  81.                     Console.WriteLine($"STT: {port} is null, removing.");
  82.                     if (bag.TryRemove(port, out TcpClient client))
  83.                         client.Close();
  84.  
  85.                     return;
  86.                 }
  87.                 SendData("ping", port, 653737934150959115);
  88.                 await Task.Delay(10000);
  89.             }
  90.         }
  91.  
  92.         public static void ListenForConn(object token)
  93.         {
  94.             Console.WriteLine("STT: Listener started.");
  95.             TcpListener listen = token as TcpListener;
  96.                 for (;;)
  97.                 {
  98.                     try
  99.                     {
  100.                         TcpClient thing = listen.AcceptTcpClient();
  101.                         ThreadPool.QueueUserWorkItem(ListenOn, thing);
  102.                     }
  103.                     catch (Exception e)
  104.                     {
  105.                         Console.WriteLine(e);
  106.                     }
  107.             }
  108.         }
  109.  
  110.         public static async Task ReceiveData(SerializedData.SerializedData data, TcpClient client)
  111.         {
  112.             try
  113.             {
  114.                 if (data == null)
  115.                 {
  116.                     Console.WriteLine("STT: Received data null");
  117.                     return;
  118.                 }
  119.  
  120.                 if (data.Data == "ping")
  121.                 {
  122.                     if (!bag.ContainsKey(data.Port))
  123.                     {
  124.                         Console.WriteLine($"STT: Adding {data.Port}");
  125.                         bag.TryAdd(data.Port, client);
  126.                     }
  127.  
  128.                     if (!bag[data.Port].Connected || bag[data.Port] == null)
  129.                     {
  130.                         Console.WriteLine($"STT: Bag {data.Port} not connected or null, removing.");
  131.                         if (bag.TryRemove(data.Port, out TcpClient cli))
  132.                         {
  133.                             cli?.Close();
  134.                         }
  135.                     }
  136.                     Console.WriteLine($"STT: Received heartbeat for: {data.Port}");
  137.                     if (!heartbeats.ContainsKey(data.Port))
  138.                     {
  139.                         Heartbeat(data.Port);
  140.                         heartbeats.TryAdd(data.Port, 0);
  141.                     }
  142.                     else
  143.                         heartbeats[data.Port]--;
  144.                     return;
  145.                 }
  146.  
  147.                 Console.WriteLine(data.Data);
  148.                 data.Data = data.Data.Substring(data.Data.IndexOf('#') + 1);
  149.  
  150.                 //Console.WriteLine("Getting guild.");
  151.                 SocketGuild guild = Bot.Discord.GetGuild(478381106798788639);
  152.                 //Console.WriteLine("Getting channel");
  153.                 SocketTextChannel chan = guild.GetTextChannel(data.Channel);
  154.                 //Console.WriteLine("Sending message.");
  155.                 await chan.SendMessageAsync($"Server {data.Port -= 7770}: {data.Data}");
  156.                 if (data.Port == 7771)
  157.                 {
  158.                     DiscordWebhookClient webhook = new DiscordWebhookClient(
  159.                         "https://discordapp.com/api/webhooks/653799215700508683/BgC-7l18H_NhXk3Qz-3lbOEqBLn2u7LZTOXNPovFB2bJ3bNT9_hsU2vQ9BdaUY6n0jKP");
  160.                     await webhook.SendMessageAsync($"{data.Data}");
  161.                 }
  162.             }
  163.             catch (Exception e)
  164.             {
  165.                 Console.WriteLine(e);
  166.             }
  167.  
  168.         }
  169.  
  170.         public static void ListenOn(object token)
  171.         {
  172.             TcpClient client = token as TcpClient;
  173.             try
  174.             {
  175.                 BinaryFormatter formatter = new BinaryFormatter();
  176.                 for (;;)
  177.                 {
  178.                     SerializedData.SerializedData serializedData;
  179.                     if (!client.Connected)
  180.                     {
  181.                         Console.WriteLine($"Client not connected..");
  182.                         client.Close();
  183.                         continue;
  184.                     }
  185.                    
  186.                     serializedData = formatter.Deserialize(client.GetStream()) as SerializedData.SerializedData;
  187.                     ReceiveData(serializedData, client);
  188.                 }
  189.             }
  190.             catch (SerializationException s)
  191.             {
  192.                 Console.WriteLine($"STT: Serialization exception, removing..");
  193.                 KeyValuePair<int, TcpClient> thingything = default;
  194.                 foreach (var thing in bag)
  195.                     if (thing.Value == client)
  196.                         thingything = thing;
  197.  
  198.                 if (bag.TryRemove(thingything.Key, out TcpClient _client))
  199.                 {
  200.                     _client.Close();
  201.                 }
  202.             }
  203.             catch (Exception e)
  204.             {
  205.                 Console.WriteLine(e);
  206.             }
  207.         }
  208.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement