开始封装Socket

This commit is contained in:
Mili 2022-11-18 01:11:20 +08:00
parent 1a6b41933c
commit 7db1e79fe5
7 changed files with 101 additions and 36 deletions

View File

@ -9,10 +9,17 @@ namespace Milimoe.FunGame.Core.Entity.Exception
{ {
public class SystemError : System.Exception public class SystemError : System.Exception
{ {
public string Name { get; set; } = ""; public string Name { get; } = "";
public new string StackTrace { get => base.StackTrace ?? ""; } public new string StackTrace { get => base.StackTrace ?? ""; }
public SystemError() { }
public SystemError(string Name)
{
this.Name = Name;
}
public string GetStackTrace() public string GetStackTrace()
{ {
return Name + "\r\n" + StackTrace; return Name + "\r\n" + StackTrace;

View File

@ -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
{
}
}

View File

@ -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失败。");
}
}
}

View File

@ -1,22 +1,45 @@
using Milimoe.FunGame.Core.Interface.Base; using System;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; 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 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(); Socket? socket = null;
} EndPoint ServerEndPoint;
try
int ISocket.Send()
{ {
throw new NotImplementedException(); 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;
} }
} }
} }

View File

@ -11,16 +11,16 @@ namespace Milimoe.FunGame.Desktop.Others
/** /**
* FunGame Desktop Configs * FunGame Desktop Configs
*/ */
public static bool FunGame_isAutoConnect = true; // 是否自动连接服务器 public static bool FunGame_isAutoConnect { get; set; } = true; // 是否自动连接服务器
public static bool FunGame_isAutoLogin = false; // 是否自动登录 public static bool FunGame_isAutoLogin { get; set; } = false; // 是否自动登录
public static bool FunGame_isMatching = false; // 是否在匹配中 public static bool FunGame_isMatching { get; set; } = false; // 是否在匹配中
public static bool FunGame_isConnected = false; // 是否连接上服务器 public static bool FunGame_isConnected { get; set; } = false; // 是否连接上服务器
public static bool FunGame_isRetrying = false; // 是否正在重连 public static bool FunGame_isRetrying { get; set; } = false; // 是否正在重连
public static bool FunGame_isAutoRetry = true; // 是否自动重连 public static bool FunGame_isAutoRetry { get; set; } = true; // 是否自动重连
public static bool Match_Mix = false; // 混战模式选项 public static bool Match_Mix { get; set; } = false; // 混战模式选项
public static bool Match_Team = false; // 团队模式选项 public static bool Match_Team { get; set; } = false; // 团队模式选项
public static bool Match_HasPass = false; // 密码房间选项 public static bool Match_HasPass { get; set; } = false; // 密码房间选项
public static string FunGame_Roomid = "-1"; // 房间号 public static string FunGame_Roomid { get; set; } = "-1"; // 房间号
public static string FunGame_Notice = ""; // 公告 public static string FunGame_Notice { get; set; } = ""; // 公告
} }
} }

View File

@ -14,8 +14,8 @@ namespace Milimoe.FunGame.Desktop.Others
/** /**
* Game Configs * Game Configs
*/ */
public static FunGameEnum.FunGame FunGameType = FunGameEnum.FunGame.FunGame_Desktop; public static FunGameEnum.FunGame FunGameType { get; } = FunGameEnum.FunGame.FunGame_Desktop;
public static ReflectionHelper ReflectionHelper = new(); public static ReflectionHelper ReflectionHelper { get; } = new();
/** /**
* SocketHelper Configs * SocketHelper Configs
@ -32,14 +32,14 @@ namespace Milimoe.FunGame.Desktop.Others
public const string SocketHelper_GetUser = "-SocketHelper .get user"; public const string SocketHelper_GetUser = "-SocketHelper .get user";
public const string SocketHelper_SetUser = "-SocketHelper .set user"; public const string SocketHelper_SetUser = "-SocketHelper .set user";
public const string SocketHelper_SetNotice = "-SocketHelper .set notice"; public const string SocketHelper_SetNotice = "-SocketHelper .set notice";
public static int SocketHelper_HeartBeatFaileds = 0; public static int SocketHelper_HeartBeatFaileds { get; set; } = 0;
/** /**
* Socket Configs * Socket Configs
*/ */
public static string SERVER_IPADRESS = ""; // 服务器IP地址 public static string SERVER_IPADRESS { get; set; } = ""; // 服务器IP地址
public static int SERVER_PORT = 0; // 服务器端口号 public static int SERVER_PORT { get; set; } = 0; // 服务器端口号
public static Encoding DEFAULT_ENCODING = Encoding.UTF8; public static Encoding DEFAULT_ENCODING { get; set; } = Encoding.UTF8;
/** /**
* FunGame Configs * FunGame Configs

View File

@ -382,14 +382,8 @@ namespace Milimoe.FunGame.Desktop.Utils
private void StartSocketHelper() private void StartSocketHelper()
{ {
Task HeartBeatStream = Task.Factory.StartNew(() => Task HeartBeatStream = Task.Factory.StartNew(CreateSendHeartBeatStream);
{ Task StreamReader = Task.Factory.StartNew(CreateStreamReader);
CreateSendHeartBeatStream();
});
Task StreamReader = Task.Factory.StartNew(() =>
{
CreateStreamReader();
});
} }
private void CreateSendHeartBeatStream() private void CreateSendHeartBeatStream()