| | 1 | | /* |
| | 2 | | * Copyright 2017 Stanislav Muhametsin. All rights Reserved. |
| | 3 | | * |
| | 4 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
| | 5 | | * you may not use this file except in compliance with the License. |
| | 6 | | * You may obtain a copy of the License at |
| | 7 | | * |
| | 8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
| | 9 | | * |
| | 10 | | * Unless required by applicable law or agreed to in writing, software |
| | 11 | | * distributed under the License is distributed on an "AS IS" BASIS, |
| | 12 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| | 13 | | * implied. |
| | 14 | | * |
| | 15 | | * See the License for the specific language governing permissions and |
| | 16 | | * limitations under the License. |
| | 17 | | */ |
| | 18 | | using CBAM.Abstractions; |
| | 19 | | using CBAM.Abstractions.Implementation.NetworkStream; |
| | 20 | | using CBAM.SQL.Implementation; |
| | 21 | | using CBAM.SQL.PostgreSQL; |
| | 22 | | using CBAM.SQL.PostgreSQL.Implementation; |
| | 23 | | using IOUtils.Network.Configuration; |
| | 24 | | using ResourcePooling.Async.Abstractions; |
| | 25 | | using System; |
| | 26 | | using System.Collections.Generic; |
| | 27 | | using System.IO; |
| | 28 | | using System.Text; |
| | 29 | | using System.Threading; |
| | 30 | | using System.Threading.Tasks; |
| | 31 | | using UtilPack; |
| | 32 | | namespace CBAM.SQL.PostgreSQL |
| | 33 | | { |
| | 34 | | using TIntermediateState = ValueTuple<BackendABIHelper, ResizableArray<Byte>, CancellationToken, Stream>; |
| | 35 | | /// <summary> |
| | 36 | | /// This is the entrypoint-class for using PostgreSQL connections. |
| | 37 | | /// Use <see cref="Factory"/> static property to acquire <see cref="AsyncResourceFactory{TResource, TParams}"/> for < |
| | 38 | | /// This <see cref="AsyncResourceFactory{TResource}"/> with one generic type parameter then has a number of extension |
| | 39 | | /// </summary> |
| | 40 | | /// <remarks> |
| | 41 | | /// This class also (explicitly) implements <see cref="AsyncResourceFactoryProvider"/> interface in order to provide |
| | 42 | | /// </remarks> |
| | 43 | | public sealed class PgSQLConnectionPoolProvider : AbstractAsyncResourceFactoryProvider<PgSQLConnection, PgSQLConnecti |
| | 44 | | { |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// Gets the <see cref="AsyncResourceFactory{TResource, TParams}"/> which can create pools that provide instances |
| | 48 | | /// </summary> |
| | 49 | | /// <value>The <see cref="AsyncResourceFactory{TResource, TParams}"/> which can create <see cref="PgSQLConnection" |
| | 50 | | /// <remarks> |
| | 51 | | /// By invoking <see cref="AsyncResourceFactory{TResource, TParams}.BindCreationParameters"/>, one gets the bound |
| | 52 | | /// Instead of directly using <see cref="AsyncResourceFactory{TResource}.CreateAcquireResourceContext"/>, typical |
| | 53 | | /// </remarks> |
| 24 | 54 | | public static AsyncResourceFactory<PgSQLConnection, PgSQLConnectionCreationInfo> Factory { get; } = new DefaultAsy |
| 24 | 55 | | config.NewFactoryParametrizer<PgSQLConnectionCreationInfo, PgSQLConnectionCreationInfoData, PgSQLConnectionConf |
| 24 | 56 | | .BindPublicConnectionType<PgSQLConnection>() |
| 24 | 57 | | .CreateStatefulDelegatingConnectionFactory( |
| 24 | 58 | | new UTF8EncodingInfo(), |
| 48 | 59 | | ( parameters, encodingInfo, stringPool, stringPoolIsDedicated, socketOrNull, stream, token ) => new TInte |
| 24 | 60 | | async ( parameters, encodingInfo, stringPool, stringPoolIsDedicated, state ) => |
| 24 | 61 | | { |
| 48 | 62 | | var sslMode = parameters.CreationData?.Connection?.ConnectionSSLMode ?? ConnectionSSLMode.NotRequired; |
| 48 | 63 | | var retVal = sslMode == ConnectionSSLMode.Required || sslMode == ConnectionSSLMode.Preferred; |
| 48 | 64 | | if ( retVal ) |
| 24 | 65 | | { |
| 25 | 66 | | await SSLRequestMessage.INSTANCE.SendMessageAsync( (state.Item1, state.Item4, state.Item3, state.It |
| 24 | 67 | |
|
| 25 | 68 | | await state.Item4.ReadSpecificAmountAsync( state.Item2.Array, 0, 1, state.Item3 ); |
| 25 | 69 | | retVal = state.Item2.Array[0] == (Byte) 'S'; |
| 24 | 70 | | } |
| 24 | 71 | |
|
| 48 | 72 | | return retVal; |
| 48 | 73 | | }, |
| 24 | 74 | | () => new PgSQLException( "Server accepted SSL request, but the creation parameters did not have callback |
| 24 | 75 | | () => new PgSQLException( "Server does not support SSL." ), |
| 24 | 76 | | () => new PgSQLException( "SSL stream creation callback returned null." ), |
| 24 | 77 | | () => new PgSQLException( "Authentication callback given by SSL stream creation callback was null." ), |
| 24 | 78 | | inner => new PgSQLException( "Unable to start SSL client.", inner ), |
| 24 | 79 | | async ( parameters, encodingInfo, stringPool, stringPoolIsDedicated, stream, socketOrNull, token, state ) |
| 24 | 80 | | { |
| 48 | 81 | | (var proto, var warnings) = await PostgreSQLProtocol.PerformStartup( |
| 48 | 82 | | new PgSQLConnectionVendorFunctionalityImpl(), |
| 48 | 83 | | parameters, |
| 48 | 84 | | token, |
| 48 | 85 | | stream, |
| 48 | 86 | | state.Item1, |
| 48 | 87 | | state.Item2 |
| 48 | 88 | | #if !NETSTANDARD1_0 |
| 48 | 89 | | , (System.Net.Sockets.Socket) socketOrNull |
| 48 | 90 | | #endif |
| 48 | 91 | | ); |
| 24 | 92 | |
|
| 48 | 93 | | return proto; |
| 48 | 94 | | }, |
| 48 | 95 | | protocol => new ValueTask<PgSQLConnectionImpl>( new PgSQLConnectionImpl( protocol, new PgSQLDatabaseMetaD |
| 48 | 96 | | ( protocol, connection ) => new PgSQLConnectionAcquireInfo( connection, protocol ), |
| 24 | 97 | | ( functionality, connection, token, error ) => functionality?.Stream |
| 24 | 98 | | ) ); |
| | 99 | |
|
| | 100 | | //new PgSQLConnectionFactory( config, new UTF8EncodingInfo() ) ); |
| | 101 | |
|
| | 102 | | /// <summary> |
| | 103 | | /// Creates a new instance of <see cref="PgSQLConnectionPoolProvider"/>. |
| | 104 | | /// </summary> |
| | 105 | | /// <remarks> |
| | 106 | | /// This constructor is not intended to be used directly, but a generic scenarios like MSBuild task dynamically lo |
| | 107 | | /// </remarks> |
| | 108 | | public PgSQLConnectionPoolProvider() |
| 0 | 109 | | : base( typeof( PgSQLConnectionCreationInfoData ) ) |
| | 110 | | { |
| 0 | 111 | | } |
| | 112 | |
|
| | 113 | | /// <summary> |
| | 114 | | /// This method implements <see cref="AbstractAsyncResourceFactoryProvider{TFactoryResource, TCreationParameters}. |
| | 115 | | /// </summary> |
| | 116 | | /// <param name="creationParameters">The untyped creation parameters.</param> |
| | 117 | | /// <returns>The <see cref="PgSQLConnectionCreationInfo"/>.</returns> |
| | 118 | | /// <exception cref="ArgumentNullException">If <paramref name="creationParameters"/> is <c>null</c>.</exception> |
| | 119 | | /// <exception cref="ArgumentException">If <paramref name="creationParameters"/> is not <see cref="PgSQLConnection |
| | 120 | | protected override PgSQLConnectionCreationInfo TransformFactoryParameters( Object creationParameters ) |
| | 121 | | { |
| 0 | 122 | | ArgumentValidator.ValidateNotNull( nameof( creationParameters ), creationParameters ); |
| | 123 | |
|
| | 124 | | PgSQLConnectionCreationInfo retVal; |
| 0 | 125 | | if ( creationParameters is PgSQLConnectionCreationInfoData creationData ) |
| | 126 | | { |
| 0 | 127 | | retVal = new PgSQLConnectionCreationInfo( creationData ); |
| | 128 | |
|
| 0 | 129 | | } |
| 0 | 130 | | else if ( creationParameters is PgSQLConnectionCreationInfo creationInfo ) |
| | 131 | | { |
| 0 | 132 | | retVal = creationInfo; |
| 0 | 133 | | } |
| | 134 | | else |
| | 135 | | { |
| 0 | 136 | | throw new ArgumentException( $"The {nameof( creationParameters )} must be instance of {typeof( PgSQLConnecti |
| | 137 | | } |
| | 138 | |
|
| 0 | 139 | | return retVal; |
| | 140 | | } |
| | 141 | |
|
| | 142 | | /// <summary> |
| | 143 | | /// This method implements <see cref="AbstractAsyncResourceFactoryProvider{TFactoryResource, TCreationParameters}. |
| | 144 | | /// </summary> |
| | 145 | | /// <returns>The value of <see cref="Factory"/> static property.</returns> |
| | 146 | | protected override AsyncResourceFactory<PgSQLConnection, PgSQLConnectionCreationInfo> CreateFactory() |
| 0 | 147 | | => Factory; |
| | 148 | | } |
| | 149 | | } |