Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.IO;
- using System.Net;
- using System.Net.Sockets;
- namespace LoginServer
- {
- public partial class LoginServer : Form
- {
- public List<ClientSocket> _clientSockets { get; set; }
- private byte[] _buffer = new byte[1024];
- private Socket _serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- private Label lStatus;
- private Label lClientsConnected;
- private ListBox lbClients;
- public TextBox tbLog;
- private TextBox tbInput;
- private Button bSendToSelected;
- private Button bbroadcast;
- private Timer tLog;
- private Button bStartServer;
- private IContainer components;
- private SpawnedObjectHolder _objHolder;
- public LoginServer()
- {
- InitializeComponent();
- CheckForIllegalCrossThreadCalls = false;
- if (!Directory.Exists("log"))
- Directory.CreateDirectory("log");
- _clientSockets = new List<ClientSocket>();
- }
- private void InitializeComponent()
- {
- this.components = new System.ComponentModel.Container();
- this.lStatus = new System.Windows.Forms.Label();
- this.lClientsConnected = new System.Windows.Forms.Label();
- this.lbClients = new System.Windows.Forms.ListBox();
- this.tbLog = new System.Windows.Forms.TextBox();
- this.tbInput = new System.Windows.Forms.TextBox();
- this.bSendToSelected = new System.Windows.Forms.Button();
- this.bbroadcast = new System.Windows.Forms.Button();
- this.tLog = new System.Windows.Forms.Timer(this.components);
- this.bStartServer = new System.Windows.Forms.Button();
- this.SuspendLayout();
- //
- // lStatus
- //
- this.lStatus.AutoSize = true;
- this.lStatus.BackColor = System.Drawing.Color.DarkRed;
- 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)));
- this.lStatus.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(5)))));
- this.lStatus.Location = new System.Drawing.Point(16, 376);
- this.lStatus.Name = "lStatus";
- this.lStatus.Size = new System.Drawing.Size(218, 31);
- this.lStatus.TabIndex = 0;
- this.lStatus.Text = "Status: Inactive";
- //
- // lClientsConnected
- //
- this.lClientsConnected.AutoSize = true;
- this.lClientsConnected.Location = new System.Drawing.Point(16, 30);
- this.lClientsConnected.Name = "lClientsConnected";
- this.lClientsConnected.Size = new System.Drawing.Size(104, 13);
- this.lClientsConnected.TabIndex = 1;
- this.lClientsConnected.Text = "Clients connected: 0";
- //
- // lbClients
- //
- this.lbClients.FormattingEnabled = true;
- this.lbClients.Location = new System.Drawing.Point(13, 47);
- this.lbClients.Name = "lbClients";
- this.lbClients.Size = new System.Drawing.Size(120, 303);
- this.lbClients.TabIndex = 2;
- //
- // tbLog
- //
- this.tbLog.Location = new System.Drawing.Point(140, 47);
- this.tbLog.Multiline = true;
- this.tbLog.Name = "tbLog";
- this.tbLog.ReadOnly = true;
- this.tbLog.Size = new System.Drawing.Size(332, 302);
- this.tbLog.TabIndex = 3;
- this.tbLog.WordWrap = false;
- //
- // tbInput
- //
- this.tbInput.Location = new System.Drawing.Point(478, 47);
- this.tbInput.Name = "tbInput";
- this.tbInput.Size = new System.Drawing.Size(274, 20);
- this.tbInput.TabIndex = 4;
- //
- // bSendToSelected
- //
- this.bSendToSelected.Location = new System.Drawing.Point(478, 73);
- this.bSendToSelected.Name = "bSendToSelected";
- this.bSendToSelected.Size = new System.Drawing.Size(274, 23);
- this.bSendToSelected.TabIndex = 5;
- this.bSendToSelected.Text = "Send To Selected";
- this.bSendToSelected.UseVisualStyleBackColor = true;
- this.bSendToSelected.Click += new System.EventHandler(this.bSendToSelected_Click);
- //
- // bbroadcast
- //
- this.bbroadcast.Location = new System.Drawing.Point(478, 102);
- this.bbroadcast.Name = "bbroadcast";
- this.bbroadcast.Size = new System.Drawing.Size(274, 23);
- this.bbroadcast.TabIndex = 6;
- this.bbroadcast.Text = "Broadcast";
- this.bbroadcast.UseVisualStyleBackColor = true;
- this.bbroadcast.Click += new System.EventHandler(this.bbroadcast_Click);
- //
- // tLog
- //
- this.tLog.Interval = 300000;
- this.tLog.Tick += new System.EventHandler(this.tLog_Tick);
- //
- // bStartServer
- //
- this.bStartServer.Location = new System.Drawing.Point(13, 536);
- this.bStartServer.Name = "bStartServer";
- this.bStartServer.Size = new System.Drawing.Size(216, 34);
- this.bStartServer.TabIndex = 7;
- this.bStartServer.Text = "StartServer";
- this.bStartServer.UseVisualStyleBackColor = true;
- this.bStartServer.Click += new System.EventHandler(this.bStartServer_Click);
- //
- // LoginServer
- //
- this.ClientSize = new System.Drawing.Size(867, 582);
- this.Controls.Add(this.bStartServer);
- this.Controls.Add(this.bbroadcast);
- this.Controls.Add(this.bSendToSelected);
- this.Controls.Add(this.tbInput);
- this.Controls.Add(this.tbLog);
- this.Controls.Add(this.lbClients);
- this.Controls.Add(this.lClientsConnected);
- this.Controls.Add(this.lStatus);
- this.Name = "LoginServer";
- this.Text = "Login server";
- this.Load += new System.EventHandler(this.LoginServer_Load);
- this.ResumeLayout(false);
- this.PerformLayout();
- }
- private void LoginServer_Load(object sender, EventArgs e)
- {
- //SetupServer();
- }
- private void SetupServer()
- {
- lStatus.Text = "Setting up server";
- tbLog.AppendText("Setting up server.... \n");
- _serverSocket.Bind(new IPEndPoint(IPAddress.Any, 24022));
- _serverSocket.Listen(1);
- //SETUP SCRIPTS
- _objHolder = new SpawnedObjectHolder(this);
- tbLog.AppendText("SpawnedObjectHolder.cs - Setted up correctly\n"); //Contains spawned obj data
- lStatus.Text = "Server is running - [v0.01]";
- tbLog.AppendText("Server is running.... \n");
- lStatus.BackColor = Color.Green;
- tLog.Enabled = true;
- _serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
- }
- private void AcceptCallback(IAsyncResult AR)
- {
- Socket s = _serverSocket.EndAccept(AR);
- _clientSockets.Add(new ClientSocket(s));
- lbClients.Items.Add(s.RemoteEndPoint.ToString());
- lClientsConnected.Text = "Clients connected: " + _clientSockets.Count.ToString();
- tbLog.AppendText("New client connected" + s.RemoteEndPoint.ToString() + "\n\n");
- s.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallBack), s);
- _serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
- _objHolder.UpdateClient(s); //Buffer the clients
- }
- private void ReceiveCallBack(IAsyncResult AR)
- {
- Socket s = (Socket)AR.AsyncState;
- if (s.Connected)
- {
- int receivedBytes;
- try
- {
- receivedBytes = s.EndReceive(AR);
- }
- catch (Exception)
- {
- for (int i = 0; i < _clientSockets.Count; i++)
- {
- if (_clientSockets[i]._socket.RemoteEndPoint.ToString().Equals(s.RemoteEndPoint.ToString()))
- {
- lbClients.Items.RemoveAt(lbClients.Items.IndexOf(s.RemoteEndPoint.ToString()));
- _clientSockets.RemoveAt(i);
- lClientsConnected.Text = "Clients connected: " + _clientSockets.Count.ToString();
- }
- }
- return;
- }
- if(receivedBytes != 0)
- {
- byte[] databuffer = new byte[receivedBytes];
- Array.Copy(_buffer, databuffer, receivedBytes);
- string txtReceived = Encoding.ASCII.GetString(databuffer);
- HandlePacket(s, txtReceived);
- }
- else
- {
- for (int i = 0; i < _clientSockets.Count; i++)
- {
- if (_clientSockets[i]._socket.RemoteEndPoint.ToString().Equals(s.RemoteEndPoint.ToString()))
- {
- lbClients.Items.RemoveAt(lbClients.Items.IndexOf(s.RemoteEndPoint.ToString()));
- _clientSockets.RemoveAt(i);
- lClientsConnected.Text = "Clients connected: " + _clientSockets.Count.ToString();
- }
- }
- }
- }
- s.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallBack), s);
- }
- public void SendData(Socket s, string msg)
- {
- byte[] data = Encoding.ASCII.GetBytes(msg);
- s.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(SendCallBack), s);
- _serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
- }
- private void SendCallBack(IAsyncResult AR)
- {
- Socket s = (Socket)AR.AsyncState;
- s.EndSend(AR);
- }
- private void SendToSelected(string msg)
- {
- byte[] data = Encoding.ASCII.GetBytes(msg);
- for (int i = 0; i < lbClients.SelectedItems.Count; i++)
- {
- for (int j = 0; j < _clientSockets.Count; j++)
- {
- if (lbClients.SelectedItems[i].ToString().Equals(_clientSockets[j]._socket.RemoteEndPoint.ToString()))
- {
- SendData(_clientSockets[j]._socket, msg);
- }
- }
- }
- }
- private void BroadCast(string msg)
- {
- for (int i = 0; i < _clientSockets.Count; i++)
- {
- SendData(_clientSockets[i]._socket, msg);
- }
- }
- private void Respond(Socket s, string msg)
- {
- SendData(s, msg);
- }
- //Server
- private void HandlePacket(Socket s, string data)
- {
- string[] splittedData = data.Split('/');
- switch (splittedData[0])
- {
- default:
- tbLog.AppendText("Unkown package: " + data + "\n");
- break;
- case "0x000":
- //FIRST LOGIN SPAWN IT SELF
- //update to all
- _objHolder.AddNewPlayer(new Vector3f(1 * _objHolder._players.Count, 1, 1), splittedData[1]);
- //Repackage data to positions
- string _pos = "/" + 1 * _objHolder._players.Count + "," + 1+ "," + 1;
- string _rot = "/" + 1 + "," + 1 + "," + 1;
- string _scale = "/" + 1 + "," + 1 + "," + 1;
- tbLog.AppendText("0x000" + _pos + _rot + _scale + " :::: " + splittedData[1] + "\n");
- BroadCast("0x000" + _pos + _rot + _scale + "/" + splittedData[1]);
- break;
- case "0x001":
- //load in all
- //Spawn
- break;
- }
- }
- private bool HandleLogin(string user, string pass)
- {
- if (user == "USERNAME" && pass == "PASSWORD")
- {
- return true;
- }
- else { return false; }
- }
- private void LogToFile()
- {
- string file = ".\\log\\" + DateTime.Now.ToString("yyyy-M-d") + ".txt";
- if (!File.Exists(file))
- {
- File.Create(file).Dispose();
- using(TextWriter tw = new StreamWriter(file))
- {
- tw.WriteLine("Log file of " + DateTime.Now.ToString("yyyy-M-d"));
- tw.Close();
- }
- }
- File.AppendAllLines(file, tbLog.Lines);
- tbLog.Clear();
- tbLog.AppendText("Logging saved");
- }
- private void bSendToSelected_Click(object sender, EventArgs e)
- {
- SendToSelected(tbInput.Text);
- tbLog.AppendText("Send to: " + tbInput.Text + "\n");
- }
- private void bbroadcast_Click(object sender, EventArgs e)
- {
- BroadCast(tbInput.Text);
- tbLog.AppendText("Broadcast all: " + tbInput.Text + "\n");
- }
- private void tLog_Tick(object sender, EventArgs e)
- {
- LogToFile();
- }
- private void bStartServer_Click(object sender, EventArgs e)
- {
- SetupServer();
- }
- }
- public class ClientSocket
- {
- public Socket _socket { get; set; }
- public string _name { get; set; }
- public ClientSocket(Socket s)
- {
- this._socket = s;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement