Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.08 KB | None | 0 0
  1. using SimpleJSON;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Net.Security;
  7. using System.Net.Sockets;
  8. using System.Text;
  9. using Verse;
  10.  
  11. namespace TwitchToolkit.SocketClient
  12. {
  13.     public class Client
  14.     {
  15.         private Socket Socket { get; set; } = null;
  16.  
  17.         private SslStream SslStream { get; set;  } = null;
  18.  
  19.         private readonly string server;
  20.  
  21.         private readonly int port;
  22.  
  23.         public Client(string server, int port)
  24.         {
  25.             this.server = server;
  26.             this.port = port;
  27.  
  28.             ConnectSocket();
  29.  
  30.             //Socket.BeginConnect(server, port, new AsyncCallback(ProcessClient), null);
  31.         }
  32.  
  33.         void ConnectSocket()
  34.         {
  35.             IPHostEntry hostEntry = null;
  36.  
  37.             // get host related information
  38.             hostEntry = Dns.GetHostEntry(server);
  39.  
  40.             foreach (IPAddress address in hostEntry.AddressList)
  41.             {
  42.                 IPEndPoint ipe = new IPEndPoint(address, port);
  43.                 Socket tempSocket =
  44.                     new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  45.  
  46.                 Helper.Log("Attempting socket connect");
  47.                 tempSocket.BeginConnect(ipe, new AsyncCallback(ProcessClient), tempSocket);
  48.             }
  49.         }
  50.  
  51.         void ProcessClient(IAsyncResult asyncResult)
  52.         {
  53.             Socket tempSocket = asyncResult.AsyncState as Socket;
  54.             if (tempSocket.Connected)
  55.             {
  56.                 Socket = tempSocket;
  57.             }
  58.             else
  59.             {
  60.                 return;
  61.             }
  62.  
  63.             SslStream = new SslStream(new NetworkStream(Socket), false, (object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, SslPolicyErrors sslPolicyError) => { return true; });
  64.  
  65.             try
  66.             {
  67.                 Helper.Log("attempting authentication");
  68.                 SslStream.AuthenticateAsClient(server);
  69.  
  70.                 SslStream.ReadTimeout = 5000;
  71.                 SslStream.WriteTimeout = 5000;
  72.  
  73.                 byte[] message = Helper.LanguageEncoding().GetBytes("{\"type\":\"hello\",\"id\":1,\"version\":\"2\"}");
  74.                 SslStream.Write(message);
  75.  
  76.                 Read();
  77.             }
  78.             catch (Exception e)
  79.             {
  80.                 Log.Error("Client Exception: " + e.Message);
  81.                 if (e.InnerException != null)
  82.                 {
  83.                     Log.Error("Inner: " + e.InnerException.Message);
  84.                 }
  85.  
  86.                 SslStream.Close();
  87.                 Socket.Close();
  88.             }
  89.            
  90.         }
  91.  
  92.         byte[] buffer;
  93.  
  94.         void Read()
  95.         {
  96.             try
  97.             {
  98.                 buffer = new byte[2048];
  99.                 SslStream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(ReadCallback), null);
  100.             }
  101.             catch (Exception e)
  102.             {
  103.                 Log.Error(e.Message);
  104.             }
  105.         }
  106.  
  107.         void ReadCallback(IAsyncResult asyncResult)
  108.         {
  109.             int bytes = SslStream.EndRead(asyncResult);
  110.             string message = ReadMessage(bytes);
  111.             if (message.Length > -1)
  112.             {
  113.                 Helper.Log("message length: " + message.Length + " - " + message);
  114.             }
  115.             Read();
  116.         }
  117.  
  118.         string ReadMessage(int bytes = -1)
  119.         {
  120.             StringBuilder messageData = new StringBuilder();
  121.  
  122.             do
  123.             {
  124.                 Decoder decoder = Helper.LanguageEncoding().GetDecoder();
  125.                 char [] chars = new char[decoder.GetCharCount(buffer, 0, bytes)];
  126.                 messageData.Append(chars);
  127.  
  128.                 if (bytes > 2048)
  129.                 {
  130.                     bytes -= 2048;
  131.                 }
  132.                 else
  133.                 {
  134.                     bytes -= bytes;
  135.                 }
  136.  
  137.             } while (bytes != 0);
  138.  
  139.             return messageData.ToString();
  140.         }
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement