mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-06-26 11:24:46 +00:00
84 lines
1.9 KiB
C#
84 lines
1.9 KiB
C#
|
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
|
|||
|
{
|
|||
|
|
|||
|
public int Client;
|
|||
|
|
|||
|
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);
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
}
|