|
| 1 | +using System.Net.Sockets; |
| 2 | + |
| 3 | +using sigtran.net.Core.Interfaces; |
| 4 | + |
| 5 | +namespace sigtran.net.Layers.SCTP; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Wraps a native SCTP socket as the SDK packet transport contract. |
| 9 | +/// </summary> |
| 10 | +public sealed class NativeSctpSocketAdapter : ISctpSocket |
| 11 | +{ |
| 12 | + private readonly Socket _socket; |
| 13 | + private readonly SctpConnectionOptions _options; |
| 14 | + private long _sentMessages; |
| 15 | + private long _receivedMessages; |
| 16 | + private bool _disposed; |
| 17 | + private SctpAssociationState _associationState; |
| 18 | + |
| 19 | + /// <summary>Creates a native SCTP socket adapter.</summary> |
| 20 | + /// <param name="socket">The native SCTP socket.</param> |
| 21 | + /// <param name="options">The SCTP connection options.</param> |
| 22 | + /// <param name="associationState">The initial association state.</param> |
| 23 | + public NativeSctpSocketAdapter( |
| 24 | + Socket socket, |
| 25 | + SctpConnectionOptions options, |
| 26 | + SctpAssociationState associationState = SctpAssociationState.Closed) |
| 27 | + { |
| 28 | + _socket = socket ?? throw new ArgumentNullException(nameof(socket)); |
| 29 | + _options = options ?? throw new ArgumentNullException(nameof(options)); |
| 30 | + _associationState = associationState; |
| 31 | + } |
| 32 | + |
| 33 | + /// <summary>The current association state.</summary> |
| 34 | + public SctpAssociationState AssociationState => _disposed ? SctpAssociationState.Closed : _associationState; |
| 35 | + |
| 36 | + /// <summary>Marks the association as established.</summary> |
| 37 | + public void MarkEstablished() |
| 38 | + { |
| 39 | + ThrowIfDisposed(); |
| 40 | + _associationState = SctpAssociationState.Established; |
| 41 | + } |
| 42 | + |
| 43 | + /// <summary>Marks the association as failed.</summary> |
| 44 | + public void MarkFailed() |
| 45 | + { |
| 46 | + if (!_disposed) |
| 47 | + { |
| 48 | + _associationState = SctpAssociationState.Failed; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /// <inheritdoc /> |
| 53 | + public async Task SendAsync(ReadOnlyMemory<byte> data, CancellationToken ct = default) |
| 54 | + { |
| 55 | + ThrowIfDisposed(); |
| 56 | + if (data.IsEmpty) |
| 57 | + { |
| 58 | + throw new ArgumentException("SCTP payload must not be empty.", nameof(data)); |
| 59 | + } |
| 60 | + |
| 61 | + int sent = await _socket.SendAsync(data, SocketFlags.None, ct).ConfigureAwait(false); |
| 62 | + if (sent != data.Length) |
| 63 | + { |
| 64 | + throw new InvalidDataException($"Native SCTP send wrote {sent} bytes for a {data.Length} byte message."); |
| 65 | + } |
| 66 | + |
| 67 | + Interlocked.Increment(ref _sentMessages); |
| 68 | + } |
| 69 | + |
| 70 | + /// <inheritdoc /> |
| 71 | + public async Task<int> ReceiveAsync(Memory<byte> buffer, CancellationToken ct = default) |
| 72 | + { |
| 73 | + ThrowIfDisposed(); |
| 74 | + if (buffer.IsEmpty) |
| 75 | + { |
| 76 | + throw new ArgumentException("Receive buffer must not be empty.", nameof(buffer)); |
| 77 | + } |
| 78 | + |
| 79 | + int received = await _socket.ReceiveAsync(buffer, SocketFlags.None, ct).ConfigureAwait(false); |
| 80 | + if (received > 0) |
| 81 | + { |
| 82 | + Interlocked.Increment(ref _receivedMessages); |
| 83 | + } |
| 84 | + |
| 85 | + return received; |
| 86 | + } |
| 87 | + |
| 88 | + /// <summary>Captures a native SCTP transport health snapshot.</summary> |
| 89 | + /// <returns>The transport health snapshot.</returns> |
| 90 | + public SctpTransportHealth GetHealthSnapshot() |
| 91 | + { |
| 92 | + return new( |
| 93 | + AssociationState, |
| 94 | + _options.RemoteEndpoint, |
| 95 | + _options.LocalEndpoint, |
| 96 | + _options.OutboundStreams, |
| 97 | + _options.InboundStreams, |
| 98 | + _options.DefaultPayloadProtocolIdentifier, |
| 99 | + Interlocked.Read(ref _sentMessages), |
| 100 | + Interlocked.Read(ref _receivedMessages)); |
| 101 | + } |
| 102 | + |
| 103 | + /// <inheritdoc /> |
| 104 | + public void Dispose() |
| 105 | + { |
| 106 | + if (_disposed) |
| 107 | + { |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + _disposed = true; |
| 112 | + _associationState = SctpAssociationState.Closed; |
| 113 | + _socket.Dispose(); |
| 114 | + } |
| 115 | + |
| 116 | + private void ThrowIfDisposed() |
| 117 | + { |
| 118 | + if (_disposed) |
| 119 | + { |
| 120 | + throw new ObjectDisposedException(nameof(NativeSctpSocketAdapter)); |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments