结构调整,实现退出登录

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

View File

@ -8,15 +8,6 @@ namespace FunGame.Core.Api.Model.Enum
{
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
{
FunGame_Core,
@ -26,6 +17,15 @@ namespace FunGame.Core.Api.Model.Enum
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)
{
string type = FunGameType switch
@ -37,10 +37,7 @@ namespace FunGame.Core.Api.Model.Enum
FunGame.FunGame_Server => FunGame_Server,
_ => ""
};
if (type.Equals(FunGame_Desktop))
return type + " [ 版本: " + FunGame_Version + FunGame_VersionPatch + " ]\n©2022 Mili.cyou. 保留所有权利\n";
else
return type + " [ 版本: " + FunGame_Version + FunGame_VersionPatch + " ]\n(C)2022 Mili.cyou. 保留所有权利\n";
return type + " [ 版本: " + FunGame_Version + FunGame_VersionPatch + " ]\n" + (type.Equals(FunGame_Desktop) ? "©" : "(C)") + "2022 Mili.cyou. 保留所有权利\n";
}
/**

View File

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

View File

@ -8,7 +8,7 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
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
{

View File

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

View File

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