Initial Client/Server Communication
#1 Followed the MagicOnion quickstart guide, borrowing code from the chat sample app to get something working
This commit is contained in:
parent
52b0d4d8ff
commit
a0c8dbee72
493 changed files with 57790 additions and 3 deletions
8
Assets/Scripts/MagicOnion/Server/Hubs.meta
Normal file
8
Assets/Scripts/MagicOnion/Server/Hubs.meta
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 5c9aa9fba89d3404eb2292310360d574
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
94
Assets/Scripts/MagicOnion/Server/Hubs/BroadcasterHelper.cs
Normal file
94
Assets/Scripts/MagicOnion/Server/Hubs/BroadcasterHelper.cs
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
using MagicOnion.Utils;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicOnion.Server.Hubs
|
||||
{
|
||||
public static class BroadcasterHelper
|
||||
{
|
||||
internal static readonly Type[] dynamicArgumentTupleTypes = typeof(DynamicArgumentTuple<,>).GetTypeInfo().Assembly
|
||||
.GetTypes()
|
||||
.Where(x => x.Name.StartsWith("DynamicArgumentTuple") && !x.Name.Contains("Formatter"))
|
||||
.OrderBy(x => x.GetGenericArguments().Length)
|
||||
.ToArray();
|
||||
|
||||
internal static MethodDefinition[] SearchDefinitions(Type interfaceType)
|
||||
{
|
||||
return interfaceType
|
||||
.GetInterfaces()
|
||||
.Concat(new[] { interfaceType })
|
||||
.SelectMany(x => x.GetMethods())
|
||||
.Where(x =>
|
||||
{
|
||||
var methodInfo = x;
|
||||
if (methodInfo.IsSpecialName && (methodInfo.Name.StartsWith("set_") || methodInfo.Name.StartsWith("get_"))) return false;
|
||||
if (methodInfo.GetCustomAttribute<IgnoreAttribute>(false) != null) return false; // ignore
|
||||
|
||||
var methodName = methodInfo.Name;
|
||||
if (methodName == "Equals"
|
||||
|| methodName == "GetHashCode"
|
||||
|| methodName == "GetType"
|
||||
|| methodName == "ToString"
|
||||
)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
})
|
||||
.Where(x => !x.IsSpecialName)
|
||||
.Select(x => new MethodDefinition
|
||||
{
|
||||
ReceiverType = interfaceType,
|
||||
MethodInfo = x,
|
||||
})
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
internal static void VerifyMethodDefinitions(MethodDefinition[] definitions)
|
||||
{
|
||||
// define and set.
|
||||
var map = new Dictionary<int, MethodDefinition>(definitions.Length);
|
||||
foreach (var item in definitions)
|
||||
{
|
||||
var methodId = item.MethodInfo.GetCustomAttribute<MethodIdAttribute>()?.MethodId ?? FNV1A32.GetHashCode(item.MethodInfo.Name);
|
||||
if (map.ContainsKey(methodId))
|
||||
{
|
||||
throw new Exception($"TReceiver does not allows duplicate methodId(hash code). Please change name or use MethodIdAttribute. {map[methodId].MethodInfo.Name} and {item.MethodInfo.Name}");
|
||||
}
|
||||
map.Add(methodId, item);
|
||||
|
||||
if (!(item.MethodInfo.ReturnType == typeof(void)))
|
||||
{
|
||||
throw new Exception($"Invalid definition, TReceiver's return type must only be `void`. {item.MethodInfo.Name}.");
|
||||
}
|
||||
|
||||
item.MethodId = methodId;
|
||||
}
|
||||
}
|
||||
|
||||
public static async void FireAndForget(Task task)
|
||||
{
|
||||
try
|
||||
{
|
||||
await task.ConfigureAwait(false);
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
Grpc.Core.GrpcEnvironment.Logger?.Error(ex, "exception occured in client broadcast.");
|
||||
}
|
||||
}
|
||||
|
||||
internal class MethodDefinition
|
||||
{
|
||||
public string Path => ReceiverType.Name + "/" + MethodInfo.Name;
|
||||
|
||||
public Type ReceiverType;
|
||||
public MethodInfo MethodInfo;
|
||||
public int MethodId;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: a482aff8c25287942af22c031e219f8d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Loading…
Reference in a new issue