Switch to SignalR 5.0 and implement using better API

This commit is contained in:
Dean Herbert
2021-02-03 20:42:27 +09:00
parent 1380717ebb
commit e3d323989c
2 changed files with 20 additions and 26 deletions

View File

@ -1,8 +1,9 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Text;
using MessagePack;
using MessagePack.Formatters;
using osu.Framework.Bindables;
@ -11,59 +12,51 @@ namespace osu.Game.Online.API
{
public class ModSettingsDictionaryFormatter : IMessagePackFormatter<Dictionary<string, object>>
{
public int Serialize(ref byte[] bytes, int offset, Dictionary<string, object> value, IFormatterResolver formatterResolver)
public void Serialize(ref MessagePackWriter writer, Dictionary<string, object> value, MessagePackSerializerOptions options)
{
int startOffset = offset;
var primitiveFormatter = PrimitiveObjectFormatter.Instance;
offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, value.Count);
writer.WriteArrayHeader(value.Count);
foreach (var kvp in value)
{
offset += MessagePackBinary.WriteString(ref bytes, offset, kvp.Key);
var stringBytes = new ReadOnlySequence<byte>(Encoding.UTF8.GetBytes(kvp.Key));
writer.WriteString(in stringBytes);
switch (kvp.Value)
{
case Bindable<double> d:
offset += primitiveFormatter.Serialize(ref bytes, offset, d.Value, formatterResolver);
primitiveFormatter.Serialize(ref writer, d.Value, options);
break;
case Bindable<float> f:
offset += primitiveFormatter.Serialize(ref bytes, offset, f.Value, formatterResolver);
primitiveFormatter.Serialize(ref writer, f.Value, options);
break;
case Bindable<bool> b:
offset += primitiveFormatter.Serialize(ref bytes, offset, b.Value, formatterResolver);
primitiveFormatter.Serialize(ref writer, b.Value, options);
break;
default:
throw new ArgumentException("A setting was of a type not supported by the messagepack serialiser", nameof(bytes));
// fall back for non-bindable cases.
primitiveFormatter.Serialize(ref writer, kvp.Value, options);
break;
}
}
return offset - startOffset;
}
public Dictionary<string, object> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
public Dictionary<string, object> Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
{
int startOffset = offset;
var output = new Dictionary<string, object>();
int itemCount = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
offset += readSize;
int itemCount = reader.ReadArrayHeader();
for (int i = 0; i < itemCount; i++)
{
var key = MessagePackBinary.ReadString(bytes, offset, out readSize);
offset += readSize;
output[key] = PrimitiveObjectFormatter.Instance.Deserialize(bytes, offset, formatterResolver, out readSize);
offset += readSize;
output[reader.ReadString()] =
PrimitiveObjectFormatter.Instance.Deserialize(ref reader, options);
}
readSize = offset - startOffset;
return output;
}
}