using System.Text.Json.Serialization;
using Milimoe.FunGame.Core.Library.Constant;
using Milimoe.FunGame.Core.Service;
namespace Milimoe.FunGame.Core.Library.Common.Network
{
///
/// 唯一指定的通信数据结构
///
public readonly struct SocketObject
{
///
/// 通信类型
///
public SocketMessageType SocketType { get; } = SocketMessageType.Unknown;
///
/// 通信令牌
///
public Guid Token { get; } = Guid.Empty;
///
/// 参数列表
///
public object[] Parameters { get; } = [];
///
/// 参数数量
///
[JsonIgnore]
public int Length => Parameters.Length;
///
/// 从参数列表中获取指定索引的参数的Json对象
/// -- 此索引器仅返回Json对象,获取实例请使用反序列化方法GetParam[T]() --
/// -- 当然也可以自己反序列化 --
/// -- 基本类型可能有效,但仍建议使用反序列化方法 --
///
///
///
/// 索引超过数组上限
public object? this[int index]
{
get
{
if (index >= Parameters.Length) throw new IndexOutOfArrayLengthException();
object? obj = Parameters[index];
return JsonManager.GetObject(obj.ToString() ?? "");
}
}
///
/// 构建通信数据对象
///
///
///
///
[JsonConstructor]
public SocketObject(SocketMessageType socketType, Guid token, params object[] parameters)
{
SocketType = socketType;
Token = token;
if (parameters != null && parameters.Length > 0) Parameters = parameters;
}
///
/// 从参数列表中获取指定类型和索引的参数
///
/// 类型
/// 索引
/// 类型的参数
/// 索引超过数组上限
public T? GetParam(int index)
{
return JsonManager.GetObject(this, index);
}
}
}