新架构初体验

This commit is contained in:
Mili 2023-01-03 21:25:48 +08:00
parent 909f078666
commit 689e711c87
6 changed files with 79 additions and 55 deletions

View File

@ -5,6 +5,7 @@ using System.Net.NetworkInformation;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Milimoe.FunGame.Core.Library.Constant;
@ -104,6 +105,35 @@ namespace Milimoe.FunGame.Core.Api.Utility
}
return -1;
}
/// <summary>
/// 将JsonElement转换为泛型
/// </summary>
/// <typeparam name="T">泛型</typeparam>
/// <param name="obj">为JsonElement的对象</param>
/// <returns></returns>
public static T? ConvertJsonObject<T>(object obj)
{
return ((JsonElement)obj).ToObject<T>();
}
}
/// <summary>
/// Json工具类
/// </summary>
public static class JsonUtility
{
/// <summary>
/// 将JsonElement转换为泛型
/// </summary>
/// <typeparam name="T">泛型</typeparam>
/// <param name="element">JsonElement</param>
/// <returns></returns>
public static T? ToObject<T>(this JsonElement element)
{
var json = element.GetRawText();
return JsonSerializer.Deserialize<T>(json);
}
}
#endregion

View File

@ -3,20 +3,22 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Milimoe.FunGame.Core.Library.Constant;
namespace Milimoe.FunGame.Core.Library.Common.Network
{
[Serializable]
internal class JsonObject
public class JsonObject
{
internal SocketMessageType MessageType { get; } = SocketMessageType.Unknown;
internal string Token { get; }
internal object[] Parameters { get; }
internal string JsonString { get; }
public SocketMessageType MessageType { get; } = SocketMessageType.Unknown;
public string Token { get; }
public object[] Parameters { get; }
public string JsonString { get; }
internal JsonObject(SocketMessageType MessageType, string Token, object[] Parameters)
[JsonConstructor]
public JsonObject(SocketMessageType MessageType, string Token, object[] Parameters)
{
this.MessageType = MessageType;
this.Token = Token;
@ -24,12 +26,12 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
this.JsonString = JsonSerializer.Serialize(this);
}
internal static string GetString(SocketMessageType MessageType, string Token, object[] Parameters)
public static string GetString(SocketMessageType MessageType, string Token, object[] Parameters)
{
return new JsonObject(MessageType, Token, Parameters).JsonString;
}
internal static JsonObject? GetObject(string JsonString)
public static JsonObject? GetObject(string JsonString)
{
return JsonSerializer.Deserialize<JsonObject>(JsonString);
}

View File

@ -169,8 +169,7 @@ namespace Milimoe.FunGame.Core.Service
Library.Common.Network.JsonObject? json = Library.Common.Network.JsonObject.GetObject(msg);
if (json != null)
{
result[0] = json.MessageType;
result[1] = json.Parameters;
result = new object[] { json.MessageType, json.Parameters };
}
return result;
}
@ -197,8 +196,7 @@ namespace Milimoe.FunGame.Core.Service
Library.Common.Network.JsonObject? json = Library.Common.Network.JsonObject.GetObject(msg);
if (json != null)
{
result[0] = json.MessageType;
result[1] = json.Parameters;
result = new object[] { json.MessageType, json.Parameters };
}
return result;
}

View File

