THRIFT-5020 Refactoring & minor fixes for netstd library
Client: netstd
Patch: Jens Geyer

This closes #1941
diff --git a/lib/netstd/Thrift/Server/TSimpleAsyncServer.cs b/lib/netstd/Thrift/Server/TSimpleAsyncServer.cs
index bdaa348..45e5513 100644
--- a/lib/netstd/Thrift/Server/TSimpleAsyncServer.cs
+++ b/lib/netstd/Thrift/Server/TSimpleAsyncServer.cs
@@ -66,7 +66,8 @@
                   outputTransportFactory,
                   inputProtocolFactory,
                   outputProtocolFactory,
-                  loggerFactory.CreateLogger<TSimpleAsyncServer>())
+                  loggerFactory.CreateLogger<TSimpleAsyncServer>(),
+                  clientWaitingDelay)
         {
         }
 
diff --git a/lib/netstd/Thrift/Transport/Client/THttpTransport.cs b/lib/netstd/Thrift/Transport/Client/THttpTransport.cs
index c84df83..4f8454c 100644
--- a/lib/netstd/Thrift/Transport/Client/THttpTransport.cs
+++ b/lib/netstd/Thrift/Transport/Client/THttpTransport.cs
@@ -99,19 +99,14 @@
         public override async ValueTask<int> ReadAsync(byte[] buffer, int offset, int length, CancellationToken cancellationToken)
         {
             if (cancellationToken.IsCancellationRequested)
-            {
                 return await Task.FromCanceled<int>(cancellationToken);
-            }
 
             if (_inputStream == null)
-            {
                 throw new TTransportException(TTransportException.ExceptionType.NotOpen, "No request has been sent");
-            }
 
             try
             {
                 var ret = await _inputStream.ReadAsync(buffer, offset, length, cancellationToken);
-
                 if (ret == -1)
                 {
                     throw new TTransportException(TTransportException.ExceptionType.EndOfFile, "No more data available");
diff --git a/lib/netstd/Thrift/Transport/Client/TMemoryBufferTransport.cs b/lib/netstd/Thrift/Transport/Client/TMemoryBufferTransport.cs
index 25895c2..cdbbc0d 100644
--- a/lib/netstd/Thrift/Transport/Client/TMemoryBufferTransport.cs
+++ b/lib/netstd/Thrift/Transport/Client/TMemoryBufferTransport.cs
@@ -30,14 +30,9 @@
         private byte[] Bytes;
         private int _bytesUsed;
 
-        public TMemoryBufferTransport()
+        public TMemoryBufferTransport(int initialCapacity = 2048)
         {
-            Bytes = new byte[2048];  // default size
-        }
-
-        public TMemoryBufferTransport(int initialCapacity)
-        {
-            Bytes = new byte[initialCapacity];  // default size
+            Bytes = new byte[initialCapacity];  
         }
 
         public TMemoryBufferTransport(byte[] buf)
diff --git a/lib/netstd/Thrift/Transport/Client/TNamedPipeTransport.cs b/lib/netstd/Thrift/Transport/Client/TNamedPipeTransport.cs
index 7dfe013..1ae6074 100644
--- a/lib/netstd/Thrift/Transport/Client/TNamedPipeTransport.cs
+++ b/lib/netstd/Thrift/Transport/Client/TNamedPipeTransport.cs
@@ -26,7 +26,7 @@
     public class TNamedPipeTransport : TTransport
     {
         private NamedPipeClientStream PipeStream;
-        private int ConnectTimeout;
+        private readonly int ConnectTimeout;
 
         public TNamedPipeTransport(string pipe, int timeout = Timeout.Infinite) 
             : this(".", pipe, timeout)
@@ -102,7 +102,10 @@
 
         protected override void Dispose(bool disposing)
         {
-            PipeStream.Dispose();
+            if(disposing) 
+            {
+              PipeStream?.Dispose();
+            }
         }
     }
 }
diff --git a/lib/netstd/Thrift/Transport/Client/TSocketTransport.cs b/lib/netstd/Thrift/Transport/Client/TSocketTransport.cs
index 00da045..dd506bc 100644
--- a/lib/netstd/Thrift/Transport/Client/TSocketTransport.cs
+++ b/lib/netstd/Thrift/Transport/Client/TSocketTransport.cs
@@ -36,12 +36,7 @@
             SetInputOutputStream();
         }
 
-        public TSocketTransport(IPAddress host, int port)
-            : this(host, port, 0)
-        {
-        }
-
-        public TSocketTransport(IPAddress host, int port, int timeout)
+        public TSocketTransport(IPAddress host, int port, int timeout = 0)
         {
             Host = host;
             Port = port;
@@ -84,7 +79,7 @@
             }
         }
 
