添加MySQL依赖

This commit is contained in:
Mili 2022-09-05 00:12:51 +08:00
parent 91bca66b60
commit e4fe75aa11
7 changed files with 124 additions and 40 deletions

View File

@ -22,6 +22,10 @@
<DebugType>embedded</DebugType> <DebugType>embedded</DebugType>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="MySql.Data" Version="8.0.30" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<Reference Include="FunGame.Core.Api"> <Reference Include="FunGame.Core.Api">
<HintPath>..\..\FunGame\bin\Debug\net6.0\FunGame.Core.Api.dll</HintPath> <HintPath>..\..\FunGame\bin\Debug\net6.0\FunGame.Core.Api.dll</HintPath>

View File

@ -28,6 +28,10 @@
<Content Include="logo.ico" /> <Content Include="logo.ico" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<PackageReference Include="MySql.Data" Version="8.0.30" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<Reference Include="FunGame.Core.Api"> <Reference Include="FunGame.Core.Api">
<HintPath>..\..\FunGame\bin\Debug\net6.0\FunGame.Core.Api.dll</HintPath> <HintPath>..\..\FunGame\bin\Debug\net6.0\FunGame.Core.Api.dll</HintPath>

View File

@ -17,10 +17,17 @@ int port = Config.SERVER_PORT;
Console.Title = Config.CONSOLE_TITLE; Console.Title = Config.CONSOLE_TITLE;
try Task t = Task.Factory.StartNew(() =>
{ {
Task t = Task.Factory.StartNew(() => try
{ {
// 连接MySQL服务器
if (!Config.DefaultDataHelper.Connect())
{
Running = false;
throw new Exception("服务器遇到问题需要关闭,请重新启动服务器!");
}
// 创建IP地址终结点对象 // 创建IP地址终结点对象
IPEndPoint ip = new(IPAddress.Any, port); IPEndPoint ip = new(IPAddress.Any, port);
@ -30,15 +37,15 @@ try
// 开始监听连接 // 开始监听连接
ServerSocket.Listen(Config.MAX_PLAYERS); ServerSocket.Listen(Config.MAX_PLAYERS);
ServerHelper.WriteLine("服务器启动成功,正在监听 . . ."); ServerHelper.WriteLine("服务器启动成功,端口号 " + port + " ,开始监听 . . .");
Task.Run(() => Task.Run(() =>
{ {
Config.ServerNotice = ServerHelper.GetServerNotice(); Config.ServerNotice = ServerHelper.GetServerNotice();
if (Config.ServerNotice != "") if (Config.ServerNotice != "")
ServerHelper.WriteLine("\n\n" + Config.ServerNotice + "\n\n"); ServerHelper.WriteLine("\n**********服务器公告**********\n" + Config.ServerNotice + "\n\n");
else else
ServerHelper.WriteLine("无法读取服务器公告"); ServerHelper.WriteLine("无法读取服务器公告");
}); });
while (Running) while (Running)
@ -65,29 +72,39 @@ try
} }
catch (Exception e) catch (Exception e)
{ {
ServerHelper.WriteLine("ERROR: 客户端断开连接!\n" + e.StackTrace); ServerHelper.WriteLine("客户端断开连接!\n" + e.StackTrace);
} }
} }
}
}); catch (Exception e)
} {
catch (Exception e) if (e.Message.Equals("服务器遇到问题需要关闭,请重新启动服务器!"))
{ {
ServerHelper.WriteLine(e.StackTrace);
if (ServerSocket != null) if (ServerSocket != null)
{ {
ServerSocket.Close(); ServerSocket.Close();
ServerSocket = null; ServerSocket = null;
} }
} }
finally ServerHelper.Error(e);
{ }
while (Running) finally
{ {
if (ServerSocket != null)
{
ServerSocket.Close();
ServerSocket = null;
}
}
});
while (Running)
{
string? order = ""; string? order = "";
order = Console.ReadLine(); order = Console.ReadLine();
ServerHelper.Type(); ServerHelper.Type();
if (order != null && !order.Equals("")) if (order != null && !order.Equals("") && Running)
{ {
switch (order) switch (order)
{ {
@ -96,7 +113,6 @@ finally
break; break;
} }
} }
}
} }
ServerHelper.WriteLine("服务器已关闭,按任意键退出程序。"); ServerHelper.WriteLine("服务器已关闭,按任意键退出程序。");

View File

@ -1,4 +1,5 @@
using FunGame.Core.Api.Util; using FunGame.Core.Api.Util;
using FunGameServer.Utils;
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
@ -23,6 +24,7 @@ namespace FunGameServer.Models.Config
public static string ServerNotice = ""; // 服务器的公告 public static string ServerNotice = ""; // 服务器的公告
public static AssemblyHelper DefaultAssemblyHelper = new AssemblyHelper(); public static AssemblyHelper DefaultAssemblyHelper = new AssemblyHelper();
public static DataHelper DefaultDataHelper = new DataHelper();
/// <summary> /// <summary>
/// string: 玩家标识ID /// string: 玩家标识ID

