1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 15:07:44 +08:00

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;
}
}

View File

@ -20,11 +20,12 @@
<ItemGroup Label="Package References">
<PackageReference Include="DiffPlex" Version="1.6.3" />
<PackageReference Include="Humanizer" Version="2.8.26" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="3.1.10" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.MessagePack" Version="3.1.11" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.NewtonsoftJson" Version="3.1.10" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="5.0.2" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.MessagePack" Version="5.0.2" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.NewtonsoftJson" Version="5.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.2.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="2.2.6" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.NETCore.Targets" Version="3.1.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="ppy.osu.Framework" Version="2021.128.0" />