diff --git a/FunGame.Server/Main.cs b/FunGame.Server/Main.cs index 185ae37..dbd29a0 100644 --- a/FunGame.Server/Main.cs +++ b/FunGame.Server/Main.cs @@ -5,7 +5,6 @@ using Milimoe.FunGame.Core.Library.Constant; using Milimoe.FunGame.Server.Model; using Milimoe.FunGame.Server.Others; using Milimoe.FunGame.Server.Utility; -using Milimoe.FunGame.Server.Utility.DataUtility; Console.Title = Config.SERVER_NAME; Console.WriteLine(FunGameInfo.GetInfo((FunGameInfo.FunGame)Config.FunGameType)); @@ -64,7 +63,6 @@ void StartServer() ServerHelper.WriteLine("未检测到配置文件,将自动创建配置文件 . . ."); INIHelper.Init((FunGameInfo.FunGame)Config.FunGameType); ServerHelper.WriteLine("配置文件FunGame.ini创建成功,请修改该配置文件,然后重启服务器。"); - ServerHelper.WriteLine("请输入 help 来获取帮助,输入 quit 关闭服务器。"); return; } else @@ -72,21 +70,19 @@ void StartServer() ServerHelper.GetServerSettings(); Console.Title = Config.SERVER_NAME + " - FunGame Server Port: " + Config.SERVER_PORT; } + ServerHelper.WriteLine("请输入 help 来获取帮助,输入 quit 关闭服务器。"); - MySQLConnection.Close(); - - // 连接MySQL服务器 - if (!MySQLConnection.Connect(out _)) + // 测试MySQL服务器连接 + if (TestSQLConnection() != SQLResult.Success) { Running = false; - throw new ServerErrorException(); + throw new SQLQueryException(); } // 创建监听 ListeningSocket = ServerSocket.StartListening(); // 开始监听连接 - //ServerSocket.Listen(Config.MAX_PLAYERS); ServerHelper.WriteLine("Listen -> " + Config.SERVER_PORT); ServerHelper.WriteLine("服务器启动成功,开始监听 . . ."); @@ -127,7 +123,7 @@ void StartServer() } catch (Exception e) { - if (e.Message.Equals("服务器遇到问题需要关闭,请重新启动服务器!")) + if (e.Message.Equals(new ServerErrorException().Message)) { if (ListeningSocket != null) { @@ -182,4 +178,13 @@ bool Send(ClientSocket socket) else ServerHelper.WriteLine("无法传输数据,与客户端的连接可能丢失。"); return false; +} + +SQLResult TestSQLConnection() +{ + SQLResult TestResult = SQLResult.Success; + MySQLHelper SQLHelper = MySQLHelper.GetHelper(); + SQLHelper.Script = SQLConstant.Insert_ServerLoginLogs(Config.SERVER_NAME, Config.SERVER_KEY); + SQLHelper.Execute(out TestResult); + return TestResult; } \ No newline at end of file diff --git a/FunGame.Server/Model/ServerModel.cs b/FunGame.Server/Model/ServerModel.cs index d244339..e2dd2d4 100644 --- a/FunGame.Server/Model/ServerModel.cs +++ b/FunGame.Server/Model/ServerModel.cs @@ -5,6 +5,7 @@ using Milimoe.FunGame.Core.Library.Constant; using Milimoe.FunGame.Core.Library.Server; using Milimoe.FunGame.Server.Others; using Milimoe.FunGame.Server.Utility; +using System.Data; namespace Milimoe.FunGame.Server.Model { @@ -26,6 +27,7 @@ namespace Milimoe.FunGame.Server.Model private string UserName = ""; private string Password = ""; private int FailedTimes = 0; // 超过一定次数断开连接 + private MySQLHelper SQLHelper = MySQLHelper.GetHelper(); public ServerModel(ClientSocket socket, bool running) { @@ -70,8 +72,11 @@ namespace Milimoe.FunGame.Server.Model if (username != null && password != null) { ServerHelper.WriteLine("[" + ServerSocket.GetTypeString(type) + "] UserName: " + username); - if (username == "test" && password == "123456".Encrypt("test")) + SQLHelper.Script = $"{SQLConstant.Select_Users} {SQLConstant.Command_Where} Username = '{username}' And Password = '{password}'"; + SQLHelper.ExecuteDataSet(out SQLResult result); + if (result == SQLResult.Success && SQLHelper.UpdateRows > 0) { + DataRow UserRow = SQLHelper.DataSet.Tables[0].Rows[0]; if (autokey != null && autokey.Trim() != "") ServerHelper.WriteLine("[" + ServerSocket.GetTypeString(type) + "] AutoKey: 已确认"); UserName = username; diff --git a/FunGame.Server/Utility/DataUtility/MySQLConnection.cs b/FunGame.Server/Utility/DataUtility/MySQLConnection.cs index 1840e98..9f06369 100644 --- a/FunGame.Server/Utility/DataUtility/MySQLConnection.cs +++ b/FunGame.Server/Utility/DataUtility/MySQLConnection.cs @@ -1,39 +1,106 @@ using MySql.Data.MySqlClient; using Milimoe.FunGame.Core.Api.Utility; +using Milimoe.FunGame.Core.Library.Server; namespace Milimoe.FunGame.Server.Utility.DataUtility { - public class MySQLConnection + public class ConnectProperties { - public static MySqlConnection? Connection = null; - public static string Name { get; set; } = ""; public static string DataSource { get; set; } = ""; public static string Port { get; set; } = ""; public static string DataBase { get; set; } = ""; public static string User { get; set; } = ""; public static string Password { get; set; } = ""; - public static string GetConnection { get; set; } = ""; - public static bool Connect(out MySqlConnection? conn) + /// + /// 读取MySQL服务器配置文件 + /// + /// + public static string GetConnectProperties() + { + if (Name == "" && DataSource == "" && Port == "" && DataBase == "" && User == "" && Password == "") + { + if (INIHelper.ExistINIFile()) + { + DataSource = INIHelper.ReadINI("MySQL", "DBServer"); + Port = INIHelper.ReadINI("MySQL", "DBPort"); + DataBase = INIHelper.ReadINI("MySQL", "DBName"); + User = INIHelper.ReadINI("MySQL", "DBUser"); + Password = INIHelper.ReadINI("MySQL", "DBPassword"); + return "data source = " + DataSource + "; port = " + Port + "; database = " + DataBase + "; user = " + User + "; password = " + Password + "; charset = utf8mb4;"; + } + else ServerHelper.Error(new MySQLConfigException()); + } + return "data source = " + DataSource + "; port = " + Port + "; database = " + DataBase + "; user = " + User + "; password = " + Password + "; charset = utf8mb4;"; + } + } + + public class MySQLConnection + { + public MySqlConnection? Connection + { + get + { + return _Connection; + } + } + public SQLServerInfo ServerInfo + { + get + { + return _ServerInfo ?? SQLServerInfo.Create(); + } + } + + private MySqlConnection? _Connection; + private SQLServerInfo? _ServerInfo; + + /// + /// 创建SQL连接 + /// + /// + public MySQLConnection(out SQLServerInfo? serverInfo) + { + _Connection = Connect(out serverInfo); + } + + /// + /// 关闭连接 + /// + public void Close() + { + if (_Connection != null && _Connection.State == System.Data.ConnectionState.Open) + { + _Connection.Close(); + } + _Connection = null; + } + + /// + /// 连接MySQL服务器 + /// + /// 服务器信息 + /// 连接对象 + /// MySQL服务启动失败:无法找到MySQL配置文件 + private MySqlConnection? Connect(out SQLServerInfo? serverInfo) { try { - GetConnection = GetConnectProperties(); - if (GetConnection != null) + string _GetConnection = ConnectProperties.GetConnectProperties(); + if (_GetConnection != null) { - string[] DataSetting = GetConnection.Split(";"); + string[] DataSetting = _GetConnection.Split(";"); if (DataSetting.Length > 1 && DataSetting[0].Length > 14 && DataSetting[1].Length > 8) { ServerHelper.WriteLine("Connect -> MySQL://" + DataSetting[0][14..] + ":" + DataSetting[1][8..]); } - Connection = new MySqlConnection(GetConnection); - Connection.Open(); - if (Connection.State == System.Data.ConnectionState.Open) + _Connection = new MySqlConnection(_GetConnection); + _Connection.Open(); + if (_Connection.State == System.Data.ConnectionState.Open) { + _ServerInfo = SQLServerInfo.Create(ConnectProperties.Name, ConnectProperties.DataSource, ConnectProperties.Port, ConnectProperties.DataBase, ConnectProperties.User, ConnectProperties.Password); ServerHelper.WriteLine("Connected: MySQL服务器连接成功"); - conn = Connection; - return true; } } else @@ -45,32 +112,9 @@ namespace Milimoe.FunGame.Server.Utility.DataUtility { ServerHelper.Error(e); } - conn = Connection; - return false; - } - public static void Close() - { - if (Connection != null && Connection.State == System.Data.ConnectionState.Open) - { - Connection.Close(); - } - Connection = null; - } - - private static string GetConnectProperties() - { - if (INIHelper.ExistINIFile()) - { - DataSource = INIHelper.ReadINI("MySQL", "DBServer"); - Port = INIHelper.ReadINI("MySQL", "DBPort"); - DataBase = INIHelper.ReadINI("MySQL", "DBName"); - User = INIHelper.ReadINI("MySQL", "DBUser"); - Password = INIHelper.ReadINI("MySQL", "DBPassword"); - return "data source = " + DataSource + "; port = " + Port + "; database = " + DataBase + "; user = " + User + "; password = " + Password + "; charset = utf8mb4;"; - } - else ServerHelper.Error(new Exception("找不到配置文件。")); - return ""; + serverInfo = _ServerInfo; + return _Connection; } } } diff --git a/FunGame.Server/Utility/DataUtility/MySQLHelper.cs b/FunGame.Server/Utility/DataUtility/MySQLHelper.cs index 15c47ef..e3b29f7 100644 --- a/FunGame.Server/Utility/DataUtility/MySQLHelper.cs +++ b/FunGame.Server/Utility/DataUtility/MySQLHelper.cs @@ -1,165 +1,90 @@ using System.Data; using MySql.Data.MySqlClient; using Milimoe.FunGame.Core.Api.Data; -using Milimoe.FunGame.Core.Library.Common.Network; using Milimoe.FunGame.Core.Library.Constant; +using Milimoe.FunGame.Core.Library.Server; using Milimoe.FunGame.Server.Utility.DataUtility; namespace Milimoe.FunGame.Server.Utility { - internal class MySQLHelper : SQLHelper + public class MySQLHelper : SQLHelper { - public new string Script { get; set; } = ""; - public new EntityType EntityType => _EntityType; - public new object Entity => _Entity; - public new SQLResult Result => _Result; - public override SQLServerInfo ServerInfo - { - get - { - return _ServerInfo ?? SQLServerInfo.Create(); - } - } - public new int UpdateRows => _UpdateRows; - public DataSet DataSet => _DataSet; + public override string Script { get; set; } = ""; + public override CommandType CommandType { get; set; } = CommandType.Text; + public MySqlParameter[] Parameters { get; set; } + public override SQLResult Result => _Result; + public override SQLServerInfo ServerInfo => _ServerInfo ?? SQLServerInfo.Create(); + public MySQLConnection? Connection => _Connection; + public override int UpdateRows => _UpdateRows; + public override DataSet DataSet => _DataSet; - private EntityType _EntityType; - private object _Entity = General.EntityInstance; - private SQLResult _Result; + private SQLResult _Result = SQLResult.Success; private SQLServerInfo? _ServerInfo; private int _UpdateRows = 0; - private DataSet _DataSet = new DataSet(); + private DataSet _DataSet = new(); + private MySQLConnection? _Connection; - public static SQLHelper GetHelper() + /// + /// 创建MySQLHelper实例 + /// + /// + /// + /// + /// + public static MySQLHelper GetHelper(string script = "", CommandType type = CommandType.Text, params MySqlParameter[] parameters) { - return new MySQLHelper(); - } - - public override SQLResult Execute() - { - return SQLResult.NotFound; - } - - public DataSet ExecuteDataSet() - { - return _DataSet; - } - - /// - /// 执行一个sql命令 - /// - /// 存储过程, 文本, 等等 - /// 存储过程名称或者sql语句 - /// 执行命令的参数 - /// 执行命令所影响的行数 - private int Execute(CommandType type, string sql, params MySqlParameter[] parameters) - { - MySqlCommand cmd = new(); - - PrepareCommand(cmd, null, type, sql, parameters); - - int updaterow = cmd.ExecuteNonQuery(); - return updaterow; - } - - /// - /// 返回DataSet - /// - /// 存储过程, 文本, 等等 - /// 存储过程名称或者sql语句 - /// 执行命令所用参数的集合 - /// - private DataSet ExecuteDataSet(CommandType type, string sql, params MySqlParameter[] parameters) - { - MySqlCommand cmd = new(); - DataSet ds = new(); - - try - { - PrepareCommand(cmd, null, type, sql, parameters); - - MySqlDataAdapter adapter = new() - { - SelectCommand = cmd - }; - adapter.Fill(ds); - - //清 除参数 - cmd.Parameters.Clear(); - return ds; - } - catch (Exception e) - { - ServerHelper.Error(e); - } - - return ds; + return new MySQLHelper(script, type, parameters); } /// - /// 返回插入值ID + /// 执行一个命令 /// - /// 存储过程, 文本, 等等 - /// 存储过程名称或者sql语句 - /// 执行命令所用参数的集合 - /// - private object ExecuteNonExist(CommandType type, string sql, params MySqlParameter[] parameters) + /// 执行结果 + /// 影响的行数 + public override int Execute(out SQLResult Result) { - MySqlCommand cmd = new(); - - PrepareCommand(cmd, null, type, sql, parameters); - cmd.ExecuteNonQuery(); - - return cmd.LastInsertedId; + _Connection = new MySQLConnection(out _ServerInfo); + ServerHelper.WriteLine("SQLQuery -> " + Script); + _UpdateRows = MySQLManager.Execute(this, out Result); + Close(); + return _UpdateRows; } - /// - /// 准备执行一个命令 - /// - /// 命令 - /// 事务 - /// 存储过程或者文本 - /// sql语句 - /// 执行命令的参数 - private void PrepareCommand(MySqlCommand cmd, MySqlTransaction? trans, CommandType type, string sql, MySqlParameter[] parameters) + /// + /// 查询DataSet + /// + /// 执行结果 + /// 结果集 + public override DataSet ExecuteDataSet(out SQLResult Result) { - MySqlConnection? conn = MySQLConnection.Connection; - if (conn != null && conn.State != ConnectionState.Open) - conn.Open(); + _Connection = new MySQLConnection(out _ServerInfo); + ServerHelper.WriteLine("SQLQuery -> " + Script); + _DataSet = MySQLManager.ExecuteDataSet(this, out Result); + _UpdateRows = _DataSet.Tables[0].Rows.Count; + Close(); + return DataSet; + } - cmd.Connection = conn; - cmd.CommandText = sql; - - if (trans != null) - { - cmd.Transaction = trans; - } - - cmd.CommandType = type; - - if (parameters != null) - { - foreach (MySqlParameter parm in parameters) - { - cmd.Parameters.Add(parm); - } - } + /// + /// 关闭连接 + /// + public override void Close() + { + _Connection?.Close(); + ServerHelper.WriteLine("Connection Release"); } /// /// 创建SQLHelper /// - /// 获取实体类的类型 - /// SQL服务器信息 - /// 执行结果 - /// 更新的行数 - private MySQLHelper(EntityType type = EntityType.Empty, SQLServerInfo? info = null, SQLResult result = SQLResult.Success, int rows = 0) + /// 存储过程名称或者script语句 + /// 存储过程, 文本, 等等 + /// 执行命令所用参数的集合 + private MySQLHelper(string script, CommandType type, params MySqlParameter[] parameters) { - _EntityType = type; - if (info == null) _ServerInfo = SQLServerInfo.Create(); - else _ServerInfo = info; - _Result = result; - _UpdateRows = rows; + Script = script; + CommandType = type; + Parameters = parameters; } } } diff --git a/FunGame.Server/Utility/DataUtility/MySQLManager.cs b/FunGame.Server/Utility/DataUtility/MySQLManager.cs index 7d05a92..c39a2e2 100644 --- a/FunGame.Server/Utility/DataUtility/MySQLManager.cs +++ b/FunGame.Server/Utility/DataUtility/MySQLManager.cs @@ -1,83 +1,140 @@ using System.Data; -using System.Text; -using Milimoe.FunGame.Core.Api.Data; +using MySql.Data.MySqlClient; using Milimoe.FunGame.Core.Library.Constant; using Milimoe.FunGame.Core.Service; namespace Milimoe.FunGame.Server.Utility { - public class MySQLManager : SQLManager + public class MySQLManager { - public new SQLHelper SQLHelper { get; } - - public MySQLManager(SQLHelper SQLHelper) + /// + /// 执行Script命令 + /// + /// MySQLHelper + /// 执行结果 + /// 影响的行数 + public static int Execute(MySQLHelper Helper, out SQLResult Result) { - this.SQLHelper = SQLHelper; + MySqlCommand cmd = new(); + int updaterow; + + try + { + PrepareCommand(Helper, cmd); + updaterow = cmd.ExecuteNonQuery(); + if (updaterow > 0) + { + Result = SQLResult.Success; + } + else Result = SQLResult.NotFound; + } + catch (Exception e) + { + ServerHelper.Error(e); + updaterow = -1; + Result = SQLResult.Fail; + } + + return updaterow; } - public override int Add(StringBuilder sql, ref SQLResult result) + /// + /// 返回DataSet + /// + /// MySQLHelper + /// 执行结果 + /// 结果集 + public static DataSet ExecuteDataSet(MySQLHelper Helper, out SQLResult Result) { - return 0; + MySqlCommand cmd = new(); + DataSet ds = new(); + + try + { + PrepareCommand(Helper, cmd); + + MySqlDataAdapter adapter = new() + { + SelectCommand = cmd + }; + adapter.Fill(ds); + + //清除参数 + cmd.Parameters.Clear(); + Result = SQLResult.Success; + } + catch (Exception e) + { + ServerHelper.Error(e); + Result = SQLResult.Fail; + } + + return ds; } - public override int Add(string sql, ref SQLResult result) + /// + /// 返回插入值ID + /// + /// MySQLHelper + /// 执行结果 + /// 插入值ID + public static object ExecuteAndGetLastInsertedID(MySQLHelper Helper, out SQLResult Result) { - return 0; + MySqlCommand cmd = new(); + int updaterow; + + try + { + PrepareCommand(Helper, cmd); + updaterow = cmd.ExecuteNonQuery(); + if (updaterow > 0) + { + Result = SQLResult.Success; + } + else Result = SQLResult.NotFound; + } + catch (Exception e) + { + ServerHelper.Error(e); + Result = SQLResult.Fail; + } + + return cmd.LastInsertedId; } - public override SQLResult Execute() + /// + /// 准备执行一个命令 + /// + /// MySQLHelper + /// 命令对象 + /// 事务 + public static void PrepareCommand(MySQLHelper Helper, MySqlCommand cmd, MySqlTransaction? trans = null) { - return SQLResult.NotFound; - } + if (Helper.Connection != null) + { + MySqlConnection? conn = Helper.Connection.Connection; - public override SQLResult Execute(StringBuilder sql) - { - return SQLResult.NotFound; - } + if (conn != null && conn.State != ConnectionState.Open) + conn.Open(); - public override SQLResult Execute(string sql) - { - return SQLResult.NotFound; - } + cmd.Connection = conn; + cmd.CommandText = Helper.Script; - public override DataSet ExecuteDataSet(StringBuilder sql) - { - return new DataSet(); - } + if (trans != null) + { + cmd.Transaction = trans; + } - public override DataSet ExecuteDataSet(string sql) - { - return new DataSet(); - } + cmd.CommandType = Helper.CommandType; - public override object Query(EntityType type, StringBuilder sql) - { - return General.EntityInstance; - } - - public override object Query(EntityType type, string sql) - { - return General.EntityInstance; - } - - public override int Remove(StringBuilder sql, ref SQLResult result) - { - return 0; - } - - public override int Remove(string sql, ref SQLResult result) - { - return 0; - } - - public override int Update(StringBuilder sql, ref SQLResult result) - { - return 0; - } - - public override int Update(string sql, ref SQLResult result) - { - return 0; + if (Helper.Parameters != null) + { + foreach (MySqlParameter parm in Helper.Parameters) + { + cmd.Parameters.Add(parm); + } + } + } } } }