| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using IceRpc.Transports.Tcp.Internal; |
| | | 4 | | using System.Net.Security; |
| | | 5 | | |
| | | 6 | | namespace IceRpc.Transports.Tcp; |
| | | 7 | | |
| | | 8 | | /// <summary>Implements <see cref="IDuplexServerTransport" /> for the tcp transport.</summary> |
| | | 9 | | public class TcpServerTransport : IDuplexServerTransport |
| | | 10 | | { |
| | | 11 | | /// <inheritdoc/> |
| | 11 | 12 | | public string DefaultName => "tcp"; |
| | | 13 | | |
| | | 14 | | private readonly TcpServerTransportOptions _options; |
| | | 15 | | |
| | | 16 | | /// <summary>Constructs a <see cref="TcpServerTransport" />.</summary> |
| | | 17 | | public TcpServerTransport() |
| | 27 | 18 | | : this(new TcpServerTransportOptions()) |
| | 27 | 19 | | { |
| | 27 | 20 | | } |
| | | 21 | | |
| | | 22 | | /// <summary>Constructs a <see cref="TcpServerTransport" />.</summary> |
| | | 23 | | /// <param name="options">The transport options.</param> |
| | 254 | 24 | | public TcpServerTransport(TcpServerTransportOptions options) => _options = options; |
| | | 25 | | |
| | | 26 | | /// <inheritdoc/> |
| | | 27 | | public IListener<IDuplexConnection> Listen( |
| | | 28 | | TransportAddress transportAddress, |
| | | 29 | | DuplexConnectionOptions options, |
| | | 30 | | SslServerAuthenticationOptions? serverAuthenticationOptions) |
| | 130 | 31 | | { |
| | | 32 | | // "ssl" is only accepted for the ice protocol, identified by the ALPN. |
| | 130 | 33 | | if (transportAddress.TransportName == "ssl") |
| | 2 | 34 | | { |
| | 2 | 35 | | if (serverAuthenticationOptions is null) |
| | 0 | 36 | | { |
| | 0 | 37 | | throw new ArgumentNullException( |
| | 0 | 38 | | nameof(serverAuthenticationOptions), |
| | 0 | 39 | | "The SSL server transport requires the SSL server authentication options to be set."); |
| | | 40 | | } |
| | 2 | 41 | | else if (serverAuthenticationOptions?.ApplicationProtocols |
| | 2 | 42 | | is not List<SslApplicationProtocol> alpnProtocols || |
| | 2 | 43 | | alpnProtocols.Count != 1 || |
| | 2 | 44 | | alpnProtocols[0] != new SslApplicationProtocol("ice")) |
| | 1 | 45 | | { |
| | 1 | 46 | | throw new NotSupportedException( |
| | 1 | 47 | | "The 'ssl' transport name is only supported with the ice protocol."); |
| | | 48 | | } |
| | 1 | 49 | | } |
| | 128 | 50 | | else if (transportAddress.TransportName is string name && name != "tcp") |
| | 1 | 51 | | { |
| | 1 | 52 | | throw new NotSupportedException($"The TCP server transport does not support transport '{name}'."); |
| | | 53 | | } |
| | | 54 | | |
| | 128 | 55 | | if (transportAddress.Params.Count > 0) |
| | 4 | 56 | | { |
| | 4 | 57 | | throw new ArgumentException( |
| | 4 | 58 | | "The transport address contains parameters that are not valid for the TCP server transport.", |
| | 4 | 59 | | nameof(transportAddress)); |
| | | 60 | | } |
| | | 61 | | |
| | 124 | 62 | | return new TcpListener(transportAddress, options, serverAuthenticationOptions, _options); |
| | 119 | 63 | | } |
| | | 64 | | } |