-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
105 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using Void.Proxy.API.Links; | ||
using Void.Proxy.API.Network.IO.Channels; | ||
using Void.Proxy.API.Players; | ||
using Void.Proxy.API.Servers; | ||
|
||
namespace Void.Proxy.API.Events.Links; | ||
|
||
public class CreateLinkEvent : IEventWithResult<ILink> | ||
{ | ||
public required IPlayer Player { get; init; } | ||
public required IServer Server { get; init; } | ||
public required IMinecraftChannel PlayerChannel { get; init; } | ||
public required IMinecraftChannel ServerChannel { get; init; } | ||
public ILink? Result { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 StartLinkEvent : IEvent | ||
{ | ||
public required ILink Link { get; init; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 StopLinkEvent : IEvent | ||
{ | ||
public required ILink Link { get; init; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,73 @@ | ||
using Void.Proxy.API.Links; | ||
using Void.Proxy.API.Events; | ||
using Void.Proxy.API.Events.Links; | ||
using Void.Proxy.API.Events.Services; | ||
using Void.Proxy.API.Links; | ||
using Void.Proxy.API.Network.IO.Channels; | ||
using Void.Proxy.API.Players; | ||
using Void.Proxy.API.Servers; | ||
|
||
namespace Void.Proxy.Links; | ||
|
||
public class LinkService( | ||
ILogger<LinkService> logger, | ||
IServerService servers) : ILinkService | ||
public class LinkService : ILinkService, IEventListener | ||
{ | ||
private readonly List<ILink> _links = []; | ||
private readonly List<ILink> _links =[]; | ||
|
||
private readonly ILogger<LinkService> _logger; | ||
private readonly IServerService _servers; | ||
private readonly IEventService _events; | ||
|
||
public LinkService(ILogger<LinkService> logger, IServerService servers, IEventService events) | ||
{ | ||
_logger = logger; | ||
_servers = servers; | ||
_events = events; | ||
|
||
events.RegisterListeners(this); | ||
} | ||
|
||
public async ValueTask ConnectPlayerAnywhereAsync(IPlayer player) | ||
{ | ||
var server = servers.RegisteredServers.First(); | ||
var server = _servers.RegisteredServers.First(); | ||
|
||
var playerChannel = await player.GetChannelAsync(); | ||
var serverChannel = await player.BuildServerChannelAsync(server); | ||
|
||
var link = new Link(player, server, playerChannel, serverChannel, FinalizeAsync); | ||
var link = await CreateLinkAsync(player, server, playerChannel, serverChannel); | ||
_links.Add(link); | ||
|
||
logger.LogInformation("Started forwarding {Link} traffic", link); | ||
_logger.LogInformation("Started forwarding {Link} traffic", link); | ||
} | ||
|
||
private async ValueTask FinalizeAsync(ILink link) | ||
[Subscribe] | ||
public async ValueTask OnStopLinkEvent(StopLinkEvent @event) | ||
{ | ||
if (!_links.Remove(link)) | ||
if (!_links.Remove(@event.Link)) | ||
return; | ||
|
||
await link.DisposeAsync(); | ||
await using var _ = @event.Link; | ||
|
||
if (@event.Link.IsAlive) | ||
throw new Exception($"Link {@event.Link} is still alive"); | ||
|
||
if (link.IsAlive) | ||
throw new Exception($"Link {link} is still alive"); | ||
_logger.LogInformation("Stopped forwarding {Link} traffic", @event.Link); | ||
} | ||
|
||
private async ValueTask<ILink> CreateLinkAsync(IPlayer player, IServer server, IMinecraftChannel playerChannel, IMinecraftChannel serverChannel) | ||
{ | ||
var @event = new CreateLinkEvent | ||
{ | ||
Player = player, | ||
Server = server, | ||
PlayerChannel = playerChannel, | ||
ServerChannel = serverChannel | ||
}; | ||
|
||
await _events.ThrowAsync(@event); | ||
|
||
var link = @event.Result ?? new Link(player, server, playerChannel, serverChannel, _events); | ||
|
||
logger.LogInformation("Stopped forwarding {Link} traffic", link); | ||
await _events.ThrowAsync(new StartLinkEvent { Link = link }); | ||
|
||
return link; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters