Added IsOpen() and GetPort() for netstd transports
Client: netstd
Patch: Mario Emmenlauer
This closes #2166
diff --git a/lib/netstd/Thrift/Transport/Server/TNamedPipeServerTransport.cs b/lib/netstd/Thrift/Transport/Server/TNamedPipeServerTransport.cs
index aea0f86..3381110 100644
--- a/lib/netstd/Thrift/Transport/Server/TNamedPipeServerTransport.cs
+++ b/lib/netstd/Thrift/Transport/Server/TNamedPipeServerTransport.cs
@@ -44,6 +44,10 @@
_pipeAddress = pipeAddress;
}
+ public override bool IsOpen() {
+ return true;
+ }
+
public override void Listen()
{
// nothing to do here
diff --git a/lib/netstd/Thrift/Transport/Server/TServerSocketTransport.cs b/lib/netstd/Thrift/Transport/Server/TServerSocketTransport.cs
index 281c7ff..d7421c9 100644
--- a/lib/netstd/Thrift/Transport/Server/TServerSocketTransport.cs
+++ b/lib/netstd/Thrift/Transport/Server/TServerSocketTransport.cs
@@ -5,9 +5,9 @@
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
-//
+//
// http://www.apache.org/licenses/LICENSE-2.0
-//
+//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -54,6 +54,32 @@
}
}
+ public override bool IsOpen()
+ {
+ return (_server != null)
+ && (_server.Server != null)
+ && _server.Server.IsBound;
+ }
+
+ public int GetPort()
+ {
+ if ((_server != null) && (_server.Server != null) && (_server.Server.LocalEndPoint != null))
+ {
+ if (_server.Server.LocalEndPoint is IPEndPoint server)
+ {
+ return server.Port;
+ }
+ else
+ {
+ throw new TTransportException("ServerSocket is not a network socket");
+ }
+ }
+ else
+ {
+ throw new TTransportException("ServerSocket is not open");
+ }
+ }
+
public override void Listen()
{
// Make sure not to block on accept
@@ -91,7 +117,7 @@
try
{
- tSocketTransport = new TSocketTransport(tcpClient,Configuration)
+ tSocketTransport = new TSocketTransport(tcpClient, Configuration)
{
Timeout = _clientTimeout
};
@@ -106,7 +132,7 @@
}
else // Otherwise, clean it up ourselves.
{
- ((IDisposable) tcpClient).Dispose();
+ ((IDisposable)tcpClient).Dispose();
}
throw;
diff --git a/lib/netstd/Thrift/Transport/Server/TServerTransport.cs b/lib/netstd/Thrift/Transport/Server/TServerTransport.cs
index 31f578d..eee50fb 100644
--- a/lib/netstd/Thrift/Transport/Server/TServerTransport.cs
+++ b/lib/netstd/Thrift/Transport/Server/TServerTransport.cs
@@ -5,9 +5,9 @@
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
-//
+//
// http://www.apache.org/licenses/LICENSE-2.0
-//
+//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -30,6 +30,8 @@
Configuration = config ?? new TConfiguration();
}
+ public abstract bool IsOpen();
+
public abstract void Listen();
public abstract void Close();
public abstract bool IsClientPending();
@@ -41,7 +43,7 @@
protected abstract ValueTask<TTransport> AcceptImplementationAsync(CancellationToken cancellationToken);
- public async ValueTask<TTransport> AcceptAsync()
+ public async ValueTask<TTransport> AcceptAsync()
{
return await AcceptAsync(CancellationToken.None);
}
diff --git a/lib/netstd/Thrift/Transport/Server/TTlsServerSocketTransport.cs b/lib/netstd/Thrift/Transport/Server/TTlsServerSocketTransport.cs
index 79d2b11..77abcae 100644
--- a/lib/netstd/Thrift/Transport/Server/TTlsServerSocketTransport.cs
+++ b/lib/netstd/Thrift/Transport/Server/TTlsServerSocketTransport.cs
@@ -5,9 +5,9 @@
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
-//
+//
// http://www.apache.org/licenses/LICENSE-2.0
-//
+//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -36,7 +36,7 @@
private readonly X509Certificate2 _serverCertificate;
private readonly SslProtocols _sslProtocols;
private TcpListener _server;
-
+
public TTlsServerSocketTransport(
TcpListener listener,
TConfiguration config,
@@ -81,6 +81,32 @@
}
}
+ public override bool IsOpen()
+ {
+ return (_server != null)
+ && (_server.Server != null)
+ && _server.Server.IsBound;
+ }
+
+ public int GetPort()
+ {
+ if ((_server != null) && (_server.Server != null) && (_server.Server.LocalEndPoint != null))
+ {
+ if (_server.Server.LocalEndPoint is IPEndPoint server)
+ {
+ return server.Port;
+ }
+ else
+ {
+ throw new TTransportException("ServerSocket is not a network socket");
+ }
+ }
+ else
+ {
+ throw new TTransportException("ServerSocket is not open");
+ }
+ }
+
public override void Listen()
{
// Make sure accept is not blocking
@@ -123,7 +149,7 @@
_localCertificateSelectionCallback, _sslProtocols);
await tTlsSocket.SetupTlsAsync();
-
+
return tTlsSocket;
}
catch (Exception ex)