⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frmmain.cs

📁 网络游戏征服的服务端部分完整源码 VC编译 绝少的源码 有兴趣的可以
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net.Sockets;
using System.Net;
using XYNetSocketLib;

namespace CO_Full_Server
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	
	public class frmMain : System.Windows.Forms.Form
	{
		private struct AuthBinder
		{
			public Socket Sock;
			public ulong Keys;
			public DateTime BoundTime;
			//public string AccountName;
			public COClient Client;
			public int Authenticated;
		}

		public const int AUTHPORT = 9958;
		public const int GAMEPORT = 5816;
		public const int MAXTHREADS = 35;
		public const int MINTHREADS = 6;

		private XYNetSocketLib.XYNetServer m_AuthServer;
		private XYNetSocketLib.XYNetServer m_GameServer;
		private System.Threading.Thread PingThread;
		private bool m_ServersUp = false;
		private Hashtable m_AuthConnectedClients;
		private Hashtable m_GameConnectedClients;
		private Hashtable m_ClientHash;
		private BackendDB m_Database;

		private System.Windows.Forms.Button btnListen;
		private System.Windows.Forms.ComboBox cmbBindIP;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.ListBox lstClients;
		private System.Windows.Forms.Button btnDisconnectClient;
		private System.Windows.Forms.Button btnRefresh;
		
		
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmMain()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			Control.CheckForIllegalCrossThreadCalls = false;
			//
			// TODO: Add any constructor code after InitializeComponent call
			//
			World.InitWorld();
			m_Database = new BackendDB();
			frmLogon NamePass = new frmLogon();
			bool DBOpen = false;
			while (!DBOpen)
			{
				NamePass.ShowDialog(this);
				DBOpen = BackendDB.StartDefault(NamePass.Username, NamePass.Password);
				if (!DBOpen)
					Console.WriteLine("Could not start database with that username/password combination", "Invalid username/password", 
						MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);
			}
			System.Net.IPHostEntry LocalHE;
			LocalHE = System.Net.Dns.GetHostEntry(Dns.GetHostName());
			cmbBindIP.Items.Clear();
			foreach (IPAddress ipCurrent in LocalHE.AddressList)
			{
				cmbBindIP.Items.Add(ipCurrent);
			}
			cmbBindIP.SelectedIndex = 0;
			m_AuthConnectedClients = new Hashtable(15);
			m_GameConnectedClients = new Hashtable(25);
			m_ClientHash = new Hashtable(25);
            //Control something = new Control();
            //something.che
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.btnListen = new System.Windows.Forms.Button();
			this.cmbBindIP = new System.Windows.Forms.ComboBox();
			this.lstClients = new System.Windows.Forms.ListBox();
			this.label1 = new System.Windows.Forms.Label();
			this.btnDisconnectClient = new System.Windows.Forms.Button();
			this.btnRefresh = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// btnListen
			// 
			this.btnListen.Location = new System.Drawing.Point(144, 8);
			this.btnListen.Name = "btnListen";
			this.btnListen.TabIndex = 3;
			this.btnListen.Text = "Listen";
			this.btnListen.Click += new System.EventHandler(this.btnListen_Click);
			// 
			// cmbBindIP
			// 
			this.cmbBindIP.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cmbBindIP.Location = new System.Drawing.Point(8, 8);
			this.cmbBindIP.Name = "cmbBindIP";
			this.cmbBindIP.Size = new System.Drawing.Size(121, 21);
			this.cmbBindIP.TabIndex = 2;
			// 
			// lstClients
			// 
			this.lstClients.Location = new System.Drawing.Point(16, 64);
			this.lstClients.Name = "lstClients";
			this.lstClients.Size = new System.Drawing.Size(248, 290);
			this.lstClients.TabIndex = 4;
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(16, 40);
			this.label1.Name = "label1";
			this.label1.TabIndex = 5;
			this.label1.Text = "Connected Clients";
			// 
			// btnDisconnectClient
			// 
			this.btnDisconnectClient.Location = new System.Drawing.Point(280, 64);
			this.btnDisconnectClient.Name = "btnDisconnectClient";
			this.btnDisconnectClient.TabIndex = 6;
			this.btnDisconnectClient.Text = "Disconnect";
			this.btnDisconnectClient.Click += new System.EventHandler(this.btnDisconnectClient_Click);
			// 
			// btnRefresh
			// 
			this.btnRefresh.Location = new System.Drawing.Point(280, 96);
			this.btnRefresh.Name = "btnRefresh";
			this.btnRefresh.TabIndex = 7;
			this.btnRefresh.Text = "Refresh";
			this.btnRefresh.Click += new System.EventHandler(this.btnRefresh_Click);
			// 
			// frmMain
			// 
			this.AcceptButton = this.btnListen;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(360, 374);
			this.Controls.Add(this.btnRefresh);
			this.Controls.Add(this.btnDisconnectClient);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.lstClients);
			this.Controls.Add(this.btnListen);
			this.Controls.Add(this.cmbBindIP);
			this.Name = "frmMain";
			this.Text = "Server";
			this.Load += new System.EventHandler(this.frmMain_Load);
			this.Closed += new System.EventHandler(this.frmMain_Closed);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new frmMain());
		}
		public void AuthExceptionHandler(Exception oBug, Socket sock)
		{
#if DEBUG
			Console.WriteLine("Auth Server: An exception of type \"" + oBug.Message + "\" has occured.");
			if (oBug.InnerException != null)
				Console.WriteLine("\tInner Exception: \"" + oBug.InnerException.Message + "\".");
#endif
			if (sock != null)
			{
				if (m_AuthConnectedClients.Contains(sock.RemoteEndPoint.ToString()))
				{
					m_AuthConnectedClients.Remove(sock.RemoteEndPoint.ToString());
					//lstClients.Items.Remove(sock.RemoteEndPoint.ToString());
				}
			}
		}
		public void AuthConnectionFilter(String sRemoteAddress, int nRemotePort, Socket sock)
		{
			//TODO: Add C#/SQL code to check for banned IPs
#if DEBUG
			Console.WriteLine("Incoming connection request on " + sRemoteAddress + ":" + nRemotePort.ToString());
#endif
			AuthBinder NewClient = new AuthBinder();
			NewClient.BoundTime = DateTime.Now;
			NewClient.Client = new COClient();

			NewClient.Sock = sock;
			m_AuthConnectedClients.Add(sock.RemoteEndPoint.ToString(), NewClient);
			//lstClients.Items.Add(sock.RemoteEndPoint.ToString());
		}
		public void AuthInputHandler(String sRemoteAddress, int nRemotePort, Byte[] pData)
		{
            byte [] Data = pData;
			AuthBinder ClientData;
			ClientData = (AuthBinder)m_AuthConnectedClients[sRemoteAddress + ":" + nRemotePort];


			ClientData.Client.Crypto.Decrypt(ref Data);
			if (Data[0] == 0x34 && Data[1] == 0x00 && Data[2] == 0x1b && Data[3] == 0x04) //This is the first packet: an authorization request
			{
				int x = 0x4;
				int Authenticated = 0;
				string AccountName = "";
				string Password = "";
				Random PRNG = new Random();
				while (x < 0x14 && Data[x] != 0x00)
				{
					AccountName = AccountName + Convert.ToChar(Data[x]);
					x++;
				}
				//ClientData.AccountName = AccountName;
				x = 0x14;
				while (x < 0x24)
				{
					Password = Password + (Convert.ToString(Data[x], 16)).PadLeft(2, '0');
					x++;
				}
				Authenticated = BackendDB.AuthenticateUser(AccountName, Password, sRemoteAddress);
				if (Authenticated != 0)  //Valid Password
				{
					ClientData.Authenticated = Authenticated;
					ClientData.Client.AccountStatus = Authenticated;
					ClientData.Keys = (uint)(PRNG.Next(10000000) << 32);// | PRNG.Next(10000000);
					ClientData.Keys = ClientData.Keys << 32;
					ClientData.Keys = (uint)(ClientData.Keys | (uint)PRNG.Next(10000000));
					ClientData.Client.Account = AccountName;
					byte[] ReturnPacket;
					byte[] Key1 = new byte[4];
					byte[] Key2 = new byte[4];
					Key1[0] = (byte)(((ulong)ClientData.Keys & 0xff00000000000000L) >> 56);
					Key1[1] = (byte)((ClientData.Keys & 0xff000000000000) >> 48);
					Key1[2] = (byte)((ClientData.Keys & 0xff0000000000) >> 40);
					Key1[3] = (byte)((ClientData.Keys & 0xff00000000) >> 32);
					Key2[0] = (byte)((ClientData.Keys & 0xff000000) >> 24);
					Key2[1] = (byte)((ClientData.Keys & 0xff0000) >> 16);
					Key2[2] = (byte)((ClientData.Keys & 0xff00) >> 8);
					Key2[3] = (byte)(ClientData.Keys & 0xff);
					//TODO: Add a table lookup to allow for multiple servers

					string Server = "";
					string ServerIP;
					x = 0x24;
					while (x < 0x34 && Data[x] != 0x00)
					{
						Server += Convert.ToChar(Data[x]);
						x++;
					}
					ServerIP = BackendDB.LookupServer(Server);

					ReturnPacket = PacketBuilder.AuthResponse(ServerIP, Key1, Key2);
					ClientData.Client.Crypto.Encrypt(ref ReturnPacket);
					m_AuthServer.SendRawData(sRemoteAddress, nRemotePort, ReturnPacket);

					m_ClientHash.Add(ClientData.Keys, ClientData);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -