forked from project-redbud/FunGame-Core
调整结构,更新Socket和反射类
This commit is contained in:
parent
0b5cbc160b
commit
35a060f693
@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@ -8,7 +9,7 @@ namespace FunGame.Core.Api.Model.Enum
|
||||
{
|
||||
/// <summary>
|
||||
/// 这里存放框架实现相关的State Type Result Method
|
||||
/// 添加FunGame.Core.Api接口和实现时,需要在这里同步添加:InterfaceType和InterfaceMethod
|
||||
/// 添加FunGame.Core.Api接口和实现时,需要在这里同步添加:InterfaceType、InterfaceMethod
|
||||
/// </summary>
|
||||
public static class CommonEnums
|
||||
{
|
||||
@ -63,6 +64,23 @@ namespace FunGame.Core.Api.Model.Enum
|
||||
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
|
||||
|
||||
#region Result
|
||||
@ -96,5 +114,56 @@ namespace FunGame.Core.Api.Model.Enum
|
||||
|
||||
#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
|
||||
}
|
||||
}
|
||||
|
||||
50
FunGame.Core.Api/Model/Enum/FunGameEnums.cs
Normal file
50
FunGame.Core.Api/Model/Enum/FunGameEnums.cs
Normal 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 : "");
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新日志
|
||||
*
|
||||
*
|
||||
*/
|
||||
}
|
||||
}
|
||||
@ -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";
|
||||
}
|
||||
}
|
||||
@ -12,57 +12,10 @@ namespace FunGame.Core.Api.Util
|
||||
{
|
||||
/// <summary>
|
||||
/// 在FunGame.Core.Api中添加新接口和新实现时,需要:
|
||||
/// 1、在这里定义类名和方法名
|
||||
/// 2、在FunGame.Core.Api.Model.Enum.CommonEnums里同步添加InterfaceType和InterfaceMethod
|
||||
/// 3、在GetClassName(int)、GetMethodName(int)中添加switch分支
|
||||
/// 在FunGame.Core.Api.Model.Enum.CommonEnums里同步添加InterfaceType、InterfaceMethod
|
||||
/// </summary>
|
||||
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
|
||||
*/
|
||||
@ -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 PluginDocPath = System.Environment.CurrentDirectory.ToString() + "\\plugins\\"; // 插件目录
|
||||
|
||||
@ -95,7 +47,7 @@ namespace FunGame.Core.Api.Util
|
||||
private Type? GetFunGameCoreImplement(int Interface)
|
||||
{
|
||||
// 通过类名获取获取命名空间+类名称
|
||||
string ClassName = GetClassName(Interface);
|
||||
string ClassName = CommonEnums.GetImplementClassName(Interface);
|
||||
List<Type>? Classes = null;
|
||||
if (Assembly != null)
|
||||
{
|
||||
@ -120,7 +72,7 @@ namespace FunGame.Core.Api.Util
|
||||
{
|
||||
Assembly = Assembly.LoadFile(EXEDocPath + @FUNGAME_CORE + ".dll");
|
||||
Type = GetFunGameCoreImplement(Interface); // 通过类名获取获取命名空间+类名称
|
||||
string MethodName = GetMethodName(Method); // 获取方法名
|
||||
string MethodName = CommonEnums.GetImplementMethodName(Method); // 获取方法名
|
||||
if (Assembly != null && Type != null) this.Method = Type.GetMethod(MethodName); // 从Type中查找方法名
|
||||
else return null;
|
||||
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 |
BIN
FunGame.Desktop/Images/yellow.png
Normal file
BIN
FunGame.Desktop/Images/yellow.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.8 KiB |
@ -27,6 +27,7 @@ namespace FunGame.Desktop.Models.Config
|
||||
public const string WebHelper_SetGreen = "-WebHelper .set green";
|
||||
public const string WebHelper_SetGreenAndPing = "-WebHelper .set greenandping";
|
||||
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_GetUser = "-WebHelper .get user";
|
||||
public static int WebHelper_HeartBeatFaileds = 0;
|
||||
|
||||
10
FunGame.Desktop/Properties/Resources.Designer.cs
generated
10
FunGame.Desktop/Properties/Resources.Designer.cs
generated
@ -139,5 +139,15 @@ namespace FunGame.Desktop.Properties {
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -133,13 +133,16 @@
|
||||
<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>
|
||||
</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">
|
||||
<value>..\Images\favicon.ico;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
|
||||
</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">
|
||||
<value>..\Images\logo.ico;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
|
||||
</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>
|
||||
6
FunGame.Desktop/UI/Main/Main.Designer.cs
generated
6
FunGame.Desktop/UI/Main/Main.Designer.cs
generated
@ -145,7 +145,7 @@ namespace FunGame.Desktop.UI
|
||||
this.Light.Image = global::FunGame.Desktop.Properties.Resources.red;
|
||||
this.Light.Location = new System.Drawing.Point(777, 426);
|
||||
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;
|
||||
//
|
||||
// SendTalkText
|
||||
@ -407,7 +407,7 @@ namespace FunGame.Desktop.UI
|
||||
this.NoticeText.ReadOnly = true;
|
||||
this.NoticeText.Size = new System.Drawing.Size(174, 86);
|
||||
this.NoticeText.TabIndex = 0;
|
||||
this.NoticeText.Text = "这里展示公告";
|
||||
this.NoticeText.Text = "";
|
||||
//
|
||||
// InfoBox
|
||||
//
|
||||
@ -452,7 +452,7 @@ namespace FunGame.Desktop.UI
|
||||
this.GameInfo.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.Vertical;
|
||||
this.GameInfo.Size = new System.Drawing.Size(452, 331);
|
||||
this.GameInfo.TabIndex = 1;
|
||||
this.GameInfo.Text = "这里展示游戏信息,包括系统通知和游戏反馈";
|
||||
this.GameInfo.Text = "";
|
||||
//
|
||||
// QuitRoom
|
||||
//
|
||||
|
||||
@ -8,6 +8,7 @@ using System.Text;
|
||||
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
|
||||
@ -53,8 +54,7 @@ namespace FunGame.Desktop.UI
|
||||
{
|
||||
this.PresetText.SelectedIndex = 0; // 快捷消息初始选择
|
||||
SetRoomid("-1"); // 房间号初始化
|
||||
WritelnGameInfo(); // 初始化消息队列
|
||||
SetLoginUser(new object[] { new User("Mili") }); // Debug,初始化玩家名字
|
||||
ShowFunGameInfo(); // 显示FunGame信息
|
||||
GetServerConnection(); // 开始连接服务器
|
||||
}
|
||||
|
||||
@ -81,7 +81,7 @@ namespace FunGame.Desktop.UI
|
||||
Usercfg.FunGame_isRetrying = false;
|
||||
WebHelper_Action = (main) =>
|
||||
{
|
||||
SetServerStatusLight(true);
|
||||
SetServerStatusLight((int)CommonEnums.LightType.Green);
|
||||
};
|
||||
if (InvokeRequired)
|
||||
BeginInvoke(WebHelper_Action, this);
|
||||
@ -94,7 +94,20 @@ namespace FunGame.Desktop.UI
|
||||
Usercfg.FunGame_isRetrying = false;
|
||||
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)
|
||||
BeginInvoke(WebHelper_Action, this);
|
||||
@ -106,7 +119,7 @@ namespace FunGame.Desktop.UI
|
||||
case Config.WebHelper_SetRed:
|
||||
WebHelper_Action = (main) =>
|
||||
{
|
||||
SetServerStatusLight(false);
|
||||
SetServerStatusLight((int)CommonEnums.LightType.Red);
|
||||
};
|
||||
if (InvokeRequired)
|
||||
BeginInvoke(WebHelper_Action, this);
|
||||
@ -118,7 +131,7 @@ namespace FunGame.Desktop.UI
|
||||
Usercfg.FunGame_isRetrying = false;
|
||||
WebHelper_Action = (main) =>
|
||||
{
|
||||
SetServerStatusLight(false);
|
||||
SetServerStatusLight((int)CommonEnums.LightType.Red);
|
||||
};
|
||||
if (InvokeRequired)
|
||||
BeginInvoke(WebHelper_Action, this);
|
||||
@ -147,7 +160,10 @@ namespace FunGame.Desktop.UI
|
||||
return LoginUser;
|
||||
return null;
|
||||
default:
|
||||
WritelnGameInfo(webHelper, msg);
|
||||
if (needTime)
|
||||
WritelnGameInfo(webHelper, GetNowShortTime() + msg);
|
||||
else
|
||||
WritelnGameInfo(webHelper, msg);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@ -188,7 +204,7 @@ namespace FunGame.Desktop.UI
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WritelnGameInfo(">> 查找可用的服务器失败,请重启FunGame!\n" + e.StackTrace);
|
||||
WritelnGameInfo(">> 查找可用的服务器失败,请重启FunGame。\n" + e.StackTrace);
|
||||
ShowMessage.ErrorMessage("查找可用的服务器失败!");
|
||||
}
|
||||
}
|
||||
@ -683,30 +699,46 @@ namespace FunGame.Desktop.UI
|
||||
/// <summary>
|
||||
/// 设置服务器连接状态指示灯
|
||||
/// </summary>
|
||||
/// <param name="green"></param>
|
||||
/// <param name="light"></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)
|
||||
{
|
||||
Connection.Text = "心跳延迟 " + ping + "ms";
|
||||
this.Light.Image = Properties.Resources.green;
|
||||
}
|
||||
else
|
||||
{
|
||||
case (int)CommonEnums.LightType.Green:
|
||||
Connection.Text = "服务器连接成功";
|
||||
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 = "服务器连接失败";
|
||||
this.Light.Image = Properties.Resources.red;
|
||||
Connection.Text = "心跳延迟 " + ping + "ms";
|
||||
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
|
||||
|
||||
#region 事件
|
||||
|
||||
@ -66,7 +66,7 @@ namespace FunGame.Desktop.Utils
|
||||
client.Connect(server);
|
||||
if (IsConnected())
|
||||
{
|
||||
Main.GetMessage(this, Config.WebHelper_SetGreen);
|
||||
Main.GetMessage(this, Config.WebHelper_SetYellow);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -79,12 +79,8 @@ namespace FunGame.Desktop.Utils
|
||||
objs = new object[] { main, socket, obj };
|
||||
else
|
||||
objs = new object[] { main, socket };
|
||||
Task.Factory.StartNew(() =>
|
||||
{
|
||||
|
||||
});
|
||||
if (Send((int)SocketEnums.Type.CheckLogin, objs)) // 确认连接的玩家
|
||||
StartWebHelper(); // 开始创建TCP流
|
||||
if (Send((int)CommonEnums.SocketType.GetNotice, objs)) // 接触服务器并获取公告
|
||||
main.GetMessage(this, " >> 连接服务器成功,请登录账号以体验FunGame。", true);
|
||||
};
|
||||
Task t = Task.Factory.StartNew(() =>
|
||||
{
|
||||
@ -142,25 +138,28 @@ namespace FunGame.Desktop.Utils
|
||||
{
|
||||
string msg = Config.DEFAULT_ENCODING.GetString(buffer, 0, length);
|
||||
int type = GetType(msg);
|
||||
string typestring = GetTypeString(type);
|
||||
string typestring = CommonEnums.GetSocketTypeName(type);
|
||||
string read = GetMessage(msg);
|
||||
main.GetMessage(this, read);
|
||||
switch (type)
|
||||
{
|
||||
case (int)SocketEnums.Type.GetNotice:
|
||||
break;
|
||||
case (int)SocketEnums.Type.Login:
|
||||
break;
|
||||
case (int)SocketEnums.Type.CheckLogin:
|
||||
case (int)CommonEnums.SocketType.GetNotice:
|
||||
main.GetMessage(this, read, true);
|
||||
return true;
|
||||
case (int)SocketEnums.Type.Logout:
|
||||
case (int)CommonEnums.SocketType.Login:
|
||||
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);
|
||||
Config.WebHelper_HeartBeatFaileds = 0;
|
||||
main.GetMessage(this, Config.WebHelper_SetGreenAndPing);
|
||||
return true;
|
||||
}
|
||||
main.GetMessage(this, read);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
throw new Exception("ERROR:未收到任何来自服务器的信息,与服务器连接可能丢失。");
|
||||
@ -198,16 +197,26 @@ namespace FunGame.Desktop.Utils
|
||||
}
|
||||
if (socket != null)
|
||||
{
|
||||
string msg = "";
|
||||
byte[] buffer;
|
||||
int length;
|
||||
// 发送消息给服务器端
|
||||
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;
|
||||
case (int)SocketEnums.Type.Login:
|
||||
case (int)CommonEnums.SocketType.Login:
|
||||
break;
|
||||
case (int)SocketEnums.Type.CheckLogin:
|
||||
case (int)CommonEnums.SocketType.CheckLogin:
|
||||
User user;
|
||||
string msg;
|
||||
if (objs != null && objs.Length > 2)
|
||||
{
|
||||
user = (User)objs[2];
|
||||
@ -216,22 +225,22 @@ namespace FunGame.Desktop.Utils
|
||||
else
|
||||
{
|
||||
Usercfg.FunGame_isAutoRetry = false;
|
||||
throw new Exception("SUCCESS:服务器连接成功,检测到未登录账号已自动断开,请登录后手动发起服务器连接。");
|
||||
throw new Exception("ERROR: 请登录账号。");
|
||||
}
|
||||
byte[] buffer = new byte[2048];
|
||||
buffer = Config.DEFAULT_ENCODING.GetBytes(MakeMessage((int)SocketEnums.Type.CheckLogin, msg));
|
||||
int l = socket.Send(buffer);
|
||||
if (l > 0)
|
||||
buffer = new byte[2048];
|
||||
buffer = Config.DEFAULT_ENCODING.GetBytes(MakeMessage((int)CommonEnums.SocketType.CheckLogin, msg));
|
||||
length = socket.Send(buffer);
|
||||
if (length > 0)
|
||||
{
|
||||
return Read(objs);
|
||||
}
|
||||
else
|
||||
throw new Exception("ERROR:消息未送达服务器,与服务器连接可能丢失。");
|
||||
case (int)SocketEnums.Type.Logout:
|
||||
case (int)CommonEnums.SocketType.Logout:
|
||||
break;
|
||||
case (int)SocketEnums.Type.HeartBeat:
|
||||
case (int)CommonEnums.SocketType.HeartBeat:
|
||||
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)
|
||||
{
|
||||
WaitHeartBeat = Task.Run(() =>
|
||||
@ -292,25 +301,6 @@ namespace FunGame.Desktop.Utils
|
||||
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)
|
||||
{
|
||||
int index = msg.IndexOf(';') + 1;
|
||||
@ -353,7 +343,7 @@ namespace FunGame.Desktop.Utils
|
||||
Main.GetMessage(this, "Creating: SendHeartBeatStream...OK");
|
||||
while (IsConnected())
|
||||
{
|
||||
Send((int)SocketEnums.Type.HeartBeat); // 发送心跳包
|
||||
Send((int)CommonEnums.SocketType.HeartBeat); // 发送心跳包
|
||||
Thread.Sleep(20000);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user