Advertisement
Guest User

Untitled

a guest
Jul 10th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.51 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.IO;
  11. using System.Net;
  12. using System.Net.Sockets;
  13.  
  14. namespace LoginServer
  15. {
  16.     public partial class LoginServer : Form
  17.     {
  18.  
  19.         public List<ClientSocket> _clientSockets { get; set; }
  20.         private byte[] _buffer = new byte[1024];
  21.         private Socket _serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  22.  
  23.  
  24.  
  25.         private Label lStatus;
  26.         private Label lClientsConnected;
  27.         private ListBox lbClients;
  28.         public TextBox tbLog;
  29.         private TextBox tbInput;
  30.         private Button bSendToSelected;
  31.         private Button bbroadcast;
  32.         private Timer tLog;
  33.         private Button bStartServer;
  34.         private IContainer components;
  35.  
  36.         private SpawnedObjectHolder _objHolder;
  37.  
  38.         public LoginServer()
  39.         {
  40.             InitializeComponent();
  41.             CheckForIllegalCrossThreadCalls = false;
  42.  
  43.             if (!Directory.Exists("log"))
  44.                 Directory.CreateDirectory("log");
  45.  
  46.             _clientSockets = new List<ClientSocket>();
  47.         }
  48.  
  49.         private void InitializeComponent()
  50.         {
  51.             this.components = new System.ComponentModel.Container();
  52.             this.lStatus = new System.Windows.Forms.Label();
  53.             this.lClientsConnected = new System.Windows.Forms.Label();
  54.             this.lbClients = new System.Windows.Forms.ListBox();
  55.             this.tbLog = new System.Windows.Forms.TextBox();
  56.             this.tbInput = new System.Windows.Forms.TextBox();
  57.             this.bSendToSelected = new System.Windows.Forms.Button();
  58.             this.bbroadcast = new System.Windows.Forms.Button();
  59.             this.tLog = new System.Windows.Forms.Timer(this.components);
  60.             this.bStartServer = new System.Windows.Forms.Button();
  61.             this.SuspendLayout();
  62.             //
  63.             // lStatus
  64.             //
  65.             this.lStatus.AutoSize = true;
  66.             this.lStatus.BackColor = System.Drawing.Color.DarkRed;
  67.             this.lStatus.Font = new System.Drawing.Font("Microsoft Sans Serif", 20F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  68.             this.lStatus.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(5)))));
  69.             this.lStatus.Location = new System.Drawing.Point(16, 376);
  70.             this.lStatus.Name = "lStatus";
  71.             this.lStatus.Size = new System.Drawing.Size(218, 31);
  72.             this.lStatus.TabIndex = 0;
  73.             this.lStatus.Text = "Status: Inactive";
  74.             //
  75.             // lClientsConnected
  76.             //
  77.             this.lClientsConnected.AutoSize = true;
  78.             this.lClientsConnected.Location = new System.Drawing.Point(16, 30);
  79.             this.lClientsConnected.Name = "lClientsConnected";
  80.             this.lClientsConnected.Size = new System.Drawing.Size(104, 13);
  81.             this.lClientsConnected.TabIndex = 1;
  82.             this.lClientsConnected.Text = "Clients connected: 0";
  83.             //
  84.             // lbClients
  85.             //
  86.             this.lbClients.FormattingEnabled = true;
  87.             this.lbClients.Location = new System.Drawing.Point(13, 47);
  88.             this.lbClients.Name = "lbClients";
  89.             this.lbClients.Size = new System.Drawing.Size(120, 303);
  90.             this.lbClients.TabIndex = 2;
  91.             //
  92.             // tbLog
  93.             //
  94.             this.tbLog.Location = new System.Drawing.Point(140, 47);
  95.             this.tbLog.Multiline = true;
  96.             this.tbLog.Name = "tbLog";
  97.             this.tbLog.ReadOnly = true;
  98.             this.tbLog.Size = new System.Drawing.Size(332, 302);
  99.             this.tbLog.TabIndex = 3;
  100.             this.tbLog.WordWrap = false;
  101.             //
  102.             // tbInput
  103.             //
  104.             this.tbInput.Location = new System.Drawing.Point(478, 47);
  105.             this.tbInput.Name = "tbInput";
  106.             this.tbInput.Size = new System.Drawing.Size(274, 20);
  107.             this.tbInput.TabIndex = 4;
  108.             //
  109.             // bSendToSelected
  110.             //
  111.             this.bSendToSelected.Location = new System.Drawing.Point(478, 73);
  112.             this.bSendToSelected.Name = "bSendToSelected";
  113.             this.bSendToSelected.Size = new System.Drawing.Size(274, 23);
  114.             this.bSendToSelected.TabIndex = 5;
  115.             this.bSendToSelected.Text = "Send To Selected";
  116.             this.bSendToSelected.UseVisualStyleBackColor = true;
  117.             this.bSendToSelected.Click += new System.EventHandler(this.bSendToSelected_Click);
  118.             //
  119.             // bbroadcast
  120.             //
  121.             this.bbroadcast.Location = new System.Drawing.Point(478, 102);
  122.             this.bbroadcast.Name = "bbroadcast";
  123.             this.bbroadcast.Size = new System.Drawing.Size(274, 23);
  124.             this.bbroadcast.TabIndex = 6;
  125.             this.bbroadcast.Text = "Broadcast";
  126.             this.bbroadcast.UseVisualStyleBackColor = true;
  127.             this.bbroadcast.Click += new System.EventHandler(this.bbroadcast_Click);
  128.             //
  129.             // tLog
  130.             //
  131.             this.tLog.Interval = 300000;
  132.             this.tLog.Tick += new System.EventHandler(this.tLog_Tick);
  133.             //
  134.             // bStartServer
  135.             //
  136.             this.bStartServer.Location = new System.Drawing.Point(13, 536);
  137.             this.bStartServer.Name = "bStartServer";
  138.             this.bStartServer.Size = new System.Drawing.Size(216, 34);
  139.             this.bStartServer.TabIndex = 7;
  140.             this.bStartServer.Text = "StartServer";
  141.             this.bStartServer.UseVisualStyleBackColor = true;
  142.             this.bStartServer.Click += new System.EventHandler(this.bStartServer_Click);
  143.             //
  144.             // LoginServer
  145.             //
  146.             this.ClientSize = new System.Drawing.Size(867, 582);
  147.             this.Controls.Add(this.bStartServer);
  148.             this.Controls.Add(this.bbroadcast);
  149.             this.Controls.Add(this.bSendToSelected);
  150.             this.Controls.Add(this.tbInput);
  151.             this.Controls.Add(this.tbLog);
  152.             this.Controls.Add(this.lbClients);
  153.             this.Controls.Add(this.lClientsConnected);
  154.             this.Controls.Add(this.lStatus);
  155.             this.Name = "LoginServer";
  156.             this.Text = "Login server";
  157.             this.Load += new System.EventHandler(this.LoginServer_Load);
  158.             this.ResumeLayout(false);
  159.             this.PerformLayout();
  160.  
  161.         }
  162.  
  163.         private void LoginServer_Load(object sender, EventArgs e)
  164.         {
  165.             //SetupServer();
  166.         }
  167.  
  168.         private void SetupServer()
  169.         {
  170.             lStatus.Text = "Setting up server";
  171.             tbLog.AppendText("Setting up server.... \n");
  172.             _serverSocket.Bind(new IPEndPoint(IPAddress.Any, 24022));
  173.             _serverSocket.Listen(1);
  174.             //SETUP SCRIPTS
  175.             _objHolder = new SpawnedObjectHolder(this);
  176.             tbLog.AppendText("SpawnedObjectHolder.cs - Setted up correctly\n"); //Contains spawned obj data
  177.  
  178.             lStatus.Text = "Server is running - [v0.01]";
  179.             tbLog.AppendText("Server is running.... \n");
  180.  
  181.             lStatus.BackColor = Color.Green;
  182.             tLog.Enabled = true;
  183.             _serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
  184.         }
  185.  
  186.         private void AcceptCallback(IAsyncResult AR)
  187.         {
  188.             Socket s = _serverSocket.EndAccept(AR);
  189.             _clientSockets.Add(new ClientSocket(s));
  190.             lbClients.Items.Add(s.RemoteEndPoint.ToString());
  191.             lClientsConnected.Text = "Clients connected: " + _clientSockets.Count.ToString();
  192.             tbLog.AppendText("New client connected" + s.RemoteEndPoint.ToString() + "\n\n");
  193.             s.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallBack), s);
  194.             _serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
  195.  
  196.             _objHolder.UpdateClient(s); //Buffer the clients
  197.         }
  198.  
  199.         private void ReceiveCallBack(IAsyncResult AR)
  200.         {
  201.             Socket s = (Socket)AR.AsyncState;
  202.             if (s.Connected)
  203.             {
  204.                 int receivedBytes;
  205.                 try
  206.                 {
  207.                     receivedBytes = s.EndReceive(AR);
  208.                 }
  209.  
  210.                 catch (Exception)
  211.                 {
  212.                     for (int i = 0; i < _clientSockets.Count; i++)
  213.                     {
  214.                         if (_clientSockets[i]._socket.RemoteEndPoint.ToString().Equals(s.RemoteEndPoint.ToString()))
  215.                         {
  216.                             lbClients.Items.RemoveAt(lbClients.Items.IndexOf(s.RemoteEndPoint.ToString()));
  217.                             _clientSockets.RemoveAt(i);
  218.                             lClientsConnected.Text = "Clients connected: " + _clientSockets.Count.ToString();
  219.                         }
  220.                     }
  221.  
  222.                     return;
  223.                 }
  224.  
  225.                 if(receivedBytes != 0)
  226.                 {
  227.                     byte[] databuffer = new byte[receivedBytes];
  228.                     Array.Copy(_buffer, databuffer, receivedBytes);
  229.                     string txtReceived = Encoding.ASCII.GetString(databuffer);
  230.                     HandlePacket(s, txtReceived);
  231.                 }
  232.  
  233.                 else
  234.                 {
  235.                     for (int i = 0; i < _clientSockets.Count; i++)
  236.                     {
  237.                         if (_clientSockets[i]._socket.RemoteEndPoint.ToString().Equals(s.RemoteEndPoint.ToString()))
  238.                         {
  239.                             lbClients.Items.RemoveAt(lbClients.Items.IndexOf(s.RemoteEndPoint.ToString()));
  240.                             _clientSockets.RemoveAt(i);
  241.                             lClientsConnected.Text = "Clients connected: " + _clientSockets.Count.ToString();
  242.                         }
  243.                     }
  244.                 }
  245.             }
  246.  
  247.             s.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallBack), s);
  248.         }
  249.  
  250.         public void SendData(Socket s, string msg)
  251.         {
  252.             byte[] data = Encoding.ASCII.GetBytes(msg);
  253.             s.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(SendCallBack), s);
  254.             _serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
  255.         }
  256.  
  257.         private void SendCallBack(IAsyncResult AR)
  258.         {
  259.             Socket s = (Socket)AR.AsyncState;
  260.             s.EndSend(AR);
  261.         }
  262.  
  263.         private void SendToSelected(string msg)
  264.         {
  265.             byte[] data = Encoding.ASCII.GetBytes(msg);
  266.  
  267.             for (int i = 0; i < lbClients.SelectedItems.Count; i++)
  268.             {
  269.                 for (int j = 0; j < _clientSockets.Count; j++)
  270.                 {
  271.                     if (lbClients.SelectedItems[i].ToString().Equals(_clientSockets[j]._socket.RemoteEndPoint.ToString()))
  272.                     {
  273.                         SendData(_clientSockets[j]._socket, msg);
  274.                     }
  275.                 }
  276.             }
  277.         }
  278.  
  279.         private void BroadCast(string msg)
  280.         {
  281.             for (int i = 0; i < _clientSockets.Count; i++)
  282.             {
  283.                 SendData(_clientSockets[i]._socket, msg);
  284.             }
  285.         }
  286.  
  287.         private void Respond(Socket s, string msg)
  288.         {
  289.             SendData(s, msg);
  290.         }
  291.         //Server
  292.         private void HandlePacket(Socket s, string data)
  293.         {
  294.             string[] splittedData = data.Split('/');
  295.  
  296.             switch (splittedData[0])
  297.             {
  298.                 default:
  299.                     tbLog.AppendText("Unkown package: " + data + "\n");
  300.                     break;
  301.  
  302.                 case "0x000":
  303.                     //FIRST LOGIN SPAWN IT SELF
  304.                     //update to all
  305.                     _objHolder.AddNewPlayer(new Vector3f(1 * _objHolder._players.Count, 1, 1), splittedData[1]);
  306.  
  307.                     //Repackage data to positions
  308.                     string _pos = "/" + 1 * _objHolder._players.Count + "," + 1+ "," + 1;
  309.                     string _rot = "/" + 1 + "," + 1 + "," + 1;
  310.                     string _scale = "/" + 1 + "," + 1 + "," + 1;
  311.  
  312.                     tbLog.AppendText("0x000" + _pos + _rot + _scale + " :::: " + splittedData[1] + "\n");
  313.  
  314.                     BroadCast("0x000" + _pos + _rot + _scale + "/" + splittedData[1]);
  315.                     break;
  316.                 case "0x001":
  317.                     //load in all
  318.                     //Spawn
  319.                     break;
  320.             }
  321.         }
  322.  
  323.         private bool HandleLogin(string user, string pass)
  324.         {
  325.             if (user == "USERNAME" && pass == "PASSWORD")
  326.             {
  327.                 return true;
  328.             }
  329.             else { return false; }
  330.         }
  331.  
  332.         private void LogToFile()
  333.         {
  334.             string file = ".\\log\\" + DateTime.Now.ToString("yyyy-M-d") + ".txt";
  335.             if (!File.Exists(file))
  336.             {
  337.                 File.Create(file).Dispose();
  338.                 using(TextWriter tw = new StreamWriter(file))
  339.                 {
  340.                     tw.WriteLine("Log file of " + DateTime.Now.ToString("yyyy-M-d"));
  341.                     tw.Close();
  342.                 }
  343.             }
  344.  
  345.             File.AppendAllLines(file, tbLog.Lines);
  346.             tbLog.Clear();
  347.             tbLog.AppendText("Logging saved");
  348.         }
  349.  
  350.         private void bSendToSelected_Click(object sender, EventArgs e)
  351.         {
  352.             SendToSelected(tbInput.Text);
  353.             tbLog.AppendText("Send to: " + tbInput.Text + "\n");
  354.         }
  355.  
  356.         private void bbroadcast_Click(object sender, EventArgs e)
  357.         {
  358.             BroadCast(tbInput.Text);
  359.             tbLog.AppendText("Broadcast all: " + tbInput.Text + "\n");
  360.         }
  361.  
  362.         private void tLog_Tick(object sender, EventArgs e)
  363.         {
  364.             LogToFile();
  365.         }
  366.  
  367.         private void bStartServer_Click(object sender, EventArgs e)
  368.         {
  369.             SetupServer();
  370.         }
  371.     }
  372.  
  373.     public class ClientSocket
  374.     {
  375.         public Socket _socket { get; set; }
  376.         public string _name { get; set; }
  377.        
  378.         public ClientSocket(Socket s)
  379.         {
  380.             this._socket = s;
  381.         }
  382.     }
  383. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement