diff --git a/FunGame.Core/Entity/Exception/SystemError.cs b/FunGame.Core/Entity/Exception/SystemError.cs index b76ca0d..ccae7b4 100644 --- a/FunGame.Core/Entity/Exception/SystemError.cs +++ b/FunGame.Core/Entity/Exception/SystemError.cs @@ -9,10 +9,17 @@ namespace Milimoe.FunGame.Core.Entity.Exception { public class SystemError : System.Exception { - public string Name { get; set; } = ""; + public string Name { get; } = ""; public new string StackTrace { get => base.StackTrace ?? ""; } + public SystemError() { } + + public SystemError(string Name) + { + this.Name = Name; + } + public string GetStackTrace() { return Name + "\r\n" + StackTrace; diff --git a/FunGame.Core/Entity/Network/SQLConnection.cs b/FunGame.Core/Entity/Network/SQLConnection.cs new file mode 100644 index 0000000..11f59ce --- /dev/null +++ b/FunGame.Core/Entity/Network/SQLConnection.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Milimoe.FunGame.Core.Entity.Network +{ + public class SQLConnection + { + } +} diff --git a/FunGame.Core/Entity/Network/Socket.cs b/FunGame.Core/Entity/Network/Socket.cs new file mode 100644 index 0000000..eb0bb71 --- /dev/null +++ b/FunGame.Core/Entity/Network/Socket.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Net.Sockets; +using System.Net; +using Milimoe.FunGame.Core.Interface; +using Milimoe.FunGame.Core.Service; + +namespace Milimoe.FunGame.Core.Entity.Network +{ + public class Socket + { + public System.Net.Sockets.Socket Instance { get; } + + private Socket(System.Net.Sockets.Socket Instance) + { + this.Instance = Instance; + } + + public static Socket Connect(string IP, int Port = 22222) + { + System.Net.Sockets.Socket? socket = SocketManager.Connect(IP, Port); + if (socket != null) return new Socket(socket); + else throw new Milimoe.FunGame.Core.Entity.Exception.SystemError("创建Socket失败。"); + } + } +} diff --git a/FunGame.Core/Service/SocketManager.cs b/FunGame.Core/Service/SocketManager.cs index efb847b..7fad323 100644 --- a/FunGame.Core/Service/SocketManager.cs +++ b/FunGame.Core/Service/SocketManager.cs @@ -1,22 +1,45 @@ -using Milimoe.FunGame.Core.Interface.Base; -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Milimoe.FunGame.Core.Interface.Base; +using System.Collections; +using System.Net.Sockets; +using System.Net; namespace Milimoe.FunGame.Core.Service { - internal class SocketManager : ISocket + internal class SocketManager { - int ISocket.Read() + internal static Socket? Connect(string IP, int Port = 22222) { - throw new NotImplementedException(); - } - - int ISocket.Send() - { - throw new NotImplementedException(); + Socket? socket = null; + EndPoint ServerEndPoint; + try + { + socket = new(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + ServerEndPoint = new IPEndPoint(IPAddress.Parse(IP), Port); + if (ServerEndPoint != null) + { + while (true) + { + if (!socket.Connected) + { + socket.Connect(ServerEndPoint); + if (socket.Connected) + { + return socket; + } + } + } + } + } + catch + { + socket?.Close(); + } + return null; } } } diff --git a/FunGame.Desktop/Others/Config.cs b/FunGame.Desktop/Others/Config.cs index d9465fd..f43d9b2 100644 --- a/FunGame.Desktop/Others/Config.cs +++ b/FunGame.Desktop/Others/Config.cs @@ -11,16 +11,16 @@ namespace Milimoe.FunGame.Desktop.Others /** * FunGame Desktop Configs */ - public static bool FunGame_isAutoConnect = true; // 是否自动连接服务器 - public static bool FunGame_isAutoLogin = false; // 是否自动登录 - public static bool FunGame_isMatching = false; // 是否在匹配中 - public static bool FunGame_isConnected = false; // 是否连接上服务器 - public static bool FunGame_isRetrying = false; // 是否正在重连 - public static bool FunGame_isAutoRetry = true; // 是否自动重连 - public static bool Match_Mix = false; // 混战模式选项 - public static bool Match_Team = false; // 团队模式选项 - public static bool Match_HasPass = false; // 密码房间选项 - public static string FunGame_Roomid = "-1"; // 房间号 - public static string FunGame_Notice = ""; // 公告 + public static bool FunGame_isAutoConnect { get; set; } = true; // 是否自动连接服务器 + public static bool FunGame_isAutoLogin { get; set; } = false; // 是否自动登录 + public static bool FunGame_isMatching { get; set; } = false; // 是否在匹配中 + public static bool FunGame_isConnected { get; set; } = false; // 是否连接上服务器 + public static bool FunGame_isRetrying { get; set; } = false; // 是否正在重连 + public static bool FunGame_isAutoRetry { get; set; } = true; // 是否自动重连 + public static bool Match_Mix { get; set; } = false; // 混战模式选项 + public static bool Match_Team { get; set; } = false; // 团队模式选项 + public static bool Match_HasPass { get; set; } = false; // 密码房间选项 + public static string FunGame_Roomid { get; set; } = "-1"; // 房间号 + public static string FunGame_Notice { get; set; } = ""; // 公告 } } diff --git a/FunGame.Desktop/Others/Constant.cs b/FunGame.Desktop/Others/Constant.cs index 5d0ef50..6aac9c5 100644 --- a/FunGame.Desktop/Others/Constant.cs +++ b/FunGame.Desktop/Others/Constant.cs @@ -14,8 +14,8 @@ namespace Milimoe.FunGame.Desktop.Others /** * Game Configs */ - public static FunGameEnum.FunGame FunGameType = FunGameEnum.FunGame.FunGame_Desktop; - public static ReflectionHelper ReflectionHelper = new(); + public static FunGameEnum.FunGame FunGameType { get; } = FunGameEnum.FunGame.FunGame_Desktop; + public static ReflectionHelper ReflectionHelper { get; } = new(); /** * SocketHelper Configs @@ -32,14 +32,14 @@ namespace Milimoe.FunGame.Desktop.Others public const string SocketHelper_GetUser = "-SocketHelper .get user"; public const string SocketHelper_SetUser = "-SocketHelper .set user"; public const string SocketHelper_SetNotice = "-SocketHelper .set notice"; - public static int SocketHelper_HeartBeatFaileds = 0; + public static int SocketHelper_HeartBeatFaileds { get; set; } = 0; /** * Socket Configs */ - public static string SERVER_IPADRESS = ""; // 服务器IP地址 - public static int SERVER_PORT = 0; // 服务器端口号 - public static Encoding DEFAULT_ENCODING = Encoding.UTF8; + public static string SERVER_IPADRESS { get; set; } = ""; // 服务器IP地址 + public static int SERVER_PORT { get; set; } = 0; // 服务器端口号 + public static Encoding DEFAULT_ENCODING { get; set; } = Encoding.UTF8; /** * FunGame Configs diff --git a/FunGame.Desktop/Utils/SocketHelper.cs b/FunGame.Desktop/Utils/SocketHelper.cs index 28210c9..e3e081e 100644 --- a/FunGame.Desktop/Utils/SocketHelper.cs +++ b/FunGame.Desktop/Utils/SocketHelper.cs @@ -382,14 +382,8 @@ namespace Milimoe.FunGame.Desktop.Utils private void StartSocketHelper() { - Task HeartBeatStream = Task.Factory.StartNew(() => - { - CreateSendHeartBeatStream(); - }); - Task StreamReader = Task.Factory.StartNew(() => - { - CreateStreamReader(); - }); + Task HeartBeatStream = Task.Factory.StartNew(CreateSendHeartBeatStream); + Task StreamReader = Task.Factory.StartNew(CreateStreamReader); } private void CreateSendHeartBeatStream()