| | 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/> |
| 218 | 12 | | public string Name => TcpName; |
| | 13 | |
|
| | 14 | | private const string SslName = "ssl"; |
| | 15 | | private const string TcpName = "tcp"; |
| | 16 | |
|
| | 17 | | private readonly TcpServerTransportOptions _options; |
| | 18 | |
|
| | 19 | | /// <summary>Constructs a <see cref="TcpServerTransport" />.</summary> |
| | 20 | | public TcpServerTransport() |
| 41 | 21 | | : this(new TcpServerTransportOptions()) |
| 41 | 22 | | { |
| 41 | 23 | | } |
| | 24 | |
|
| | 25 | | /// <summary>Constructs a <see cref="TcpServerTransport" />.</summary> |
| | 26 | | /// <param name="options">The transport options.</param> |
| 482 | 27 | | public TcpServerTransport(TcpServerTransportOptions options) => _options = options; |
| | 28 | |
|
| | 29 | | /// <inheritdoc/> |
| | 30 | | public IListener<IDuplexConnection> Listen( |
| | 31 | | ServerAddress serverAddress, |
| | 32 | | DuplexConnectionOptions options, |
| | 33 | | SslServerAuthenticationOptions? serverAuthenticationOptions) |
| 260 | 34 | | { |
| 260 | 35 | | if (serverAddress.Transport is string transport && !IsValidTransportName(transport, serverAddress.Protocol)) |
| 6 | 36 | | { |
| 6 | 37 | | throw new NotSupportedException( |
| 6 | 38 | | $"The Tcp server transport does not support server addresses with transport '{transport}'."); |
| | 39 | | } |
| | 40 | |
|
| 254 | 41 | | if (serverAddress.Params.Count > 0) |
| 10 | 42 | | { |
| 10 | 43 | | throw new ArgumentException( |
| 10 | 44 | | $"The server address '{serverAddress}' contains parameters that are not valid for the Tcp server transpo |
| 10 | 45 | | nameof(serverAddress)); |
| | 46 | | } |
| | 47 | |
|
| 244 | 48 | | if (serverAddress.Transport is null) |
| 198 | 49 | | { |
| 198 | 50 | | serverAddress = serverAddress with { Transport = Name }; |
| 198 | 51 | | } |
| 46 | 52 | | else if (serverAddress.Transport == SslName && serverAuthenticationOptions is null) |
| 2 | 53 | | { |
| 2 | 54 | | throw new ArgumentNullException( |
| 2 | 55 | | nameof(serverAuthenticationOptions), |
| 2 | 56 | | "The Ssl server transport requires the Ssl server authentication options to be set."); |
| | 57 | | } |
| | 58 | |
|
| 242 | 59 | | return new TcpListener(serverAddress, options, serverAuthenticationOptions, _options); |
| | 60 | |
|
| | 61 | | static bool IsValidTransportName(string transportName, Protocol protocol) => |
| 56 | 62 | | protocol == Protocol.Ice ? transportName is TcpName or SslName : transportName is TcpName; |
| 230 | 63 | | } |
| | 64 | | } |