| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using System.Collections.Concurrent; |
| | | 4 | | using System.Net.Security; |
| | | 5 | | |
| | | 6 | | namespace IceRpc.Transports.Coloc.Internal; |
| | | 7 | | |
| | | 8 | | /// <summary>Implements <see cref="IDuplexServerTransport" /> for the coloc transport.</summary> |
| | | 9 | | internal class ColocServerTransport : IDuplexServerTransport |
| | | 10 | | { |
| | | 11 | | /// <inheritdoc/> |
| | 130 | 12 | | public string DefaultName => ColocTransport.Name; |
| | | 13 | | |
| | | 14 | | private readonly ConcurrentDictionary<(string Host, ushort Port), ColocListener> _listeners; |
| | | 15 | | private readonly ColocTransportOptions _options; |
| | | 16 | | |
| | | 17 | | /// <inheritdoc/> |
| | | 18 | | public IListener<IDuplexConnection> Listen( |
| | | 19 | | TransportAddress transportAddress, |
| | | 20 | | DuplexConnectionOptions options, |
| | | 21 | | SslServerAuthenticationOptions? serverAuthenticationOptions) |
| | 518 | 22 | | { |
| | 518 | 23 | | if (serverAuthenticationOptions is not null) |
| | 0 | 24 | | { |
| | 0 | 25 | | throw new NotSupportedException("The Coloc server transport does not support SSL."); |
| | | 26 | | } |
| | | 27 | | |
| | 518 | 28 | | if (transportAddress.TransportName is string name && name != DefaultName) |
| | 0 | 29 | | { |
| | 0 | 30 | | throw new NotSupportedException($"The Coloc server transport does not support transport '{name}'."); |
| | | 31 | | } |
| | | 32 | | |
| | 518 | 33 | | if (transportAddress.Params.Count > 0) |
| | 2 | 34 | | { |
| | 2 | 35 | | throw new ArgumentException( |
| | 2 | 36 | | "The transport address contains parameters that are not valid for the Coloc server transport.", |
| | 2 | 37 | | nameof(transportAddress)); |
| | | 38 | | } |
| | | 39 | | |
| | 516 | 40 | | var key = (transportAddress.Host, transportAddress.Port); |
| | | 41 | | |
| | 516 | 42 | | var listener = new ColocListener( |
| | 516 | 43 | | transportAddress, |
| | 516 | 44 | | onDispose: OnDispose, |
| | 516 | 45 | | colocTransportOptions: _options, |
| | 516 | 46 | | duplexConnectionOptions: options); |
| | | 47 | | |
| | 516 | 48 | | if (!_listeners.TryAdd(key, listener)) |
| | 2 | 49 | | { |
| | 2 | 50 | | throw new IceRpcException(IceRpcError.AddressInUse); |
| | | 51 | | } |
| | 514 | 52 | | return listener; |
| | | 53 | | |
| | | 54 | | void OnDispose(ColocListener disposedListener) => |
| | 514 | 55 | | _listeners.TryRemove(new KeyValuePair<(string, ushort), ColocListener>(key, disposedListener)); |
| | 514 | 56 | | } |
| | | 57 | | |
| | 509 | 58 | | internal ColocServerTransport( |
| | 509 | 59 | | ConcurrentDictionary<(string Host, ushort Port), ColocListener> listeners, |
| | 509 | 60 | | ColocTransportOptions options) |
| | 509 | 61 | | { |
| | 509 | 62 | | _listeners = listeners; |
| | 509 | 63 | | _options = options; |
| | 509 | 64 | | } |
| | | 65 | | } |