重新架构

This commit is contained in:
Mili 2022-10-01 23:58:38 +08:00
parent c1cc63073c
commit 487bcc4567
10 changed files with 121 additions and 79 deletions

View File

@ -23,8 +23,8 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="FunGame.Core.Api"> <Reference Include="FunGame.Core">
<HintPath>..\..\FunGame\bin\Server\Debug\net6.0\FunGame.Core.Api.dll</HintPath> <HintPath>..\..\FunGame\bin\Server\Debug\net6.0\FunGame.Core.dll</HintPath>
</Reference> </Reference>
</ItemGroup> </ItemGroup>

View File

@ -0,0 +1,15 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Milimoe.FunGame.Core.Interface;
namespace MIilimoe.FunGame.Core.Implement
{
public class IServerImpl : IServer
{
}
}

View File

@ -5,7 +5,7 @@
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<ApplicationIcon>logo.ico</ApplicationIcon> <ApplicationIcon>Images\logo.ico</ApplicationIcon>
<PackageIcon>logo.ico</PackageIcon> <PackageIcon>logo.ico</PackageIcon>
<BaseOutputPath>C:\milimoe\FunGame\bin\Server</BaseOutputPath> <BaseOutputPath>C:\milimoe\FunGame\bin\Server</BaseOutputPath>
<Title>FunGameServer</Title> <Title>FunGameServer</Title>
@ -14,6 +14,7 @@
<PackageOutputPath>C:\milimoe\FunGame\bin</PackageOutputPath> <PackageOutputPath>C:\milimoe\FunGame\bin</PackageOutputPath>
<AssemblyVersion>1.0</AssemblyVersion> <AssemblyVersion>1.0</AssemblyVersion>
<FileVersion>1.0</FileVersion> <FileVersion>1.0</FileVersion>
<AssemblyName>FunGameServer</AssemblyName>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
@ -25,12 +26,12 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Content Include="logo.ico" /> <Content Include="Images\logo.ico" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Reference Include="FunGame.Core.Api"> <Reference Include="FunGame.Core">
<HintPath>..\..\FunGame\bin\Server\Debug\net6.0\FunGame.Core.Api.dll</HintPath> <HintPath>..\..\FunGame\bin\Server\Debug\net6.0\FunGame.Core.dll</HintPath>
</Reference> </Reference>
<Reference Include="MySql.Data"> <Reference Include="MySql.Data">
<HintPath>..\..\FunGame\bin\Server\Debug\net6.0\MySql.Data.dll</HintPath> <HintPath>..\..\FunGame\bin\Server\Debug\net6.0\MySql.Data.dll</HintPath>
@ -44,10 +45,14 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Update="logo.ico"> <None Update="Images\logo.ico">
<Pack>True</Pack> <Pack>True</Pack>
<PackagePath>\</PackagePath> <PackagePath>\</PackagePath>
</None> </None>
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Controller\" />
</ItemGroup>
</Project> </Project>

View File

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 17 KiB

View File