@ -16,7 +16,7 @@ namespace Milimoe.FunGame.Desktop.Controller
{
public class MainController
{
private MainModel MainModel;
private MainModel MainModel { get; }
public MainController(Main Main)
{

View File

@ -1,4 +1,9 @@
using Milimoe.FunGame.Core.Api.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Milimoe.FunGame.Core.Api.Utility;
using Milimoe.FunGame.Core.Entity;
using Milimoe.FunGame.Core.Library.Common.Event;
using Milimoe.FunGame.Core.Library.Constant;
@ -6,11 +11,6 @@ using Milimoe.FunGame.Core.Library.Exception;
using Milimoe.FunGame.Desktop.Library.Component;
using Milimoe.FunGame.Desktop.Others;
using Milimoe.FunGame.Desktop.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Milimoe.FunGame.Desktop.Model
{
@ -19,8 +19,6 @@ namespace Milimoe.FunGame.Desktop.Model
public Core.Library.Common.Network.Socket? Socket { get; private set; }
public Main Main { get; }
private int CurrentRetryTimes = -1;
private int MaxRetryTimes { get; } = SocketSet.MaxRetryTimes;
private Task? ReceivingTask;
public MainModel(Main main)
@ -117,11 +115,11 @@ namespace Milimoe.FunGame.Desktop.Model
}
if (!Others.Config.FunGame_isConnected)
{
CurrentRetryTimes++;
if (CurrentRetryTimes == 0) Main?.GetMessage("开始连接服务器...", true, TimeType.General);
else Main?.GetMessage("第" + CurrentRetryTimes + "次重试连接服务器...");
Main!.CurrentRetryTimes++;
if (Main!.CurrentRetryTimes == 0) Main!.GetMessage("开始连接服务器...", true, TimeType.General);
else Main!.GetMessage("第" + Main!.CurrentRetryTimes + "次重试连接服务器...");
// 超过重连次数上限
if (CurrentRetryTimes + 1 > MaxRetryTimes)
if (Main!.CurrentRetryTimes + 1 > Main!.MaxRetryTimes)
{
throw new Exception("无法连接至服务器,请检查网络并重启游戏再试。");
}
@ -141,12 +139,9 @@ namespace Milimoe.FunGame.Desktop.Model
Main?.GetMessage("连接服务器成功请登录账号以体验FunGame。");
Main?.UpdateUI(MainControllerSet.Connected);
StartReceiving();
return ConnectResult.Success;
}
return ConnectResult.ConnectFailed;
});
t.Wait(5000);
Main?.GetMessage("ERROR: 连接超时,远程服务器没有回应。", false);
return ConnectResult.Success;
}
Socket?.Close();
Config.FunGame_isRetrying = false;
@ -273,13 +268,13 @@ namespace Milimoe.FunGame.Desktop.Model
private void SocketHandle_Connect(object[] objs)
{
string msg = "";
if (objs.Length > 0) msg = (string)objs[0];
if (objs.Length > 0) msg = NetworkUtility.ConvertJsonObject<string>(objs[0])!;
string[] strings = msg.Split(';');
string ServerName = strings[0];
string ServerNotice = strings[1];
Config.FunGame_ServerName = ServerName;
Config.FunGame_Notice = ServerNotice;
if (objs.Length > 1) msg = (string)objs[1];
if (objs.Length > 1) msg = NetworkUtility.ConvertJsonObject<string>(objs[1])!;
Socket!.Token = msg;
Main?.GetMessage($"已连接服务器:{ServerName}。\n\n********** 服务器公告 **********\n\n{ServerNotice}\n\n");
// 设置等待登录的黄灯
@ -288,14 +283,14 @@ namespace Milimoe.FunGame.Desktop.Model
private void SocketHandle_GetNotice(object[] objs)
{
if (objs.Length > 0) Config.FunGame_Notice = (string)objs[0];
if (objs.Length > 0) Config.FunGame_Notice = NetworkUtility.ConvertJsonObject<string>(objs[0])!;
}
private void SocketHandle_CheckLogin(object[] objs)
{
string msg = "";
// 返回的objs是该Login的User对象的各个属性
if (objs.Length > 0) msg = (string)objs[0];
if (objs.Length > 0) msg = NetworkUtility.ConvertJsonObject<string>(objs[0])!;
Main?.GetMessage(msg);
Main?.UpdateUI(MainControllerSet.SetUser, new object[] { Factory.New<User>(msg) });
}
@ -303,9 +298,9 @@ namespace Milimoe.FunGame.Desktop.Model
private void SocketHandle_Disconnect(object[] objs)
{
string msg = "";
if (objs.Length > 0) msg = (string)objs[0];
if (objs.Length > 0) msg = NetworkUtility.ConvertJsonObject<string>(objs[0])!;
Main?.GetMessage(msg);
Main?.UpdateUI(MainControllerSet.Disconnected);
Main?.UpdateUI(MainControllerSet.Disconnect);
Close();
}

View File

@ -25,9 +25,10 @@ namespace Milimoe.FunGame.Desktop.UI
/**
*
*/
private int LOCATION_X, LOCATION_Y; // 窗口当前坐标
private int MAX_CONNECTEDRETRY = 20; // 最大重试连接次数
private int NOW_CONNECTEDRETRY = -1; // 当前重试连接次数
public int MaxRetryTimes { get; } = SocketSet.MaxRetryTimes; // 最大重试连接次数
public int CurrentRetryTimes { get; set; } = -1; // 当前重试连接次数
private int loc_x, loc_y; // 窗口当前坐标
/**
*
@ -105,7 +106,7 @@ namespace Milimoe.FunGame.Desktop.UI
SetServerStatusLight((int)LightType.Green);
SetButtonEnableIfLogon(true, ClientState.Online);
Others.Config.FunGame_isConnected = true;
NOW_CONNECTEDRETRY = 0;
CurrentRetryTimes = 0;
break;
case Others.MainControllerSet.SetGreenAndPing:
@ -113,7 +114,7 @@ namespace Milimoe.FunGame.Desktop.UI
SetServerStatusLight((int)LightType.Green, ping: NetworkUtility.GetServerPing(Others.Constant.SERVER_IPADRESS));
SetButtonEnableIfLogon(true, ClientState.Online);
Others.Config.FunGame_isConnected = true;
NOW_CONNECTEDRETRY = 0;
CurrentRetryTimes = 0;
break;
case Others.MainControllerSet.SetYellow:
@ -121,7 +122,7 @@ namespace Milimoe.FunGame.Desktop.UI
SetServerStatusLight((int)LightType.Yellow);
SetButtonEnableIfLogon(false, ClientState.WaitConnect);
Others.Config.FunGame_isConnected = true;
NOW_CONNECTEDRETRY = 0;
CurrentRetryTimes = 0;
break;
case Others.MainControllerSet.WaitConnectAndSetYellow:
@ -129,7 +130,7 @@ namespace Milimoe.FunGame.Desktop.UI
SetServerStatusLight((int)LightType.Yellow);
SetButtonEnableIfLogon(false, ClientState.WaitConnect);
Others.Config.FunGame_isConnected = true;
NOW_CONNECTEDRETRY = 0;
CurrentRetryTimes = 0;
if (MainController != null && Others.Config.FunGame_isAutoConnect)
{
// 自动连接服务器
@ -142,7 +143,7 @@ namespace Milimoe.FunGame.Desktop.UI
SetServerStatusLight((int)LightType.Yellow, true);
SetButtonEnableIfLogon(false, ClientState.WaitLogin);
Others.Config.FunGame_isConnected = true;
NOW_CONNECTEDRETRY = 0;
CurrentRetryTimes = 0;
break;
case Others.MainControllerSet.SetRed:
@ -157,7 +158,7 @@ namespace Milimoe.FunGame.Desktop.UI
SetServerStatusLight((int)LightType.Red);
SetButtonEnableIfLogon(false, ClientState.WaitConnect);
LogoutAccount();
if (Others.Config.FunGame_isAutoRetry && NOW_CONNECTEDRETRY <= MAX_CONNECTEDRETRY)
if (Others.Config.FunGame_isAutoRetry && CurrentRetryTimes <= MaxRetryTimes)
{
Task.Run(() =>
{
@ -190,7 +191,7 @@ namespace Milimoe.FunGame.Desktop.UI
LogoutAccount();
if (Others.Config.FunGame_isAutoConnect)
{
NOW_CONNECTEDRETRY = -1;
CurrentRetryTimes = -1;
Task.Run(() =>
{
Thread.Sleep(1000);
@ -824,8 +825,8 @@ namespace Milimoe.FunGame.Desktop.UI
if (e.Button == MouseButtons.Left)
{
//获取鼠标左键按下时的位置
LOCATION_X = e.Location.X;
LOCATION_Y = e.Location.Y;
loc_x = e.Location.X;
loc_y = e.Location.Y;
}
}
@ -839,8 +840,8 @@ namespace Milimoe.FunGame.Desktop.UI
if (e.Button == MouseButtons.Left)
{
//计算鼠标移动距离
Left += e.Location.X - LOCATION_X;
Top += e.Location.Y - LOCATION_Y;
Left += e.Location.X - loc_x;
Top += e.Location.Y - loc_y;
}
}
@ -1255,7 +1256,7 @@ namespace Milimoe.FunGame.Desktop.UI
case Others.Constant.FunGame_Retry:
if (!Others.Config.FunGame_isRetrying)
{
NOW_CONNECTEDRETRY = -1;
CurrentRetryTimes = -1;
MainController?.Do<object>(MainControllerSet.Connect);
}
else
@ -1264,7 +1265,7 @@ namespace Milimoe.FunGame.Desktop.UI
case Others.Constant.FunGame_Connect:
if (!Others.Config.FunGame_isConnected)
{
NOW_CONNECTEDRETRY = -1;
CurrentRetryTimes = -1;
MainController?.Do<bool>(MainControllerSet.GetServerConnection);
}
break;
@ -1277,9 +1278,7 @@ namespace Milimoe.FunGame.Desktop.UI
case Others.Constant.FunGame_DisconnectWhenNotLogin:
if (Others.Config.FunGame_isConnected && MainController != null)
{
MainController?.Do<bool>(MainControllerSet.Close);
UpdateUI(MainControllerSet.Disconnect);
WritelnGameInfo(DateTimeUtility.GetNowShortTime() + " >> 你已成功断开与服务器的连接。 ");
MainController?.Do<object>(MainControllerSet.Disconnect);
}
break;
case Others.Constant.FunGame_ConnectTo:
@ -1308,7 +1307,7 @@ namespace Milimoe.FunGame.Desktop.UI
{
Others.Constant.SERVER_IPADRESS = ip;
Others.Constant.SERVER_PORT = port;
NOW_CONNECTEDRETRY = -1;
CurrentRetryTimes = -1;
MainController?.Do<object>(MainControllerSet.Connect);
}
else if (ErrorType == Core.Library.Constant.ErrorType.IsNotIP) ShowMessage.ErrorMessage("这不是一个IP地址");