mirror of
https://github.com/project-redbud/FunGame-Server.git
synced 2025-04-20 19:19:39 +08:00
重新架构
This commit is contained in:
parent
c1cc63073c
commit
487bcc4567
@ -23,8 +23,8 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Reference Include="FunGame.Core.Api">
|
||||
<HintPath>..\..\FunGame\bin\Server\Debug\net6.0\FunGame.Core.Api.dll</HintPath>
|
||||
<Reference Include="FunGame.Core">
|
||||
<HintPath>..\..\FunGame\bin\Server\Debug\net6.0\FunGame.Core.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
|
15
FunGame.Implement/Implement/ServerInterfaceImpl.cs
Normal file
15
FunGame.Implement/Implement/ServerInterfaceImpl.cs
Normal 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
|
||||
{
|
||||
|
||||
}
|
||||
}
|
@ -5,7 +5,7 @@
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<ApplicationIcon>logo.ico</ApplicationIcon>
|
||||
<ApplicationIcon>Images\logo.ico</ApplicationIcon>
|
||||
<PackageIcon>logo.ico</PackageIcon>
|
||||
<BaseOutputPath>C:\milimoe\FunGame\bin\Server</BaseOutputPath>
|
||||
<Title>FunGameServer</Title>
|
||||
@ -14,6 +14,7 @@
|
||||
<PackageOutputPath>C:\milimoe\FunGame\bin</PackageOutputPath>
|
||||
<AssemblyVersion>1.0</AssemblyVersion>
|
||||
<FileVersion>1.0</FileVersion>
|
||||
<AssemblyName>FunGameServer</AssemblyName>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||
@ -25,12 +26,12 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="logo.ico" />
|
||||
<Content Include="Images\logo.ico" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Reference Include="FunGame.Core.Api">
|
||||
<HintPath>..\..\FunGame\bin\Server\Debug\net6.0\FunGame.Core.Api.dll</HintPath>
|
||||
<Reference Include="FunGame.Core">
|
||||
<HintPath>..\..\FunGame\bin\Server\Debug\net6.0\FunGame.Core.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="MySql.Data">
|
||||
<HintPath>..\..\FunGame\bin\Server\Debug\net6.0\MySql.Data.dll</HintPath>
|
||||
@ -44,10 +45,14 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="logo.ico">
|
||||
<None Update="Images\logo.ico">
|
||||
<Pack>True</Pack>
|
||||
<PackagePath>\</PackagePath>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Controller\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
Before Width: | Height: | Size: 17 KiB After Width: | Height: | Size: 17 KiB |
@ -4,11 +4,11 @@ using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System;
|
||||
using System.Net.WebSockets;
|
||||
using FunGameServer.Models.Config;
|
||||
using FunGameServer.Utils;
|
||||
using FunGame.Core.Api.Model.Enum;
|
||||
using FunGame.Core.Api.Util;
|
||||
using FunGameServer.ServerCore;
|
||||
using Milimoe.FunGame.Server.Others;
|
||||
using Milimoe.FunGame.Server.Utility;
|
||||
using Milimoe.FunGame.Core.Entity.Enum;
|
||||
using Milimoe.FunGame.Core.Api.Utility;
|
||||
using FunGame.Server.Model;
|
||||
|
||||
Console.Title = Config.SERVER_NAME;
|
||||
Console.WriteLine(FunGameEnums.GetInfo(Config.FunGameType));
|
||||
@ -105,27 +105,30 @@ void StartServer()
|
||||
while (Running)
|
||||
{
|
||||
Socket socket;
|
||||
string clientIPaddress = "";
|
||||
try
|
||||
{
|
||||
socket = ServerSocket.Accept();
|
||||
IPEndPoint? clientIP = (IPEndPoint?)socket.RemoteEndPoint;
|
||||
string clientIPaddress = (clientIP != null) ? clientIP.ToString() : "Unknown";
|
||||
clientIPaddress = (clientIP != null) ? clientIP.ToString() : "Unknown";
|
||||
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(() =>
|
||||
{
|
||||
cs.Start();
|
||||
});
|
||||
cs.Task = t;
|
||||
cs.ClientName = clientIPaddress;
|
||||
Config.OnlineClients.Add(clientIPaddress, clientIPaddress);
|
||||
}
|
||||
else
|
||||
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];
|
||||
@ -165,18 +168,18 @@ bool Read(Socket socket)
|
||||
msg = SocketHelper.GetMessage(msg);
|
||||
if (typestring != SocketMessageType.Unknown.ToString())
|
||||
{
|
||||
ServerHelper.WriteLine("[ 客户端(" + typestring + ")] -> " + msg);
|
||||
ServerHelper.WriteLine("[" + typestring + "] " + SocketHelper.MakeClientName(name) + " -> " + msg);
|
||||
return true;
|
||||
}
|
||||
ServerHelper.WriteLine("客户端发送了不符合FunGame规定的字符,拒绝连接。");
|
||||
return false;
|
||||
}
|
||||
else
|
||||
ServerHelper.WriteLine("客户端没有回应。");
|
||||
ServerHelper.WriteLine(SocketHelper.MakeClientName(name) + " 没有回应。");
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Send(Socket socket)
|
||||
bool Send(Socket socket, string name)
|
||||
{
|
||||
// 发送消息给客户端
|
||||
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));
|
||||
if (socket.Send(buffer) > 0)
|
||||
{
|
||||
ServerHelper.WriteLine("[ 客户端 ] <- " + "已确认连接");
|
||||
ServerHelper.WriteLine(SocketHelper.MakeClientName(name) + " <- " + "已确认连接");
|
||||
return true;
|
||||
}
|
||||
else
|
@ -1,5 +1,4 @@
|
||||
using FunGameServer.Models.Config;
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.IO;
|
||||
@ -7,22 +6,30 @@ using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Data.SqlTypes;
|
||||
using FunGameServer.Utils;
|
||||
using System.Reflection.Metadata;
|
||||
using FunGame.Core.Api.Model.Entity;
|
||||
using System.Net;
|
||||
using FunGame.Core.Api.Model.Enum;
|
||||
using FunGame.Core.Api.Util;
|
||||
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 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 string ClientName = "";
|
||||
|
||||
/**
|
||||
* Private
|
||||
*/
|
||||
private User? User = null;
|
||||
|
||||
public ClientSocket(Socket socket, bool running)
|
||||
@ -46,7 +53,7 @@ namespace FunGameServer.ServerCore
|
||||
int type = SocketHelper.GetType(msg);
|
||||
string typestring = EnumHelper.GetSocketTypeName(type);
|
||||
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)
|
||||
{
|
||||
case (int)SocketMessageType.GetNotice:
|
||||
@ -79,9 +86,9 @@ namespace FunGameServer.ServerCore
|
||||
}
|
||||
throw new Exception();
|
||||
}
|
||||
catch (Exception e)
|
||||
catch
|
||||
{
|
||||
ServerHelper.WriteLine("客户端没有回应。\n" + e.StackTrace);
|
||||
ServerHelper.WriteLine(SocketHelper.MakeClientName(ClientName, User) + " 没有回应。");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -97,14 +104,14 @@ namespace FunGameServer.ServerCore
|
||||
if (socket.Send(buffer) > 0)
|
||||
{
|
||||
if (msg != "")
|
||||
ServerHelper.WriteLine("[ 客户端(" + typestring + ")] <- " + msg);
|
||||
ServerHelper.WriteLine("[" + typestring + "] " + SocketHelper.MakeClientName(ClientName, User) + " <- " + msg);
|
||||
return true;
|
||||
}
|
||||
throw new Exception();
|
||||
}
|
||||
catch (Exception e)
|
||||
catch
|
||||
{
|
||||
ServerHelper.WriteLine("客户端没有回应。" + e.StackTrace);
|
||||
ServerHelper.WriteLine(SocketHelper.MakeClientName(ClientName, User) + " 没有回应。");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -170,7 +177,7 @@ namespace FunGameServer.ServerCore
|
||||
private void CreateStreamReader()
|
||||
{
|
||||
Thread.Sleep(100);
|
||||
ServerHelper.WriteLine("Creating: StreamReader...OK");
|
||||
ServerHelper.WriteLine("Creating: StreamReader -> " + SocketHelper.MakeClientName(ClientName, User) + " ...OK");
|
||||
while (Running)
|
||||
{
|
||||
if (Socket != null)
|
||||
@ -182,8 +189,8 @@ namespace FunGameServer.ServerCore
|
||||
{
|
||||
RemoveUser();
|
||||
GetUserCount();
|
||||
ServerHelper.WriteLine("ERROR -> Too Many Faileds.");
|
||||
ServerHelper.WriteLine("CLOSE -> StreamReader is Closed.");
|
||||
ServerHelper.WriteLine(SocketHelper.MakeClientName(ClientName, User) + " ERROR -> Too Many Faileds.");
|
||||
ServerHelper.WriteLine(SocketHelper.MakeClientName(ClientName, User) + " CLOSE -> StreamReader is Closed.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -193,34 +200,11 @@ namespace FunGameServer.ServerCore
|
||||
{
|
||||
RemoveUser();
|
||||
GetUserCount();
|
||||
ServerHelper.WriteLine("ERROR -> Socket is Closed.");
|
||||
ServerHelper.WriteLine("CLOSE -> StringStream is Closed.");
|
||||
ServerHelper.WriteLine(SocketHelper.MakeClientName(ClientName, User) + " ERROR -> Socket is Closed.");
|
||||
ServerHelper.WriteLine(SocketHelper.MakeClientName(ClientName, User) + " CLOSE -> StringStream is Closed.");
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,7 +1,4 @@
|
||||
using FunGame.Core.Api.Model.Enum;
|
||||
using FunGame.Core.Api.Util;
|
||||
using FunGameServer.Utils;
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
@ -10,8 +7,10 @@ using System.Net.Sockets;
|
||||
using System.Reflection.Metadata;
|
||||
using System.Text;
|
||||
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
|
||||
{
|
||||
@ -32,6 +31,8 @@ namespace FunGameServer.Models.Config
|
||||
public static Hashtable OrderList = new();
|
||||
public static ReflectionHelper ReflectionHelper = new();
|
||||
|
||||
public static Hashtable OnlineClients = new Hashtable();
|
||||
|
||||
/// <summary>
|
||||
/// string: 玩家标识ID
|
||||
/// Task:玩家线程
|
@ -4,7 +4,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FunGameServer.Models.Config
|
||||
namespace Milimoe.FunGame.Server.Others
|
||||
{
|
||||
public class OrderDictionary
|
||||
{
|
@ -1,7 +1,4 @@
|
||||
using FunGame.Core.Api.Model.Enum;
|
||||
using FunGame.Core.Api.Util;
|
||||
using FunGameServer.Models.Config;
|
||||
using MySql.Data.MySqlClient;
|
||||
using MySql.Data.MySqlClient;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
@ -9,10 +6,14 @@ using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
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, "重启服务器");
|
||||
}
|
||||
}
|
||||
|
||||
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 "客户端";
|
||||
}
|
||||
}
|
||||
}
|
@ -3,9 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.3.32804.467
|
||||
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
|
||||
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
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
|
Loading…
x
Reference in New Issue
Block a user