Browse Source

Embed Metsys.Bson as a serializer to avoid referencing JSON.NET

pull/1105/head
Nikita Tsukanov 9 years ago
parent
commit
a5f0d23ccb
  1. 1
      src/Avalonia.DotNetFrameworkRuntime/Avalonia.DotNetFrameworkRuntime.csproj
  2. 3
      src/Avalonia.Remote.Protocol/Avalonia.Remote.Protocol.csproj
  3. 13
      src/Avalonia.Remote.Protocol/BsonStreamTransport.cs
  4. 1632
      src/Avalonia.Remote.Protocol/MetsysBson.cs

1
src/Avalonia.DotNetFrameworkRuntime/Avalonia.DotNetFrameworkRuntime.csproj

@ -10,7 +10,6 @@
<Compile Include="..\Shared\SharedAssemblyInfo.cs"> <Compile Include="..\Shared\SharedAssemblyInfo.cs">
<Link>Properties\SharedAssemblyInfo.cs</Link> <Link>Properties\SharedAssemblyInfo.cs</Link>
</Compile> </Compile>
<Reference Include="System.Runtime"/>
<ProjectReference Include="..\Avalonia.Base\Avalonia.Base.csproj" /> <ProjectReference Include="..\Avalonia.Base\Avalonia.Base.csproj" />
<ProjectReference Include="..\Avalonia.Controls\Avalonia.Controls.csproj" /> <ProjectReference Include="..\Avalonia.Controls\Avalonia.Controls.csproj" />
<ProjectReference Include="..\Avalonia.Visuals\Avalonia.Visuals.csproj" /> <ProjectReference Include="..\Avalonia.Visuals\Avalonia.Visuals.csproj" />

3
src/Avalonia.Remote.Protocol/Avalonia.Remote.Protocol.csproj

@ -1,10 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard1.3</TargetFramework> <TargetFramework>netstandard2.0</TargetFramework>
<DefineConstants>AVALONIA_REMOTE_PROTOCOL;$(DefineConstants)</DefineConstants> <DefineConstants>AVALONIA_REMOTE_PROTOCOL;$(DefineConstants)</DefineConstants>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
<Compile Include="..\Avalonia.Input\Key.cs" /> <Compile Include="..\Avalonia.Input\Key.cs" />
</ItemGroup> </ItemGroup>
</Project> </Project>

13
src/Avalonia.Remote.Protocol/BsonStreamTransport.cs

@ -2,9 +2,8 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Threading; using System.Threading;
using Newtonsoft.Json;
using System.Threading.Tasks; using System.Threading.Tasks;
using Newtonsoft.Json.Bson; using Metsys.Bson;
namespace Avalonia.Remote.Protocol namespace Avalonia.Remote.Protocol
{ {
@ -20,8 +19,7 @@ namespace Avalonia.Remote.Protocol
private readonly object _lock = new object(); private readonly object _lock = new object();
private bool _writeOperationPending; private bool _writeOperationPending;
private bool _readingAlreadyStarted; private bool _readingAlreadyStarted;
private bool _writerIsBroken; private bool _writerIsBroken;
static readonly JsonSerializer Serializer = new JsonSerializer();
private static readonly byte[] ZeroLength = new byte[4]; private static readonly byte[] ZeroLength = new byte[4];
public BsonStreamTransportConnection(IMessageTypeResolver resolver, Stream inputStream, Stream outputStream, Action disposeCallback) public BsonStreamTransportConnection(IMessageTypeResolver resolver, Stream inputStream, Stream outputStream, Action disposeCallback)
@ -79,7 +77,8 @@ namespace Avalonia.Remote.Protocol
var guid = new Guid(guidBytes); var guid = new Guid(guidBytes);
var buffer = new byte[length]; var buffer = new byte[length];
await ReadExact(buffer).ConfigureAwait(false); await ReadExact(buffer).ConfigureAwait(false);
var message = Serializer.Deserialize(new BsonReader(new MemoryStream(buffer)), _resolver.GetByGuid(guid)); var message = Deserializer.Deserialize(new BinaryReader(new MemoryStream(buffer)),
_resolver.GetByGuid(guid));
OnMessage?.Invoke(this, message); OnMessage?.Invoke(this, message);
} }
} }
@ -107,8 +106,8 @@ namespace Avalonia.Remote.Protocol
_outputBlock.SetLength(0); _outputBlock.SetLength(0);
_outputBlock.Write(ZeroLength, 0, 4); _outputBlock.Write(ZeroLength, 0, 4);
_outputBlock.Write(guid, 0, guid.Length); _outputBlock.Write(guid, 0, guid.Length);
var writer = new BsonWriter(_outputBlock); var serialized = Serializer.Serialize(data);
Serializer.Serialize(writer, data); _outputBlock.Write(serialized, 0, serialized.Length);
_outputBlock.Seek(0, SeekOrigin.Begin); _outputBlock.Seek(0, SeekOrigin.Begin);
var length = BitConverter.GetBytes((int)_outputBlock.Length - 20); var length = BitConverter.GetBytes((int)_outputBlock.Length - 20);
_outputBlock.Write(length, 0, length.Length); _outputBlock.Write(length, 0, length.Length);

1632
src/Avalonia.Remote.Protocol/MetsysBson.cs

File diff suppressed because it is too large
Loading…
Cancel
Save