结构调整,实现退出登录

This commit is contained in:
Mili 2022-09-16 22:30:39 +08:00
parent 2da0407fa9
commit 938bc19e12
7 changed files with 198 additions and 151 deletions

View File

@ -5,124 +5,128 @@ using System.Security.Cryptography;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace FunGame.Core.Api.Model.Enum namespace FunGame.Core.Api.Model.Enum
{ {
/// <summary> /**
/// 这里存放框架实现相关的State Type Result Method * State Type Result Method
/// 添加FunGame.Core.Api接口和实现时需要在这里同步添加InterfaceType、InterfaceMethod * FunGame.Core.Api接口和实现时InterfaceTypeInterfaceMethod
/// </summary> */
public static class CommonEnums
#region State
public enum StartMatch_State
{ {
#region State Matching,
Success,
Enable,
Cancel
}
public enum StartMatch_State public enum CreateRoom_State
{ {
Matching, Creating,
Success, Success
Enable, }
Cancel
}
public enum CreateRoom_State public enum RoomState
{ {
Creating, Created,
Success Gaming,
} Close,
Complete
}
public enum RoomState public enum OnlineState
{ {
Created, Offline,
Gaming, Online,
Close, Matching,
Complete InRoom,
} Gaming
}
public enum OnlineState #endregion
{
Offline,
Online,
Matching,
InRoom,
Gaming
}
#endregion #region Type
#region Type public enum RoomType
{
Mix,
Team,
MixHasPass,
TeamHasPass
}
public enum RoomType public enum MessageButtonType
{ {
Mix, OK,
Team, OKCancel,
MixHasPass, YesNo,
TeamHasPass RetryCancel
} }
public enum MessageButtonType public enum InterfaceType
{ {
OK, ClientConnectInterface,
OKCancel, ServerInterface
YesNo, }
RetryCancel
}
public enum InterfaceType public enum LightType
{ {
ClientConnectInterface, Green,
ServerInterface Yellow,
} Red
}
public enum LightType public enum SocketMessageType
{ {
Green, Unknown,
Yellow, GetNotice,
Red Login,
} CheckLogin,
Logout,
HeartBeat
}
public enum SocketType #endregion
{
Unknown,
GetNotice,
Login,
CheckLogin,
Logout,
HeartBeat
}
#endregion #region Result
#region Result public enum MessageResult
{
OK,
Cancel,
Yes,
No,
Retry
}
public enum MessageResult #endregion
{
OK,
Cancel,
Yes,
No,
Retry
}
#endregion #region Method
#region Method public enum WebHelperMethod
{
CreateSocket,
CloseSocket,
StartWebHelper,
Login,
Logout
}
public enum WebHelperMethod public enum InterfaceMethod
{ {
CreateSocket, RemoteServerIP,
CloseSocket, DBConnection,
StartWebHelper, GetServerSettings
Login }
}
public enum InterfaceMethod #endregion
{
RemoteServerIP,
DBConnection,
GetServerSettings
}
#endregion
public class EnumHelper
{
#region ¹¤¾ß·½·¨ #region ¹¤¾ß·½·¨
/// <summary> /// <summary>
@ -164,9 +168,9 @@ namespace FunGame.Core.Api.Model.Enum
/// <returns></returns> /// <returns></returns>
public static string GetSocketTypeName(int SocketType) public static string GetSocketTypeName(int SocketType)
{ {
foreach (string str in System.Enum.GetNames(typeof(SocketType))) foreach (string str in System.Enum.GetNames(typeof(SocketMessageType)))
{ {
SocketType temp = (SocketType)System.Enum.Parse(typeof(SocketType), SocketType.ToString(), true); SocketMessageType temp = (SocketMessageType)System.Enum.Parse(typeof(SocketMessageType), SocketType.ToString(), true);
if (temp.ToString() == str) if (temp.ToString() == str)
return temp.ToString(); return temp.ToString();
} }

View File

@ -8,15 +8,6 @@ namespace FunGame.Core.Api.Model.Enum
{ {
public static class FunGameEnums public static class FunGameEnums
{ {
private const string FunGame_Core = "FunGame Core";
private const string FunGame_Core_Api = "FunGame Core Api";
private const string FunGame_Console = "FunGame Console";
private const string FunGame_Desktop = "FunGame Desktop";
private const string FunGame_Server = "FunGame Server Console";
private const string FunGame_Version = "v1.0";
private const string FunGame_VersionPatch = "";
public enum FunGame public enum FunGame
{ {
FunGame_Core, FunGame_Core,
@ -26,6 +17,15 @@ namespace FunGame.Core.Api.Model.Enum
FunGame_Server FunGame_Server
} }
private const string FunGame_Core = "FunGame Core";
private const string FunGame_Core_Api = "FunGame Core Api";
private const string FunGame_Console = "FunGame Console";
private const string FunGame_Desktop = "FunGame Desktop";
private const string FunGame_Server = "FunGame Server Console";
private const string FunGame_Version = "v1.0";
private const string FunGame_VersionPatch = "";
public static string GetInfo(FunGame FunGameType) public static string GetInfo(FunGame FunGameType)
{ {
string type = FunGameType switch string type = FunGameType switch
@ -37,10 +37,7 @@ namespace FunGame.Core.Api.Model.Enum
FunGame.FunGame_Server => FunGame_Server, FunGame.FunGame_Server => FunGame_Server,
_ => "" _ => ""
}; };
if (type.Equals(FunGame_Desktop)) return type + " [ 版本: " + FunGame_Version + FunGame_VersionPatch + " ]\n" + (type.Equals(FunGame_Desktop) ? "©" : "(C)") + "2022 Mili.cyou. 保留所有权利\n";
return type + " [ 版本: " + FunGame_Version + FunGame_VersionPatch + " ]\n©2022 Mili.cyou. 保留所有权利\n";
else
return type + " [ 版本: " + FunGame_Version + FunGame_VersionPatch + " ]\n(C)2022 Mili.cyou. 保留所有权利\n";
} }
/** /**

View File

@ -47,7 +47,7 @@ namespace FunGame.Core.Api.Util
private Type? GetFunGameCoreImplement(int Interface) private Type? GetFunGameCoreImplement(int Interface)
{ {
// 通过类名获取获取命名空间+类名称 // 通过类名获取获取命名空间+类名称
string ClassName = CommonEnums.GetImplementClassName(Interface); string ClassName = EnumHelper.GetImplementClassName(Interface);
List<Type>? Classes = null; List<Type>? Classes = null;
if (Assembly != null) if (Assembly != null)
{ {
@ -72,7 +72,7 @@ namespace FunGame.Core.Api.Util
{ {
Assembly = Assembly.LoadFile(EXEDocPath + @FUNGAME_CORE + ".dll"); Assembly = Assembly.LoadFile(EXEDocPath + @FUNGAME_CORE + ".dll");
Type = GetFunGameCoreImplement(Interface); // 通过类名获取获取命名空间+类名称 Type = GetFunGameCoreImplement(Interface); // 通过类名获取获取命名空间+类名称
string MethodName = CommonEnums.GetImplementMethodName(Method); // 获取方法名 string MethodName = EnumHelper.GetImplementMethodName(Method); // 获取方法名
if (Assembly != null && Type != null) this.Method = Type.GetMethod(MethodName); // 从Type中查找方法名 if (Assembly != null && Type != null) this.Method = Type.GetMethod(MethodName); // 从Type中查找方法名
else return null; else return null;
Instance = Assembly.CreateInstance(Type.Namespace + "." + Type.Name); Instance = Assembly.CreateInstance(Type.Namespace + "." + Type.Name);

View File

@ -8,7 +8,7 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using FunGame.Core.Api.Model.Enum; using FunGame.Core.Api.Model.Enum;
using static FunGame.Core.Api.Model.Enum.CommonEnums; using static FunGame.Core.Api.Model.Enum.EnumHelper;
namespace FunGame.Desktop.Models.Component namespace FunGame.Desktop.Models.Component
{ {

View File

@ -27,6 +27,7 @@ namespace FunGame.Desktop.Models.Config
public const string WebHelper_SetRed = "-WebHelper .set red"; public const string WebHelper_SetRed = "-WebHelper .set red";
public const string WebHelper_SetYellow = "-WebHelper .set yellow"; public const string WebHelper_SetYellow = "-WebHelper .set yellow";
public const string WebHelper_Disconnected = "-WebHelper .disconnected"; public const string WebHelper_Disconnected = "-WebHelper .disconnected";
public const string WebHelper_LogOut = "-WebHelper .logout";
public const string WebHelper_GetUser = "-WebHelper .get user"; public const string WebHelper_GetUser = "-WebHelper .get user";
public const string WebHelper_SetUser = "-WebHelper .set user"; public const string WebHelper_SetUser = "-WebHelper .set user";
public static int WebHelper_HeartBeatFaileds = 0; public static int WebHelper_HeartBeatFaileds = 0;

View File

@ -9,7 +9,6 @@ using FunGame.Core.Api.Model.Entity;
using FunGame.Desktop.Models.Config; using FunGame.Desktop.Models.Config;
using FunGame.Desktop.Utils; using FunGame.Desktop.Utils;
using FunGame.Core.Api.Model.Enum; using FunGame.Core.Api.Model.Enum;
using static FunGame.Core.Api.Model.Enum.CommonEnums;
namespace FunGame.Desktop.UI namespace FunGame.Desktop.UI
{ {
@ -80,7 +79,7 @@ namespace FunGame.Desktop.UI
Usercfg.FunGame_isRetrying = false; Usercfg.FunGame_isRetrying = false;
WebHelper_Action = (main) => WebHelper_Action = (main) =>
{ {
SetServerStatusLight((int)CommonEnums.LightType.Green); SetServerStatusLight((int)LightType.Green);
SetButtonEnableIfLogon(true); SetButtonEnableIfLogon(true);
}; };
if (InvokeRequired) if (InvokeRequired)
@ -94,7 +93,7 @@ namespace FunGame.Desktop.UI
Usercfg.FunGame_isRetrying = false; Usercfg.FunGame_isRetrying = false;
WebHelper_Action = (main) => WebHelper_Action = (main) =>
{ {
SetServerStatusLight((int)CommonEnums.LightType.Green, GetServerPing(Config.SERVER_IPADRESS)); SetServerStatusLight((int)LightType.Green, GetServerPing(Config.SERVER_IPADRESS));
SetButtonEnableIfLogon(true); SetButtonEnableIfLogon(true);
}; };
if (InvokeRequired) if (InvokeRequired)
@ -108,7 +107,7 @@ namespace FunGame.Desktop.UI
Usercfg.FunGame_isRetrying = false; Usercfg.FunGame_isRetrying = false;
WebHelper_Action = (main) => WebHelper_Action = (main) =>
{ {
SetServerStatusLight((int)CommonEnums.LightType.Yellow); SetServerStatusLight((int)LightType.Yellow);
SetButtonEnableIfLogon(false); SetButtonEnableIfLogon(false);
}; };
if (InvokeRequired) if (InvokeRequired)
@ -121,7 +120,7 @@ namespace FunGame.Desktop.UI
case Config.WebHelper_SetRed: case Config.WebHelper_SetRed:
WebHelper_Action = (main) => WebHelper_Action = (main) =>
{ {
SetServerStatusLight((int)CommonEnums.LightType.Red); SetServerStatusLight((int)LightType.Red);
SetButtonEnableIfLogon(false); SetButtonEnableIfLogon(false);
}; };
if (InvokeRequired) if (InvokeRequired)
@ -132,22 +131,22 @@ namespace FunGame.Desktop.UI
break; break;
case Config.WebHelper_Disconnected: case Config.WebHelper_Disconnected:
Usercfg.FunGame_isRetrying = false; Usercfg.FunGame_isRetrying = false;
Usercfg.FunGame_isConnected = false;
WebHelper_Action = (main) => WebHelper_Action = (main) =>
{ {
SetServerStatusLight((int)CommonEnums.LightType.Red); SetServerStatusLight((int)LightType.Red);
SetButtonEnableIfLogon(false); SetButtonEnableIfLogon(false);
}; };
if (InvokeRequired) if (InvokeRequired)
BeginInvoke(WebHelper_Action, this); BeginInvoke(WebHelper_Action, this);
else else
WebHelper_Action(this); WebHelper_Action(this);
Usercfg.FunGame_isConnected = false;
if (Usercfg.FunGame_isAutoRetry && NOW_CONNECTEDRETRY <= MAX_CONNECTEDRETRY) if (Usercfg.FunGame_isAutoRetry && NOW_CONNECTEDRETRY <= MAX_CONNECTEDRETRY)
{ {
Task.Run(() => Task.Run(() =>
{ {
Thread.Sleep(5000); Thread.Sleep(5000);
Connect(); if (Usercfg.FunGame_isAutoRetry) Connect(); // 再次判断是否开启自动重连
}); });
if (needTime) if (needTime)
throw new Exception(GetNowShortTime() + "\nERROR连接服务器失败5秒后自动尝试重连。"); throw new Exception(GetNowShortTime() + "\nERROR连接服务器失败5秒后自动尝试重连。");
@ -159,6 +158,29 @@ namespace FunGame.Desktop.UI
throw new Exception(GetNowShortTime() + "\nERROR无法连接至服务器请检查你的网络连接。"); throw new Exception(GetNowShortTime() + "\nERROR无法连接至服务器请检查你的网络连接。");
else else
throw new Exception("ERROR无法连接至服务器请检查你的网络连接。"); throw new Exception("ERROR无法连接至服务器请检查你的网络连接。");
case Config.WebHelper_LogOut:
Usercfg.FunGame_isRetrying = false;
Usercfg.FunGame_isConnected = false;
WebHelper_Action = (main) =>
{
SetServerStatusLight((int)LightType.Red);
SetButtonEnableIfLogon(false);
LogoutAccount();
};
if (InvokeRequired)
BeginInvoke(WebHelper_Action, this);
else
WebHelper_Action(this);
if (Usercfg.FunGame_isAutoRetry)
{
NOW_CONNECTEDRETRY = -1;
Task.Run(() =>
{
Thread.Sleep(1000);
Connect();
});
}
break;
case Config.WebHelper_GetUser: case Config.WebHelper_GetUser:
if (Usercfg.LoginUser != null) if (Usercfg.LoginUser != null)
return Usercfg.LoginUser; return Usercfg.LoginUser;
@ -636,6 +658,8 @@ namespace FunGame.Desktop.UI
/// </summary> /// </summary>
private void LogoutAccount() private void LogoutAccount()
{ {
Usercfg.LoginUser = null;
Usercfg.LoginUserName = "";
NowAccount.Text = "请登录账号"; NowAccount.Text = "请登录账号";
Logout.Visible = false; Logout.Visible = false;
Login.Visible = true; Login.Visible = true;
@ -757,15 +781,15 @@ namespace FunGame.Desktop.UI
{ {
switch(light) switch(light)
{ {
case (int)CommonEnums.LightType.Green: case (int)LightType.Green:
Connection.Text = "服务器连接成功"; Connection.Text = "服务器连接成功";
this.Light.Image = Properties.Resources.green; this.Light.Image = Properties.Resources.green;
break; break;
case (int)CommonEnums.LightType.Yellow: case (int)LightType.Yellow:
Connection.Text = "等待登录账号"; Connection.Text = "等待登录账号";
this.Light.Image = Properties.Resources.yellow; this.Light.Image = Properties.Resources.yellow;
break; break;
case (int)CommonEnums.LightType.Red: case (int)LightType.Red:
default: default:
Connection.Text = "服务器连接失败"; Connection.Text = "服务器连接失败";
this.Light.Image = Properties.Resources.red; this.Light.Image = Properties.Resources.red;
@ -972,7 +996,11 @@ namespace FunGame.Desktop.UI
/// <param name="e"></param> /// <param name="e"></param>
private void Logout_Click(object sender, EventArgs e) private void Logout_Click(object sender, EventArgs e)
{ {
LogoutAccount(); if (ShowMessage.OKCancelMessage("你确定要退出登录吗?", "退出登录") == MessageResult.OK)
{
if (WebHelper == null || !WebHelper.WebHelpMethod((int)WebHelperMethod.Logout))
ShowMessage.WarningMessage("请求无效:退出登录失败!");
}
} }
/// <summary> /// <summary>
@ -983,7 +1011,7 @@ namespace FunGame.Desktop.UI
private void Login_Click(object sender, EventArgs e) private void Login_Click(object sender, EventArgs e)
{ {
if (WebHelper != null) if (WebHelper != null)
WebHelper.WebHelpMethod((int)CommonEnums.WebHelperMethod.Login); WebHelper.WebHelpMethod((int)WebHelperMethod.Login);
else else
ShowMessage.WarningMessage("请先连接服务器!"); ShowMessage.WarningMessage("请先连接服务器!");
} }

View File

@ -38,23 +38,29 @@ namespace FunGame.Desktop.Utils
{ {
switch (i) switch (i)
{ {
case (int)CommonEnums.WebHelperMethod.CreateSocket: case (int)WebHelperMethod.CreateSocket:
CreateSocket(); CreateSocket();
break; break;
case (int)CommonEnums.WebHelperMethod.CloseSocket: case (int)WebHelperMethod.CloseSocket:
Close(); Close();
break; break;
case (int)CommonEnums.WebHelperMethod.StartWebHelper: case (int)WebHelperMethod.StartWebHelper:
StartWebHelper(); StartWebHelper();
break; break;
case (int)CommonEnums.WebHelperMethod.Login: case (int)WebHelperMethod.Login:
if (client != null) if (client != null)
{ {
Send((int)CommonEnums.SocketType.CheckLogin, new object[] { Main, client, new User("Mili") }); Send((int)SocketMessageType.CheckLogin, new object[] { Main, client, new User("Mili") });
return true; return true;
} }
else return false;
return false; case (int)WebHelperMethod.Logout:
if (client != null && Usercfg.LoginUser != null)
{
Send((int)SocketMessageType.Logout, new object[] { Main, client, Usercfg.LoginUser });
return true;
}
return false;
} }
return true; return true;
} }
@ -88,7 +94,7 @@ namespace FunGame.Desktop.Utils
objs = new object[] { main, socket, obj }; objs = new object[] { main, socket, obj };
else else
objs = new object[] { main, socket }; objs = new object[] { main, socket };
if (Send((int)CommonEnums.SocketType.GetNotice, objs)) // 接触服务器并获取公告 if (Send((int)SocketMessageType.GetNotice, objs)) // 接触服务器并获取公告
main.GetMessage(this, " >> 连接服务器成功请登录账号以体验FunGame。", true); main.GetMessage(this, " >> 连接服务器成功请登录账号以体验FunGame。", true);
}; };
Task t = Task.Factory.StartNew(() => Task t = Task.Factory.StartNew(() =>
@ -147,23 +153,27 @@ namespace FunGame.Desktop.Utils
{ {
string msg = Config.DEFAULT_ENCODING.GetString(buffer, 0, length); string msg = Config.DEFAULT_ENCODING.GetString(buffer, 0, length);
int type = GetType(msg); int type = GetType(msg);
string typestring = CommonEnums.GetSocketTypeName(type); string typestring = EnumHelper.GetSocketTypeName(type);
string read = GetMessage(msg); string read = GetMessage(msg);
switch (type) switch (type)
{ {
case (int)CommonEnums.SocketType.GetNotice: case (int)SocketMessageType.GetNotice:
main.GetMessage(this, read, true); main.GetMessage(this, read, true);
return true; return true;
case (int)CommonEnums.SocketType.Login: case (int)SocketMessageType.Login:
break; break;
case (int)CommonEnums.SocketType.CheckLogin: case (int)SocketMessageType.CheckLogin:
Main.GetMessage(this, Config.WebHelper_SetUser, false, objs); Main.GetMessage(this, Config.WebHelper_SetUser, false, objs);
Main.GetMessage(this, read, true); Main.GetMessage(this, read, true);
StartWebHelper(); // 开始创建TCP流 StartWebHelper(); // 开始创建TCP流
return true; return true;
case (int)CommonEnums.SocketType.Logout: case (int)SocketMessageType.Logout:
break; Main.GetMessage(this, Config.WebHelper_SetUser, false, objs);
case (int)CommonEnums.SocketType.HeartBeat: Main.GetMessage(this, read, true);
Main.GetMessage(this, Config.WebHelper_LogOut);
Close();
return true;
case (int)SocketMessageType.HeartBeat:
if (WaitHeartBeat != null && !WaitHeartBeat.IsCompleted) WaitHeartBeat.Wait(1); if (WaitHeartBeat != null && !WaitHeartBeat.IsCompleted) WaitHeartBeat.Wait(1);
Config.WebHelper_HeartBeatFaileds = 0; Config.WebHelper_HeartBeatFaileds = 0;
main.GetMessage(this, Config.WebHelper_SetGreenAndPing); main.GetMessage(this, Config.WebHelper_SetGreenAndPing);
@ -209,11 +219,11 @@ namespace FunGame.Desktop.Utils
if (socket != null) if (socket != null)
{ {
string msg = ""; string msg = "";
CommonEnums.SocketType type = (CommonEnums.SocketType)i; SocketMessageType type = (SocketMessageType)i;
// 发送消息给服务器端 // 发送消息给服务器端
switch (type) switch (type)
{ {
case CommonEnums.SocketType.GetNotice: case SocketMessageType.GetNotice:
msg = MakeMessage(type, "获取公告"); msg = MakeMessage(type, "获取公告");
if (Send(msg, socket) > 0) if (Send(msg, socket) > 0)
{ {
@ -221,9 +231,9 @@ namespace FunGame.Desktop.Utils
} }
else else
throw new Exception("ERROR消息未送达服务器与服务器连接可能丢失。"); throw new Exception("ERROR消息未送达服务器与服务器连接可能丢失。");
case CommonEnums.SocketType.Login: case SocketMessageType.Login:
break; break;
case CommonEnums.SocketType.CheckLogin: case SocketMessageType.CheckLogin:
User user; User user;
if (objs != null && objs.Length > 2) if (objs != null && objs.Length > 2)
{ {
@ -236,9 +246,16 @@ namespace FunGame.Desktop.Utils
throw new Exception("ERROR: 请登录账号。"); throw new Exception("ERROR: 请登录账号。");
} }
break; break;
case CommonEnums.SocketType.Logout: case SocketMessageType.Logout:
break; if (objs != null && objs.Length > 2)
case CommonEnums.SocketType.HeartBeat: {
user = (User)objs[2];
msg = MakeMessage(type, user.Userame);
if (Send(msg, socket) > 0)
return true;
}
return false;
case SocketMessageType.HeartBeat:
msg = MakeMessage(type, "心跳检测"); msg = MakeMessage(type, "心跳检测");
if (Send(msg, socket) > 0) if (Send(msg, socket) > 0)
{ {
@ -321,7 +338,7 @@ namespace FunGame.Desktop.Utils
return msg[index..]; return msg[index..];
} }
private string MakeMessage(CommonEnums.SocketType type, string msg) private string MakeMessage(SocketMessageType type, string msg)
{ {
return (int)type + ";" + msg; return (int)type + ";" + msg;
} }
@ -357,7 +374,7 @@ namespace FunGame.Desktop.Utils
Main.GetMessage(this, "Creating: SendHeartBeatStream...OK"); Main.GetMessage(this, "Creating: SendHeartBeatStream...OK");
while (IsConnected()) while (IsConnected())
{ {
Send((int)CommonEnums.SocketType.HeartBeat); // 发送心跳包 Send((int)SocketMessageType.HeartBeat); // 发送心跳包
Thread.Sleep(20000); Thread.Sleep(20000);
} }
} }