mirror of
https://github.com/project-redbud/FunGame-Server.git
synced 2025-04-22 03:59:36 +08:00
逻辑优化
This commit is contained in:
parent
0d66815b39
commit
4f36624d02
@ -7,9 +7,9 @@ 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;
|
using FunGame.Core.Api.Model.Enum;
|
||||||
|
|
||||||
Console.Title = Config.CONSOLE_TITLE;
|
Console.Title = Config.SERVER_NAME;
|
||||||
|
|
||||||
bool Running = true;
|
bool Running = true;
|
||||||
Socket? ServerSocket = null;
|
Socket? ServerSocket = null;
|
||||||
@ -47,58 +47,6 @@ while (Running)
|
|||||||
ServerHelper.WriteLine("服务器已关闭,按任意键退出程序。");
|
ServerHelper.WriteLine("服务器已关闭,按任意键退出程序。");
|
||||||
Console.ReadKey();
|
Console.ReadKey();
|
||||||
|
|
||||||
|
|
||||||
bool Read(Socket socket)
|
|
||||||
{
|
|
||||||
// 接收客户端消息
|
|
||||||
byte[] buffer = new byte[2048];
|
|
||||||
int length = socket.Receive(buffer);
|
|
||||||
if (length > 0)
|
|
||||||
{
|
|
||||||
string msg = Config.DEFAULT_ENCODING.GetString(buffer, 0, length);
|
|
||||||
string typestring = SocketHelper.GetTypeString(SocketHelper.GetType(msg));
|
|
||||||
msg = SocketHelper.GetMessage(msg);
|
|
||||||
if (typestring != SocketEnums.TYPE_UNKNOWN)
|
|
||||||
{
|
|
||||||
ServerHelper.WriteLine("[ 客户端(" + typestring + ")] -> " + msg);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
ServerHelper.WriteLine("客户端发送了不符合FunGame规定的字符,拒绝连接。");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
ServerHelper.WriteLine("客户端没有回应。");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Send(Socket socket)
|
|
||||||
{
|
|
||||||
// 发送消息给客户端
|
|
||||||
string msg = ">> 已连接至服务器 -> [ " + Config.SERVER_NAME + " ] 连接成功";
|
|
||||||
byte[] buffer = new byte[2048];
|
|
||||||
buffer = Config.DEFAULT_ENCODING.GetBytes(SocketHelper.MakeMessage((int)SocketEnums.Type.CheckLogin, msg));
|
|
||||||
if (socket.Send(buffer) > 0)
|
|
||||||
{
|
|
||||||
ServerHelper.WriteLine("[ 客户端 ] <- " + msg);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
ServerHelper.WriteLine("无法传输数据,与客户端的连接可能丢失。");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool IsIP(string ip)
|
|
||||||
{
|
|
||||||
//判断是否为IP
|
|
||||||
return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
|
|
||||||
}
|
|
||||||
|
|
||||||
bool IsEmail(string ip)
|
|
||||||
{
|
|
||||||
//判断是否为Email
|
|
||||||
return Regex.IsMatch(ip, @"^(\w)+(\.\w)*@(\w)+((\.\w+)+)$");
|
|
||||||
}
|
|
||||||
|
|
||||||
void StartServer()
|
void StartServer()
|
||||||
{
|
{
|
||||||
Task t = Task.Factory.StartNew(() =>
|
Task t = Task.Factory.StartNew(() =>
|
||||||
@ -116,7 +64,10 @@ void StartServer()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
ServerHelper.GetServerSettings();
|
ServerHelper.GetServerSettings();
|
||||||
|
Console.Title = Config.SERVER_NAME + " - FunGame Server Port: " + Config.SERVER_PORT;
|
||||||
|
}
|
||||||
|
|
||||||
// 连接MySQL服务器
|
// 连接MySQL服务器
|
||||||
if (!Config.DefaultDataHelper.Connect())
|
if (!Config.DefaultDataHelper.Connect())
|
||||||
@ -137,7 +88,7 @@ void StartServer()
|
|||||||
ServerHelper.WriteLine("服务器启动成功,端口号 " + Config.SERVER_PORT + " ,开始监听 . . .");
|
ServerHelper.WriteLine("服务器启动成功,端口号 " + Config.SERVER_PORT + " ,开始监听 . . .");
|
||||||
|
|
||||||
if (Config.SERVER_NOTICE != "")
|
if (Config.SERVER_NOTICE != "")
|
||||||
ServerHelper.WriteLine("\n\n**********服务器公告**********\n\n" + Config.SERVER_NOTICE + "\n");
|
ServerHelper.WriteLine("\n\n********** 服务器公告 **********\n\n" + Config.SERVER_NOTICE + "\n");
|
||||||
else
|
else
|
||||||
ServerHelper.WriteLine("无法读取服务器公告");
|
ServerHelper.WriteLine("无法读取服务器公告");
|
||||||
|
|
||||||
@ -191,4 +142,43 @@ void StartServer()
|
|||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Read(Socket socket)
|
||||||
|
{
|
||||||
|
// 接收客户端消息
|
||||||
|
byte[] buffer = new byte[2048];
|
||||||
|
int length = socket.Receive(buffer);
|
||||||
|
if (length > 0)
|
||||||
|
{
|
||||||
|
string msg = Config.DEFAULT_ENCODING.GetString(buffer, 0, length);
|
||||||
|
string typestring = CommonEnums.GetSocketTypeName(SocketHelper.GetType(msg));
|
||||||
|
msg = SocketHelper.GetMessage(msg);
|
||||||
|
if (typestring != CommonEnums.SocketType.Unknown.ToString())
|
||||||
|
{
|
||||||
|
ServerHelper.WriteLine("[ 客户端(" + typestring + ")] -> " + msg);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
ServerHelper.WriteLine("客户端发送了不符合FunGame规定的字符,拒绝连接。");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
ServerHelper.WriteLine("客户端没有回应。");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Send(Socket socket)
|
||||||
|
{
|
||||||
|
// 发送消息给客户端
|
||||||
|
string msg = " >> 已连接至服务器:" + Config.SERVER_NAME + "。\n\n********** 服务器公告 **********\n\n" + Config.SERVER_NOTICE + "\n\n";
|
||||||
|
byte[] buffer = new byte[2048];
|
||||||
|
buffer = Config.DEFAULT_ENCODING.GetBytes(SocketHelper.MakeMessage((int)CommonEnums.SocketType.GetNotice, msg));
|
||||||
|
if (socket.Send(buffer) > 0)
|
||||||
|
{
|
||||||
|
ServerHelper.WriteLine("[ 客户端 ] <- " + "已确认连接");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
ServerHelper.WriteLine("无法传输数据,与客户端的连接可能丢失。");
|
||||||
|
return false;
|
||||||
}
|
}
|
@ -1,6 +1,7 @@
|
|||||||
using FunGame.Core.Api.Util;
|
using FunGame.Core.Api.Util;
|
||||||
using FunGameServer.Utils;
|
using FunGameServer.Utils;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@ -25,7 +26,8 @@ namespace FunGameServer.Models.Config
|
|||||||
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 Encoding DEFAULT_ENCODING = Encoding.UTF8; // 默认传输字符集
|
public static Encoding DEFAULT_ENCODING = Encoding.UTF8; // 默认传输字符集
|
||||||
public const string CONSOLE_TITLE = "FunGame Server"; // 控制台的标题
|
|
||||||
|
public static Hashtable OrderList = new();
|
||||||
|
|
||||||
public static AssemblyHelper DefaultAssemblyHelper = new();
|
public static AssemblyHelper DefaultAssemblyHelper = new();
|
||||||
public static DataHelper DefaultDataHelper = new();
|
public static DataHelper DefaultDataHelper = new();
|
||||||
|
@ -1,27 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace FunGameServer.Models.Config
|
|
||||||
{
|
|
||||||
public static class SocketEnums
|
|
||||||
{
|
|
||||||
public enum Type
|
|
||||||
{
|
|
||||||
GetNotice = 1,
|
|
||||||
Login = 2,
|
|
||||||
CheckLogin = 3,
|
|
||||||
Logout = 4,
|
|
||||||
HeartBeat = 5
|
|
||||||
}
|
|
||||||
|
|
||||||
public const string TYPE_UNKNOWN = "Unknown Type";
|
|
||||||
public const string TYPE_GetNotice = "GetNotice";
|
|
||||||
public const string TYPE_Login = "Login";
|
|
||||||
public const string TYPE_CheckLogin = "CheckLogin";
|
|
||||||
public const string TYPE_Logout = "Logout";
|
|
||||||
public const string TYPE_HeartBeat = "HeartBeat";
|
|
||||||
}
|
|
||||||
}
|
|
@ -11,7 +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;
|
using FunGame.Core.Api.Model.Enum;
|
||||||
|
|
||||||
namespace FunGameServer.Sockets
|
namespace FunGameServer.Sockets
|
||||||
{
|
{
|
||||||
@ -41,20 +41,22 @@ namespace FunGameServer.Sockets
|
|||||||
{
|
{
|
||||||
string msg = Config.DEFAULT_ENCODING.GetString(buffer, 0, length);
|
string msg = Config.DEFAULT_ENCODING.GetString(buffer, 0, length);
|
||||||
int type = SocketHelper.GetType(msg);
|
int type = SocketHelper.GetType(msg);
|
||||||
string typestring = SocketHelper.GetTypeString(type);
|
string typestring = CommonEnums.GetSocketTypeName(type);
|
||||||
msg = SocketHelper.GetMessage(msg);
|
msg = SocketHelper.GetMessage(msg);
|
||||||
if (type != (int)SocketEnums.Type.HeartBeat) ServerHelper.WriteLine("[ 客户端(" + typestring + ")] -> " + msg);
|
if (type != (int)CommonEnums.SocketType.HeartBeat) ServerHelper.WriteLine("[ 客户端(" + typestring + ")] -> " + msg);
|
||||||
switch (type)
|
switch (type)
|
||||||
{
|
{
|
||||||
case (int)SocketEnums.Type.GetNotice:
|
case (int)CommonEnums.SocketType.GetNotice:
|
||||||
|
msg = Config.SERVER_NOTICE;
|
||||||
break;
|
break;
|
||||||
case (int)SocketEnums.Type.Login:
|
case (int)CommonEnums.SocketType.Login:
|
||||||
break;
|
break;
|
||||||
case (int)SocketEnums.Type.CheckLogin:
|
case (int)CommonEnums.SocketType.CheckLogin:
|
||||||
return true;
|
msg = ">> 已连接至服务器 -> [ " + Config.SERVER_NAME + " ] 连接成功";
|
||||||
case (int)SocketEnums.Type.Logout:
|
|
||||||
break;
|
break;
|
||||||
case (int)SocketEnums.Type.HeartBeat:
|
case (int)CommonEnums.SocketType.Logout:
|
||||||
|
break;
|
||||||
|
case (int)CommonEnums.SocketType.HeartBeat:
|
||||||
msg = "";
|
msg = "";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -76,7 +78,7 @@ namespace FunGameServer.Sockets
|
|||||||
{
|
{
|
||||||
byte[] buffer = new byte[2048];
|
byte[] buffer = new byte[2048];
|
||||||
buffer = Config.DEFAULT_ENCODING.GetBytes(Convert.ToString(SocketHelper.MakeMessage(type, msg)));
|
buffer = Config.DEFAULT_ENCODING.GetBytes(Convert.ToString(SocketHelper.MakeMessage(type, msg)));
|
||||||
string typestring = SocketHelper.GetTypeString(type);
|
string typestring = CommonEnums.GetSocketTypeName(type);
|
||||||
if (socket.Send(buffer) > 0)
|
if (socket.Send(buffer) > 0)
|
||||||
{
|
{
|
||||||
if (msg != "")
|
if (msg != "")
|
||||||
|
@ -30,7 +30,7 @@ namespace FunGameServer.Utils
|
|||||||
string[] DataSetting = GetConnection.Split(";");
|
string[] DataSetting = GetConnection.Split(";");
|
||||||
if (DataSetting.Length > 1 && DataSetting[0].Length > 14 && DataSetting[1].Length > 8)
|
if (DataSetting.Length > 1 && DataSetting[0].Length > 14 && DataSetting[1].Length > 8)
|
||||||
{
|
{
|
||||||
ServerHelper.WriteLine("Connect -> MySQL:\\\\" + DataSetting[0][14..] + ":" + DataSetting[1][8..]);
|
ServerHelper.WriteLine("Connect -> MySQL://" + DataSetting[0][14..] + ":" + DataSetting[1][8..]);
|
||||||
}
|
}
|
||||||
msc = new MySqlConnection(GetConnection);
|
msc = new MySqlConnection(GetConnection);
|
||||||
msc.Open();
|
msc.Open();
|
||||||
|
@ -4,6 +4,7 @@ using System.Collections;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using static FunGame.Core.Api.Model.Enum.CommonEnums;
|
using static FunGame.Core.Api.Model.Enum.CommonEnums;
|
||||||
|
|
||||||
@ -64,5 +65,17 @@ namespace FunGameServer.Utils
|
|||||||
ServerHelper.WriteLine(e.StackTrace);
|
ServerHelper.WriteLine(e.StackTrace);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static bool IsIP(string ip)
|
||||||
|
{
|
||||||
|
//判断是否为IP
|
||||||
|
return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool IsEmail(string ip)
|
||||||
|
{
|
||||||
|
//判断是否为Email
|
||||||
|
return Regex.IsMatch(ip, @"^(\w)+(\.\w)*@(\w)+((\.\w+)+)$");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ using System.Linq;
|
|||||||
using System.Net.NetworkInformation;
|
using System.Net.NetworkInformation;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using FunGame.Core.Api.Model.Enum;
|
||||||
|
|
||||||
namespace FunGameServer.Utils
|
namespace FunGameServer.Utils
|
||||||
{
|
{
|
||||||
@ -19,25 +20,6 @@ namespace FunGameServer.Utils
|
|||||||
return Convert.ToInt32(msg[..1]);
|
return Convert.ToInt32(msg[..1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string GetTypeString(int type)
|
|
||||||
{
|
|
||||||
switch (type)
|
|
||||||
{
|
|
||||||
case (int)SocketEnums.Type.GetNotice:
|
|
||||||
return SocketEnums.TYPE_GetNotice;
|
|
||||||
case (int)SocketEnums.Type.Login:
|
|
||||||
return SocketEnums.TYPE_Login;
|
|
||||||
case (int)SocketEnums.Type.CheckLogin:
|
|
||||||
return SocketEnums.TYPE_CheckLogin;
|
|
||||||
case (int)SocketEnums.Type.Logout:
|
|
||||||
return SocketEnums.TYPE_Logout;
|
|
||||||
case (int)SocketEnums.Type.HeartBeat:
|
|
||||||
return SocketEnums.TYPE_HeartBeat;
|
|
||||||
default:
|
|
||||||
return SocketEnums.TYPE_UNKNOWN;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static string GetMessage(string msg)
|
public static string GetMessage(string msg)
|
||||||
{
|
{
|
||||||
int index = msg.IndexOf(';') + 1;
|
int index = msg.IndexOf(';') + 1;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user