@ -4,11 +4,11 @@ using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System; using System;
using System.Net.WebSockets; using System.Net.WebSockets;
using FunGameServer.Models.Config; using Milimoe.FunGame.Server.Others;
using FunGameServer.Utils; using Milimoe.FunGame.Server.Utility;
using FunGame.Core.Api.Model.Enum; using Milimoe.FunGame.Core.Entity.Enum;
using FunGame.Core.Api.Util; using Milimoe.FunGame.Core.Api.Utility;
using FunGameServer.ServerCore; using FunGame.Server.Model;
Console.Title = Config.SERVER_NAME; Console.Title = Config.SERVER_NAME;
Console.WriteLine(FunGameEnums.GetInfo(Config.FunGameType)); Console.WriteLine(FunGameEnums.GetInfo(Config.FunGameType));
@ -105,27 +105,30 @@ void StartServer()
while (Running) while (Running)
{ {
Socket socket; Socket socket;
string clientIPaddress = "";
try try
{ {
socket = ServerSocket.Accept(); socket = ServerSocket.Accept();
IPEndPoint? clientIP = (IPEndPoint?)socket.RemoteEndPoint; IPEndPoint? clientIP = (IPEndPoint?)socket.RemoteEndPoint;
string clientIPaddress = (clientIP != null) ? clientIP.ToString() : "Unknown"; clientIPaddress = (clientIP != null) ? clientIP.ToString() : "Unknown";
ServerHelper.WriteLine("客户端" + clientIPaddress + "连接 . . ."); ServerHelper.WriteLine("客户端" + clientIPaddress + "连接 . . .");
if (Read(socket) && Send(socket)) if (Read(socket, clientIPaddress) && Send(socket, clientIPaddress))
{ {
ClientSocket cs = new ClientSocket(socket, Running); ClientSocket cs = new(socket, Running);
Task t = Task.Factory.StartNew(() => Task t = Task.Factory.StartNew(() =>
{ {
cs.Start(); cs.Start();
}); });
cs.Task = t; cs.Task = t;
cs.ClientName = clientIPaddress;
Config.OnlineClients.Add(clientIPaddress, clientIPaddress);
} }
else else
ServerHelper.WriteLine("客户端" + clientIPaddress + "连接失败。"); ServerHelper.WriteLine("客户端" + clientIPaddress + "连接失败。");
} }
catch (Exception e) catch
{ {
ServerHelper.WriteLine("客户端断开连接!\n" + e.StackTrace); ServerHelper.WriteLine("客户端" + clientIPaddress + "断开连接!");
} }
} }
} }
@ -153,7 +156,7 @@ void StartServer()
}); });
} }
bool Read(Socket socket) bool Read(Socket socket, string name)
{ {
// 接收客户端消息 // 接收客户端消息
byte[] buffer = new byte[2048]; byte[] buffer = new byte[2048];
@ -165,18 +168,18 @@ bool Read(Socket socket)
msg = SocketHelper.GetMessage(msg); msg = SocketHelper.GetMessage(msg);
if (typestring != SocketMessageType.Unknown.ToString()) if (typestring != SocketMessageType.Unknown.ToString())
{ {
ServerHelper.WriteLine("[ 客户端(" + typestring + "] -> " + msg); ServerHelper.WriteLine("[" + typestring + "] " + SocketHelper.MakeClientName(name) + " -> " + msg);
return true; return true;
} }
ServerHelper.WriteLine("客户端发送了不符合FunGame规定的字符拒绝连接。"); ServerHelper.WriteLine("客户端发送了不符合FunGame规定的字符拒绝连接。");
return false; return false;
} }
else else
ServerHelper.WriteLine("客户端没有回应。"); ServerHelper.WriteLine(SocketHelper.MakeClientName(name) + " 没有回应。");
return false; return false;
} }
bool Send(Socket socket) bool Send(Socket socket, string name)
{ {
// 发送消息给客户端 // 发送消息给客户端
string msg = Config.SERVER_NAME + ";" + Config.SERVER_NOTICE; string msg = Config.SERVER_NAME + ";" + Config.SERVER_NOTICE;
@ -184,7 +187,7 @@ bool Send(Socket socket)
buffer = Config.DEFAULT_ENCODING.GetBytes(SocketHelper.MakeMessage((int)SocketMessageType.GetNotice, msg)); buffer = Config.DEFAULT_ENCODING.GetBytes(SocketHelper.MakeMessage((int)SocketMessageType.GetNotice, msg));
if (socket.Send(buffer) > 0) if (socket.Send(buffer) > 0)
{ {
ServerHelper.WriteLine("[ 客户端 ] <- " + "已确认连接"); ServerHelper.WriteLine(SocketHelper.MakeClientName(name) + " <- " + "已确认连接");
return true; return true;
} }
else else

View File

@ -1,5 +1,4 @@
using FunGameServer.Models.Config; using System;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.IO; using System.IO;
@ -7,22 +6,30 @@ using System.Net.Sockets;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Data.SqlTypes; using System.Data.SqlTypes;
using FunGameServer.Utils;
using System.Reflection.Metadata; using System.Reflection.Metadata;
using FunGame.Core.Api.Model.Entity;
using System.Net; using System.Net;
using FunGame.Core.Api.Model.Enum;
using FunGame.Core.Api.Util;
using MySqlX.XDevAPI.Common; using MySqlX.XDevAPI.Common;
using Milimoe.FunGame.Server.Utility;
using Milimoe.FunGame.Core.Entity.General;
using Milimoe.FunGame.Core.Entity.Enum;
using Milimoe.FunGame.Core.Api.Utility;
using Milimoe.FunGame.Server.Others;
namespace FunGameServer.ServerCore namespace FunGame.Server.Model
{ {
public class ClientSocket public class ClientSocket
{ {
public bool Running { get; set; } = false; /**
public Socket? Socket { get; set; } = null; * Public
*/
public bool Running = false;
public Socket? Socket = null;
public Task? Task = null; public Task? Task = null;
public string ClientName = "";
/**
* Private
*/
private User? User = null; private User? User = null;
public ClientSocket(Socket socket, bool running) public ClientSocket(Socket socket, bool running)
@ -46,7 +53,7 @@ namespace FunGameServer.ServerCore
int type = SocketHelper.GetType(msg); int type = SocketHelper.GetType(msg);
string typestring = EnumHelper.GetSocketTypeName(type); string typestring = EnumHelper.GetSocketTypeName(type);
msg = SocketHelper.GetMessage(msg); msg = SocketHelper.GetMessage(msg);
if (type != (int)SocketMessageType.HeartBeat) ServerHelper.WriteLine("[ 客户端(" + typestring + "] -> " + msg); if (type != (int)SocketMessageType.HeartBeat) ServerHelper.WriteLine("[" + typestring + "] " + SocketHelper.MakeClientName(ClientName, User) + " -> " + msg);
switch (type) switch (type)
{ {
case (int)SocketMessageType.GetNotice: case (int)SocketMessageType.GetNotice:
@ -79,9 +86,9 @@ namespace FunGameServer.ServerCore
} }
throw new Exception(); throw new Exception();
} }
catch (Exception e) catch
{ {
ServerHelper.WriteLine("客户端没有回应。\n" + e.StackTrace); ServerHelper.WriteLine(SocketHelper.MakeClientName(ClientName, User) + " 没有回应。");
return false; return false;
} }
} }
@ -97,14 +104,14 @@ namespace FunGameServer.ServerCore
if (socket.Send(buffer) > 0) if (socket.Send(buffer) > 0)
{ {
if (msg != "") if (msg != "")
ServerHelper.WriteLine("[ 客户端(" + typestring + "] <- " + msg); ServerHelper.WriteLine("[" + typestring + "] " + SocketHelper.MakeClientName(ClientName, User) + " <- " + msg);
return true; return true;
} }
throw new Exception(); throw new Exception();
} }
catch (Exception e) catch
{ {
ServerHelper.WriteLine("客户端没有回应。" + e.StackTrace); ServerHelper.WriteLine(SocketHelper.MakeClientName(ClientName, User) + " 没有回应。");
return false; return false;
} }
} }
@ -170,7 +177,7 @@ namespace FunGameServer.ServerCore
private void CreateStreamReader() private void CreateStreamReader()
{ {
Thread.Sleep(100); Thread.Sleep(100);
ServerHelper.WriteLine("Creating: StreamReader...OK"); ServerHelper.WriteLine("Creating: StreamReader -> " + SocketHelper.MakeClientName(ClientName, User) + " ...OK");
while (Running) while (Running)
{ {
if (Socket != null) if (Socket != null)
@ -182,8 +189,8 @@ namespace FunGameServer.ServerCore
{ {
RemoveUser(); RemoveUser();
GetUserCount(); GetUserCount();
ServerHelper.WriteLine("ERROR -> Too Many Faileds."); ServerHelper.WriteLine(SocketHelper.MakeClientName(ClientName, User) + " ERROR -> Too Many Faileds.");
ServerHelper.WriteLine("CLOSE -> StreamReader is Closed."); ServerHelper.WriteLine(SocketHelper.MakeClientName(ClientName, User) + " CLOSE -> StreamReader is Closed.");
break; break;
} }
} }
@ -193,34 +200,11 @@ namespace FunGameServer.ServerCore
{ {
RemoveUser(); RemoveUser();
GetUserCount(); GetUserCount();
ServerHelper.WriteLine("ERROR -> Socket is Closed."); ServerHelper.WriteLine(SocketHelper.MakeClientName(ClientName, User) + " ERROR -> Socket is Closed.");
ServerHelper.WriteLine("CLOSE -> StringStream is Closed."); ServerHelper.WriteLine(SocketHelper.MakeClientName(ClientName, User) + " CLOSE -> StringStream is Closed.");
break; break;
} }
} }
} }
} }
public class SocketHelper
{
public static int GetType(string msg)
{
int index = msg.IndexOf(';') - 1;
if (index > 0)
return Convert.ToInt32(msg[..index]);
else
return Convert.ToInt32(msg[..1]);
}
public static string GetMessage(string msg)
{
int index = msg.IndexOf(';') + 1;
return msg[index..];
}
public static string MakeMessage(int type, string msg)
{
return type + ";" + msg;
}
}
} }

