* Fix #15; Start to Rebuild Architecture.

* Fixed #15
This commit is contained in:
milimoe 2023-04-21 01:06:23 +08:00 committed by GitHub
parent ecdb321eb6
commit b08d62d6f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 132 additions and 2 deletions

View File

@ -8,6 +8,7 @@ namespace Milimoe.FunGame.Core.Interface.Base
public void StartReceiving(Task t);
public SocketResult Send(SocketMessageType type, params object[] objs);
public Library.Common.Network.SocketObject Receive();
public Library.Common.Network.SocketObject[] ReceiveArray();
public void BindEvent(Delegate Method, bool Remove = false);
}
}

View File

@ -49,6 +49,18 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
throw new SocketWrongInfoException();
}
}
public SocketObject[] ReceiveArray()
{
try
{
return SocketManager.ReceiveArray(Instance);
}
catch
{
throw new SocketWrongInfoException();
}
}
public SocketResult Send(SocketMessageType type, params object[] objs)
{

View File

@ -38,6 +38,30 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
{
return JsonConvert.DeserializeObject<JsonObject>(JsonString);
}
public static JsonObject[] GetObjects(string JsonString)
{
List<JsonObject> jsons = new();
JsonSerializer serializer = new();
JsonTextReader reader = new(new StringReader(JsonString))
{
SupportMultipleContent = true
};
while (true)
{
if (!reader.Read())
{
break;
}
JsonObject json = serializer.Deserialize<JsonObject>(reader);
jsons.Add(json);
}
return jsons.ToArray();
}
}
}

View File

@ -71,6 +71,27 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
throw new SocketWrongInfoException();
}
}
public SocketObject[] ReceiveArray()
{
try
{
SocketObject[] result = SocketManager.ReceiveArray();
foreach (SocketObject obj in result)
{
if (obj.SocketType == SocketMessageType.HeartBeat)
{
if (WaitHeartBeatReply != null && !WaitHeartBeatReply.IsCompleted) WaitHeartBeatReply.Wait(1);
_HeartBeatFaileds = 0;
}
}
return result;
}
catch
{
throw new SocketWrongInfoException();
}
}
public void BindEvent(Delegate Method, bool Remove = false)
{

7
Model/RunTimeModel.cs Normal file
View File

@ -0,0 +1,7 @@
namespace Milimoe.FunGame.Core.Model
{
public class RunTimeModel
{
}
}

10
Model/SessionModel.cs Normal file
View File

@ -0,0 +1,10 @@
namespace Milimoe.FunGame.Core.Model
{
public class SessionModel
{
public SessionModel()
{
}
}
}

View File

@ -179,7 +179,7 @@ namespace Milimoe.FunGame.Core.Service
}
return result;
}
/// <summary>
/// 用于服务器接收客户端信息
/// </summary>
@ -204,6 +204,61 @@ namespace Milimoe.FunGame.Core.Service
return result;
}
/// <summary>
/// 用于客户端接收服务器信息(数组版)
/// </summary>
/// <returns>SocketObjects</returns>
internal static Library.Common.Network.SocketObject[] ReceiveArray()
{
List<Library.Common.Network.SocketObject> result = new();
if (Socket != null)
{
// 从服务器接收消息
byte[] buffer = new byte[General.SocketByteSize];
int length = Socket.Receive(buffer);
if (length > 0)
{
string msg = General.DefaultEncoding.GetString(buffer, 0, length);
Library.Common.Network.JsonObject[] jsons = Library.Common.Network.JsonObject.GetObjects(msg);
foreach (Library.Common.Network.JsonObject json in jsons)
{
// 客户端接收消息广播ScoketObject到每个UIModel
Library.Common.Network.SocketObject obj = new(json);
result.Add(obj);
OnSocketReceive(obj);
}
}
}
return result.ToArray();
}
/// <summary>
/// 用于服务器接收客户端信息(数组版)
/// </summary>
/// <param name="ClientSocket">客户端Socket</param>
/// <returns>SocketObjects</returns>
internal static Library.Common.Network.SocketObject[] ReceiveArray(Socket ClientSocket)
{
List<Library.Common.Network.SocketObject> result = new();
if (ClientSocket != null)
{
// 从客户端接收消息
byte[] buffer = new byte[General.SocketByteSize];
int length = ClientSocket.Receive(buffer);
if (length > 0)
{
string msg = General.DefaultEncoding.GetString(buffer, 0, length);
Library.Common.Network.JsonObject[] jsons = Library.Common.Network.JsonObject.GetObjects(msg);
foreach (Library.Common.Network.JsonObject json in jsons)
{
Library.Common.Network.SocketObject so = new(json);
result.Add(so);
}
}
}
return result.ToArray();
}
/// <summary>
/// 将通信类型的枚举转换为字符串
/// </summary>