View File

@ -43,7 +43,7 @@ namespace FunGameServer.Sockets
int type = SocketHelper.GetType(msg); int type = SocketHelper.GetType(msg);
string typestring = SocketHelper.GetTypeString(type); string typestring = SocketHelper.GetTypeString(type);
msg = SocketHelper.GetMessage(msg); msg = SocketHelper.GetMessage(msg);
ServerHelper.WriteLine("[ 客户端(" + typestring + "] -> " + msg); if (type != (int)SocketEnums.Type.HeartBeat) ServerHelper.WriteLine("[ 客户端(" + typestring + "] -> " + msg);
switch (type) switch (type)
{ {
case (int)SocketEnums.Type.GetNotice: case (int)SocketEnums.Type.GetNotice:
@ -64,7 +64,7 @@ namespace FunGameServer.Sockets
} }
catch (Exception e) catch (Exception e)
{ {
ServerHelper.WriteLine("ERROR客户端没有回应。\n" + e.StackTrace); ServerHelper.WriteLine("客户端没有回应。\n" + e.StackTrace);
return false; return false;
} }
} }
@ -87,7 +87,7 @@ namespace FunGameServer.Sockets
} }
catch (Exception e) catch (Exception e)
{ {
ServerHelper.WriteLine("ERROR客户端没有回应。" + e.StackTrace); ServerHelper.WriteLine("客户端没有回应。" + e.StackTrace);
return false; return false;
} }
} }
@ -113,8 +113,8 @@ namespace FunGameServer.Sockets
FailedTimes++; FailedTimes++;
if (FailedTimes >= Config.MAX_CONNECTFAILED) if (FailedTimes >= Config.MAX_CONNECTFAILED)
{ {
ServerHelper.WriteLine("ERROR: Too Many Faileds."); ServerHelper.WriteLine("ERROR -> Too Many Faileds.");
ServerHelper.WriteLine("CLOSE: StreamReader is Closed."); ServerHelper.WriteLine("CLOSE -> StreamReader is Closed.");
break; break;
} }
} }
@ -122,8 +122,8 @@ namespace FunGameServer.Sockets
} }
else else
{ {
ServerHelper.WriteLine("ERROR: Socket is Closed."); ServerHelper.WriteLine("ERROR -> Socket is Closed.");
ServerHelper.WriteLine("CLOSE: StringStream is Closed."); ServerHelper.WriteLine("CLOSE -> StringStream is Closed.");
break; break;
} }
} }

View File

@ -0,0 +1,55 @@
using FunGame.Core.Api.Model.Entity;
using FunGameServer.Models.Config;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static FunGame.Core.Api.Model.Enum.CommonEnums;
namespace FunGameServer.Utils
{
public class DataHelper
{
private string? GetConnection = "";
private MySqlConnection? msc = null;
public DataHelper()
{
}
public bool Connect()
{
try
{
GetConnection = (string?)Config.DefaultAssemblyHelper.GetFunGameCoreValue((int)InterfaceType.ServerInterface, (int)InterfaceMethod.DBConnection);
if (GetConnection != null)
{
string[] DataSetting = GetConnection.Split(";");
if (DataSetting.Length > 1 && DataSetting[0].Length > 14 && DataSetting[1].Length > 8)
{
ServerHelper.WriteLine("Connecting: " + DataSetting[0][14..] + ":" + DataSetting[1][8..]);
}
msc = new MySqlConnection(GetConnection);
msc.Open();
if (msc.State == System.Data.ConnectionState.Open)
{
ServerHelper.WriteLine("Connected: MySQL服务器连接成功");
return true;
}
}
else
{
throw new Exception("MySQL服务启动失败无法找到MySQL配置文件。");
}
}
catch (Exception e)
{
ServerHelper.Error(e);
}
return false;
}
}
}

View File

@ -16,6 +16,11 @@ namespace FunGameServer.Utils
return now.AddMilliseconds(-now.Millisecond).ToString() + " " + Config.SERVER_NAME + ""; return now.AddMilliseconds(-now.Millisecond).ToString() + " " + Config.SERVER_NAME + "";
} }
public static void Error(Exception e)
{
Console.Write("\r" + GetPrefix() + e.Message + "\n" + e.StackTrace + "\n\r> ");
}
public static void WriteLine(string? msg) public static void WriteLine(string? msg)
{ {
Console.Write("\r" + GetPrefix() + msg + "\n\r> "); Console.Write("\r" + GetPrefix() + msg + "\n\r> ");
@ -30,9 +35,7 @@ namespace FunGameServer.Utils
{ {
try try
{ {
string? ServerNotice = (string?)Config.DefaultAssemblyHelper.GetFunGameCoreValue((int)InterfaceType.ServerInterface, (int)InterfaceMethod.ServerNotice);
if (ServerNotice != null)
return ServerNotice;
} }
catch (Exception e) catch (Exception e)
{ {