milimoe 14ff58f4f4
为服务器统一数据访问连接 (#37)
* 添加 Web API 和 RESTful API 模式;
* 添加 SQLite 模式;
* 添加 ISocketMessageProcessor 和 ISocketListener<> 接口,用于统一数据访问;
* 重做了 ISocketModel;
* 完善了 WebSocket 的连接模式。
2024-10-04 12:39:15 +08:00

88 lines
2.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Milimoe.FunGame.Core.Model;
using MySql.Data.MySqlClient;
namespace Milimoe.FunGame.Server.Utility.DataUtility
{
public class MySQLConnection
{
public MySqlConnection? Connection
{
get
{
return _Connection;
}
}
public SQLServerInfo ServerInfo
{
get
{
return _ServerInfo ?? SQLServerInfo.Create();
}
}
private MySqlConnection? _Connection;
private SQLServerInfo? _ServerInfo;
/// <summary>
/// 创建SQL连接
/// </summary>
/// <param name="serverInfo"></param>
public MySQLConnection(out SQLServerInfo? serverInfo)
{
_Connection = Connect(out serverInfo);
}
/// <summary>
/// 关闭连接
/// </summary>
public void Close()
{
if (_Connection != null && _Connection.State == System.Data.ConnectionState.Open)
{
_Connection.Close();
}
_Connection = null;
}
/// <summary>
/// 连接MySQL服务器
/// </summary>
/// <param name="serverInfo">服务器信息</param>
/// <returns>连接对象</returns>
/// <exception cref="MySQLConfigException">MySQL服务启动失败无法找到MySQL配置文件</exception>
private MySqlConnection? Connect(out SQLServerInfo? serverInfo)
{
try
{
string connectionString = ConnectProperties.GetConnectPropertiesForMySQL();
if (connectionString != null)
{
string[] strings = connectionString.Split(";");
if (strings.Length > 1 && strings[0].Length > 14 && strings[1].Length > 8)
{
ServerHelper.WriteLine("Connect -> MySQL://" + strings[0][14..] + ":" + strings[1][8..]);
}
_Connection = new MySqlConnection(connectionString);
_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服务器连接成功");
}
}
else
{
throw new MySQLConfigException();
}
}
catch (Exception e)
{
ServerHelper.Error(e);
}
serverInfo = _ServerInfo;
return _Connection;
}
}
}