| | | 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/> |
| | 1059 | 13 | | public string Name => ColocTransport.Name; |
| | | 14 | | |
| | | 15 | | private readonly ConcurrentDictionary<ServerAddress, ColocListener> _listeners; |
| | | 16 | | private readonly ColocTransportOptions _options; |
| | | 17 | | |
| | | 18 | | /// <inheritdoc/> |
| | | 19 | | public IDuplexConnection CreateConnection( |
| | | 20 | | ServerAddress serverAddress, |
| | | 21 | | DuplexConnectionOptions duplexConnectionOptions, |
| | | 22 | | SslClientAuthenticationOptions? clientAuthenticationOptions) |
| | 1011 | 23 | | { |
| | 1011 | 24 | | if (clientAuthenticationOptions is not null) |
| | 0 | 25 | | { |
| | 0 | 26 | | throw new NotSupportedException("The Coloc client transport does not support SSL."); |
| | | 27 | | } |
| | | 28 | | |
| | 1011 | 29 | | if ((serverAddress.Transport is string transport && transport != ColocTransport.Name) || |
| | 1011 | 30 | | !ColocTransport.CheckParams(serverAddress)) |
| | 4 | 31 | | { |
| | 4 | 32 | | throw new ArgumentException( |
| | 4 | 33 | | $"The server address '{serverAddress}' contains parameters that are not valid for the Coloc client trans |
| | 4 | 34 | | nameof(serverAddress)); |
| | | 35 | | } |
| | | 36 | | |
| | 1007 | 37 | | serverAddress = serverAddress with { Transport = Name }; |
| | | 38 | | |
| | 1007 | 39 | | var localPipe = new Pipe(new PipeOptions( |
| | 1007 | 40 | | pool: duplexConnectionOptions.Pool, |
| | 1007 | 41 | | minimumSegmentSize: duplexConnectionOptions.MinSegmentSize, |
| | 1007 | 42 | | pauseWriterThreshold: _options.PauseWriterThreshold, |
| | 1007 | 43 | | resumeWriterThreshold: _options.ResumeWriterThreshold, |
| | 1007 | 44 | | useSynchronizationContext: false)); |
| | 1007 | 45 | | return new ClientColocConnection(serverAddress, localPipe, ConnectAsync); |
| | | 46 | | |
| | | 47 | | // The client connection connect operation calls this method to queue a connection establishment request with |
| | | 48 | | // the listener. The returned task is completed once the listener accepts the connection establishment request. |
| | | 49 | | Task<PipeReader> ConnectAsync(PipeReader clientPipeReader, CancellationToken cancellationToken) |
| | 949 | 50 | | { |
| | 949 | 51 | | if (_listeners.TryGetValue(serverAddress, out ColocListener? listener) && |
| | 949 | 52 | | listener.TryQueueConnect( |
| | 949 | 53 | | clientPipeReader, |
| | 949 | 54 | | cancellationToken, |
| | 949 | 55 | | out Task<PipeReader>? serverPipeReaderTask)) |
| | 931 | 56 | | { |
| | 931 | 57 | | return serverPipeReaderTask; |
| | | 58 | | } |
| | | 59 | | else |
| | 18 | 60 | | { |
| | 18 | 61 | | throw new IceRpcException(IceRpcError.ConnectionRefused); |
| | | 62 | | } |
| | 931 | 63 | | } |
| | 1007 | 64 | | } |
| | | 65 | | |
| | 978 | 66 | | internal ColocClientTransport( |
| | 978 | 67 | | ConcurrentDictionary<ServerAddress, ColocListener> listeners, |
| | 978 | 68 | | ColocTransportOptions options) |
| | 978 | 69 | | { |
| | 978 | 70 | | _listeners = listeners; |
| | 978 | 71 | | _options = options; |
| | 978 | 72 | | } |
| | | 73 | | } |