-    public TcpClient TcpClient { get; private set; }
+        public TcpClient TcpClient { get; private set; }
         public IPAddress Host { get; }
         public int Port { get; }
 
@@ -159,4 +154,4 @@
             _isDisposed = true;
         }
     }
-}
\ No newline at end of file
+}
diff --git a/lib/netstd/Thrift/Transport/Client/TTlsSocketTransport.cs b/lib/netstd/Thrift/Transport/Client/TTlsSocketTransport.cs
index 9295bb0..a926a38 100644
--- a/lib/netstd/Thrift/Transport/Client/TTlsSocketTransport.cs
+++ b/lib/netstd/Thrift/Transport/Client/TTlsSocketTransport.cs
@@ -42,7 +42,8 @@
         private SslStream _secureStream;
         private int _timeout;
 
-        public TTlsSocketTransport(TcpClient client, X509Certificate2 certificate, bool isServer = false,
+        public TTlsSocketTransport(TcpClient client,
+            X509Certificate2 certificate, bool isServer = false,
             RemoteCertificateValidationCallback certValidator = null,
             LocalCertificateSelectionCallback localCertificateSelectionCallback = null,
             SslProtocols sslProtocols = SslProtocols.Tls12)
@@ -67,7 +68,8 @@
             }
         }
 
