| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using System.Collections.Concurrent; |
| | | 4 | | using System.IO.Pipelines; |
| | | 5 | | using System.Net.Security; |
| | | 6 | | |
| | | 7 | | namespace IceRpc.Transports.Coloc.Internal; |
| | | 8 | | |
| | | 9 | | /// <summary>Implements <see cref="IDuplexClientTransport" /> for the coloc transport.</summary> |
| | | 10 | | internal class ColocClientTransport : IDuplexClientTransport |
| | | 11 | | { |
| | | 12 | | /// <inheritdoc/> |
| | 83 | 13 | | public string DefaultName => ColocTransport.Name; |
| | | 14 | | |
| | | 15 | | /// <inheritdoc/> |
| | | 16 | | public bool IsSslRequired(string? transportName) => |
| | 76 | 17 | | transportName is null || transportName == ColocTransport.Name ? false : |
| | 76 | 18 | | throw new NotSupportedException($"The Coloc client transport does not support transport '{transportName}'.") |
| | | 19 | | |
| | | 20 | | private readonly ConcurrentDictionary<(string Host, ushort Port), ColocListener> _listeners; |
| | | 21 | | private readonly ColocTransportOptions _options; |
| | | 22 | | |
| | | 23 | | /// <inheritdoc/> |
| | | 24 | | public IDuplexConnection CreateConnection( |
| | | 25 | | TransportAddress transportAddress, |
| | | 26 | | DuplexConnectionOptions duplexConnectionOptions, |
| | | 27 | | SslClientAuthenticationOptions? clientAuthenticationOptions) |
| | 527 | 28 | | { |
| | 527 | 29 | | if (clientAuthenticationOptions is not null) |
| | 0 | 30 | | { |
| | 0 | 31 | | throw new NotSupportedException("The Coloc client transport does not support SSL."); |
| | | 32 | | } |
| | | 33 | | |
| | 527 | 34 | | if (transportAddress.TransportName is string name && name != DefaultName) |
| | 0 | 35 | | { |
| | 0 | 36 | | throw new NotSupportedException($"The Coloc client transport does not support transport '{name}'."); |
| | | 37 | | } |
| | | 38 | | |
| | 527 | 39 | | if (transportAddress.Params.Count > 0) |
| | 2 | 40 | | { |
| | 2 | 41 | | throw new ArgumentException( |
| | 2 | 42 | | "The transport address contains parameters that are not valid for the Coloc client transport.", |
| | 2 | 43 | | nameof(transportAddress)); |
| | | 44 | | } |
| | | 45 | | |
| | 525 | 46 | | var localPipe = new Pipe(new PipeOptions( |
| | 525 | 47 | | pool: duplexConnectionOptions.Pool, |
| | 525 | 48 | | minimumSegmentSize: duplexConnectionOptions.MinSegmentSize, |
| | 525 | 49 | | pauseWriterThreshold: _options.PauseWriterThreshold, |
| | 525 | 50 | | resumeWriterThreshold: _options.ResumeWriterThreshold, |
| | 525 | 51 | | useSynchronizationContext: false)); |
| | 525 | 52 | | return new ClientColocConnection(transportAddress, localPipe, ConnectAsync); |
| | | 53 | | |
| | | 54 | | // The client connection connect operation calls this method to queue a connection establishment request with |
| | | 55 | | // the listener. The returned task is completed once the listener accepts the connection establishment request. |
| | | 56 | | Task<PipeReader> ConnectAsync(PipeReader clientPipeReader, CancellationToken cancellationToken) |
| | 496 | 57 | | { |
| | 496 | 58 | | if (_listeners.TryGetValue((transportAddress.Host, transportAddress.Port), out ColocListener? listener) && |
| | 496 | 59 | | listener.TryQueueConnect( |
| | 496 | 60 | | clientPipeReader, |
| | 496 | 61 | | cancellationToken, |
| | 496 | 62 | | out Task<PipeReader>? serverPipeReaderTask)) |
| | 487 | 63 | | { |
| | 487 | 64 | | return serverPipeReaderTask; |
| | | 65 | | } |
| | | 66 | | else |
| | 9 | 67 | | { |
| | 9 | 68 | | throw new IceRpcException(IceRpcError.ConnectionRefused); |
| | | 69 | | } |
| | 487 | 70 | | } |
| | 525 | 71 | | } |
| | | 72 | | |
| | 509 | 73 | | internal ColocClientTransport( |
| | 509 | 74 | | ConcurrentDictionary<(string Host, ushort Port), ColocListener> listeners, |
| | 509 | 75 | | ColocTransportOptions options) |
| | 509 | 76 | | { |
| | 509 | 77 | | _listeners = listeners; |
| | 509 | 78 | | _options = options; |
| | 509 | 79 | | } |
| | | 80 | | } |