实现新接口

This commit is contained in:
Mili 2022-09-02 00:55:13 +08:00
parent dfe4c17cbb
commit 91bca66b60
9 changed files with 87 additions and 51 deletions

View File

@ -1,12 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FunGame.Core.Implement
{
internal class ServerInterfaceImpl
{
}
}

View File

@ -5,7 +5,7 @@ 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}") = "FunGameServer", "FunGameServer\FunGameServer.csproj", "{CFC4F490-967B-4F12-883E-86C2A6E61461}"
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FunGame.Core", "FunGameServer.Core\FunGame.Core.csproj", "{F5BACA36-3DE2-450A-8518-E5DC29991875}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FunGame.Core", "FunGame.Core\FunGame.Core.csproj", "{F5BACA36-3DE2-450A-8518-E5DC29991875}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution

View File

@ -31,6 +31,7 @@
<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>
<Private>True</Private>
</Reference> </Reference>
</ItemGroup> </ItemGroup>

View File

@ -7,6 +7,7 @@ using FunGameServer.Sockets;
using System.Net.WebSockets; using System.Net.WebSockets;
using FunGameServer.Models.Config; using FunGameServer.Models.Config;
using FunGameServer.Utils; using FunGameServer.Utils;
using static FunGame.Core.Api.Model.Enum.CommonEnums;
bool Running = true; bool Running = true;
Socket? ServerSocket = null; Socket? ServerSocket = null;
@ -29,7 +30,16 @@ try
// 开始监听连接 // 开始监听连接
ServerSocket.Listen(Config.MAX_PLAYERS); ServerSocket.Listen(Config.MAX_PLAYERS);
SocketHelper.WriteLine(SocketHelper.GetPrefix() + "服务器启动成功,正在监听 . . ."); ServerHelper.WriteLine("服务器启动成功,正在监听 . . .");
Task.Run(() =>
{
Config.ServerNotice = ServerHelper.GetServerNotice();
if (Config.ServerNotice != "")
ServerHelper.WriteLine("\n\n" + Config.ServerNotice + "\n\n");
else
ServerHelper.WriteLine("无法读取服务器公告。");
});
while (Running) while (Running)
{ {
@ -39,9 +49,9 @@ try
socket = ServerSocket.Accept(); socket = ServerSocket.Accept();
IPEndPoint? clientIP = (IPEndPoint?)socket.RemoteEndPoint; IPEndPoint? clientIP = (IPEndPoint?)socket.RemoteEndPoint;
if (clientIP != null) if (clientIP != null)
SocketHelper.WriteLine(SocketHelper.GetPrefix() + "客户端" + clientIP.ToString() + "连接 . . ."); ServerHelper.WriteLine("客户端" + clientIP.ToString() + "连接 . . .");
else else
SocketHelper.WriteLine(SocketHelper.GetPrefix() + "未知地点客户端连接 . . ."); ServerHelper.WriteLine("未知地点客户端连接 . . .");
if (Read(socket) && Send(socket)) if (Read(socket) && Send(socket))
Task.Factory.StartNew(() => Task.Factory.StartNew(() =>
{ {
@ -49,13 +59,13 @@ try
}); });
else else
if (clientIP != null) if (clientIP != null)
SocketHelper.WriteLine(SocketHelper.GetPrefix() + "客户端" + clientIP.ToString() + "连接失败。"); ServerHelper.WriteLine("客户端" + clientIP.ToString() + "连接失败。");
else else
SocketHelper.WriteLine(SocketHelper.GetPrefix() + "客户端连接失败。"); ServerHelper.WriteLine("客户端连接失败。");
} }
catch (Exception e) catch (Exception e)
{ {
SocketHelper.WriteLine(SocketHelper.GetPrefix() + "ERROR: 客户端断开连接!\n" + e.StackTrace); ServerHelper.WriteLine("ERROR: 客户端断开连接!\n" + e.StackTrace);
} }
} }
@ -63,7 +73,7 @@ try
} }
catch (Exception e) catch (Exception e)
{ {
SocketHelper.WriteLine(e.StackTrace); ServerHelper.WriteLine(e.StackTrace);
if (ServerSocket != null) if (ServerSocket != null)
{ {
ServerSocket.Close(); ServerSocket.Close();
@ -76,7 +86,7 @@ finally
{ {
string? order = ""; string? order = "";
order = Console.ReadLine(); order = Console.ReadLine();
SocketHelper.WriteLine("\r> " + order); ServerHelper.Type();
if (order != null && !order.Equals("")) if (order != null && !order.Equals(""))
{ {
switch (order) switch (order)
@ -89,7 +99,7 @@ finally
} }
} }
SocketHelper.WriteLine(SocketHelper.GetPrefix() + "服务器已关闭,按任意键退出程序。"); ServerHelper.WriteLine("服务器已关闭,按任意键退出程序。");
Console.ReadKey(); Console.ReadKey();
@ -105,14 +115,14 @@ bool Read(Socket socket)
msg = SocketHelper.GetMessage(msg); msg = SocketHelper.GetMessage(msg);
if (typestring != SocketEnums.TYPE_UNKNOWN) if (typestring != SocketEnums.TYPE_UNKNOWN)
{ {
SocketHelper.WriteLine(SocketHelper.GetPrefix() + "[ 客户端(" + typestring + "] -> " + msg); ServerHelper.WriteLine("[ 客户端(" + typestring + "] -> " + msg);
return true; return true;
} }
SocketHelper.WriteLine(SocketHelper.GetPrefix() + "客户端发送了不符合FunGame规定的字符拒绝连接。"); ServerHelper.WriteLine("客户端发送了不符合FunGame规定的字符拒绝连接。");
return false; return false;
} }
else else
SocketHelper.WriteLine(SocketHelper.GetPrefix() + "客户端没有回应。"); ServerHelper.WriteLine("客户端没有回应。");
return false; return false;
} }
@ -124,11 +134,11 @@ bool Send(Socket socket)
buffer = Config.DEFAULT_ENCODING.GetBytes(SocketHelper.MakeMessage((int)SocketEnums.Type.CheckLogin, msg)); buffer = Config.DEFAULT_ENCODING.GetBytes(SocketHelper.MakeMessage((int)SocketEnums.Type.CheckLogin, msg));
if (socket.Send(buffer) > 0) if (socket.Send(buffer) > 0)
{ {
SocketHelper.WriteLine(SocketHelper.GetPrefix() + "[ 客户端 ] <- " + msg); ServerHelper.WriteLine("[ 客户端 ] <- " + msg);
return true; return true;
} }
else else
SocketHelper.WriteLine(SocketHelper.GetPrefix() + "无法传输数据,与客户端的连接可能丢失。"); ServerHelper.WriteLine("无法传输数据,与客户端的连接可能丢失。");
return false; return false;
} }

