Something went wrong. Try again.
Modular scanner/radio client and multi-broadcast server. uniscan.dexrn.me
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106using System.Data;using DotNetty.Common.Utilities;using DotNetty.Transport.Bootstrapping;using DotNetty.Transport.Channels;using DotNetty.Transport.Channels.Sockets;using Serilog;using Shiki.Common.Result;using UniScan.Network.Client.Extensions;using UniScan.Network.Client.Remote.Connection;using UniScan.Network.Protocol.PayloadPart;using UniScan.Network.Request;using UniScan.Network.Socket;using UniScan.Network.Util;
namespace UniScan.Network.Client;
public class ClientSocket : ISocket{ public ILogger Logger => Log.ForContext<ClientSocket>(); private IEventLoopGroup? _group;
private readonly UniScanClientChannelInitializer _channelInitializer; public UniScanChannelInitializer ChannelInitializer => _channelInitializer; public ConnectionStateTracker ConnectionState { get; } = new();
private readonly IRemoteConnectionMethod _connectionMethod; public IChannel? Channel { get; private set; }
public bool Connected => Channel?.Active == true;
private readonly RequestManager _requestManager = new();
public ClientSocket(UniScanClientChannelInitializer channelInitializer, IRemoteConnectionMethod connectionMethod, IEventLoopGroup group) { ArgumentNullException.ThrowIfNull(channelInitializer); ArgumentNullException.ThrowIfNull(connectionMethod);
_connectionMethod = connectionMethod; _channelInitializer = channelInitializer; _group = group; }
public async Task StartAsync() { if (_connectionMethod is null) throw new NullReferenceException(nameof(_connectionMethod)); try { Bootstrap bs = new Bootstrap().Group(_group) .ConnectionMethod(_connectionMethod) .Handler(new ActionChannelInitializer<IChannel>((channel) => { IChannelPipeline pipeline = channel.Pipeline;
pipeline.AddFirst(ConnectionState);
pipeline.AddLast(ChannelInitializer); pipeline.AddLast(new ResponseHandler(_requestManager)); }));
Channel = await _connectionMethod.ConnectAsync(bs); } catch (Exception) { await StopAsync(); throw; } }
public async Task StopAsync() { if (_requestManager != null) await _requestManager.RejectAllAsync(new OperationCanceledException("Socket is shutting down")); if (Channel != null) { await Channel.CloseAsync(); Channel = null; } }
public async Task<bool> SendPacketAsync(IPacket packet) => await SendPacketAsync(Channel, packet);
public async Task<bool> SendPacketAsync(IChannel? channel, IPacket packet) { if (channel is not { Active: true }) return false;
await channel.WriteAndFlushAsync(packet); return true; }
public async Task<Result<TResponse, Exception>> SendRequestAsync<TResponse>( IRequestPayloadPart<TResponse> request, CancellationToken ct = default) where TResponse : IPacket, IResponsePayloadPart => await _requestManager.MakeRequestAsync(Channel, request, ct);
public async Task<Result<TResponse, Exception>> SendRequestAsync<TResponse>( IChannel? channel, IRequestPayloadPart<TResponse> request, CancellationToken ct = default) where TResponse : IPacket, IResponsePayloadPart { if (channel is not { Active: true }) return new Result<TResponse, Exception>(new ArgumentNullException(nameof(channel)));
return await _requestManager.MakeRequestAsync(channel, request, ct); }}