调整结构,更新Socket和反射类

This commit is contained in:
Mili 2022-09-07 00:23:22 +08:00
parent 0b5cbc160b
commit 35a060f693
13 changed files with 235 additions and 155 deletions

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Security.Cryptography;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -8,7 +9,7 @@ namespace FunGame.Core.Api.Model.Enum
{ {
/// <summary> /// <summary>
/// 这里存放框架实现相关的State Type Result Method /// 这里存放框架实现相关的State Type Result Method
/// 添加FunGame.Core.Api接口和实现时需要在这里同步添加InterfaceTypeInterfaceMethod /// 添加FunGame.Core.Api接口和实现时需要在这里同步添加InterfaceTypeInterfaceMethod
/// </summary> /// </summary>
public static class CommonEnums public static class CommonEnums
{ {
@ -63,6 +64,23 @@ namespace FunGame.Core.Api.Model.Enum
ServerInterface = 2 ServerInterface = 2
} }
public enum LightType
{
Green = 1,
Yellow = 2,
Red = 3
}
public enum SocketType
{
Unknown = 0,
GetNotice = 1,
Login = 2,
CheckLogin = 3,
Logout = 4,
HeartBeat = 5
}
#endregion #endregion
#region Result #region Result
@ -96,5 +114,56 @@ namespace FunGame.Core.Api.Model.Enum
#endregion #endregion
#region
/// <summary>
/// 获取实现类类名
/// </summary>
/// <param name="Interface">接口代号</param>
/// <returns></returns>
public static string GetImplementClassName(int Interface)
{
foreach (string str in System.Enum.GetNames(typeof(InterfaceType)))
{
InterfaceType temp = (InterfaceType)System.Enum.Parse(typeof(InterfaceType), Interface.ToString(), true);
if (temp.ToString() == str)
return temp + "Impl";
}
return "";
}
/// <summary>
/// 获取实现类的方法名
/// </summary>
/// <param name="Method">方法代号</param>
/// <returns></returns>
public static string GetImplementMethodName(int Method)
{
foreach (string str in System.Enum.GetNames(typeof(InterfaceMethod)))
{
InterfaceMethod temp = (InterfaceMethod)System.Enum.Parse(typeof(InterfaceMethod), Method.ToString(), true);
if (temp.ToString() == str)
return temp.ToString();
}
return "";
}
/// <summary>
/// 获取Socket枚举名
/// </summary>
/// <param name="SocketType">Socket枚举</param>
/// <returns></returns>
public static string GetSocketTypeName(int SocketType)
{
foreach (string str in System.Enum.GetNames(typeof(SocketType)))
{
SocketType temp = (SocketType)System.Enum.Parse(typeof(SocketType), SocketType.ToString(), true);
if (temp.ToString() == str)
return temp.ToString();
}
return "";
}
#endregion
} }
} }

View File

@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FunGame.Core.Api.Model.Enum
{
/// <summary>
/// 用于记录版本号和更新日志
/// </summary>
public static class FunGameEnums
{
public const string FunGame_Core = "FunGame Core";
public const string FunGame_Core_Api = "FunGame Core Api";
public const string FunGame_Console = "FunGame Console";
public const string FunGame_Desktop = "FunGame Desktop";
public const string FunGame_Server = "FunGame Server";
public const int FirstVersion = 1;
public const int SecondVersion = 0;
public const int ThirdVersion = 0;
public enum Patch
{
Latest = 20221001,
Patch20220906 = 20220906
}
public enum History
{
Latest = 20221001,
R20220906 = 20220906
}
public static string GetVersion()
{
return "=/=\\=/=\\=/=\\=/= > FunGame版本信息 < =\\=/=\\=/=\\=/=\\=" + "\n" +
FunGame_Core + " -> v" + FirstVersion + "." + SecondVersion + ((int)Patch.Latest == (int)History.Latest ? " Patch" + (int)Patch.Latest : "") + "\n" +
FunGame_Core_Api + " -> v" + FirstVersion + "." + SecondVersion + ((int)Patch.Latest == (int)History.Latest ? " Patch" + (int)Patch.Latest : "") + "\n" +
FunGame_Desktop + " -> v" + FirstVersion + "." + SecondVersion + ((int)Patch.Latest == (int)History.Latest ? " Patch" + (int)Patch.Latest : "");
}
/**
*
*
*
*/
}
}

