From f8d34d354c5f90205b906c005884e3317cd82d8f Mon Sep 17 00:00:00 2001 From: Vitalii Kotenko Date: Fri, 26 Jul 2024 19:51:18 +0300 Subject: [PATCH] more link events --- Void.sln | 73 ------------------- Void.slnx | 14 ++++ .../Void.Proxy-before.csproj | 4 +- ...{StartLinkEvent.cs => LinkStartedEvent.cs} | 2 +- .../Events/Links/LinkStartingEvent.cs | 8 ++ .../{StopLinkEvent.cs => LinkStoppedEvent.cs} | 2 +- .../Events/Links/LinkStoppingEvent.cs | 8 ++ src/Void.Proxy.API/Links/ILink.cs | 5 +- src/Void.Proxy.API/Void.Proxy.API.csproj | 2 +- src/Void.Proxy/Links/Link.cs | 3 +- src/Void.Proxy/Links/LinkService.cs | 10 ++- 11 files changed, 46 insertions(+), 85 deletions(-) delete mode 100644 Void.sln create mode 100644 Void.slnx rename src/Void.Proxy.API/Events/Links/{StartLinkEvent.cs => LinkStartedEvent.cs} (75%) create mode 100644 src/Void.Proxy.API/Events/Links/LinkStartingEvent.cs rename src/Void.Proxy.API/Events/Links/{StopLinkEvent.cs => LinkStoppedEvent.cs} (75%) create mode 100644 src/Void.Proxy.API/Events/Links/LinkStoppingEvent.cs diff --git a/Void.sln b/Void.sln deleted file mode 100644 index a6e9b03..0000000 --- a/Void.sln +++ /dev/null @@ -1,73 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# 17 -VisualStudioVersion = 17.8.34212.112 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Void.Proxy-before", "src\Void.Proxy-before\Void.Proxy-before.csproj", "{8EF40869-E6E1-4E1E-834E-4CCF76040444}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Void.NBT", "src\Void.NBT\Void.NBT.csproj", "{48C0447B-BE31-46D9-8EDC-5ED4EBC3965C}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Void.Proxy", "src\Void.Proxy\Void.Proxy.csproj", "{D54116EE-75EA-491C-84E3-F1DE82A45728}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Void.Proxy.API", "src\Void.Proxy.API\Void.Proxy.API.csproj", "{E042DC85-7EFC-495E-9426-ECE20BF28B0E}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Void.Proxy.Plugins.ExamplePlugin", "src\Void.Proxy.Plugins.ExamplePlugin\Void.Proxy.Plugins.ExamplePlugin.csproj", "{81E0F934-36EA-49C3-BBAA-3459E37E5D7C}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Void.Proxy.Plugins.ProtocolSupport.Java.v1_20_2_to_latest", "src\Void.Proxy.Plugins.ProtocolSupport.Java.v1_20_2_to_latest\Void.Proxy.Plugins.ProtocolSupport.Java.v1_20_2_to_latest.csproj", "{0F6BA9E5-EE31-46D5-B000-29940A1D79A9}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Void.Proxy.Plugins.ProtocolSupport.Java.v1_13_to_1_20_1", "src\Void.Proxy.Plugins.ProtocolSupport.Java.v1_13_to_1_20_1\Void.Proxy.Plugins.ProtocolSupport.Java.v1_13_to_1_20_1.csproj", "{24C6F3E8-C33E-479A-96C3-F8DAAF097E4B}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Void.Proxy.Plugins.ProtocolSupport.Java.v1_7_2_to_1_12_2", "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", "{1056EA02-B6CF-491A-B9C8-76CAEDFB35F7}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Void.Proxy.Plugins.ModsSupport.Forge", "src\Void.Proxy.Plugins.ModsSupport.Forge\Void.Proxy.Plugins.ModsSupport.Forge.csproj", "{72BD4B67-9ED0-43FE-86C9-E9A744F702E7}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {8EF40869-E6E1-4E1E-834E-4CCF76040444}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {8EF40869-E6E1-4E1E-834E-4CCF76040444}.Debug|Any CPU.Build.0 = Debug|Any CPU - {8EF40869-E6E1-4E1E-834E-4CCF76040444}.Release|Any CPU.ActiveCfg = Release|Any CPU - {8EF40869-E6E1-4E1E-834E-4CCF76040444}.Release|Any CPU.Build.0 = Release|Any CPU - {48C0447B-BE31-46D9-8EDC-5ED4EBC3965C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {48C0447B-BE31-46D9-8EDC-5ED4EBC3965C}.Debug|Any CPU.Build.0 = Debug|Any CPU - {48C0447B-BE31-46D9-8EDC-5ED4EBC3965C}.Release|Any CPU.ActiveCfg = Release|Any CPU - {48C0447B-BE31-46D9-8EDC-5ED4EBC3965C}.Release|Any CPU.Build.0 = Release|Any CPU - {D54116EE-75EA-491C-84E3-F1DE82A45728}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {D54116EE-75EA-491C-84E3-F1DE82A45728}.Debug|Any CPU.Build.0 = Debug|Any CPU - {D54116EE-75EA-491C-84E3-F1DE82A45728}.Release|Any CPU.ActiveCfg = Release|Any CPU - {D54116EE-75EA-491C-84E3-F1DE82A45728}.Release|Any CPU.Build.0 = Release|Any CPU - {E042DC85-7EFC-495E-9426-ECE20BF28B0E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {E042DC85-7EFC-495E-9426-ECE20BF28B0E}.Debug|Any CPU.Build.0 = Debug|Any CPU - {E042DC85-7EFC-495E-9426-ECE20BF28B0E}.Release|Any CPU.ActiveCfg = Release|Any CPU - {E042DC85-7EFC-495E-9426-ECE20BF28B0E}.Release|Any CPU.Build.0 = Release|Any CPU - {81E0F934-36EA-49C3-BBAA-3459E37E5D7C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {81E0F934-36EA-49C3-BBAA-3459E37E5D7C}.Debug|Any CPU.Build.0 = Debug|Any CPU - {81E0F934-36EA-49C3-BBAA-3459E37E5D7C}.Release|Any CPU.ActiveCfg = Release|Any CPU - {81E0F934-36EA-49C3-BBAA-3459E37E5D7C}.Release|Any CPU.Build.0 = Release|Any CPU - {0F6BA9E5-EE31-46D5-B000-29940A1D79A9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {0F6BA9E5-EE31-46D5-B000-29940A1D79A9}.Debug|Any CPU.Build.0 = Debug|Any CPU - {0F6BA9E5-EE31-46D5-B000-29940A1D79A9}.Release|Any CPU.ActiveCfg = Release|Any CPU - {0F6BA9E5-EE31-46D5-B000-29940A1D79A9}.Release|Any CPU.Build.0 = Release|Any CPU - {24C6F3E8-C33E-479A-96C3-F8DAAF097E4B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {24C6F3E8-C33E-479A-96C3-F8DAAF097E4B}.Debug|Any CPU.Build.0 = Debug|Any CPU - {24C6F3E8-C33E-479A-96C3-F8DAAF097E4B}.Release|Any CPU.ActiveCfg = Release|Any CPU - {24C6F3E8-C33E-479A-96C3-F8DAAF097E4B}.Release|Any CPU.Build.0 = Release|Any CPU - {1056EA02-B6CF-491A-B9C8-76CAEDFB35F7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {1056EA02-B6CF-491A-B9C8-76CAEDFB35F7}.Debug|Any CPU.Build.0 = Debug|Any CPU - {1056EA02-B6CF-491A-B9C8-76CAEDFB35F7}.Release|Any CPU.ActiveCfg = Release|Any CPU - {1056EA02-B6CF-491A-B9C8-76CAEDFB35F7}.Release|Any CPU.Build.0 = Release|Any CPU - {72BD4B67-9ED0-43FE-86C9-E9A744F702E7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {72BD4B67-9ED0-43FE-86C9-E9A744F702E7}.Debug|Any CPU.Build.0 = Debug|Any CPU - {72BD4B67-9ED0-43FE-86C9-E9A744F702E7}.Release|Any CPU.ActiveCfg = Release|Any CPU - {72BD4B67-9ED0-43FE-86C9-E9A744F702E7}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {C3C28736-41CC-4E17-B043-99E29D9E1122} - EndGlobalSection -EndGlobal diff --git a/Void.slnx b/Void.slnx new file mode 100644 index 0000000..0cff4d5 --- /dev/null +++ b/Void.slnx @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + diff --git a/src/Void.Proxy-before/Void.Proxy-before.csproj b/src/Void.Proxy-before/Void.Proxy-before.csproj index 3a5e401..fe034c8 100644 --- a/src/Void.Proxy-before/Void.Proxy-before.csproj +++ b/src/Void.Proxy-before/Void.Proxy-before.csproj @@ -23,9 +23,9 @@ - + - + diff --git a/src/Void.Proxy.API/Events/Links/StartLinkEvent.cs b/src/Void.Proxy.API/Events/Links/LinkStartedEvent.cs similarity index 75% rename from src/Void.Proxy.API/Events/Links/StartLinkEvent.cs rename to src/Void.Proxy.API/Events/Links/LinkStartedEvent.cs index 290c875..8117090 100644 --- a/src/Void.Proxy.API/Events/Links/StartLinkEvent.cs +++ b/src/Void.Proxy.API/Events/Links/LinkStartedEvent.cs @@ -2,7 +2,7 @@ namespace Void.Proxy.API.Events.Links; -public class StartLinkEvent : IEvent +public class LinkStartedEvent : IEvent { public required ILink Link { get; init; } } \ No newline at end of file diff --git a/src/Void.Proxy.API/Events/Links/LinkStartingEvent.cs b/src/Void.Proxy.API/Events/Links/LinkStartingEvent.cs new file mode 100644 index 0000000..d1da9d6 --- /dev/null +++ b/src/Void.Proxy.API/Events/Links/LinkStartingEvent.cs @@ -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; } +} \ No newline at end of file diff --git a/src/Void.Proxy.API/Events/Links/StopLinkEvent.cs b/src/Void.Proxy.API/Events/Links/LinkStoppedEvent.cs similarity index 75% rename from src/Void.Proxy.API/Events/Links/StopLinkEvent.cs rename to src/Void.Proxy.API/Events/Links/LinkStoppedEvent.cs index dc9a280..d6b782c 100644 --- a/src/Void.Proxy.API/Events/Links/StopLinkEvent.cs +++ b/src/Void.Proxy.API/Events/Links/LinkStoppedEvent.cs @@ -2,7 +2,7 @@ namespace Void.Proxy.API.Events.Links; -public class StopLinkEvent : IEvent +public class LinkStoppedEvent : IEvent { public required ILink Link { get; init; } } \ No newline at end of file diff --git a/src/Void.Proxy.API/Events/Links/LinkStoppingEvent.cs b/src/Void.Proxy.API/Events/Links/LinkStoppingEvent.cs new file mode 100644 index 0000000..4e6e410 --- /dev/null +++ b/src/Void.Proxy.API/Events/Links/LinkStoppingEvent.cs @@ -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; } +} \ No newline at end of file diff --git a/src/Void.Proxy.API/Links/ILink.cs b/src/Void.Proxy.API/Links/ILink.cs index 1c73bb1..9bf355c 100644 --- a/src/Void.Proxy.API/Links/ILink.cs +++ b/src/Void.Proxy.API/Links/ILink.cs @@ -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; } diff --git a/src/Void.Proxy.API/Void.Proxy.API.csproj b/src/Void.Proxy.API/Void.Proxy.API.csproj index 2d2b083..7f3f832 100644 --- a/src/Void.Proxy.API/Void.Proxy.API.csproj +++ b/src/Void.Proxy.API/Void.Proxy.API.csproj @@ -11,7 +11,7 @@ - + diff --git a/src/Void.Proxy/Links/Link.cs b/src/Void.Proxy/Links/Link.cs index 159fc03..5c1d537 100644 --- a/src/Void.Proxy/Links/Link.cs +++ b/src/Void.Proxy/Links/Link.cs @@ -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; @@ -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); } } } \ No newline at end of file diff --git a/src/Void.Proxy/Links/LinkService.cs b/src/Void.Proxy/Links/LinkService.cs index 05404f0..3bb172a 100644 --- a/src/Void.Proxy/Links/LinkService.cs +++ b/src/Void.Proxy/Links/LinkService.cs @@ -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 CreateLinkAsync(IPlayer player, IServer server, IMinecraftChannel playerChannel, IMinecraftChannel serverChannel) @@ -66,7 +70,7 @@ private async ValueTask 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; }