View File

@ -1,7 +1,4 @@
using FunGame.Core.Api.Model.Enum; using System;
using FunGame.Core.Api.Util;
using FunGameServer.Utils;
using System;
using System.Collections; using System.Collections;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
@ -10,8 +7,10 @@ using System.Net.Sockets;
using System.Reflection.Metadata; using System.Reflection.Metadata;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Milimoe.FunGame.Core.Entity.Enum;
using Milimoe.FunGame.Core.Api.Utility;
namespace FunGameServer.Models.Config namespace Milimoe.FunGame.Server.Others
{ {
public static class Config public static class Config
{ {
@ -32,6 +31,8 @@ namespace FunGameServer.Models.Config
public static Hashtable OrderList = new(); public static Hashtable OrderList = new();
public static ReflectionHelper ReflectionHelper = new(); public static ReflectionHelper ReflectionHelper = new();
public static Hashtable OnlineClients = new Hashtable();
/// <summary> /// <summary>
/// string: 玩家标识ID /// string: 玩家标识ID
/// Task玩家线程 /// Task玩家线程

View File

@ -4,7 +4,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace FunGameServer.Models.Config namespace Milimoe.FunGame.Server.Others
{ {
public class OrderDictionary public class OrderDictionary
{ {

View File

@ -1,7 +1,4 @@
using FunGame.Core.Api.Model.Enum; using MySql.Data.MySqlClient;
using FunGame.Core.Api.Util;
using FunGameServer.Models.Config;
using MySql.Data.MySqlClient;
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
@ -9,10 +6,14 @@ using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Milimoe.FunGame.Core.Entity.General;
using Milimoe.FunGame.Core.Entity.Enum;
using Milimoe.FunGame.Core.Api.Utility;
using Milimoe.FunGame.Server.Others;
namespace FunGameServer.Utils namespace Milimoe.FunGame.Server.Utility
{ {
public class Utility : FunGame.Core.Api.Util.Utility public class Utility : FunGame.Core.Api.Utility.Utility
{ {
} }
@ -184,4 +185,37 @@ namespace FunGameServer.Utils
Config.OrderList.Add(OrderDictionary.Restart, "重启服务器"); Config.OrderList.Add(OrderDictionary.Restart, "重启服务器");
} }
} }
public class SocketHelper
{
public static int GetType(string msg)
{
int index = msg.IndexOf(';') - 1;
if (index > 0)
return Convert.ToInt32(msg[..index]);
else
return Convert.ToInt32(msg[..1]);
}
public static string GetMessage(string msg)
{
int index = msg.IndexOf(';') + 1;
return msg[index..];
}
public static string MakeMessage(int type, string msg)
{
return type + ";" + msg;
}
public static string MakeClientName(string name, User? user = null)
{
if (user != null)
{
return "玩家 " + user.Userame;
}
if (name != "") return "客户端(" + name + ")";
return "客户端";
}
}
} }

View File

@ -3,9 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17 # Visual Studio Version 17
VisualStudioVersion = 17.3.32804.467 VisualStudioVersion = 17.3.32804.467
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FunGameServer", "FunGameServer\FunGameServer.csproj", "{CFC4F490-967B-4F12-883E-86C2A6E61461}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FunGame.Server", "FunGame.Server\FunGame.Server.csproj", "{CFC4F490-967B-4F12-883E-86C2A6E61461}"
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FunGame.Core", "FunGame.Core\FunGame.Core.csproj", "{F5BACA36-3DE2-450A-8518-E5DC29991875}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FunGame.Implement", "FunGame.Implement\FunGame.Implement.csproj", "{F5BACA36-3DE2-450A-8518-E5DC29991875}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution