This commit is contained in:
Mili 2022-12-09 23:16:54 +08:00
parent 0a88d420c2
commit 6471474c59
8 changed files with 34 additions and 17 deletions

View File

@ -12,11 +12,11 @@ namespace Milimoe.FunGame.Core.Interface.Base
{ {
public System.Net.Sockets.Socket Instance { get; } public System.Net.Sockets.Socket Instance { get; }
public int Runtime { get; } public int Runtime { get; }
public string Token { get; }
public string ServerIP { get; } public string ServerIP { get; }
public int ServerPort { get; } public int ServerPort { get; }
public string ServerName { get; } public string ServerName { get; }
public string ServerNotice { get; } public string ServerNotice { get; }
public int HeartBeatFaileds { get; }
public bool Connected public bool Connected
{ {
get get
@ -25,7 +25,6 @@ namespace Milimoe.FunGame.Core.Interface.Base
} }
} }
public bool Receiving { get; } public bool Receiving { get; }
public bool SendingHeartBeat { get; }
public SocketResult Send(SocketMessageType type, params object[] objs); public SocketResult Send(SocketMessageType type, params object[] objs);
public object[] Receive(); public object[] Receive();
public void Close(); public void Close();

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Milimoe.FunGame.Core.Interface.Base
{
public interface ISocketHeartBeat
{
public int HeartBeatFaileds { get; }
public bool SendingHeartBeat { get; }
}
}

View File

@ -13,13 +13,13 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
{ {
public System.Net.Sockets.Socket Instance { get; } public System.Net.Sockets.Socket Instance { get; }
public int Runtime { get; } = (int)SocketRuntimeType.Server; public int Runtime { get; } = (int)SocketRuntimeType.Server;
public string Token { get; } = "";
public string ServerIP { get; } = ""; public string ServerIP { get; } = "";
public int ServerPort { get; } = 0; public int ServerPort { get; } = 0;
public string ServerName { get; } = ""; public string ServerName { get; } = "";
public string ServerNotice { get; } = ""; public string ServerNotice { get; } = "";
public string ClientIP { get; } = ""; public string ClientIP { get; } = "";
public string ClientName { get; private set; } = ""; public string ClientName { get; private set; } = "";
public int HeartBeatFaileds { get; private set; } = 0;
public bool Connected public bool Connected
{ {
get get
@ -28,7 +28,6 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
} }
} }
public bool Receiving { get; private set; } = false; public bool Receiving { get; private set; } = false;
public bool SendingHeartBeat { get; private set; } = false;
private Task? ReceivingTask; private Task? ReceivingTask;
@ -57,7 +56,7 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
{ {
if (Instance != null) if (Instance != null)
{ {
if (SocketManager.Send(Instance, type, objs) == SocketResult.Success) if (SocketManager.Send(Instance, type, Token, objs) == SocketResult.Success)
{ {
return SocketResult.Success; return SocketResult.Success;
} }

View File

@ -12,19 +12,21 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
internal class JsonObject internal class JsonObject
{ {
internal SocketMessageType MessageType { get; } = SocketMessageType.Unknown; internal SocketMessageType MessageType { get; } = SocketMessageType.Unknown;
internal string Token { get; }
internal object[] Parameters { get; } internal object[] Parameters { get; }
internal string JsonString { get; } internal string JsonString { get; }
internal JsonObject(SocketMessageType MessageType, object[] Parameters) internal JsonObject(SocketMessageType MessageType, string Token, object[] Parameters)
{ {
this.MessageType = MessageType; this.MessageType = MessageType;
this.Token = Token;
this.Parameters = Parameters; this.Parameters = Parameters;
this.JsonString = JsonSerializer.Serialize(this); this.JsonString = JsonSerializer.Serialize(this);
} }
internal static string GetString(SocketMessageType MessageType, object[] Parameters) internal static string GetString(SocketMessageType MessageType, string Token, object[] Parameters)
{ {
return new JsonObject(MessageType, Parameters).JsonString; return new JsonObject(MessageType, Token, Parameters).JsonString;
} }
internal static JsonObject? GetObject(string JsonString) internal static JsonObject? GetObject(string JsonString)

View File

@ -14,11 +14,11 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
{ {
public System.Net.Sockets.Socket Instance { get; } public System.Net.Sockets.Socket Instance { get; }
public int Runtime { get; } = (int)SocketRuntimeType.Server; public int Runtime { get; } = (int)SocketRuntimeType.Server;
public string Token { get; } = "";
public string ServerIP { get; } = ""; public string ServerIP { get; } = "";
public int ServerPort { get; } = 0; public int ServerPort { get; } = 0;
public string ServerName { get; } = ""; public string ServerName { get; } = "";
public string ServerNotice { get; } = ""; public string ServerNotice { get; } = "";
public int HeartBeatFaileds { get; private set; } = 0;
public bool Connected public bool Connected
{ {
get get
@ -27,7 +27,6 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
} }
} }
public bool Receiving { get; private set; } = false; public bool Receiving { get; private set; } = false;
public bool SendingHeartBeat { get; private set; } = false;
private readonly ThreadManager PlayerThreads; private readonly ThreadManager PlayerThreads;

View File

@ -5,6 +5,7 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Net.Sockets; using System.Net.Sockets;
using System.Net; using System.Net;
using System.Security.Cryptography;
using Milimoe.FunGame.Core.Interface; using Milimoe.FunGame.Core.Interface;
using Milimoe.FunGame.Core.Service; using Milimoe.FunGame.Core.Service;
using Milimoe.FunGame.Core.Library.Constant; using Milimoe.FunGame.Core.Library.Constant;
@ -12,10 +13,11 @@ using Milimoe.FunGame.Core.Interface.Base;
namespace Milimoe.FunGame.Core.Library.Common.Network namespace Milimoe.FunGame.Core.Library.Common.Network
{ {
public class Socket : ISocket public class Socket : ISocket, ISocketHeartBeat
{ {
public System.Net.Sockets.Socket Instance { get; } public System.Net.Sockets.Socket Instance { get; }
public int Runtime { get; } = (int)SocketRuntimeType.Client; public int Runtime { get; } = (int)SocketRuntimeType.Client;
public string Token { get; set; } = "";
public string ServerIP { get; } = ""; public string ServerIP { get; } = "";
public int ServerPort { get; } = 0; public int ServerPort { get; } = 0;
public string ServerName { get; } = ""; public string ServerName { get; } = "";
@ -54,11 +56,11 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
{ {
if (Instance != null) if (Instance != null)
{ {
if (SocketManager.Send(type, objs) == SocketResult.Success) if (SocketManager.Send(type, Token, objs) == SocketResult.Success)
{ {
return SocketResult.Success; return SocketResult.Success;
} }
else return SocketResult.Fail; return SocketResult.Fail;
} }
return SocketResult.NotSent; return SocketResult.NotSent;
} }

View File

@ -115,11 +115,11 @@ namespace Milimoe.FunGame.Core.Service
/// <param name="type">通信类型</param> /// <param name="type">通信类型</param>
/// <param name="objs">参数</param> /// <param name="objs">参数</param>
/// <returns>通信结果</returns> /// <returns>通信结果</returns>
internal static SocketResult Send(Socket ClientSocket, SocketMessageType type, object[] objs) internal static SocketResult Send(Socket ClientSocket, SocketMessageType type, string token, object[] objs)
{ {
if (ClientSocket != null && objs != null && objs.Length > 0) if (ClientSocket != null && objs != null && objs.Length > 0)
{ {
if (ClientSocket.Send(General.DEFAULT_ENCODING.GetBytes(Library.Common.Network.JsonObject.GetString(type, objs))) > 0) if (ClientSocket.Send(General.DEFAULT_ENCODING.GetBytes(Library.Common.Network.JsonObject.GetString(type, token, objs))) > 0)
{ {
return SocketResult.Success; return SocketResult.Success;
} }
@ -134,7 +134,7 @@ namespace Milimoe.FunGame.Core.Service
/// <param name="type">通信类型</param> /// <param name="type">通信类型</param>
/// <param name="objs">参数</param> /// <param name="objs">参数</param>
/// <returns>通信结果</returns> /// <returns>通信结果</returns>
internal static SocketResult Send(SocketMessageType type, object[] objs) internal static SocketResult Send(SocketMessageType type, string token, object[] objs)
{ {
if (objs is null || objs.Length <= 0) if (objs is null || objs.Length <= 0)
{ {
@ -142,7 +142,7 @@ namespace Milimoe.FunGame.Core.Service
} }
if (Socket != null) if (Socket != null)
{ {
if (Socket.Send(General.DEFAULT_ENCODING.GetBytes(Library.Common.Network.JsonObject.GetString(type, objs))) > 0) if (Socket.Send(General.DEFAULT_ENCODING.GetBytes(Library.Common.Network.JsonObject.GetString(type, token, objs))) > 0)
{ {
return SocketResult.Success; return SocketResult.Success;
} }

View File

@ -279,6 +279,8 @@ namespace Milimoe.FunGame.Desktop.Model
string ServerNotice = strings[1]; string ServerNotice = strings[1];
Config.FunGame_ServerName = ServerName; Config.FunGame_ServerName = ServerName;
Config.FunGame_Notice = ServerNotice; Config.FunGame_Notice = ServerNotice;
if (objs.Length > 1) msg = (string)objs[1];
Socket!!.Token = msg;
Main?.GetMessage($"已连接服务器:{ServerName}。\n\n********** 服务器公告 **********\n\n{ServerNotice}\n\n"); Main?.GetMessage($"已连接服务器:{ServerName}。\n\n********** 服务器公告 **********\n\n{ServerNotice}\n\n");
// 设置等待登录的黄灯 // 设置等待登录的黄灯
Main?.UpdateUI(MainControllerSet.WaitLoginAndSetYellow); Main?.UpdateUI(MainControllerSet.WaitLoginAndSetYellow);