View File

@ -1,27 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FunGame.Core.Api.Model.Enum
{
public static class SocketEnums
{
public enum Type
{
GetNotice = 1,
Login = 2,
CheckLogin = 3,
Logout = 4,
HeartBeat = 5
}
public const string TYPE_UNKNOWN = "Unknown Type";
public const string TYPE_GetNotice = "GetNotice";
public const string TYPE_Login = "Login";
public const string TYPE_CheckLogin = "CheckLogin";
public const string TYPE_Logout = "Logout";
public const string TYPE_HeartBeat = "HeartBeat";
}
}

View File

@ -12,57 +12,10 @@ namespace FunGame.Core.Api.Util
{ {
/// <summary> /// <summary>
/// 在FunGame.Core.Api中添加新接口和新实现时需要 /// 在FunGame.Core.Api中添加新接口和新实现时需要
/// 1、在这里定义类名和方法名 /// 在FunGame.Core.Api.Model.Enum.CommonEnums里同步添加InterfaceType、InterfaceMethod
/// 2、在FunGame.Core.Api.Model.Enum.CommonEnums里同步添加InterfaceType和InterfaceMethod
/// 3、在GetClassName(int)、GetMethodName(int)中添加switch分支
/// </summary> /// </summary>
public class AssemblyHelper public class AssemblyHelper
{ {
/**
*
*/
public const string ClientConnectInterface = "ClientConnectInterface";
public const string ServerInterface = "ServerInterface";
/**
*
*/
public const string RemoteServerIP = "RemoteServerIP";
public const string DBConnection = "DBConnection";
public const string GetServerSettings = "GetServerSettings";
/// <summary>
/// 获取实现类类名xxInterfaceImpl
/// </summary>
/// <param name="Interface">接口代号</param>
/// <returns></returns>
private string GetClassName(int Interface)
{
return Interface switch
{
(int)CommonEnums.InterfaceType.ClientConnectInterface => ClientConnectInterface + Implement,
(int)CommonEnums.InterfaceType.ServerInterface => ServerInterface + Implement,
_ => "",
};
}
/// <summary>
/// 获取方法名
/// </summary>
/// <param name="Method">方法代号</param>
/// <returns></returns>
private string GetMethodName(int Method)
{
// 通过AssemblyHelperType来获取方法名
return Method switch
{
(int)CommonEnums.InterfaceMethod.RemoteServerIP => RemoteServerIP,
(int)CommonEnums.InterfaceMethod.DBConnection => DBConnection,
(int)CommonEnums.InterfaceMethod.GetServerSettings => GetServerSettings,
_ => "",
};
}
/** /**
* DLL * DLL
*/ */
@ -71,7 +24,6 @@ namespace FunGame.Core.Api.Util
/** /**
* *
*/ */
public const string Implement = "Impl"; // 实现类的后缀
public static string EXEDocPath = System.Environment.CurrentDirectory.ToString() + "\\"; // 程序目录 public static string EXEDocPath = System.Environment.CurrentDirectory.ToString() + "\\"; // 程序目录
public static string PluginDocPath = System.Environment.CurrentDirectory.ToString() + "\\plugins\\"; // 插件目录 public static string PluginDocPath = System.Environment.CurrentDirectory.ToString() + "\\plugins\\"; // 插件目录
@ -95,7 +47,7 @@ namespace FunGame.Core.Api.Util
private Type? GetFunGameCoreImplement(int Interface) private Type? GetFunGameCoreImplement(int Interface)
{ {
// 通过类名获取获取命名空间+类名称 // 通过类名获取获取命名空间+类名称
string ClassName = GetClassName(Interface); string ClassName = CommonEnums.GetImplementClassName(Interface);
List<Type>? Classes = null; List<Type>? Classes = null;
if (Assembly != null) if (Assembly != null)
{ {
@ -120,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 = GetMethodName(Method); // 获取方法名 string MethodName = CommonEnums.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);

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.7 KiB

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.7 KiB

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

View File

@ -27,6 +27,7 @@ namespace FunGame.Desktop.Models.Config
public const string WebHelper_SetGreen = "-WebHelper .set green"; public const string WebHelper_SetGreen = "-WebHelper .set green";
public const string WebHelper_SetGreenAndPing = "-WebHelper .set greenandping"; public const string WebHelper_SetGreenAndPing = "-WebHelper .set greenandping";
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_Disconnected = "-WebHelper .disconnected"; public const string WebHelper_Disconnected = "-WebHelper .disconnected";
public const string WebHelper_GetUser = "-WebHelper .get user"; public const string WebHelper_GetUser = "-WebHelper .get user";
public static int WebHelper_HeartBeatFaileds = 0; public static int WebHelper_HeartBeatFaileds = 0;

View File

@ -139,5 +139,15 @@ namespace FunGame.Desktop.Properties {
return ((System.Drawing.Bitmap)(obj)); return ((System.Drawing.Bitmap)(obj));
} }
} }
/// <summary>
/// 查找 System.Drawing.Bitmap 类型的本地化资源。
/// </summary>
internal static System.Drawing.Bitmap yellow {
get {
object obj = ResourceManager.GetObject("yellow", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
}
}
} }
} }

View File

@ -133,13 +133,16 @@
<data name="back" type="System.Resources.ResXFileRef, System.Windows.Forms"> <data name="back" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\images\back.jpg;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value> <value>..\images\back.jpg;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data> </data>
<data name="min" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\images\min.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="favicon" type="System.Resources.ResXFileRef, System.Windows.Forms"> <data name="favicon" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Images\favicon.ico;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value> <value>..\Images\favicon.ico;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data> </data>
<data name="min" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\images\min.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="logo" type="System.Resources.ResXFileRef, System.Windows.Forms"> <data name="logo" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Images\logo.ico;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value> <value>..\Images\logo.ico;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data> </data>
<data name="yellow" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Images\yellow.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
</root> </root>

View File

@ -145,7 +145,7 @@ namespace FunGame.Desktop.UI
this.Light.Image = global::FunGame.Desktop.Properties.Resources.red; this.Light.Image = global::FunGame.Desktop.Properties.Resources.red;
this.Light.Location = new System.Drawing.Point(777, 426); this.Light.Location = new System.Drawing.Point(777, 426);
this.Light.Name = "Light"; this.Light.Name = "Light";
this.Light.Size = new System.Drawing.Size(20, 20); this.Light.Size = new System.Drawing.Size(18, 18);
this.Light.TabIndex = 93; this.Light.TabIndex = 93;
// //
// SendTalkText // SendTalkText
@ -407,7 +407,7 @@ namespace FunGame.Desktop.UI
this.NoticeText.ReadOnly = true; this.NoticeText.ReadOnly = true;
this.NoticeText.Size = new System.Drawing.Size(174, 86); this.NoticeText.Size = new System.Drawing.Size(174, 86);
this.NoticeText.TabIndex = 0; this.NoticeText.TabIndex = 0;
this.NoticeText.Text = "这里展示公告"; this.NoticeText.Text = "";
// //
// InfoBox // InfoBox
// //
@ -452,7 +452,7 @@ namespace FunGame.Desktop.UI
this.GameInfo.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.Vertical; this.GameInfo.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.Vertical;
this.GameInfo.Size = new System.Drawing.Size(452, 331); this.GameInfo.Size = new System.Drawing.Size(452, 331);
this.GameInfo.TabIndex = 1; this.GameInfo.TabIndex = 1;
this.GameInfo.Text = "这里展示游戏信息,包括系统通知和游戏反馈"; this.GameInfo.Text = "";
// //
// QuitRoom // QuitRoom
// //

View File

@ -8,6 +8,7 @@ using System.Text;
using FunGame.Core.Api.Model.Entity; 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 static FunGame.Core.Api.Model.Enum.CommonEnums; using static FunGame.Core.Api.Model.Enum.CommonEnums;
namespace FunGame.Desktop.UI namespace FunGame.Desktop.UI
@ -53,8 +54,7 @@ namespace FunGame.Desktop.UI
{ {
this.PresetText.SelectedIndex = 0; // 快捷消息初始选择 this.PresetText.SelectedIndex = 0; // 快捷消息初始选择
SetRoomid("-1"); // 房间号初始化 SetRoomid("-1"); // 房间号初始化
WritelnGameInfo(); // 初始化消息队列 ShowFunGameInfo(); // 显示FunGame信息
SetLoginUser(new object[] { new User("Mili") }); // Debug初始化玩家名字
GetServerConnection(); // 开始连接服务器 GetServerConnection(); // 开始连接服务器
} }
@ -81,7 +81,7 @@ namespace FunGame.Desktop.UI
Usercfg.FunGame_isRetrying = false; Usercfg.FunGame_isRetrying = false;
WebHelper_Action = (main) => WebHelper_Action = (main) =>
{ {
SetServerStatusLight(true); SetServerStatusLight((int)CommonEnums.LightType.Green);
}; };
if (InvokeRequired) if (InvokeRequired)
BeginInvoke(WebHelper_Action, this); BeginInvoke(WebHelper_Action, this);
@ -94,7 +94,20 @@ namespace FunGame.Desktop.UI
Usercfg.FunGame_isRetrying = false; Usercfg.FunGame_isRetrying = false;
WebHelper_Action = (main) => WebHelper_Action = (main) =>
{ {
SetServerStatusLight(true, GetServerPing(Config.SERVER_IPADRESS)); SetServerStatusLight((int)CommonEnums.LightType.Green, GetServerPing(Config.SERVER_IPADRESS));
};
if (InvokeRequired)
BeginInvoke(WebHelper_Action, this);
else
WebHelper_Action(this);
Usercfg.FunGame_isConnected = true;
NOW_CONNECTEDRETRY = 0;
break;
case Config.WebHelper_SetYellow:
Usercfg.FunGame_isRetrying = false;
WebHelper_Action = (main) =>
{
SetServerStatusLight((int)CommonEnums.LightType.Yellow);
}; };
if (InvokeRequired) if (InvokeRequired)
BeginInvoke(WebHelper_Action, this); BeginInvoke(WebHelper_Action, this);
@ -106,7 +119,7 @@ namespace FunGame.Desktop.UI
case Config.WebHelper_SetRed: case Config.WebHelper_SetRed:
WebHelper_Action = (main) => WebHelper_Action = (main) =>
{ {
SetServerStatusLight(false); SetServerStatusLight((int)CommonEnums.LightType.Red);
}; };
if (InvokeRequired) if (InvokeRequired)
BeginInvoke(WebHelper_Action, this); BeginInvoke(WebHelper_Action, this);
@ -118,7 +131,7 @@ namespace FunGame.Desktop.UI
Usercfg.FunGame_isRetrying = false; Usercfg.FunGame_isRetrying = false;
WebHelper_Action = (main) => WebHelper_Action = (main) =>
{ {
SetServerStatusLight(false); SetServerStatusLight((int)CommonEnums.LightType.Red);
}; };
if (InvokeRequired) if (InvokeRequired)
BeginInvoke(WebHelper_Action, this); BeginInvoke(WebHelper_Action, this);
@ -147,7 +160,10 @@ namespace FunGame.Desktop.UI
return LoginUser; return LoginUser;
return null; return null;
default: default:
WritelnGameInfo(webHelper, msg); if (needTime)
WritelnGameInfo(webHelper, GetNowShortTime() + msg);
else
WritelnGameInfo(webHelper, msg);
return null; return null;
} }
} }
@ -188,7 +204,7 @@ namespace FunGame.Desktop.UI
} }
catch (Exception e) catch (Exception e)
{ {
WritelnGameInfo(">> 查找可用的服务器失败请重启FunGame\n" + e.StackTrace); WritelnGameInfo(">> 查找可用的服务器失败请重启FunGame\n" + e.StackTrace);
ShowMessage.ErrorMessage("查找可用的服务器失败!"); ShowMessage.ErrorMessage("查找可用的服务器失败!");
} }
} }
@ -683,30 +699,46 @@ namespace FunGame.Desktop.UI
/// <summary> /// <summary>
/// 设置服务器连接状态指示灯 /// 设置服务器连接状态指示灯
/// </summary> /// </summary>
/// <param name="green"></param> /// <param name="light"></param>
/// <param name="ping"></param> /// <param name="ping"></param>
private void SetServerStatusLight(bool green, int ping = 0) private void SetServerStatusLight(int light, int ping = 0)
{ {
if (green) switch(light)
{ {
if (ping > 0) case (int)CommonEnums.LightType.Green:
{
Connection.Text = "心跳延迟 " + ping + "ms";
this.Light.Image = Properties.Resources.green;
}
else
{
Connection.Text = "服务器连接成功"; Connection.Text = "服务器连接成功";
this.Light.Image = Properties.Resources.green; this.Light.Image = Properties.Resources.green;
} break;
case (int)CommonEnums.LightType.Yellow:
Connection.Text = "等待登录账号";
this.Light.Image = Properties.Resources.yellow;
break;
case (int)CommonEnums.LightType.Red:
default:
Connection.Text = "服务器连接失败";
this.Light.Image = Properties.Resources.red;
break;
} }
else if (ping > 0)
{ {
Connection.Text = "服务器连接失败"; Connection.Text = "心跳延迟 " + ping + "ms";
this.Light.Image = Properties.Resources.red; if (ping < 100)
this.Light.Image = Properties.Resources.green;
else if (ping >= 100 && ping < 200)
this.Light.Image = Properties.Resources.yellow;
else if (ping >= 200)
this.Light.Image = Properties.Resources.red;
} }
} }
/// <summary>
/// 显示FunGame信息
/// </summary>
private void ShowFunGameInfo()
{
WritelnGameInfo(FunGameEnums.GetVersion());
}
#endregion #endregion
#region #region

