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
114
Assets/Scripts/MessagePack/Unity/Extension/UnityBlitResolver.cs
Normal file
114
Assets/Scripts/MessagePack/Unity/Extension/UnityBlitResolver.cs
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
// Copyright (c) All contributors. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using MessagePack.Formatters;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MessagePack.Unity.Extension
|
||||
{
|
||||
/// <summary>
|
||||
/// Special Resolver for Vector2[], Vector3[], Vector4[], Quaternion[], Color[], Bounds[], Rect[].
|
||||
/// </summary>
|
||||
public class UnityBlitResolver : IFormatterResolver
|
||||
{
|
||||
public static readonly UnityBlitResolver Instance = new UnityBlitResolver();
|
||||
|
||||
private UnityBlitResolver()
|
||||
{
|
||||
}
|
||||
|
||||
public IMessagePackFormatter<T> GetFormatter<T>()
|
||||
{
|
||||
return FormatterCache<T>.Formatter;
|
||||
}
|
||||
|
||||
private static class FormatterCache<T>
|
||||
{
|
||||
public static readonly IMessagePackFormatter<T> Formatter;
|
||||
|
||||
static FormatterCache()
|
||||
{
|
||||
Formatter = (IMessagePackFormatter<T>)UnityBlitResolverGetFormatterHelper.GetFormatter(typeof(T));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Special Resolver for Vector2[], Vector3[], Vector4[], Quaternion[], Color[], Bounds[], Rect[] + int[], float[], double[].
|
||||
/// </summary>
|
||||
public class UnityBlitWithPrimitiveArrayResolver : IFormatterResolver
|
||||
{
|
||||
public static readonly UnityBlitWithPrimitiveArrayResolver Instance = new UnityBlitWithPrimitiveArrayResolver();
|
||||
|
||||
private UnityBlitWithPrimitiveArrayResolver()
|
||||
{
|
||||
}
|
||||
|
||||
public IMessagePackFormatter<T> GetFormatter<T>()
|
||||
{
|
||||
return FormatterCache<T>.Formatter;
|
||||
}
|
||||
|
||||
private static class FormatterCache<T>
|
||||
{
|
||||
internal static readonly IMessagePackFormatter<T> Formatter;
|
||||
|
||||
static FormatterCache()
|
||||
{
|
||||
Formatter = (IMessagePackFormatter<T>)UnityBlitWithPrimitiveResolverGetFormatterHelper.GetFormatter(typeof(T));
|
||||
if (Formatter == null)
|
||||
{
|
||||
Formatter = UnityBlitResolver.Instance.GetFormatter<T>();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal static class UnityBlitResolverGetFormatterHelper
|
||||
{
|
||||
private static readonly Dictionary<Type, Type> FormatterMap = new Dictionary<Type, Type>()
|
||||
{
|
||||
{ typeof(Vector2[]), typeof(Vector2ArrayBlitFormatter) },
|
||||
{ typeof(Vector3[]), typeof(Vector3ArrayBlitFormatter) },
|
||||
{ typeof(Vector4[]), typeof(Vector4ArrayBlitFormatter) },
|
||||
{ typeof(Quaternion[]), typeof(QuaternionArrayBlitFormatter) },
|
||||
{ typeof(Color[]), typeof(ColorArrayBlitFormatter) },
|
||||
{ typeof(Bounds[]), typeof(BoundsArrayBlitFormatter) },
|
||||
{ typeof(Rect[]), typeof(RectArrayBlitFormatter) },
|
||||
};
|
||||
|
||||
internal static object GetFormatter(Type t)
|
||||
{
|
||||
Type formatterType;
|
||||
if (FormatterMap.TryGetValue(t, out formatterType))
|
||||
{
|
||||
return Activator.CreateInstance(formatterType);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
internal static class UnityBlitWithPrimitiveResolverGetFormatterHelper
|
||||
{
|
||||
private static readonly Dictionary<Type, Type> FormatterMap = new Dictionary<Type, Type>()
|
||||
{
|
||||
{ typeof(int[]), typeof(IntArrayBlitFormatter) },
|
||||
{ typeof(float[]), typeof(FloatArrayBlitFormatter) },
|
||||
{ typeof(double[]), typeof(DoubleArrayBlitFormatter) },
|
||||
};
|
||||
|
||||
internal static object GetFormatter(Type t)
|
||||
{
|
||||
Type formatterType;
|
||||
if (FormatterMap.TryGetValue(t, out formatterType))
|
||||
{
|
||||
return Activator.CreateInstance(formatterType);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: ef834ed39aa3bf457860be80e97a5d9c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,179 @@
|
|||
// Copyright (c) All contributors. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using MessagePack.Formatters;
|
||||
using UnityEngine;
|
||||
|
||||
#pragma warning disable SA1402 // multiple types in a file
|
||||
#pragma warning disable SA1649 // file name matches type name
|
||||
|
||||
namespace MessagePack.Unity.Extension
|
||||
{
|
||||
// use ext instead of ArrayFormatter to extremely boost up performance.
|
||||
// Layout: [extHeader, byteSize(integer), isLittleEndian(bool), bytes()]
|
||||
// Used Ext:30~36
|
||||
public abstract class UnsafeBlitFormatterBase<T> : IMessagePackFormatter<T[]>
|
||||
where T : struct
|
||||
{
|
||||
protected abstract sbyte TypeCode { get; }
|
||||
|
||||
protected void CopyDeserializeUnsafe(ReadOnlySpan<byte> src, Span<T> dest) => src.CopyTo(MemoryMarshal.Cast<T, byte>(dest));
|
||||
|
||||
public void Serialize(ref MessagePackWriter writer, T[] value, MessagePackSerializerOptions options)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
writer.WriteNil();
|
||||
return;
|
||||
}
|
||||
|
||||
var byteLen = value.Length * Marshal.SizeOf<T>();
|
||||
|
||||
writer.WriteExtensionFormatHeader(new ExtensionHeader(this.TypeCode, byteLen));
|
||||
writer.Write(byteLen); // write original header(not array header)
|
||||
writer.Write(BitConverter.IsLittleEndian);
|
||||
writer.WriteRaw(MemoryMarshal.Cast<T, byte>(value));
|
||||
}
|
||||
|
||||
public T[] Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
|
||||
{
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
ExtensionHeader header = reader.ReadExtensionFormatHeader();
|
||||
if (header.TypeCode != this.TypeCode)
|
||||
{
|
||||
throw new InvalidOperationException("Invalid typeCode.");
|
||||
}
|
||||
|
||||
var byteLength = reader.ReadInt32();
|
||||
var isLittleEndian = reader.ReadBoolean();
|
||||
|
||||
// Allocate a T[] that we will return. We'll then cast the T[] as byte[] so we can copy the byte sequence directly into it.
|
||||
var result = new T[byteLength / Marshal.SizeOf<T>()];
|
||||
Span<byte> resultAsBytes = MemoryMarshal.Cast<T, byte>(result);
|
||||
reader.ReadRaw(byteLength).CopyTo(resultAsBytes);
|
||||
|
||||
// Reverse the byte order if necessary.
|
||||
if (isLittleEndian != BitConverter.IsLittleEndian)
|
||||
{
|
||||
for (int i = 0, j = resultAsBytes.Length - 1; i < j; i++, j--)
|
||||
{
|
||||
byte tmp = resultAsBytes[i];
|
||||
resultAsBytes[i] = resultAsBytes[j];
|
||||
resultAsBytes[j] = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public class Vector2ArrayBlitFormatter : UnsafeBlitFormatterBase<Vector2>
|
||||
{
|
||||
protected override sbyte TypeCode
|
||||
{
|
||||
get
|
||||
{
|
||||
return ThisLibraryExtensionTypeCodes.UnityVector2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class Vector3ArrayBlitFormatter : UnsafeBlitFormatterBase<Vector3>
|
||||
{
|
||||
protected override sbyte TypeCode
|
||||
{
|
||||
get
|
||||
{
|
||||
return ThisLibraryExtensionTypeCodes.UnityVector3;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class Vector4ArrayBlitFormatter : UnsafeBlitFormatterBase<Vector4>
|
||||
{
|
||||
protected override sbyte TypeCode
|
||||
{
|
||||
get
|
||||
{
|
||||
return ThisLibraryExtensionTypeCodes.UnityVector4;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class QuaternionArrayBlitFormatter : UnsafeBlitFormatterBase<Quaternion>
|
||||
{
|
||||
protected override sbyte TypeCode
|
||||
{
|
||||
get
|
||||
{
|
||||
return ThisLibraryExtensionTypeCodes.UnityQuaternion;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class ColorArrayBlitFormatter : UnsafeBlitFormatterBase<Color>
|
||||
{
|
||||
protected override sbyte TypeCode
|
||||
{
|
||||
get
|
||||
{
|
||||
return ThisLibraryExtensionTypeCodes.UnityColor;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class BoundsArrayBlitFormatter : UnsafeBlitFormatterBase<Bounds>
|
||||
{
|
||||
protected override sbyte TypeCode
|
||||
{
|
||||
get
|
||||
{
|
||||
return ThisLibraryExtensionTypeCodes.UnityBounds;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class RectArrayBlitFormatter : UnsafeBlitFormatterBase<Rect>
|
||||
{
|
||||
protected override sbyte TypeCode
|
||||
{
|
||||
get
|
||||
{
|
||||
return ThisLibraryExtensionTypeCodes.UnityRect;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class IntArrayBlitFormatter : UnsafeBlitFormatterBase<int>
|
||||
{
|
||||
protected override sbyte TypeCode
|
||||
{
|
||||
get { return ThisLibraryExtensionTypeCodes.UnityInt; }
|
||||
}
|
||||
}
|
||||
|
||||
public class FloatArrayBlitFormatter : UnsafeBlitFormatterBase<float>
|
||||
{
|
||||
protected override sbyte TypeCode
|
||||
{
|
||||
get { return ThisLibraryExtensionTypeCodes.UnityFloat; }
|
||||
}
|
||||
}
|
||||
|
||||
public class DoubleArrayBlitFormatter : UnsafeBlitFormatterBase<double>
|
||||
{
|
||||
protected override sbyte TypeCode
|
||||
{
|
||||
get { return ThisLibraryExtensionTypeCodes.UnityDouble; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 2955ae5d4f31948548df2c805124f4c6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Loading…
Reference in a new issue