188 lines
5.2 KiB
C#
188 lines
5.2 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Google.Protobuf;
|
|
using Google.Protobuf.Collections;
|
|
using Google.Protobuf.Reflection;
|
|
using Google.Protobuf.WellKnownTypes;
|
|
using Microsoft.AspNetCore.DataProtection.KeyManagement;
|
|
using RabbitMQ.Client.Events;
|
|
|
|
|
|
|
|
using ServerCore; using ServerBase;
|
|
|
|
|
|
|
|
namespace ServerBase;
|
|
|
|
//=============================================================================================
|
|
// Protobuf 관련 각종 지원 함수
|
|
//
|
|
// author : kangms
|
|
//
|
|
//=============================================================================================
|
|
|
|
public static class ProtoHelper
|
|
{
|
|
public static string toBasicString(this IMessage _this)
|
|
{
|
|
// IM : IMessage
|
|
return $"IM: {_this.ToString()}";
|
|
}
|
|
|
|
public static string toBasicString(this FieldDescriptor _this)
|
|
{
|
|
// FD : FieldDescriptor
|
|
return $"FD: {_this.ToString()}";
|
|
}
|
|
|
|
public static string toJson(this IMessage message)
|
|
{
|
|
return JsonFormatter.Default.Format(message);
|
|
}
|
|
|
|
public static List<T> toList<T>(this RepeatedField<T> repeatedField)
|
|
{
|
|
return new List<T>(repeatedField);
|
|
}
|
|
|
|
public static RepeatedField<T> toRepeatedField<T>(this List<T> list)
|
|
{
|
|
var repeated_field = new RepeatedField<T>();
|
|
repeated_field.AddRange(list);
|
|
return repeated_field;
|
|
}
|
|
|
|
public static Dictionary<TKey, TValue> toDictionary<TKey, TValue>(this MapField<TKey, TValue> mapField)
|
|
where TKey : notnull
|
|
where TValue : notnull
|
|
{
|
|
var dictionary = new Dictionary<TKey, TValue>();
|
|
foreach (var each in mapField)
|
|
{
|
|
dictionary.Add(each.Key, each.Value);
|
|
}
|
|
return dictionary;
|
|
}
|
|
|
|
public static MapField<TKey, TValue> toMapField<TKey, TValue>(this Dictionary<TKey, TValue> dictionary)
|
|
where TKey : notnull
|
|
where TValue : notnull
|
|
{
|
|
var map_field = new MapField<TKey, TValue>();
|
|
foreach (var each in dictionary)
|
|
{
|
|
map_field.Add(each.Key, each.Value);
|
|
}
|
|
return map_field;
|
|
}
|
|
|
|
private static IMessage? parsePacketActionType(this IMessage rootMsg)
|
|
{
|
|
var found_oneof = rootMsg.Descriptor.Oneofs.First<OneofDescriptor>();
|
|
if (null == found_oneof)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var curr_oneof_field = found_oneof.Accessor.GetCaseFieldDescriptor(rootMsg);
|
|
if (null == curr_oneof_field)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var packet_action_type = (IMessage)curr_oneof_field.Accessor.GetValue(rootMsg);
|
|
if (null == packet_action_type)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return packet_action_type;
|
|
}
|
|
|
|
private static IMessage? parsePacketCommandType(this IMessage childMsg)
|
|
{
|
|
var found_oneof = childMsg.Descriptor.Oneofs.First<OneofDescriptor>();
|
|
if (null == found_oneof)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var curr_oneof_field = found_oneof.Accessor.GetCaseFieldDescriptor(childMsg);
|
|
if (null == curr_oneof_field)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var packet_command_type = (IMessage)curr_oneof_field.Accessor.GetValue(childMsg);
|
|
if (null == packet_command_type)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return packet_command_type;
|
|
}
|
|
|
|
private static bool parsePacketCommandMsgHook(this IMessage message)
|
|
{
|
|
var message_hook_property = message.GetType().GetProperty("MessageHook");
|
|
if (message_hook_property != null)
|
|
{
|
|
var msgHookValue = message_hook_property.GetValue(message);
|
|
if (msgHookValue is bool)
|
|
{
|
|
return (bool)msgHookValue;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public static bool makeProudNetPacketCommand(this IMessage message, out ProudNetPacketCommand? packetCommand)
|
|
{
|
|
packetCommand = null;
|
|
|
|
var packet_action_type = message.parsePacketActionType();
|
|
if (null == packet_action_type)
|
|
{
|
|
Log.getLogger().fatal($"Not found Packet Action Type !!! : {message.toBasicString()}");
|
|
return false;
|
|
}
|
|
|
|
var packet_command_type = parsePacketCommandType(packet_action_type);
|
|
if (null == packet_command_type)
|
|
{
|
|
Log.getLogger().fatal($"Not found Packet Command Type !!! : {message.toBasicString()}");
|
|
return false;
|
|
}
|
|
|
|
var msg_hook = parsePacketCommandMsgHook(packet_command_type);
|
|
|
|
packetCommand = new ProudNetPacketCommand(packet_action_type.GetType(), packet_command_type.GetType());
|
|
|
|
return true;
|
|
}
|
|
|
|
public static bool makeRabbitMqPacketCommand(this IMessage message, BasicDeliverEventArgs ea, out RabbitMqPacketCommand? packetCommand)
|
|
{
|
|
packetCommand = null;
|
|
|
|
var packet_command_type = message.parsePacketCommandType();
|
|
if (null == packet_command_type)
|
|
{
|
|
ServerCore.Log.getLogger().fatal($"Not found Packet Command Type !!! : {message.toBasicString()}");
|
|
return false;
|
|
}
|
|
|
|
packetCommand = new RabbitMqPacketCommand(ea.Exchange, packet_command_type.GetType());
|
|
return true;
|
|
}
|
|
}
|