Advertisement
CraftLifeGaming

ProxyServer.cs

Nov 28th, 2017
513
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.74 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Net;
  7. using System.Net.Sockets;
  8. using System.Threading;
  9. using System.Windows.Forms;
  10. using System.IO;
  11.  
  12. namespace WouldRevealProjectName.Proxy
  13. {
  14.     public class ProxyServer
  15.     {
  16.         #region Header Variables
  17.         private Thread listenerThread;
  18.         private TcpClient mcServer;
  19.  
  20.         private NetworkStream stream = null;
  21.  
  22.         private List<ProxyClient> clients = new List<ProxyClient>();
  23.  
  24.         private string host;
  25.         private string remoteHost;
  26.         private int port;
  27.         private int serverport;
  28.         private bool acceptClients = false;
  29.         #endregion
  30.  
  31.         public ProxyServer()
  32.         {
  33.             remoteHost = Program.GetConfigProperty("remote-address", "127.0.0.1");
  34.             host = Program.GetServerProperty("server-ip", "127.0.0.1");
  35.             port = Convert.ToInt32(Program.GetConfigProperty("listening-port", "25565"));
  36.             serverport = Convert.ToInt32(Program.GetServerProperty("server-port", "25566"));
  37.  
  38.             listenerThread = new Thread(__ListenerThread);
  39.             mcServer = new TcpClient();
  40.         }
  41.  
  42.         public List<ProxyClient> GetClientList()
  43.         {
  44.             return clients;
  45.         }
  46.  
  47.         public bool Start()
  48.         {
  49.             try
  50.             {
  51.                 listenerThread.Start();
  52.                 mcServer.Connect(IPAddress.Parse("127.0.0.1"), serverport);
  53.                 acceptClients = true;
  54.  
  55.                 return true;
  56.             }
  57.             catch (Exception ex)
  58.             {
  59.                 this.Stop();
  60.                 MessageBox.Show("Exception caught inside of ProxyServer::Start\r\n" + ex.Message);
  61.                 return false;
  62.             }
  63.         }
  64.  
  65.         public bool Stop()
  66.         {
  67.             try
  68.             {
  69.                 listenerThread.Abort();
  70.                 acceptClients = false;
  71.  
  72.                 return true;
  73.             }
  74.             catch (Exception ex)
  75.             {
  76.                 MessageBox.Show("Exception caught inside of ProxyServer::Stop\r\n" + ex.Message);
  77.                 return false;
  78.             }
  79.         }
  80.  
  81.         public bool Dispose()
  82.         {
  83.             try
  84.             {
  85.                 listenerThread.Abort();
  86.  
  87.                 while (listenerThread.IsAlive)
  88.                 {
  89.                     listenerThread.Abort();
  90.                     Thread.Sleep(10);
  91.                 }
  92.  
  93.                 return true;
  94.             }
  95.             catch (Exception ex)
  96.             {
  97.                 MessageBox.Show("Exception caught inside of ProxyServer::Dispose\r\n" + ex.Message);
  98.                 return false;
  99.             }
  100.         }
  101.  
  102.         #region Threads
  103.         private void __ListenerThread()
  104.         {
  105.             TcpListener listener;
  106.  
  107.             try
  108.             {
  109.                 listener = new TcpListener(IPAddress.Parse(host), port);
  110.                 listener.Start();
  111.                
  112.                 if (listener.Server.Connected)
  113.                 {
  114.                     SetStream(new NetworkStream(listener.Server));
  115.                 }
  116.             }
  117.             catch (Exception ex) { SimpleLogger.log(System.Drawing.Color.Crimson, "Exception::ListenerThread: " + ex.Message); return; }
  118.  
  119.             while (acceptClients)
  120.             {
  121.                 if (listener.Pending())
  122.                 {
  123.                     try
  124.                     {
  125.                         TcpClient acceptTcpClient = listener.AcceptTcpClient();
  126.  
  127.                         ProxyClient client = new ProxyClient(acceptTcpClient, this);
  128.                         client.Start(host, serverport);
  129.                         clients.Add(client);
  130.  
  131.                         SimpleLogger.log(System.Drawing.Color.Green, "(" + acceptTcpClient.Client.RemoteEndPoint.ToString() + ") connected!");
  132.                     }
  133.                     catch (Exception ex) { SimpleLogger.log(System.Drawing.Color.Crimson, "Exception::ListenerThread: " + ex.Message); }
  134.                 }
  135.             }
  136.         }
  137.  
  138.         private void __PlayerListRefreshThread()
  139.         {
  140.             while (true)
  141.             {
  142.                 foreach (ProxyClient pc in clients)
  143.                 {
  144.                     Program.mainFrm.lbPlayers.Items.Clear();
  145.                     Program.mainFrm.lbPlayers.Items.Add(pc);
  146.                 }
  147.  
  148.                 Thread.Sleep(1000);
  149.             }
  150.         }
  151.         #endregion
  152.  
  153.         #region Networking Methods
  154.         public NetworkStream GetStream()
  155.         {
  156.             return stream;
  157.         }
  158.  
  159.         public void SetStream(NetworkStream stream)
  160.         {
  161.             this.stream = stream;
  162.         }
  163.         #endregion
  164.     }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement