THRIFT-4822 Convert bool CTOR flags into enum type
Client: netstd
Patch: Jens Geyer
This closes #1759
diff --git a/lib/netstd/Thrift/Transport/Server/TServerSocketTransport.cs b/lib/netstd/Thrift/Transport/Server/TServerSocketTransport.cs
index bc06640..4f46f99 100644
--- a/lib/netstd/Thrift/Transport/Server/TServerSocketTransport.cs
+++ b/lib/netstd/Thrift/Transport/Server/TServerSocketTransport.cs
@@ -16,6 +16,7 @@
// under the License.
using System;
+using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Threading;
@@ -24,27 +25,25 @@
namespace Thrift.Transport.Server
{
+
// ReSharper disable once InconsistentNaming
public class TServerSocketTransport : TServerTransport
{
private readonly int _clientTimeout;
- private readonly bool _useBufferedSockets;
- private readonly bool _useFramedTransport;
+ private readonly Buffering _buffering;
private TcpListener _server;
- public TServerSocketTransport(TcpListener listener, int clientTimeout = 0, bool useBufferedSockets = false, bool useFramedTransport = false)
+ public TServerSocketTransport(TcpListener listener, int clientTimeout = 0, Buffering buffering = Buffering.None)
{
_server = listener;
_clientTimeout = clientTimeout;
- _useBufferedSockets = useBufferedSockets;
- _useFramedTransport = useFramedTransport;
+ _buffering = buffering;
}
- public TServerSocketTransport(int port, int clientTimeout = 0, bool useBufferedSockets = false, bool useFramedTransport = false)
+ public TServerSocketTransport(int port, int clientTimeout = 0, Buffering buffering = Buffering.None)
{
_clientTimeout = clientTimeout;
- _useBufferedSockets = useBufferedSockets;
- _useFramedTransport = useFramedTransport;
+ _buffering = buffering;
try
{
// Make server socket
@@ -103,14 +102,19 @@
Timeout = _clientTimeout
};
- if (_useBufferedSockets)
+ switch (_buffering)
{
- tSocketTransport = new TBufferedTransport(tSocketTransport);
- }
+ case Buffering.BufferedTransport:
+ tSocketTransport = new TBufferedTransport(tSocketTransport);
+ break;
- if (_useFramedTransport)
- {
- tSocketTransport = new TFramedTransport(tSocketTransport);
+ case Buffering.FramedTransport:
+ tSocketTransport = new TFramedTransport(tSocketTransport);
+ break;
+
+ default:
+ Debug.Assert(_buffering == Buffering.None);
+ break;
}
return tSocketTransport;
diff --git a/lib/netstd/Thrift/Transport/Server/TTlsServerSocketTransport.cs b/lib/netstd/Thrift/Transport/Server/TTlsServerSocketTransport.cs
index 14d7ff2..2025b65 100644
--- a/lib/netstd/Thrift/Transport/Server/TTlsServerSocketTransport.cs
+++ b/lib/netstd/Thrift/Transport/Server/TTlsServerSocketTransport.cs
@@ -16,6 +16,7 @@
// under the License.
using System;
+using System.Diagnostics;
using System.Net;
using System.Net.Security;
using System.Net.Sockets;
@@ -36,31 +37,17 @@
private readonly int _port;
private readonly X509Certificate2 _serverCertificate;
private readonly SslProtocols _sslProtocols;
- private readonly bool _useBufferedSockets;
- private readonly bool _useFramedTransport;
+ private readonly Buffering _buffering;
private TcpListener _server;
public TTlsServerSocketTransport(int port, X509Certificate2 certificate)
- : this(port, false, certificate)
+ : this(port, Buffering.None, certificate)
{
}
public TTlsServerSocketTransport(
int port,
- bool useBufferedSockets,
- X509Certificate2 certificate,
- RemoteCertificateValidationCallback clientCertValidator = null,
- LocalCertificateSelectionCallback localCertificateSelectionCallback = null,
- SslProtocols sslProtocols = SslProtocols.Tls12)
- : this(port, useBufferedSockets, false, certificate,
- clientCertValidator, localCertificateSelectionCallback, sslProtocols)
- {
- }
-
- public TTlsServerSocketTransport(
- int port,
- bool useBufferedSockets,
- bool useFramedTransport,
+ Buffering buffering,
X509Certificate2 certificate,
RemoteCertificateValidationCallback clientCertValidator = null,
LocalCertificateSelectionCallback localCertificateSelectionCallback = null,
@@ -74,8 +61,7 @@
_port = port;
_serverCertificate = certificate;
- _useBufferedSockets = useBufferedSockets;
- _useFramedTransport = useFramedTransport;
+ _buffering = buffering;
_clientCertValidator = clientCertValidator;
_localCertificateSelectionCallback = localCertificateSelectionCallback;
_sslProtocols = sslProtocols;
@@ -138,15 +124,20 @@
await tTlsSocket.SetupTlsAsync();
TTransport trans = tTlsSocket;
-
- if (_useBufferedSockets)
- {
- trans = new TBufferedTransport(trans);
- }
- if (_useFramedTransport)
+ switch(_buffering)
{
- trans = new TFramedTransport(trans);
+ case Buffering.BufferedTransport:
+ trans = new TBufferedTransport(trans);
+ break;
+
+ case Buffering.FramedTransport:
+ trans = new TFramedTransport(trans);
+ break;
+
+ default:
+ Debug.Assert(_buffering == Buffering.None);
+ break;
}
return trans;
@@ -174,4 +165,4 @@
}
}
}
-}
\ No newline at end of file
+}
diff --git a/lib/netstd/Thrift/Transport/TFramedTransport.cs b/lib/netstd/Thrift/Transport/TFramedTransport.cs
index 59963f1..6d0802d 100644
--- a/lib/netstd/Thrift/Transport/TFramedTransport.cs
+++ b/lib/netstd/Thrift/Transport/TFramedTransport.cs
@@ -1,4 +1,4 @@
-// Licensed to the Apache Software Foundation(ASF) under one
+// Licensed to the Apache Software Foundation(ASF) under one
// or more contributor license agreements.See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.The ASF licenses this file
@@ -22,7 +22,14 @@
namespace Thrift.Transport
{
- //TODO: check for correct implementation
+ // it does not make much sense to use buffered when we already use framed
+ public enum Buffering
+ {
+ None,
+ BufferedTransport,
+ FramedTransport
+ }
+
// ReSharper disable once InconsistentNaming
public class TFramedTransport : TTransport
@@ -206,4 +213,4 @@
_isDisposed = true;
}
}
-}
\ No newline at end of file
+}
diff --git a/test/netstd/Server/TestServer.cs b/test/netstd/Server/TestServer.cs
index e25e611..8db92dc 100644
--- a/test/netstd/Server/TestServer.cs
+++ b/test/netstd/Server/TestServer.cs
@@ -43,15 +43,6 @@
Json
}
- // it does not make much sense to use buffered when we already use framed
- internal enum LayeredChoice
- {
- None,
- Buffered,
- Framed
- }
-
-
internal enum TransportChoice
{
Socket,
@@ -61,7 +52,7 @@
internal class ServerParam
{
- internal LayeredChoice layered = LayeredChoice.None;
+ internal Buffering buffering = Buffering.None;
internal ProtocolChoice protocol = ProtocolChoice.Binary;
internal TransportChoice transport = TransportChoice.Socket;
internal int port = 9090;
@@ -84,11 +75,11 @@
}
else if (args[i] == "-b" || args[i] == "--buffered" || args[i] == "--transport=buffered")
{
- layered = LayeredChoice.Buffered;
+ buffering = Buffering.BufferedTransport;
}
else if (args[i] == "-f" || args[i] == "--framed" || args[i] == "--transport=framed")
{
- layered = LayeredChoice.Framed;
+ buffering = Buffering.FramedTransport;
}
else if (args[i] == "--binary" || args[i] == "--protocol=binary")
{
@@ -552,8 +543,7 @@
// Transport
TServerTransport trans;
- var useBuffered = (param.layered == LayeredChoice.Buffered);
- var useFramed = (param.layered == LayeredChoice.Framed);
+
switch (param.transport)
{
case TransportChoice.NamedPipe:
@@ -570,7 +560,7 @@
}
transFactory = new TTransportFactory(); // framed/buffered is built into socket transports
- trans = new TTlsServerSocketTransport( param.port, useBuffered, useFramed, cert,
+ trans = new TTlsServerSocketTransport( param.port, param.buffering, cert,
(sender, certificate, chain, errors) => true,
null, SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12);
break;
@@ -578,19 +568,19 @@
case TransportChoice.Socket:
default:
transFactory = new TTransportFactory(); // framed/buffered is built into socket transports
- trans = new TServerSocketTransport(param.port, 0, useBuffered, useFramed);
+ trans = new TServerSocketTransport(param.port, 0, param.buffering);
break;
}
// add layered transport, if not already set above
if (transFactory == null)
{
- switch (param.layered)
+ switch (param.buffering)
{
- case LayeredChoice.Framed:
+ case Buffering.FramedTransport:
transFactory = new TFramedTransport.Factory();
break;
- case LayeredChoice.Buffered:
+ case Buffering.BufferedTransport:
transFactory = new TBufferedTransport.Factory();
break;
}
@@ -627,8 +617,8 @@
var where = (! string.IsNullOrEmpty(param.pipe)) ? "on pipe " + param.pipe : "on port " + param.port;
Console.WriteLine("Starting the AsyncBaseServer " + where +
" with processor TPrototypeProcessorFactory prototype factory " +
- (param.layered == LayeredChoice.Buffered ? " with buffered transport" : "") +
- (param.layered == LayeredChoice.Framed ? " with framed transport" : "") +
+ (param.buffering == Buffering.BufferedTransport ? " with buffered transport" : "") +
+ (param.buffering == Buffering.FramedTransport ? " with framed transport" : "") +
(param.transport == TransportChoice.TlsSocket ? " with encryption" : "") +
(param.protocol == ProtocolChoice.Compact ? " with compact protocol" : "") +
(param.protocol == ProtocolChoice.Json ? " with json protocol" : "") +
diff --git a/tutorial/netstd/Server/Program.cs b/tutorial/netstd/Server/Program.cs
index 9a650c5..1d9dd15 100644
--- a/tutorial/netstd/Server/Program.cs
+++ b/tutorial/netstd/Server/Program.cs
@@ -143,13 +143,13 @@
serverTransport = new TServerSocketTransport(9090);
break;
case Transport.TcpBuffered:
- serverTransport = new TServerSocketTransport(port: 9090, clientTimeout: 10000, useBufferedSockets: true);
+ serverTransport = new TServerSocketTransport(port: 9090, clientTimeout: 10000, buffering: Buffering.BufferedTransport);
break;
case Transport.NamedPipe:
serverTransport = new TNamedPipeServerTransport(".test");
break;
case Transport.TcpTls:
- serverTransport = new TTlsServerSocketTransport(9090, false, GetCertificate(), ClientCertValidator, LocalCertificateSelectionCallback);
+ serverTransport = new TTlsServerSocketTransport(9090, Buffering.None, GetCertificate(), ClientCertValidator, LocalCertificateSelectionCallback);
break;
case Transport.Framed:
serverTransport = new TServerFramedTransport(9090);