From 3cfe9ceda296eb12b60335d8fd3d915af42e8609 Mon Sep 17 00:00:00 2001 From: Mili Date: Sat, 27 Aug 2022 00:36:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90Socket=E5=B0=81=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Main.cs | 5 +++-- Models/Config/Config.cs | 2 +- Models/Config/SocketEnums.cs | 24 ++++++++++++++-------- Sockets/ClientSocket.cs | 39 +++++++++++------------------------- Utils/SocketHelper.cs | 31 ++++++++++++++++++++++++++++ 5 files changed, 63 insertions(+), 38 deletions(-) create mode 100644 Utils/SocketHelper.cs diff --git a/Main.cs b/Main.cs index b459508..a8d4a90 100644 --- a/Main.cs +++ b/Main.cs @@ -6,6 +6,7 @@ using System; using FunGameServer.Sockets; using System.Net.WebSockets; using FunGameServer.Models.Config; +using FunGameServer.Utils; bool Running = true; Socket? ServerSocket = null; @@ -96,7 +97,7 @@ bool Read(Socket socket) int length = socket.Receive(buffer); if (length > 0) { - string msg = Encoding.GetEncoding("unicode").GetString(buffer, 0, length); + string msg = Config.DEFAULT_ENCODING.GetString(buffer, 0, length); Console.WriteLine("收到来自:客户端(玩家ID) -> " + msg); return true; } @@ -110,7 +111,7 @@ bool Send(Socket socket) // 发送消息给客户端 string msg = ">> 已连接至服务器 -> [ " + host + " ] 连接成功"; byte[] buffer = new byte[2048]; - buffer = Encoding.GetEncoding("unicode").GetBytes(msg); + buffer = Config.DEFAULT_ENCODING.GetBytes(SocketHelper.MakeMessage((int)SocketEnums.SendType.CheckLogin, msg)); if (socket.Send(buffer) > 0) { Console.WriteLine("发送给:客户端 <- " + msg); diff --git a/Models/Config/Config.cs b/Models/Config/Config.cs index 1839afd..c07540b 100644 --- a/Models/Config/Config.cs +++ b/Models/Config/Config.cs @@ -16,7 +16,7 @@ namespace FunGameServer.Models.Config public static int CONNECTING_PLAYERS = 0; // 正在连接的玩家数量 public static string SERVER_IPADRESS = "127.0.0.1"; // 默认IP地址 public static int SERVER_PORT = 22222; // 默认端口 - public static string DEFAULT_ENCODING = "unicode"; // 默认传输字符集 + public static Encoding DEFAULT_ENCODING = Encoding.UTF8; // 默认传输字符集 public static int MAX_CONNECTFAILED = 5; // 最大连接失败次数 /// diff --git a/Models/Config/SocketEnums.cs b/Models/Config/SocketEnums.cs index acaf86b..7489d1f 100644 --- a/Models/Config/SocketEnums.cs +++ b/Models/Config/SocketEnums.cs @@ -10,18 +10,26 @@ namespace FunGameServer.Models.Config { public enum SendType { - Login = 1, - CheckLogin = 2, - Logout = 3, - HeartBeat = 4, + GetNotice = 1, + Login = 2, + CheckLogin = 3, + Logout = 4, + HeartBeat = 5 } public enum ReadType { - Login = 1, - CheckLogin = 2, - Logout = 3, - HeartBeat = 4, + GetNotice = 1, + Login = 2, + CheckLogin = 3, + Logout = 4, + HeartBeat = 5 } + + public static string SENDTYPE_GetNotice = "GetNotice"; + public static string SENDTYPE_Login = "Login"; + public static string SENDTYPE_CheckLogin = "CheckLogin"; + public static string SENDTYPE_Logout = "Logout"; + public static string SENDTYPE_HeartBeat = "HeartBeat"; } } diff --git a/Sockets/ClientSocket.cs b/Sockets/ClientSocket.cs index 7b26002..d078441 100644 --- a/Sockets/ClientSocket.cs +++ b/Sockets/ClientSocket.cs @@ -7,6 +7,7 @@ using System.Net.Sockets; using System.Text; using System.Threading.Tasks; using System.Data.SqlTypes; +using FunGameServer.Utils; namespace FunGameServer.Sockets { @@ -32,14 +33,14 @@ namespace FunGameServer.Sockets int length = socket.Receive(buffer); if (length > 0) { - string msg = Encoding.GetEncoding(Config.DEFAULT_ENCODING).GetString(buffer, 0, length); - int type = GetType(msg); - msg = GetMessage(msg); - Console.Write("收到来自:客户端(" + type + ") -> " + msg); - buffer = new byte[2048]; - length = socket.Receive(buffer); + string msg = Config.DEFAULT_ENCODING.GetString(buffer, 0, length); + int type = SocketHelper.GetType(msg); + msg = SocketHelper.GetMessage(msg); + Console.WriteLine("收到来自:客户端(" + type + ") -> " + msg); switch (type) { + case (int)SocketEnums.ReadType.GetNotice: + break; case (int)SocketEnums.ReadType.Login: break; case (int)SocketEnums.ReadType.CheckLogin: @@ -49,9 +50,7 @@ namespace FunGameServer.Sockets case (int)SocketEnums.ReadType.HeartBeat: break; } - if (Send(socket, type, msg)) - return true; - throw new Exception(); + return Send(socket, type, msg); } throw new Exception(); } @@ -67,9 +66,8 @@ namespace FunGameServer.Sockets // 发送消息给客户端 try { - string send = MakeMessage(type, msg); byte[] buffer = new byte[2048]; - buffer = Encoding.GetEncoding(Config.DEFAULT_ENCODING).GetBytes(Convert.ToString(send)); + buffer = Config.DEFAULT_ENCODING.GetBytes(Convert.ToString(SocketHelper.MakeMessage(type, msg))); if (socket.Send(buffer) > 0) { Console.WriteLine("发送给:客户端(" + type + ") <- " + msg); @@ -84,21 +82,6 @@ namespace FunGameServer.Sockets } } - private int GetType(string msg) - { - return Convert.ToInt32(msg[..(msg.IndexOf(';') - 1)]); - } - - private string GetMessage(string msg) - { - return msg.Substring(msg.IndexOf(';') + 1, msg.Length - 1); - } - - private string MakeMessage(int type, string msg) - { - return type + ";" + msg; - } - public void Start() { Task StreamReader = Task.Factory.StartNew(() => @@ -114,17 +97,19 @@ namespace FunGameServer.Sockets while (Running) { if (Socket != null) + { if (!Read(Socket)) { FailedTimes++; if (FailedTimes >= Config.MAX_CONNECTFAILED) { Console.WriteLine("ERROR: Too Many Faileds."); - Console.WriteLine("DONE: StringStream is Closed."); + Console.WriteLine("DONE: StreamReader is Closed."); break; } } else if (FailedTimes - 1 >= 0) FailedTimes--; + } else { Console.WriteLine("ERROR: Socket is Closed."); diff --git a/Utils/SocketHelper.cs b/Utils/SocketHelper.cs new file mode 100644 index 0000000..212f4bb --- /dev/null +++ b/Utils/SocketHelper.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FunGameServer.Utils +{ + 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; + } + } +}