Skip to content

Commit

Permalink
more link events
Browse files Browse the repository at this point in the history
  • Loading branch information
caunt committed Jul 26, 2024
1 parent 33dd93e commit f8d34d3
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 85 deletions.
73 changes: 0 additions & 73 deletions Void.sln

This file was deleted.

14 changes: 14 additions & 0 deletions Void.slnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Solution>
<Project Path="src\Void.NBT\Void.NBT.csproj" />
<Project Path="src\Void.Proxy-before\Void.Proxy-before.csproj" />
<Project Path="src\Void.Proxy.API\Void.Proxy.API.csproj" />
<Project Path="src\Void.Proxy.Plugins.ExamplePlugin\Void.Proxy.Plugins.ExamplePlugin.csproj" />
<Project Path="src\Void.Proxy.Plugins.ModsSupport.Forge\Void.Proxy.Plugins.ModsSupport.Forge.csproj" />
<Project Path="src\Void.Proxy.Plugins.ProtocolSupport.Java.v1_13_to_1_20_1\Void.Proxy.Plugins.ProtocolSupport.Java.v1_13_to_1_20_1.csproj" />
<Project Path="src\Void.Proxy.Plugins.ProtocolSupport.Java.v1_20_2_to_latest\Void.Proxy.Plugins.ProtocolSupport.Java.v1_20_2_to_latest.csproj" />
<Project Path="src\Void.Proxy.Plugins.ProtocolSupport.Java.v1_7_2_to_1_12_2\Void.Proxy.Plugins.ProtocolSupport.Java.v1_7_2_to_1_12_2.csproj" />
<Project Path="src\Void.Proxy\Void.Proxy.csproj" />
<Properties Name="Visual Studio">
<Property Name="OpenWith" Value="17" />
</Properties>
</Solution>
4 changes: 2 additions & 2 deletions src/Void.Proxy-before/Void.Proxy-before.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
<PackageReference Include="ini-parser-netstandard" Version="2.5.2" />
<PackageReference Include="NET.Minecraft.Component" Version="1.0.5" />
<PackageReference Include="Nito.AsyncEx" Version="5.1.2" />
<PackageReference Include="Serilog" Version="3.1.2-dev-02097" />
<PackageReference Include="Serilog" Version="4.0.1" />
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.1-dev-10370" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageReference Include="Zlib.Portable.Signed" Version="1.11.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Void.Proxy.API.Events.Links;

public class StartLinkEvent : IEvent
public class LinkStartedEvent : IEvent
{
public required ILink Link { get; init; }
}
8 changes: 8 additions & 0 deletions src/Void.Proxy.API/Events/Links/LinkStartingEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Void.Proxy.API.Links;

namespace Void.Proxy.API.Events.Links;

public class LinkStartingEvent : IEvent
{
public required ILink Link { get; init; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Void.Proxy.API.Events.Links;

public class StopLinkEvent : IEvent
public class LinkStoppedEvent : IEvent
{
public required ILink Link { get; init; }
}
8 changes: 8 additions & 0 deletions src/Void.Proxy.API/Events/Links/LinkStoppingEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Void.Proxy.API.Links;

namespace Void.Proxy.API.Events.Links;

public class LinkStoppingEvent : IEvent
{
public required ILink Link { get; init; }
}
5 changes: 3 additions & 2 deletions src/Void.Proxy.API/Links/ILink.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using Void.Proxy.API.Network.IO.Channels;
using Void.Proxy.API.Events;
using Void.Proxy.API.Network.IO.Channels;
using Void.Proxy.API.Players;
using Void.Proxy.API.Servers;

namespace Void.Proxy.API.Links;

public interface ILink : IAsyncDisposable
public interface ILink : IEventListener, IAsyncDisposable
{
public IPlayer Player { get; }
public IServer Server { get; }
Expand Down
2 changes: 1 addition & 1 deletion src/Void.Proxy.API/Void.Proxy.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.0-preview.6.24327.7" />
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.0-preview.6.24327.7" />
<PackageReference Include="Nito.AsyncEx" Version="5.1.2" />
<PackageReference Include="Serilog" Version="4.0.1-dev-02205" />
<PackageReference Include="Serilog" Version="4.0.1" />
</ItemGroup>

</Project>
3 changes: 1 addition & 2 deletions src/Void.Proxy/Links/Link.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Nito.AsyncEx;
using Void.Proxy.API.Events;
using Void.Proxy.API.Events.Links;
using Void.Proxy.API.Events.Services;
using Void.Proxy.API.Links;
Expand Down Expand Up @@ -139,7 +138,7 @@ protected async Task ExecuteAsync(IMinecraftChannel sourceChannel, IMinecraftCha
await PlayerChannel.FlushAsync();
await ServerChannel.FlushAsync();

_ = _events.ThrowAsync(new StopLinkEvent { Link = this }, forceCancellationToken);
_ = _events.ThrowAsync(new LinkStoppingEvent { Link = this }, forceCancellationToken);
}
}
}
10 changes: 7 additions & 3 deletions src/Void.Proxy/Links/LinkService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,27 @@ public async ValueTask ConnectPlayerAnywhereAsync(IPlayer player)
var serverChannel = await player.BuildServerChannelAsync(server);

var link = await CreateLinkAsync(player, server, playerChannel, serverChannel);

_events.RegisterListeners(link);
_links.Add(link);

_logger.LogInformation("Started forwarding {Link} traffic", link);
await _events.ThrowAsync(new LinkStartedEvent { Link = link });
}

[Subscribe]
public async ValueTask OnStopLinkEvent(StopLinkEvent @event)
public async ValueTask OnStopLinkEvent(LinkStoppingEvent @event)
{
if (!_links.Remove(@event.Link))
return;

await using var _ = @event.Link;
await @event.Link.DisposeAsync();

if (@event.Link.IsAlive)
throw new Exception($"Link {@event.Link} is still alive");

_logger.LogInformation("Stopped forwarding {Link} traffic", @event.Link);
await _events.ThrowAsync(new LinkStoppedEvent { Link = @event.Link });
}

private async ValueTask<ILink> CreateLinkAsync(IPlayer player, IServer server, IMinecraftChannel playerChannel, IMinecraftChannel serverChannel)
Expand All @@ -66,7 +70,7 @@ private async ValueTask<ILink> CreateLinkAsync(IPlayer player, IServer server, I

var link = @event.Result ?? new Link(player, server, playerChannel, serverChannel, _events);

await _events.ThrowAsync(new StartLinkEvent { Link = link });
await _events.ThrowAsync(new LinkStartingEvent { Link = link });

return link;
}
Expand Down

0 comments on commit f8d34d3

Please sign in to comment.