초기커밋
This commit is contained in:
155
ServerBase/Network/Packet/PacketCommand.cs
Normal file
155
ServerBase/Network/Packet/PacketCommand.cs
Normal file
@@ -0,0 +1,155 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
|
||||
namespace ServerBase;
|
||||
|
||||
|
||||
//=============================================================================================
|
||||
// ProudNet Packet Header 처리 클래스 이다.
|
||||
//
|
||||
// author : kangms
|
||||
//
|
||||
//=============================================================================================
|
||||
|
||||
public class ProudNetPacketCommand : IPacketCommand
|
||||
{
|
||||
private string m_command_key;
|
||||
|
||||
private Type m_packet_action_type;
|
||||
private Type m_command_type;
|
||||
|
||||
public ProudNetPacketCommand(Type packetActionType, Type commandType)
|
||||
{
|
||||
m_packet_action_type = packetActionType;
|
||||
m_command_type = commandType;
|
||||
m_command_key = toKeyString();
|
||||
}
|
||||
|
||||
public override Int32 GetHashCode()
|
||||
{
|
||||
return string.Format(m_command_key).GetHashCode();
|
||||
}
|
||||
|
||||
public override bool Equals(object? packetCommand)
|
||||
{
|
||||
var packet_command = packetCommand as ProudNetPacketCommand;
|
||||
if (null == packet_command)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( m_packet_action_type.Equals(packet_command.getPacketActionType())
|
||||
&& m_command_type.Equals(packet_command.getCommandType()) )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public string getCommandKey()
|
||||
{
|
||||
return m_command_key;
|
||||
}
|
||||
|
||||
public Type getPacketActionType()
|
||||
{
|
||||
return m_packet_action_type;
|
||||
}
|
||||
|
||||
public Type getCommandType()
|
||||
{
|
||||
return m_command_type;
|
||||
}
|
||||
|
||||
public string toKeyString()
|
||||
{
|
||||
return $"a:{m_packet_action_type.Name.ToString()},c:{m_command_type.Name.ToString()}";
|
||||
}
|
||||
|
||||
public string toBasicString()
|
||||
{
|
||||
return $"AT:{m_packet_action_type.Name.ToString()},CT:{m_command_type.Name.ToString()}";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//=============================================================================================
|
||||
// RabbitMQ Packet Header 처리 클래스 이다.
|
||||
//
|
||||
// author : kangms
|
||||
//
|
||||
//=============================================================================================
|
||||
|
||||
public class RabbitMqPacketCommand : IPacketCommand
|
||||
{
|
||||
private string m_command_key;
|
||||
|
||||
private string m_exchange_name;
|
||||
private Type m_command_type;
|
||||
|
||||
public RabbitMqPacketCommand(string exchangeName, Type commandType)
|
||||
{
|
||||
m_exchange_name = exchangeName;
|
||||
m_command_type = commandType;
|
||||
|
||||
m_command_key = toKeyString();
|
||||
}
|
||||
|
||||
public override Int32 GetHashCode()
|
||||
{
|
||||
return string.Format(m_command_key).GetHashCode();
|
||||
}
|
||||
|
||||
public override bool Equals(object? packetCommand)
|
||||
{
|
||||
var packet_command = packetCommand as RabbitMqPacketCommand;
|
||||
if (null == packet_command)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_exchange_name.Equals(packet_command.getExchangeName())
|
||||
&& m_command_type.Equals(packet_command.getCommandType()))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public string getCommandKey()
|
||||
{
|
||||
return m_command_key;
|
||||
}
|
||||
|
||||
public string getExchangeName()
|
||||
{
|
||||
return m_exchange_name;
|
||||
}
|
||||
|
||||
public Type getCommandType()
|
||||
{
|
||||
return m_command_type;
|
||||
}
|
||||
|
||||
public bool msgHook()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public string toKeyString()
|
||||
{
|
||||
return $"e:{m_exchange_name},c:{m_command_type.Name.ToString()}";
|
||||
}
|
||||
|
||||
public string toBasicString()
|
||||
{
|
||||
return $"EN:{m_exchange_name},CT:{m_command_type.Name.ToString()}";
|
||||
}
|
||||
}
|
||||
184
ServerBase/Network/Packet/PacketHandler.cs
Normal file
184
ServerBase/Network/Packet/PacketHandler.cs
Normal file
@@ -0,0 +1,184 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
using Nettention.Proud;
|
||||
|
||||
|
||||
using ServerCore;
|
||||
|
||||
|
||||
namespace ServerBase;
|
||||
|
||||
|
||||
//=============================================================================================
|
||||
// 패킷 응답/통지 수신 처리자 연결을 위한 AttributeUsage 등록
|
||||
//
|
||||
// author : kangms
|
||||
//
|
||||
//=============================================================================================
|
||||
|
||||
[AttributeUsage(AttributeTargets.Class)]
|
||||
public class PacketHandlerAttribute : Attribute
|
||||
{
|
||||
private readonly IPacketCommand m_packet_command;
|
||||
private readonly Type m_handler_class;
|
||||
private readonly Type m_handlable_owner;
|
||||
private readonly bool m_message_hook;
|
||||
|
||||
public PacketHandlerAttribute( Type packetAction, Type command
|
||||
, Type handlerClass, Type handlerOwner
|
||||
, bool message_hook = false)
|
||||
{
|
||||
var packet_cmd = new ProudNetPacketCommand(packetAction, command);
|
||||
|
||||
m_packet_command = packet_cmd;
|
||||
m_handler_class = handlerClass;
|
||||
m_handlable_owner = handlerOwner;
|
||||
m_message_hook = message_hook;
|
||||
}
|
||||
|
||||
public PacketHandlerAttribute( string exchangeName, Type command
|
||||
, Type handlerClass, Type handlerOwner)
|
||||
{
|
||||
var packet_cmd = new RabbitMqPacketCommand(exchangeName, command);
|
||||
|
||||
m_packet_command = packet_cmd;
|
||||
m_handler_class = handlerClass;
|
||||
m_handlable_owner = handlerOwner;
|
||||
}
|
||||
|
||||
public IPacketCommand getPacketCommand()
|
||||
{
|
||||
return m_packet_command;
|
||||
}
|
||||
|
||||
public Type getHandlerClass()
|
||||
{
|
||||
return m_handler_class;
|
||||
}
|
||||
|
||||
public Type getHandlerOwner()
|
||||
{
|
||||
return m_handlable_owner;
|
||||
}
|
||||
|
||||
public bool getMessageHook()
|
||||
{
|
||||
return m_message_hook;
|
||||
}
|
||||
}
|
||||
|
||||
//===========================================================================================
|
||||
// 패킷 요청 Handler
|
||||
//
|
||||
// author : kangms
|
||||
//
|
||||
//===========================================================================================
|
||||
|
||||
public abstract class PacketSendHandler
|
||||
{
|
||||
public PacketSendHandler()
|
||||
{
|
||||
}
|
||||
|
||||
public virtual Result onFillupPacket(Google.Protobuf.IMessage toFillupMessage)
|
||||
{
|
||||
return new Result();
|
||||
}
|
||||
|
||||
public abstract Google.Protobuf.IMessage onGetToSendMessage();
|
||||
}
|
||||
|
||||
//===========================================================================================
|
||||
// 패킷 수신 Handler
|
||||
//
|
||||
// author : kangms
|
||||
//
|
||||
//===========================================================================================
|
||||
|
||||
public abstract class PacketRecvHandler
|
||||
{
|
||||
public PacketRecvHandler()
|
||||
{
|
||||
}
|
||||
|
||||
public virtual async Task<Result> onCheckValid(ISession session, Google.Protobuf.IMessage recvMessage)
|
||||
{
|
||||
await Task.CompletedTask;
|
||||
return new Result();
|
||||
}
|
||||
|
||||
public abstract Task<Result> onProcessPacket(ISession session, Google.Protobuf.IMessage recvMessage);
|
||||
|
||||
public virtual async Task onProcessPacketException( ISession session, Google.Protobuf.IMessage recvMessage
|
||||
, Result errorResult )
|
||||
{
|
||||
await Task.CompletedTask;
|
||||
|
||||
Log.getLogger().warn($"Not implemented PacketRecvHandler.onProcessPacketException() !!! : {errorResult?.toBasicString()} - {recvMessage?.toBasicString()}, {session?.toBasicString()}");
|
||||
}
|
||||
}
|
||||
|
||||
//===========================================================================================
|
||||
// 패킷 응답 Handler
|
||||
//
|
||||
// author : kangms
|
||||
//
|
||||
//===========================================================================================
|
||||
|
||||
public abstract class PacketAckHandler : PacketRecvHandler
|
||||
{
|
||||
public PacketAckHandler()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
//===========================================================================================
|
||||
// 패킷 통지 Handler
|
||||
//
|
||||
// author : kangms
|
||||
//
|
||||
//===========================================================================================
|
||||
|
||||
public abstract class PacketNtfHandler : PacketRecvHandler
|
||||
{
|
||||
public PacketNtfHandler()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
//===========================================================================================
|
||||
// p2p 패킷 수신 Handler
|
||||
//
|
||||
// author : khlee
|
||||
//
|
||||
//===========================================================================================
|
||||
|
||||
public abstract class P2PPacketRecvHandler
|
||||
{
|
||||
public P2PPacketRecvHandler()
|
||||
{
|
||||
|
||||
}
|
||||
public virtual async Task<Result> onCheckValid(ISession session, Google.Protobuf.IMessage recvMessage)
|
||||
{
|
||||
await Task.CompletedTask;
|
||||
return new Result();
|
||||
}
|
||||
|
||||
public abstract Task<Result> onProcessP2PPacket(ISession session, ByteArray recvMessage);
|
||||
|
||||
// public virtual async Task onProcessP2PPacketException( ISession session, Google.Protobuf.IMessage recvMessage
|
||||
// , Result errorResult )
|
||||
// {
|
||||
// await Task.CompletedTask;
|
||||
//
|
||||
// Log.getLogger().warn($"Not implemented PacketRecvHandler.onProcessPacketException() !!! : {errorResult?.toBasicString()} - {recvMessage?.toBasicString()}, {session?.toBasicString()}");
|
||||
// }
|
||||
}
|
||||
280
ServerBase/Network/Packet/PacketReceiver.cs
Normal file
280
ServerBase/Network/Packet/PacketReceiver.cs
Normal file
@@ -0,0 +1,280 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
using NLog.Layouts;
|
||||
using NLog.Internal.Fakeables;
|
||||
using Google.Protobuf;
|
||||
using Google.Protobuf.Reflection;
|
||||
using Amazon.Runtime.Internal.Transform;
|
||||
using RabbitMQ.Client.Events;
|
||||
|
||||
|
||||
|
||||
using ServerCore; using ServerBase;
|
||||
|
||||
|
||||
|
||||
namespace ServerBase;
|
||||
|
||||
|
||||
//=============================================================================================
|
||||
// 패킷 수신 관리자
|
||||
//
|
||||
// author : kangms
|
||||
//
|
||||
//=============================================================================================
|
||||
|
||||
public class PacketReceiver
|
||||
{
|
||||
private readonly ISession m_owner;
|
||||
private readonly Dictionary<string, PacketRecvHandler> m_packet_recv_handlers = new();
|
||||
private readonly Dictionary<string, P2PPacketRecvHandler> m_p2p_packet_recv_handler = new();
|
||||
|
||||
public delegate bool FnIsValidPacketNamespace(string toCheckNamespace, IPacketCommand packetCommand);
|
||||
|
||||
public PacketReceiver(ISession session)
|
||||
{
|
||||
m_owner = session;
|
||||
}
|
||||
|
||||
public async Task<bool> registerRecvHandlerAll()
|
||||
{
|
||||
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
|
||||
|
||||
FnIsValidPacketNamespace fn_is_valid_packet_namespace;
|
||||
var packet_namespace_verifier = m_owner as IWithPacketNamespaceVerifier;
|
||||
if (null != packet_namespace_verifier)
|
||||
{
|
||||
fn_is_valid_packet_namespace = packet_namespace_verifier.isValidPacketNamespace;
|
||||
}
|
||||
else
|
||||
{
|
||||
fn_is_valid_packet_namespace = isValidDefaultNamespace;
|
||||
}
|
||||
|
||||
return await Task.Run(() =>
|
||||
{
|
||||
foreach(var assembly in assemblies)
|
||||
{
|
||||
foreach (Type type in assembly.GetTypes())
|
||||
{
|
||||
var custom_attribs = (PacketHandlerAttribute[])type.GetCustomAttributes(typeof(PacketHandlerAttribute), true);
|
||||
if (custom_attribs.Length <= 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (var each in custom_attribs)
|
||||
{
|
||||
if (false == m_owner.GetType().Equals(each.getHandlerOwner()))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (false == fn_is_valid_packet_namespace(type.Namespace ?? string.Empty, each.getPacketCommand()))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
var packet_command = each.getPacketCommand();
|
||||
if (false == isValidPacketRecvHander(each.getHandlerClass()))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (true == hasRecvHandler(packet_command))
|
||||
{
|
||||
Log.getLogger().error($"Already register PacketCommand !!! : packetCommnad:{packet_command.toBasicString()}");
|
||||
return false;
|
||||
}
|
||||
|
||||
var handler_class = Activator.CreateInstance(each.getHandlerClass()) as PacketRecvHandler;
|
||||
if (null == handler_class)
|
||||
{
|
||||
Log.getLogger().error($"Failed to create PacketRecvHandler !!! : packetCommnad:{packet_command.toBasicString()}");
|
||||
return false;
|
||||
}
|
||||
|
||||
registerRecvHandler(packet_command, handler_class);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(0 >= m_packet_recv_handlers.Count)
|
||||
{
|
||||
Log.getLogger().error($"No PacketRecvHandler registered !!!");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
public bool registerRecvHandler(IPacketCommand packetCommand, PacketRecvHandler toRegisterRecvHandler)
|
||||
{
|
||||
if (true == m_packet_recv_handlers.ContainsKey(packetCommand.getCommandKey()))
|
||||
{
|
||||
Log.getLogger().error($"Already registered Recv Handler !!! : {packetCommand.toBasicString()}");
|
||||
return false;
|
||||
}
|
||||
|
||||
m_packet_recv_handlers.Add(packetCommand.getCommandKey(), toRegisterRecvHandler);
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool fillupProudNetPacketCommand(Google.Protobuf.IMessage protoMessage, out ProudNetPacketCommand? packetCommand)
|
||||
{
|
||||
packetCommand = null;
|
||||
|
||||
if (null == protoMessage)
|
||||
{
|
||||
Log.getLogger().fatal($"Invalid param Protobuf Message !!!");
|
||||
return false;
|
||||
}
|
||||
|
||||
if(false == protoMessage.makeProudNetPacketCommand(out packetCommand))
|
||||
{
|
||||
Log.getLogger().fatal($"Failed to make ProudNet PacketCommand !!! : {protoMessage.toBasicString()}");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool fillupRabbitMqPacketCommand(BasicDeliverEventArgs ea, Google.Protobuf.IMessage protoMessage, out RabbitMqPacketCommand? packetCommand)
|
||||
{
|
||||
packetCommand = null;
|
||||
|
||||
if (null == protoMessage)
|
||||
{
|
||||
ServerCore.Log.getLogger().fatal($"Invalid param Protobuf Message !!!");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (false == protoMessage.makeRabbitMqPacketCommand(ea, out packetCommand))
|
||||
{
|
||||
ServerCore.Log.getLogger().fatal($"Failed to make RabbitMq PacketCommand !!! : {protoMessage.toBasicString()}");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public bool isValidPacketRecvHander(Type pakcetRecvHandler)
|
||||
{
|
||||
if (false == pakcetRecvHandler.IsSubclassOf(typeof(PacketRecvHandler)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public PacketRecvHandler? findRecvHandler(IPacketCommand packetCommand)
|
||||
{
|
||||
if (false == m_packet_recv_handlers.ContainsKey(packetCommand.getCommandKey()))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return m_packet_recv_handlers[packetCommand.getCommandKey()];
|
||||
}
|
||||
|
||||
public bool hasRecvHandler(IPacketCommand packetCommand)
|
||||
{
|
||||
if (true == m_packet_recv_handlers.ContainsKey(packetCommand.getCommandKey()))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool isValidDefaultNamespace(string toCheckNamespace, IPacketCommand packetCommand)
|
||||
{
|
||||
var packet_namespace = ".PacketHandler";
|
||||
|
||||
if ( null != toCheckNamespace
|
||||
&& true == toCheckNamespace.Contains(packet_namespace))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
ServerCore.Log.getLogger().error($"Invalid Default PacketNamespace !!!, not included Namespace : {packet_namespace} ⊆ {toCheckNamespace}, packetCommnad:{packetCommand.toBasicString()}");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool isEqualPacket<T>(Google.Protobuf.IMessage recvMessage)
|
||||
where T : class
|
||||
{
|
||||
if (false == typeof(T).Equals(recvMessage))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public async Task<bool> registerP2PRecvHandlerAll()
|
||||
{
|
||||
await Task.CompletedTask;
|
||||
|
||||
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
|
||||
|
||||
//추후 packet 단위로 쪼개야 할때 이부분 수정해야한다.
|
||||
foreach(var assembly in assemblies)
|
||||
{
|
||||
foreach (Type type in assembly.GetTypes())
|
||||
{
|
||||
if (typeof(P2PPacketRecvHandler).IsAssignableFrom(type) && !type.IsAbstract)
|
||||
{
|
||||
try
|
||||
{
|
||||
var obj = Activator.CreateInstance(type);
|
||||
if (obj is P2PPacketRecvHandler instance)
|
||||
{
|
||||
m_p2p_packet_recv_handler.Add(instance.getTypeName(), instance);
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.getLogger().error($"Failed to create instance of {type.FullName} or not P2PPacketRecvHandler");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.getLogger().error($"Failed to create instance of {type.FullName}: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (m_p2p_packet_recv_handler.Count == 0)
|
||||
{
|
||||
Log.getLogger().debug("P2PPacketRecvHandler instance is zero");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public P2PPacketRecvHandler? findP2PPacketHandler(string key)
|
||||
{
|
||||
if (false == m_p2p_packet_recv_handler.ContainsKey(key))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
//일단 handler 1개만 등록 해놓고 거기서 처리 하도록 한다. 패킷별로 로직 처리 해야되면 패킷별로 핸들러 등록한다.
|
||||
return m_p2p_packet_recv_handler.Values.ToArray()[0];
|
||||
}
|
||||
}
|
||||
21
ServerBase/Network/Packet/PacketResponseDelayProfiler.cs
Normal file
21
ServerBase/Network/Packet/PacketResponseDelayProfiler.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
|
||||
namespace ServerBase;
|
||||
|
||||
//=============================================================================================
|
||||
// 패킷 응답 지연 분석기
|
||||
//
|
||||
// author : kangms
|
||||
//
|
||||
//=============================================================================================
|
||||
|
||||
public class PacketResponseDelayProfiler
|
||||
{
|
||||
|
||||
}
|
||||
167
ServerBase/Network/Packet/PacketSender.cs
Normal file
167
ServerBase/Network/Packet/PacketSender.cs
Normal file
@@ -0,0 +1,167 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
using Google.Protobuf;
|
||||
|
||||
|
||||
using ServerCore; using ServerBase;
|
||||
|
||||
|
||||
namespace ServerBase;
|
||||
|
||||
|
||||
//=============================================================================================
|
||||
// 패킷 송신 관리자
|
||||
//
|
||||
// author : kangms
|
||||
//
|
||||
//=============================================================================================
|
||||
|
||||
public class PacketSender
|
||||
{
|
||||
private readonly ISession m_owner;
|
||||
private readonly Dictionary<IPacketCommand, PacketSendHandler> m_packet_send_handlers = new();
|
||||
|
||||
public PacketSender(ISession session)
|
||||
{
|
||||
m_owner = session;
|
||||
}
|
||||
|
||||
public async Task<bool> registerSendHandlerAll()
|
||||
{
|
||||
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
|
||||
|
||||
return await Task.Run(() =>
|
||||
{
|
||||
foreach (var assembly in assemblies)
|
||||
{
|
||||
foreach (Type type in assembly.GetTypes())
|
||||
{
|
||||
var custom_attribs = (PacketHandlerAttribute[])type.GetCustomAttributes(typeof(PacketHandlerAttribute), false);
|
||||
if (custom_attribs.Length <= 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (var each in custom_attribs)
|
||||
{
|
||||
if (false == hasNamespace(type.Namespace, each.getPacketCommand()))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (false == m_owner.GetType().Equals(each.getHandlerOwner()))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
var packet_command = each.getPacketCommand();
|
||||
if (false == isValidPacketSendHandler(each.getHandlerClass()))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (true == hasSendHandler(packet_command))
|
||||
{
|
||||
Log.getLogger().error($"Already register PacketCommand !!! : packetCommnad:{packet_command.toBasicString()}");
|
||||
return false;
|
||||
}
|
||||
|
||||
var handler_class = Activator.CreateInstance(each.getHandlerClass()) as PacketSendHandler;
|
||||
if (null == handler_class)
|
||||
{
|
||||
Log.getLogger().error($"Failed to create PacketSendHandler !!! : packetCommnad:{packet_command.toBasicString()}");
|
||||
return false;
|
||||
}
|
||||
|
||||
registerSendHandler(packet_command, handler_class);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (0 >= m_packet_send_handlers.Count)
|
||||
{
|
||||
Log.getLogger().warn($"No PacketSendHandler registered !!!");
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
public bool registerSendHandler(IPacketCommand packetCommand, PacketSendHandler toRegisterSendHandler)
|
||||
{
|
||||
if (true == m_packet_send_handlers.ContainsKey(packetCommand))
|
||||
{
|
||||
Log.getLogger().error($"Already registered Send Handler !!! : {packetCommand.toBasicString()}");
|
||||
return false;
|
||||
}
|
||||
|
||||
m_packet_send_handlers.Add(packetCommand, toRegisterSendHandler);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public bool isValidPacketSendHandler(Type pakcetSendHandler)
|
||||
{
|
||||
if(false == pakcetSendHandler.IsSubclassOf(typeof(PacketSendHandler)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public PacketSendHandler? findSendHandler(IPacketCommand packetCommand)
|
||||
{
|
||||
if (false == m_packet_send_handlers.ContainsKey(packetCommand))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return m_packet_send_handlers[packetCommand];
|
||||
}
|
||||
|
||||
public bool hasSendHandler(IPacketCommand packetCommand)
|
||||
{
|
||||
if (true == m_packet_send_handlers.ContainsKey(packetCommand))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool hasNamespace(string? toCheckNamespace, IPacketCommand packetCommand)
|
||||
{
|
||||
string packet_handler_namespace = "PacketHandler";
|
||||
|
||||
if ( null != toCheckNamespace
|
||||
&& true == toCheckNamespace.Contains(packet_handler_namespace))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
ServerCore.Log.getLogger().error($"Invalid Namespace !!! : {packet_handler_namespace} != {toCheckNamespace}, packetCommnad:{packetCommand.toBasicString()}");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool isEqualPacket<T>(Google.Protobuf.IMessage recvMessage)
|
||||
where T : class
|
||||
{
|
||||
if(false == typeof(T).Equals(recvMessage))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user