84 lines
1.9 KiB
C#
Raw Normal View History

2024-08-17 14:27:18 +08:00
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Reflection;
using DotRecast.Core.Collections;
using Google.Protobuf;
using Plugins.JNGame.Network.Entity;
using Plugins.JNGame.Network.Util;
using Plugins.JNGame.System;
using Plugins.JNGame.Util;
namespace Plugins.JNGame.Network
{
public class JNServerParam
{
2024-08-19 11:51:17 +08:00
public string Client;
2024-08-17 14:27:18 +08:00
public byte[] Message;
}
/// <summary>
/// 基础服务端网络类
/// </summary>
public abstract class JNServerBase : SystemBase
{
//消息Id
protected int _id = 0;
//创建通知
protected EventDispatcher _event = new();
//计入字节大小
protected Dictionary<int, int> _byteSize = new();
/// <summary>
/// 监听服务端事件
/// </summary>
/// <param name="hId"></param>
/// <param name="listener"></param>
public void AddListener(int hId,Action<JNServerParam> listener)
{
_event.AddListener($"{hId}",listener);
}
/// <summary>
/// 发送消息
/// </summary>
/// <param name="hId"></param>
/// <param name="data"></param>
public void Dispatch(int hId,JNServerParam data)
{
_event.Dispatch($"{hId}",data);
}
//获取字节大小
public int GetByteSize(int hId = 0)
{
if (hId == 0)
{
int size = 0;
try
{
_byteSize.ForEach(child =>
{
size += child.Value;
});
}
catch (Exception e)
{
// ignored
}
return size;
}
return _byteSize.GetValueOrDefault(hId, 0);
}
}
}