초기커밋
This commit is contained in:
251
ServerBase/Network/Handler/LargePacketHandler.cs
Normal file
251
ServerBase/Network/Handler/LargePacketHandler.cs
Normal file
@@ -0,0 +1,251 @@
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
|
||||
using Google.Protobuf;
|
||||
using NeoSmart.AsyncLock;
|
||||
using Nettention.Proud;
|
||||
|
||||
|
||||
using ServerCore; using ServerBase;
|
||||
|
||||
|
||||
namespace ServerBase;
|
||||
|
||||
public class LargePacketHandler
|
||||
{
|
||||
//private ConcurrentDictionary<(Player, string), SortedSet<CollectedPacketInfo>> m_large_packets = new();
|
||||
private ConcurrentDictionary<IEntityWithSession, SortedSet<CollectedPacketInfo>> m_large_packets = new(); // 하나의 패킷만 처리
|
||||
private static AsyncLock _lock = new();
|
||||
|
||||
private SessionBase? m_network_session { get; set; }
|
||||
|
||||
public void onInit(SessionBase networkSession)
|
||||
{
|
||||
m_network_session = networkSession;
|
||||
var func = checkLargePacket;
|
||||
_ = PeriodicTaskHelper.runTask(func, TimeSpan.FromMilliseconds(Constant.LARGE_PACKET_CHECK_INTERVAL_MSEC));
|
||||
}
|
||||
|
||||
public Result collectPacket(IEntityWithSession ssesion, string packetUid, Int32 totalPacketCount, Int32 packetIdx, ByteString datas)
|
||||
{
|
||||
Log.getLogger().debug($"LargePacketCollect packetUid : {packetUid}, totalPacketCount : {totalPacketCount}, packetIdx : {packetIdx}, datasize : {datas.Length}");
|
||||
|
||||
var collected_packet = new CollectedPacketInfo(packetUid, totalPacketCount, packetIdx, datas);
|
||||
//var user_guid = player.getUserGuid();
|
||||
|
||||
var result = addCollectedPacket(ssesion, collected_packet);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
private Result addCollectedPacket(IEntityWithSession ssesion, CollectedPacketInfo packet)
|
||||
{
|
||||
using (_lock.Lock())
|
||||
{
|
||||
if (false == m_large_packets.TryGetValue(ssesion, out var collected_packets))
|
||||
{
|
||||
collected_packets = new SortedSet<CollectedPacketInfo>();
|
||||
collected_packets.Add(packet);
|
||||
m_large_packets.TryAdd(ssesion, collected_packets);
|
||||
}
|
||||
else
|
||||
{
|
||||
collected_packets.Add(packet);
|
||||
}
|
||||
|
||||
}
|
||||
Log.getLogger().debug($"addCollectedPacket done");
|
||||
return new Result();
|
||||
}
|
||||
|
||||
private Result removeCollectedPacket(List<IEntityWithSession> sessionIds)
|
||||
{
|
||||
|
||||
if (sessionIds.Count == 0) return new();
|
||||
|
||||
var result = new Result();
|
||||
using (_lock.Lock())
|
||||
{
|
||||
foreach (var session_id in sessionIds)
|
||||
{
|
||||
m_large_packets.TryRemove(session_id, out var _);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public Task<Result> checkLargePacket()
|
||||
{
|
||||
var result = new Result();
|
||||
List<IEntityWithSession> delete_users = new();
|
||||
foreach (var packets in m_large_packets)
|
||||
{
|
||||
var session = packets.Key;
|
||||
var packet_set = packets.Value;
|
||||
|
||||
var isRecvResult = validCheck(session, packet_set);
|
||||
if (isRecvResult.isSuccess())
|
||||
{
|
||||
if (false == processPacket(session, packet_set))
|
||||
{
|
||||
Log.getLogger().error("processPacket error!!");
|
||||
delete_users.Add(session);
|
||||
break;
|
||||
}
|
||||
|
||||
delete_users.Add(session);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isRecvResult.getErrorCode() == ServerErrorCode.LargePacketNotAllReceived) continue;
|
||||
if (isRecvResult.getErrorCode() != ServerErrorCode.LargePacketRecvTimeOver)
|
||||
{
|
||||
send_GS2C_NTF_LARGE_PACKET_TIMEOUT(session, isRecvResult, packet_set);
|
||||
delete_users.Add(session);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (delete_users.Count > 0)
|
||||
{
|
||||
removeCollectedPacket(delete_users);
|
||||
}
|
||||
|
||||
return Task.FromResult(result);
|
||||
}
|
||||
|
||||
private void send_GS2C_NTF_LARGE_PACKET_TIMEOUT(IEntityWithSession session, Result result, SortedSet<CollectedPacketInfo> packetSet)
|
||||
{
|
||||
var last_recv_time = packetSet.OrderByDescending(p => p.m_last_recv_time).FirstOrDefault();
|
||||
NullReferenceCheckHelper.throwIfNull(last_recv_time, () => $"Last Receive time is null !! - player:{session.toBasicString()}");
|
||||
|
||||
// var uid = packetSet.Select(p => p.m_packet_uid).FirstOrDefault() ?? string.Empty;
|
||||
|
||||
ClientToGame msg = new();
|
||||
msg.Message = new();
|
||||
msg.Message.NtfLargePacketTimeout = new();
|
||||
msg.Message.NtfLargePacketTimeout.Uid = last_recv_time.m_packet_uid;
|
||||
msg.Message.NtfLargePacketTimeout.LastCheckTime = Google.Protobuf.WellKnownTypes.Timestamp.FromDateTime(last_recv_time.m_last_recv_time);
|
||||
msg.Message.NtfLargePacketTimeout.LastCheckTime = Google.Protobuf.WellKnownTypes.Timestamp.FromDateTime(DateTime.UtcNow);
|
||||
}
|
||||
|
||||
private bool processPacket(IEntityWithSession session, SortedSet<CollectedPacketInfo> packets)
|
||||
{
|
||||
var network_session = getNetworkSession();
|
||||
|
||||
using (var stream = new MemoryStream())
|
||||
{
|
||||
foreach (var packet in packets)
|
||||
{
|
||||
if (packet.m_datas == null)
|
||||
{
|
||||
Log.getLogger().warn("Warning: packet.m_datas is null");
|
||||
continue;
|
||||
}
|
||||
packet.m_datas.WriteTo(stream);
|
||||
}
|
||||
|
||||
if (stream.Length == 0)
|
||||
{
|
||||
Log.getLogger().warn("Warning: stream is empty after writing packets.");
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
stream.Position = 0;
|
||||
var req = ClientToGame.Parser.ParseFrom(stream);
|
||||
|
||||
var func = network_session.onCallProtocolHandler(session, req);
|
||||
return true;
|
||||
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.getLogger().error($"Exception !!!, Failed to perform !!!, in LargePacketHandler.processPacket() : exception:{e} - {session.toBasicString()}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Result validCheck(IEntityWithSession session, SortedSet<CollectedPacketInfo> packets)
|
||||
{
|
||||
var err_msg = string.Empty;
|
||||
var result = new Result();
|
||||
|
||||
var sorted_packet_idxs = packets.OrderBy(p => p.m_packet_idx).ToList();
|
||||
var total_packet_count = packets.Select(p => p.m_total_packet_count).FirstOrDefault();
|
||||
|
||||
//아직 패킷이 다 안들어온거다.
|
||||
if (sorted_packet_idxs.Count < total_packet_count)
|
||||
{
|
||||
err_msg = $"Not all packets were received !!! : recvPacketCount:{sorted_packet_idxs.Count} < totalPacketCount:{total_packet_count} - {session.toBasicString()}";
|
||||
result.setFail(ServerErrorCode.LargePacketNotAllReceived, err_msg);
|
||||
return result;
|
||||
}
|
||||
|
||||
var last_recv_time = packets.OrderByDescending(p => p.m_last_recv_time).FirstOrDefault();
|
||||
NullReferenceCheckHelper.throwIfNull(last_recv_time, () => $"last receive time is null !!! - {session.toBasicString()}");
|
||||
|
||||
var diff = DateTimeHelper.Current - last_recv_time.m_last_recv_time;
|
||||
if (diff.TotalSeconds > Constant.LARGE_PACKET_CHECK_WAIT_SEC)
|
||||
{
|
||||
err_msg = $"Packet receive timeout exceeded !!! : waitingSec:{diff.TotalSeconds} > WaitMaxSec:{Constant.LARGE_PACKET_CHECK_WAIT_SEC}, lastRecvTime:{last_recv_time} - {session.toBasicString()}";
|
||||
result.setFail(ServerErrorCode.LargePacketRecvTimeOver, err_msg);
|
||||
Log.getLogger().error(result.toBasicString());
|
||||
return result;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public SessionBase getNetworkSession()
|
||||
{
|
||||
NullReferenceCheckHelper.throwIfNull(m_network_session, () => $"m_network_session is null !!!");
|
||||
return m_network_session;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class CollectedPacketInfo : IComparable<CollectedPacketInfo>
|
||||
{
|
||||
public string m_packet_uid { get; } = string.Empty;
|
||||
public Int32 m_total_packet_count { get; } = 0;
|
||||
public Int32 m_packet_idx { get; } = 0;
|
||||
public ByteString? m_datas { get; } = null;
|
||||
public DateTime m_last_recv_time { get; } = DateTime.Now;
|
||||
public CollectedPacketInfo(string packetUid, Int32 totalPacketCount, Int32 packetIdx, ByteString datas)
|
||||
{
|
||||
m_packet_uid = packetUid;
|
||||
m_total_packet_count = totalPacketCount;
|
||||
m_packet_idx = packetIdx;
|
||||
m_datas = datas;
|
||||
}
|
||||
|
||||
public Int32 CompareTo(CollectedPacketInfo? other)
|
||||
{
|
||||
if (other == null) return 1;
|
||||
return this.m_packet_idx.CompareTo(other.m_packet_idx);
|
||||
}
|
||||
|
||||
public override bool Equals(object? obj)
|
||||
{
|
||||
if (obj == null || GetType() != obj.GetType())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
CollectedPacketInfo other = (CollectedPacketInfo)obj;
|
||||
//return m_packet_uid == other.m_packet_uid &&
|
||||
// m_total_packet_count == other.m_total_packet_count &&
|
||||
// m_packet_idx == other.m_packet_idx &&
|
||||
// m_last_recv_time == other.m_last_recv_time;
|
||||
return m_packet_idx == other.m_packet_idx;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return HashCode.Combine(m_packet_uid, m_total_packet_count, m_packet_idx, m_last_recv_time);
|
||||
}
|
||||
}
|
||||
|
||||
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