This commit is contained in:
DESKTOP-5RP3AKU\Jisol
2024-10-24 02:21:21 +08:00
parent 0827295804
commit 6cf78b53bb
61 changed files with 9651 additions and 12551 deletions

View File

@@ -0,0 +1,198 @@
using System;
using System.Collections.Generic;
using System.IO;
namespace JNGame.Sync.System.Data
{
/// <summary>
/// 事件型数据系统
/// 用于通知客户端
/// </summary>
public abstract class SEventDataSystem : SDataSystemBase
{
/// <summary>
/// 服务器事件
/// </summary>
private Queue<SEvent> ServerEvents = new();
/// <summary>
/// 客户端事件
/// </summary>
private Queue<SEvent> ClientEvents = new();
public SEventDataEnum Type;
public bool isServer => Type is SEventDataEnum.ServerClient or SEventDataEnum.Server;
public bool isClient => Type is SEventDataEnum.ServerClient or SEventDataEnum.Client;
//待插入的数据
protected Queue<byte[]> WaitUBytes = new ();
public SEventDataSystem(SEventDataEnum type)
{
Type = type;
}
public override void OnSyncStart()
{
}
public override void OnSyncUpdate(int dt)
{
while (WaitUBytes.Count > 0)
{
OnUByteUpdate(WaitUBytes.Dequeue());
}
if (isServer)
{
if (ServerEvents.Count <= 0) return;
using (MemoryStream ms = new MemoryStream())
{
using (BinaryWriter writer = new BinaryWriter(ms))
{
int count = ServerEvents.Count;
//写入数量
writer.Write(count);
for (int i = count - 1; i >= 0; i--)
{
var data = ServerEvents.Dequeue().ToByteArray();
//写入数据大小
writer.Write(data.Length);
//写入数据
writer.Write(data);
}
}
// 发送完整的字节数组
OnSendUBytes(ms.ToArray());
}
}
}
/// <summary>
/// 发送字节数据
/// </summary>
/// <param name="bytes"></param>
/// <returns>是否清空UBytes</returns>
public abstract void OnSendUBytes(byte[] bytes);
/// <summary>
/// 插入字节
/// </summary>
/// <returns></returns>
public void OnInsertUBytes(byte[] bytes)
{
WaitUBytes.Enqueue(bytes);
}
/// <summary>
/// 提交更新到ClientEvents
/// </summary>
private void OnUByteUpdate(byte[] dequeue)
{
using (MemoryStream ms = new MemoryStream(dequeue))
{
using (BinaryReader reader = new BinaryReader(ms))
{
// 读取 数量
int count = reader.ReadInt32();
for (int i = count - 1; i >= 0; i--)
{
// 读取 数据 字段的长度
int length = reader.ReadInt32();
// 读取 数据
ClientEvents.Enqueue(SEvent.FromByteArray(reader.ReadBytes(length)));
}
}
}
}
public void AddEvent(int type,byte[] data)
{
//只有服务器才可以添加事件
if (isServer)
{
ServerEvents.Enqueue(new SEvent()
{
Type = type,
Data = data,
});
}
}
public class SEvent
{
/// <summary>
/// 数据类型
/// </summary>
public int Type;
/// <summary>
/// 数据
/// </summary>
public byte[] Data;
/// <summary>
/// 将 SEvent 对象转换为字节数组
/// </summary>
/// <returns>SEvent 对象的字节表示</returns>
public byte[] ToByteArray()
{
using (MemoryStream ms = new MemoryStream())
{
using (BinaryWriter writer = new BinaryWriter(ms))
{
// 写入 Type 字段,使用小端字节序
writer.Write(Type);
// 写入 Data 字段的长度
writer.Write(Data?.Length ?? 0);
// 写入 Data 字段的数据
if (Data != null)
{
writer.Write(Data);
}
}
// 返回完整的字节数组
return ms.ToArray();
}
}
/// <summary>
/// 从字节数组还原 SEvent 对象
/// </summary>
/// <param name="byteArray">包含 SEvent 数据的字节数组</param>
/// <returns>还原后的 SEvent 对象</returns>
public static SEvent FromByteArray(byte[] byteArray)
{
if (byteArray == null || byteArray.Length < 8)
{
throw new ArgumentException("字节数组长度不足");
}
using (MemoryStream ms = new MemoryStream(byteArray))
{
using (BinaryReader reader = new BinaryReader(ms))
{
// 读取 Type 字段
int type = reader.ReadInt32();
// 读取 Data 字段的长度
int dataLength = reader.ReadInt32();
// 读取 Data 字段的数据
byte[] data = reader.ReadBytes(dataLength);
// 返回还原后的 SEvent 对象
return new SEvent { Type = type, Data = data };
}
}
}
}
public enum SEventDataEnum
{
Server,
Client,
ServerClient,
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 9b2443b1ebaf4f66b259d21757cc3c7e
timeCreated: 1729705001