-        public TTlsSocketTransport(IPAddress host, int port, string certificatePath,
+        public TTlsSocketTransport(IPAddress host, int port, 
+            string certificatePath,
             RemoteCertificateValidationCallback certValidator = null,
             LocalCertificateSelectionCallback localCertificateSelectionCallback = null,
             SslProtocols sslProtocols = SslProtocols.Tls12)
@@ -79,7 +81,7 @@
         {
         }
 
-        public TTlsSocketTransport(IPAddress host, int port,
+        public TTlsSocketTransport(IPAddress host, int port, 
             X509Certificate2 certificate = null,
             RemoteCertificateValidationCallback certValidator = null,
             LocalCertificateSelectionCallback localCertificateSelectionCallback = null,
@@ -92,7 +94,7 @@
         {
         }
 
-        public TTlsSocketTransport(IPAddress host, int port, int timeout,
+        public TTlsSocketTransport(IPAddress host, int port,  int timeout,
             X509Certificate2 certificate,
             RemoteCertificateValidationCallback certValidator = null,
             LocalCertificateSelectionCallback localCertificateSelectionCallback = null,
@@ -109,7 +111,7 @@
             InitSocket();
         }
 
-        public TTlsSocketTransport(string host, int port, int timeout,
+        public TTlsSocketTransport(string host, int port,  int timeout,
             X509Certificate2 certificate,
             RemoteCertificateValidationCallback certValidator = null,
             LocalCertificateSelectionCallback localCertificateSelectionCallback = null,
diff --git a/lib/netstd/Thrift/Transport/Server/THttpServerTransport.cs b/lib/netstd/Thrift/Transport/Server/THttpServerTransport.cs
index 056300c..2a40db3 100644
--- a/lib/netstd/Thrift/Transport/Server/THttpServerTransport.cs
+++ b/lib/netstd/Thrift/Transport/Server/THttpServerTransport.cs
@@ -43,13 +43,16 @@
 
         protected ITAsyncProcessor Processor;
 
-        public THttpServerTransport(ITAsyncProcessor processor, RequestDelegate next = null, ILoggerFactory loggerFactory = null)
+        public THttpServerTransport(
+            ITAsyncProcessor processor,
+            RequestDelegate next = null,
+            ILoggerFactory loggerFactory = null)
             : this(processor, new TBinaryProtocol.Factory(), null, next, loggerFactory)
         {
         }
 
         public THttpServerTransport(
-            ITAsyncProcessor processor, 
+            ITAsyncProcessor processor,
             TProtocolFactory protocolFactory, 
             TTransportFactory transFactory = null, 
             RequestDelegate next = null,
@@ -59,7 +62,7 @@
         }
 
         public THttpServerTransport(
-            ITAsyncProcessor processor, 
+            ITAsyncProcessor processor,
             TProtocolFactory inputProtocolFactory,
             TProtocolFactory outputProtocolFactory,
             TTransportFactory inputTransFactory = null,
diff --git a/lib/netstd/Thrift/Transport/Server/TNamedPipeServerTransport.cs b/lib/netstd/Thrift/Transport/Server/TNamedPipeServerTransport.cs
index 77b8251..b2f29b4 100644
--- a/lib/netstd/Thrift/Transport/Server/TNamedPipeServerTransport.cs
+++ b/lib/netstd/Thrift/Transport/Server/TNamedPipeServerTransport.cs
@@ -92,10 +92,16 @@
                 try
                 {
                     var handle = CreatePipeNative(_pipeAddress, inbuf, outbuf);
-                    if( (handle != null) && (!handle.IsInvalid))
+                    if ((handle != null) && (!handle.IsInvalid))
+                    {
                         _stream = new NamedPipeServerStream(PipeDirection.InOut, _asyncMode, false, handle);
+                        handle = null; // we don't own it any longer
+                    }
                     else
+                    {
+                        handle?.Dispose();
                         _stream = new NamedPipeServerStream(_pipeAddress, direction, maxconn, mode, options, inbuf, outbuf/*, pipesec*/);
+                    }
                 }
                 catch (NotImplementedException) // Mono still does not support async, fallback to sync
                 {
@@ -301,7 +307,10 @@
 
             protected override void Dispose(bool disposing)
             {
-                PipeStream?.Dispose();
+                if (disposing)
+                {
+                    PipeStream?.Dispose();
+                }
             }
         }
     }
diff --git a/lib/netstd/Thrift/Transport/TServerTransport.cs b/lib/netstd/Thrift/Transport/Server/TServerTransport.cs
similarity index 96%
rename from lib/netstd/Thrift/Transport/TServerTransport.cs
rename to lib/netstd/Thrift/Transport/Server/TServerTransport.cs
index 74c54cd..dd60f6a 100644
--- a/lib/netstd/Thrift/Transport/TServerTransport.cs
+++ b/lib/netstd/Thrift/Transport/Server/TServerTransport.cs
@@ -34,7 +34,7 @@
 
         protected abstract ValueTask<TTransport> AcceptImplementationAsync(CancellationToken cancellationToken);
 
-        public async ValueTask<TTransport> AcceptAsync()
+        public async ValueTask<TTransport> AcceptAsync() 
         {
             return await AcceptAsync(CancellationToken.None);
         }
@@ -51,4 +51,4 @@
             return transport;
         }
     }
-}
\ No newline at end of file
+}
diff --git a/lib/netstd/Thrift/Transport/Server/TTlsServerSocketTransport.cs b/lib/netstd/Thrift/Transport/Server/TTlsServerSocketTransport.cs
index 1286805..231b83f 100644
--- a/lib/netstd/Thrift/Transport/Server/TTlsServerSocketTransport.cs
+++ b/lib/netstd/Thrift/Transport/Server/TTlsServerSocketTransport.cs
@@ -63,7 +63,7 @@
             RemoteCertificateValidationCallback clientCertValidator = null,
             LocalCertificateSelectionCallback localCertificateSelectionCallback = null,
             SslProtocols sslProtocols = SslProtocols.Tls12)
-            : this(null, certificate, clientCertValidator, localCertificateSelectionCallback)
+            : this(null, certificate, clientCertValidator, localCertificateSelectionCallback, sslProtocols)
         {
             try
             {
@@ -117,7 +117,9 @@
                 client.SendTimeout = client.ReceiveTimeout = _clientTimeout;
 
                 //wrap the client in an SSL Socket passing in the SSL cert
-                var tTlsSocket = new TTlsSocketTransport(client, _serverCertificate, true, _clientCertValidator,
+                var tTlsSocket = new TTlsSocketTransport( 
+                    client, 
+                    _serverCertificate, true, _clientCertValidator,
                     _localCertificateSelectionCallback, _sslProtocols);
 
                 await tTlsSocket.SetupTlsAsync();