THRIFT-4822 Convert bool CTOR flags into enum type

Client: netstd
Patch: Jens Geyer

This closes #1759
This commit is contained in:
Jens Geyer 2019-03-14 21:42:16 +01:00
parent cf51a84391
commit bf27637960
5 changed files with 59 additions and 67 deletions

View File

@ -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 @@ using Thrift.Transport.Client;
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 @@ namespace Thrift.Transport.Server
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;

View File

@ -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 @@ namespace Thrift.Transport.Server
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 @@ namespace Thrift.Transport.Server
_port = port;
_serverCertificate = certificate;
_useBufferedSockets = useBufferedSockets;
_useFramedTransport = useFramedTransport;
_buffering = buffering;
_clientCertValidator = clientCertValidator;
_localCertificateSelectionCallback = localCertificateSelectionCallback;
_sslProtocols = sslProtocols;
@ -138,15 +124,20 @@ namespace Thrift.Transport.Server
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 @@ namespace Thrift.Transport.Server
}
}
}
}
}

View File

@ -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 @@ using System.Threading.Tasks;
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 @@ namespace Thrift.Transport
_isDisposed = true;
}
}
}
}

View File

@ -43,15 +43,6 @@ namespace ThriftTest
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 @@ namespace ThriftTest
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 @@ namespace ThriftTest
}
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 @@ namespace ThriftTest
// 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 @@ namespace ThriftTest
}
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 @@ namespace ThriftTest
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 @@ namespace ThriftTest
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" : "") +

View File

@ -143,13 +143,13 @@ Sample:
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);