Skip to content

Commit e24858c

Browse files
authored
Upgrade to .net core 2.0 & Some performance upgrade (#35)
* upgrade to .net core 2.0 * Performance upgrade for packet deserialization
1 parent 4b3e64d commit e24858c

38 files changed

+473
-164
lines changed

NuGet.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<packageSources>
66
<clear />
77
<add key="orleans-prerlease" value="https://dotnet.myget.org/F/orleans-prerelease/api/v3/index.json" />
8+
<add key="corefxlab" value="https://dotnet.myget.org/F/dotnet-corefxlab/api/v3/index.json"/>
89
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
910
<add key="imageSharp" value="https://www.myget.org/F/imagesharp/api/v3/index.json"/>
1011
</packageSources>

Readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
## 要求
1313
运行这个服务器之前,请保证你的环境满足:
1414

15-
* `.net core` sdk 1.1
15+
* `.net core` sdk 2.0
1616

1717
目前支持的 `minecraft` 版本:
1818

@@ -63,7 +63,7 @@ Press Ctrl+C to terminate...
6363
## Required
6464
Before running this server, please make sure your environment meet:
6565

66-
* `.net core` sdk 1.1
66+
* `.net core` sdk 2.0
6767

6868
Currently supported version of `minecraft`:
6969

src/MineCase.Algorithm/MineCase.Algorithm.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard1.0</TargetFramework>
4+
<TargetFramework>netstandard2.0</TargetFramework>
55
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
66
<CodeAnalysisRuleSet>../../build/Analyzers.ruleset</CodeAnalysisRuleSet>
77
</PropertyGroup>
88

99
<ItemGroup>
1010
<PackageReference Include="StyleCop.Analyzers" Version="1.1.0-beta004" PrivateAssets="All" />
1111
<PackageReference Include="System.Numerics.Vectors" Version="4.4.0" />
12-
<PackageReference Include="System.ValueTuple" Version="4.4.0" />
1312
</ItemGroup>
1413

1514
<ItemGroup>

src/MineCase.Gateway/MineCase.Gateway.csproj

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>netcoreapp1.1</TargetFramework>
5+
<TargetFramework>netcoreapp2.0</TargetFramework>
6+
<ServerGarbageCollection>true</ServerGarbageCollection>
7+
<ConcurrentGarbageCollection>true</ConcurrentGarbageCollection>
68
</PropertyGroup>
79

810
<ItemGroup>
@@ -20,10 +22,11 @@
2022
</ItemGroup>
2123

2224
<ItemGroup>
23-
<PackageReference Include="Microsoft.Extensions.Configuration" Version="1.1.2" />
24-
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.2" />
25-
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.2" />
26-
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.2" />
25+
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.0.0" />
26+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.0" />
27+
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0" />
28+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.0.0" />
29+
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="2.0.0" />
2730
<PackageReference Include="Microsoft.Orleans.Client" Version="2.0.0-preview2-20170724" />
2831
</ItemGroup>
2932

src/MineCase.Gateway/Network/ClientSession.cs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using MineCase.Protocol;
1+
using Microsoft.Extensions.ObjectPool;
2+
using MineCase.Protocol;
23
using MineCase.Server.Network;
34
using Orleans;
45
using System;
@@ -21,12 +22,14 @@ class ClientSession : IDisposable
2122
private readonly Guid _sessionId;
2223
private readonly OutcomingPacketObserver _outcomingPacketObserver;
2324
private readonly ActionBlock<UncompressedPacket> _outcomingPacketDispatcher;
25+
private readonly ObjectPool<UncompressedPacket> _uncompressedPacketObjectPool;
2426

25-
public ClientSession(TcpClient tcpClient, IGrainFactory grainFactory)
27+
public ClientSession(TcpClient tcpClient, IGrainFactory grainFactory, ObjectPool<UncompressedPacket> uncompressedPacketObjectPool)
2628
{
2729
_sessionId = Guid.NewGuid();
2830
_tcpClient = tcpClient;
2931
_grainFactory = grainFactory;
32+
_uncompressedPacketObjectPool = uncompressedPacketObjectPool;
3033
_outcomingPacketObserver = new OutcomingPacketObserver(this);
3134
_outcomingPacketDispatcher = new ActionBlock<UncompressedPacket>(SendOutcomingPacket);
3235
}
@@ -60,17 +63,24 @@ private void OnClosed()
6063

6164
private async Task DispatchIncomingPacket()
6265
{
63-
UncompressedPacket packet;
64-
if (_useCompression)
66+
var packet = _uncompressedPacketObjectPool.Get();
67+
try
6568
{
66-
var compressedPacket = await CompressedPacket.DeserializeAsync(_remoteStream);
67-
packet = PacketCompress.Decompress(ref compressedPacket);
69+
if (_useCompression)
70+
{
71+
var compressedPacket = await CompressedPacket.DeserializeAsync(_remoteStream, null);
72+
packet = PacketCompress.Decompress(ref compressedPacket);
73+
}
74+
else
75+
{
76+
packet = await UncompressedPacket.DeserializeAsync(_remoteStream, packet);
77+
}
78+
await DispatchIncomingPacket(packet);
6879
}
69-
else
80+
finally
7081
{
71-
packet = await UncompressedPacket.DeserializeAsync(_remoteStream);
82+
_uncompressedPacketObjectPool.Return(packet);
7283
}
73-
await DispatchIncomingPacket(packet);
7484
}
7585

7686
private async Task SendOutcomingPacket(UncompressedPacket packet)

src/MineCase.Gateway/Network/ConnectionRouter.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using Microsoft.Extensions.Logging;
2+
using Microsoft.Extensions.ObjectPool;
3+
using MineCase.Protocol;
24
using MineCase.Server.Settings;
35
using Orleans;
46
using System;
@@ -16,11 +18,13 @@ class ConnectionRouter
1618
private readonly TcpListener _listener;
1719
private readonly IGrainFactory _grainFactory;
1820
private readonly ILogger _logger;
21+
private readonly ObjectPool<UncompressedPacket> _uncompressedPacketObjectPool;
1922

20-
public ConnectionRouter(IGrainFactory grainFactory, ILoggerFactory loggerFactory)
23+
public ConnectionRouter(IGrainFactory grainFactory, ILoggerFactory loggerFactory, ObjectPool<UncompressedPacket> uncompressedPacketObjectPool)
2124
{
2225
_grainFactory = grainFactory;
2326
_logger = loggerFactory.CreateLogger<ConnectionRouter>();
27+
_uncompressedPacketObjectPool = uncompressedPacketObjectPool;
2428
_listener = new TcpListener(new IPEndPoint(IPAddress.Any, 25565));
2529
}
2630

@@ -40,7 +44,7 @@ private async void DispatchIncomingClient(TcpClient tcpClient, CancellationToken
4044
try
4145
{
4246
_logger.LogInformation($"Incoming connection from {tcpClient.Client.RemoteEndPoint}.");
43-
using (var session = new ClientSession(tcpClient, _grainFactory))
47+
using (var session = new ClientSession(tcpClient, _grainFactory, _uncompressedPacketObjectPool))
4448
{
4549
await session.Startup(cancellationToken);
4650
}

src/MineCase.Gateway/Program.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
using System.Threading;
77
using Microsoft.Extensions.Logging;
88
using MineCase.Gateway.Network;
9+
using Microsoft.Extensions.ObjectPool;
10+
using MineCase.Protocol;
911

1012
namespace MineCase.Gateway
1113
{
@@ -34,6 +36,7 @@ private static void ConfigureServices(IServiceCollection services)
3436
services.AddSingleton(ConfigureLogging());
3537
services.AddLogging();
3638
services.AddSingleton<ConnectionRouter>();
39+
ConfigureObjectPools(services);
3740
}
3841

3942
private static ILoggerFactory ConfigureLogging()
@@ -44,6 +47,16 @@ private static ILoggerFactory ConfigureLogging()
4447
return factory;
4548
}
4649

50+
private static void ConfigureObjectPools(IServiceCollection services)
51+
{
52+
services.AddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>();
53+
services.AddSingleton<ObjectPool<UncompressedPacket>>(s =>
54+
{
55+
var provider = s.GetRequiredService<ObjectPoolProvider>();
56+
return provider.Create<UncompressedPacket>();
57+
});
58+
}
59+
4760
private static IConfiguration LoadConfiguration()
4861
{
4962
var builder = new ConfigurationBuilder()

src/MineCase.NBT/MineCase.Nbt.csproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard1.0</TargetFramework>
4+
<TargetFramework>netstandard2.0</TargetFramework>
55
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
66
<CodeAnalysisRuleSet>../../build/Analyzers.ruleset</CodeAnalysisRuleSet>
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="System.Diagnostics.Contracts" Version="4.3.0" />
11-
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.3.0" />
10+
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.4.0" />
1211
<PackageReference Include="StyleCop.Analyzers" Version="1.1.0-beta004" PrivateAssets="All" />
1312
</ItemGroup>
1413

src/MineCase.Protocol/MineCase.Protocol.csproj

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard1.5</TargetFramework>
4+
<TargetFramework>netstandard2.0</TargetFramework>
55
<RootNamespace>MineCase</RootNamespace>
66
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
7+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
78
<CodeAnalysisRuleSet>../../build/Analyzers.ruleset</CodeAnalysisRuleSet>
89
</PropertyGroup>
910

1011
<ItemGroup>
1112
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
12-
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.3.0" />
13+
<PackageReference Include="System.Binary" Version="0.1.0-e170811-6" />
14+
<PackageReference Include="System.Buffers.Primitives" Version="0.1.0-e170811-6" />
15+
<PackageReference Include="System.Memory" Version="4.4.0-preview2-25405-01" />
16+
<PackageReference Include="System.Numerics.Vectors" Version="4.4.0" />
17+
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.4.0" />
1318
<PackageReference Include="Microsoft.Orleans.Core" Version="2.0.0-preview2-20170724" />
1419
<PackageReference Include="StyleCop.Analyzers" Version="1.1.0-beta004" PrivateAssets="All" />
1520
</ItemGroup>

src/MineCase.Protocol/Protocol/Handshaking/Handshake.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public sealed class Handshake
2323
[SerializeAs(DataType.VarInt)]
2424
public uint NextState;
2525

26-
public static Handshake Deserialize(BinaryReader br)
26+
public static Handshake Deserialize(ref SpanReader br)
2727
{
2828
return new Handshake
2929
{

0 commit comments

Comments
 (0)