View File

@ -66,7 +66,7 @@ namespace FunGame.Desktop.Utils
client.Connect(server); client.Connect(server);
if (IsConnected()) if (IsConnected())
{ {
Main.GetMessage(this, Config.WebHelper_SetGreen); Main.GetMessage(this, Config.WebHelper_SetYellow);
break; break;
} }
} }
@ -79,12 +79,8 @@ 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 };
Task.Factory.StartNew(() => if (Send((int)CommonEnums.SocketType.GetNotice, objs)) // 接触服务器并获取公告
{ main.GetMessage(this, " >> 连接服务器成功请登录账号以体验FunGame。", true);
});
if (Send((int)SocketEnums.Type.CheckLogin, objs)) // 确认连接的玩家
StartWebHelper(); // 开始创建TCP流
}; };
Task t = Task.Factory.StartNew(() => Task t = Task.Factory.StartNew(() =>
{ {
@ -142,25 +138,28 @@ 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 = GetTypeString(type); string typestring = CommonEnums.GetSocketTypeName(type);
string read = GetMessage(msg); string read = GetMessage(msg);
main.GetMessage(this, read);
switch (type) switch (type)
{ {
case (int)SocketEnums.Type.GetNotice: case (int)CommonEnums.SocketType.GetNotice:
break; main.GetMessage(this, read, true);
case (int)SocketEnums.Type.Login:
break;
case (int)SocketEnums.Type.CheckLogin:
return true; return true;
case (int)SocketEnums.Type.Logout: case (int)CommonEnums.SocketType.Login:
break; break;
case (int)SocketEnums.Type.HeartBeat: case (int)CommonEnums.SocketType.CheckLogin:
StartWebHelper(); // 开始创建TCP流
return true;
case (int)CommonEnums.SocketType.Logout:
break;
case (int)CommonEnums.SocketType.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);
return true; return true;
} }
main.GetMessage(this, read);
return true;
} }
else else
throw new Exception("ERROR未收到任何来自服务器的信息与服务器连接可能丢失。"); throw new Exception("ERROR未收到任何来自服务器的信息与服务器连接可能丢失。");
@ -198,16 +197,26 @@ namespace FunGame.Desktop.Utils
} }
if (socket != null) if (socket != null)
{ {
string msg = "";
byte[] buffer;
int length;
// 发送消息给服务器端 // 发送消息给服务器端
switch (i) switch (i)
{ {
case (int)SocketEnums.Type.GetNotice: case (int)CommonEnums.SocketType.GetNotice:
msg = "获取公告";
buffer = new byte[2048];
buffer = Config.DEFAULT_ENCODING.GetBytes(MakeMessage((int)CommonEnums.SocketType.GetNotice, msg));
length = socket.Send(buffer);
if (length > 0)
{
return Read(objs);
}
break; break;
case (int)SocketEnums.Type.Login: case (int)CommonEnums.SocketType.Login:
break; break;
case (int)SocketEnums.Type.CheckLogin: case (int)CommonEnums.SocketType.CheckLogin:
User user; User user;
string msg;
if (objs != null && objs.Length > 2) if (objs != null && objs.Length > 2)
{ {
user = (User)objs[2]; user = (User)objs[2];
@ -216,22 +225,22 @@ namespace FunGame.Desktop.Utils
else else
{ {
Usercfg.FunGame_isAutoRetry = false; Usercfg.FunGame_isAutoRetry = false;
throw new Exception("SUCCESS服务器连接成功检测到未登录账号已自动断开请登录后手动发起服务器连接。"); throw new Exception("ERROR: 请登录账号。");
} }
byte[] buffer = new byte[2048]; buffer = new byte[2048];
buffer = Config.DEFAULT_ENCODING.GetBytes(MakeMessage((int)SocketEnums.Type.CheckLogin, msg)); buffer = Config.DEFAULT_ENCODING.GetBytes(MakeMessage((int)CommonEnums.SocketType.CheckLogin, msg));
int l = socket.Send(buffer); length = socket.Send(buffer);
if (l > 0) if (length > 0)
{ {
return Read(objs); return Read(objs);
} }
else else
throw new Exception("ERROR消息未送达服务器与服务器连接可能丢失。"); throw new Exception("ERROR消息未送达服务器与服务器连接可能丢失。");
case (int)SocketEnums.Type.Logout: case (int)CommonEnums.SocketType.Logout:
break; break;
case (int)SocketEnums.Type.HeartBeat: case (int)CommonEnums.SocketType.HeartBeat:
buffer = new byte[2048]; buffer = new byte[2048];
buffer = Config.DEFAULT_ENCODING.GetBytes(Convert.ToString(MakeMessage((int)SocketEnums.Type.HeartBeat, "心跳检测"))); buffer = Config.DEFAULT_ENCODING.GetBytes(Convert.ToString(MakeMessage((int)CommonEnums.SocketType.HeartBeat, "心跳检测")));
if (socket.Send(buffer) > 0) if (socket.Send(buffer) > 0)
{ {
WaitHeartBeat = Task.Run(() => WaitHeartBeat = Task.Run(() =>
@ -292,25 +301,6 @@ namespace FunGame.Desktop.Utils
return Convert.ToInt32(msg[..1]); return Convert.ToInt32(msg[..1]);
} }
private string GetTypeString(int type)
{
switch (type)
{
case (int)SocketEnums.Type.GetNotice:
return SocketEnums.TYPE_GetNotice;
case (int)SocketEnums.Type.Login:
return SocketEnums.TYPE_Login;
case (int)SocketEnums.Type.CheckLogin:
return SocketEnums.TYPE_CheckLogin;
case (int)SocketEnums.Type.Logout:
return SocketEnums.TYPE_Logout;
case (int)SocketEnums.Type.HeartBeat:
return SocketEnums.TYPE_HeartBeat;
default:
return SocketEnums.TYPE_UNKNOWN;
}
}
private string GetMessage(string msg) private string GetMessage(string msg)
{ {
int index = msg.IndexOf(';') + 1; int index = msg.IndexOf(';') + 1;
@ -353,7 +343,7 @@ namespace FunGame.Desktop.Utils
Main.GetMessage(this, "Creating: SendHeartBeatStream...OK"); Main.GetMessage(this, "Creating: SendHeartBeatStream...OK");
while (IsConnected()) while (IsConnected())
{ {
Send((int)SocketEnums.Type.HeartBeat); // 发送心跳包 Send((int)CommonEnums.SocketType.HeartBeat); // 发送心跳包
Thread.Sleep(20000); Thread.Sleep(20000);
} }
} }