|
| 1 | +using System.Net; |
| 2 | +using System.Net.Sockets; |
| 3 | +using System.Security.Authentication; |
| 4 | +using System.Security.Cryptography.X509Certificates; |
| 5 | +using Fleck; |
| 6 | +using GenHTTP.Api.Protocol; |
| 7 | + |
| 8 | +namespace GenHTTP.Modules.Websockets.Handler; |
| 9 | + |
| 10 | +public sealed class WrappedSocket : ISocket |
| 11 | +{ |
| 12 | + |
| 13 | + #region Get-/Setters |
| 14 | + |
| 15 | + public Socket Socket { get; } |
| 16 | + |
| 17 | + public Stream Stream { get; } |
| 18 | + |
| 19 | + public CancellationTokenSource TokenSource { get; } |
| 20 | + |
| 21 | + public bool Connected => Socket.Connected; |
| 22 | + |
| 23 | + public bool NoDelay |
| 24 | + { |
| 25 | + get => Socket.NoDelay; |
| 26 | + set => Socket.NoDelay = value; |
| 27 | + } |
| 28 | + |
| 29 | + public string RemoteIpAddress => (Socket.RemoteEndPoint as IPEndPoint)?.Address.ToString() ?? string.Empty; |
| 30 | + |
| 31 | + public int RemotePort => (Socket.RemoteEndPoint as IPEndPoint)?.Port ?? -1; |
| 32 | + |
| 33 | + #endregion |
| 34 | + |
| 35 | + #region Initialization |
| 36 | + |
| 37 | + public WrappedSocket(UpgradeInfo upgradeInfo) |
| 38 | + { |
| 39 | + Socket = upgradeInfo.Socket; |
| 40 | + Stream = upgradeInfo.Stream; |
| 41 | + |
| 42 | + TokenSource = new(); |
| 43 | + } |
| 44 | + |
| 45 | + #endregion |
| 46 | + |
| 47 | + #region Functionality |
| 48 | + |
| 49 | + public async Task Send(byte[] buffer, Action callback, Action<Exception> error) |
| 50 | + { |
| 51 | + try |
| 52 | + { |
| 53 | + await Stream.WriteAsync(buffer.AsMemory(), TokenSource.Token); |
| 54 | + await Stream.FlushAsync(); |
| 55 | + |
| 56 | + callback(); |
| 57 | + } |
| 58 | + catch (Exception ex) |
| 59 | + { |
| 60 | + error(ex); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + public async Task<int> Receive(byte[] buffer, Action<int> callback, Action<Exception> error, int offset = 0) |
| 65 | + { |
| 66 | + try |
| 67 | + { |
| 68 | + var result = await Stream.ReadAsync(buffer.AsMemory(offset), TokenSource.Token); |
| 69 | + |
| 70 | + callback(result); |
| 71 | + |
| 72 | + return result; |
| 73 | + } |
| 74 | + catch (Exception ex) |
| 75 | + { |
| 76 | + error(ex); |
| 77 | + return -1; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + public void Close() => Dispose(); |
| 82 | + |
| 83 | + public void Dispose() |
| 84 | + { |
| 85 | + TokenSource.Cancel(); |
| 86 | + Stream.Dispose(); |
| 87 | + Socket.Dispose(); |
| 88 | + } |
| 89 | + |
| 90 | + #endregion |
| 91 | + |
| 92 | + #region Unnecessary stuff |
| 93 | + |
| 94 | + private const string NotRequiredByIntegration = "Not required by integration"; |
| 95 | + |
| 96 | + public EndPoint LocalEndPoint => throw new NotImplementedException(NotRequiredByIntegration); |
| 97 | + |
| 98 | + public Task<ISocket> Accept(Action<ISocket> callback, Action<Exception> error) |
| 99 | + => throw new NotImplementedException(NotRequiredByIntegration); |
| 100 | + |
| 101 | + public void Bind(EndPoint ipLocal) |
| 102 | + => throw new NotImplementedException(NotRequiredByIntegration); |
| 103 | + |
| 104 | + public void Listen(int backlog) |
| 105 | + => throw new NotImplementedException(NotRequiredByIntegration); |
| 106 | + |
| 107 | + public Task Authenticate(X509Certificate2 certificate, SslProtocols enabledSslProtocols, Action callback, Action<Exception> error) |
| 108 | + => throw new NotImplementedException(NotRequiredByIntegration); |
| 109 | + |
| 110 | + #endregion |
| 111 | + |
| 112 | +} |
0 commit comments