View File

@ -1,4 +1,5 @@
using System; using FunGame.Core.Api.Util;
using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -11,7 +12,7 @@ namespace FunGameServer.Models.Config
{ {
public static class Config public static class Config
{ {
public static int MAX_PLAYERS = 16; // 最多接受连接的玩家数量 public static int MAX_PLAYERS = 20; // 最多接受连接的玩家数量
public static int ONLINE_PLAYERS = 0; // 已连接的玩家数量 public static int ONLINE_PLAYERS = 0; // 已连接的玩家数量
public static int CONNECTING_PLAYERS = 0; // 正在连接的玩家数量 public static int CONNECTING_PLAYERS = 0; // 正在连接的玩家数量
public static string SERVER_NAME = "米粒的糖果屋"; // 服务器名称 public static string SERVER_NAME = "米粒的糖果屋"; // 服务器名称
@ -19,6 +20,9 @@ namespace FunGameServer.Models.Config
public static Encoding DEFAULT_ENCODING = Encoding.UTF8; // 默认传输字符集 public static Encoding DEFAULT_ENCODING = Encoding.UTF8; // 默认传输字符集
public static int MAX_CONNECTFAILED = 5; // 最大连接失败次数 public static int MAX_CONNECTFAILED = 5; // 最大连接失败次数
public const string CONSOLE_TITLE = "FunGame Server"; // 控制台的标题 public const string CONSOLE_TITLE = "FunGame Server"; // 控制台的标题
public static string ServerNotice = ""; // 服务器的公告
public static AssemblyHelper DefaultAssemblyHelper = new AssemblyHelper();
/// <summary> /// <summary>
/// string: 玩家标识ID /// string: 玩家标识ID
@ -28,10 +32,9 @@ namespace FunGameServer.Models.Config
/** /**
* string * string
* stringID
* Task线 * Task线
*/ */
public static ConcurrentDictionary<string, ConcurrentDictionary<string, Task>> PlayingPlayers= new ConcurrentDictionary<string, ConcurrentDictionary<string, Task>>(); public static ConcurrentDictionary<string, Task> PlayingPlayers= new ConcurrentDictionary<string, Task>();
} }
} }

View File

@ -11,6 +11,7 @@ using FunGameServer.Utils;
using System.Reflection.Metadata; using System.Reflection.Metadata;
using FunGame.Core.Api.Model.Entity; using FunGame.Core.Api.Model.Entity;
using System.Net; using System.Net;
using static FunGame.Core.Api.Model.Enum.CommonEnums;
namespace FunGameServer.Sockets namespace FunGameServer.Sockets
{ {
@ -42,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);
SocketHelper.WriteLine(SocketHelper.GetPrefix() + "[ 客户端(" + typestring + "] -> " + msg); ServerHelper.WriteLine("[ 客户端(" + typestring + "] -> " + msg);
switch (type) switch (type)
{ {
case (int)SocketEnums.Type.GetNotice: case (int)SocketEnums.Type.GetNotice:
@ -63,7 +64,7 @@ namespace FunGameServer.Sockets
} }
catch (Exception e) catch (Exception e)
{ {
SocketHelper.WriteLine(SocketHelper.GetPrefix() + "ERROR客户端没有回应。\n" + e.StackTrace); ServerHelper.WriteLine("ERROR客户端没有回应。\n" + e.StackTrace);
return false; return false;
} }
} }
@ -79,14 +80,14 @@ namespace FunGameServer.Sockets
if (socket.Send(buffer) > 0) if (socket.Send(buffer) > 0)
{ {
if (msg != "") if (msg != "")
SocketHelper.WriteLine(SocketHelper.GetPrefix() + "[ 客户端(" + typestring + "] <- " + msg); ServerHelper.WriteLine("[ 客户端(" + typestring + "] <- " + msg);
return true; return true;
} }
throw new Exception(); throw new Exception();
} }
catch (Exception e) catch (Exception e)
{ {
SocketHelper.WriteLine(SocketHelper.GetPrefix() + "ERROR客户端没有回应。" + e.StackTrace); ServerHelper.WriteLine("ERROR客户端没有回应。" + e.StackTrace);
return false; return false;
} }
} }
@ -102,7 +103,7 @@ namespace FunGameServer.Sockets
private void CreateStreamReader() private void CreateStreamReader()
{ {
Thread.Sleep(1000); Thread.Sleep(1000);
SocketHelper.WriteLine(SocketHelper.GetPrefix() + "Creating: StreamReader...OK"); ServerHelper.WriteLine("Creating: StreamReader...OK");
while (Running) while (Running)
{ {
if (Socket != null) if (Socket != null)
@ -112,8 +113,8 @@ namespace FunGameServer.Sockets
FailedTimes++; FailedTimes++;
if (FailedTimes >= Config.MAX_CONNECTFAILED) if (FailedTimes >= Config.MAX_CONNECTFAILED)
{ {
SocketHelper.WriteLine(SocketHelper.GetPrefix() + "ERROR: Too Many Faileds."); ServerHelper.WriteLine("ERROR: Too Many Faileds.");
SocketHelper.WriteLine(SocketHelper.GetPrefix() + "CLOSE: StreamReader is Closed."); ServerHelper.WriteLine("CLOSE: StreamReader is Closed.");
break; break;
} }
} }
@ -121,8 +122,8 @@ namespace FunGameServer.Sockets
} }
else else
{ {
SocketHelper.WriteLine(SocketHelper.GetPrefix() + "ERROR: Socket is Closed."); ServerHelper.WriteLine("ERROR: Socket is Closed.");
SocketHelper.WriteLine(SocketHelper.GetPrefix() + "CLOSE: StringStream is Closed."); ServerHelper.WriteLine("CLOSE: StringStream is Closed.");
break; break;
} }
} }

View File

@ -0,0 +1,44 @@
using FunGameServer.Models.Config;
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 ServerHelper
{
public static string GetPrefix()
{
DateTime now = System.DateTime.Now;
return now.AddMilliseconds(-now.Millisecond).ToString() + " " + Config.SERVER_NAME + "";
}
public static void WriteLine(string? msg)
{
Console.Write("\r" + GetPrefix() + msg + "\n\r> ");
}
public static void Type()
{
Console.Write("\r> ");
}
public static string GetServerNotice()
{
try
{
string? ServerNotice = (string?)Config.DefaultAssemblyHelper.GetFunGameCoreValue((int)InterfaceType.ServerInterface, (int)InterfaceMethod.ServerNotice);
if (ServerNotice != null)
return ServerNotice;
}
catch (Exception e)
{
ServerHelper.WriteLine(e.StackTrace);
}
return "";
}
}
}

View File

@ -48,16 +48,5 @@ namespace FunGameServer.Utils
{ {
return type + ";" + msg; return type + ";" + msg;
} }
public static string GetPrefix()
{
DateTime now = System.DateTime.Now;
return now.AddMilliseconds(-now.Millisecond).ToString() + " " + Config.SERVER_NAME + "";
}
public static void WriteLine(string? msg)
{
Console.Write("\r" + msg + "\n\r> ");
}
} }
} }