From 84abd33d48d1e003d53de2d4270eaf1d33fa0677 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roland=20K=C3=B6nig?= Date: Fri, 11 Sep 2020 15:57:09 +0200 Subject: [PATCH] Added access to ByteStreamHandler and MessageRecognizer of MessageChannel --- MessageCommunicator/MessageChannel.cs | 25 +++++++++++++++++++ .../_Tcp/TcpPassiveByteStreamHandler.cs | 20 +++++++++------ 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/MessageCommunicator/MessageChannel.cs b/MessageCommunicator/MessageChannel.cs index 3240f10..1225bd5 100644 --- a/MessageCommunicator/MessageChannel.cs +++ b/MessageCommunicator/MessageChannel.cs @@ -26,6 +26,12 @@ public IMessageReceiveHandler? ReceiveHandler set => _messageRecognizer.ReceiveHandler = value; } + /// + /// Access to internal objects. + /// Be careful when using them, wrong method calls can cause unexpected state! + /// + public MessageChannelInternals Internals { get; } + public MessageChannel( ByteStreamHandlerSettings byteStreamHandlerSettings, MessageRecognizerSettings messageRecognizerSettings, @@ -43,6 +49,8 @@ public MessageChannel( _messageRecognizer.ByteStreamHandler = _byteStreamHandler; this.ReceiveHandler = receiveHandler; + + this.Internals = new MessageChannelInternals(this); } public MessageChannel( @@ -90,5 +98,22 @@ public Task StopAsync() { return _byteStreamHandler.StopAsync(); } + + //********************************************************************* + //********************************************************************* + //********************************************************************* + public class MessageChannelInternals + { + private MessageChannel _owner; + + public MessageChannelInternals(MessageChannel owner) + { + _owner = owner; + } + + public IByteStreamHandler ByteStreamHandler => _owner._byteStreamHandler; + + public IMessageRecognizer MessageRecognizer => _owner._messageRecognizer; + } } } diff --git a/MessageCommunicator/_ByteStreamHandler/_Tcp/TcpPassiveByteStreamHandler.cs b/MessageCommunicator/_ByteStreamHandler/_Tcp/TcpPassiveByteStreamHandler.cs index dca5ad7..84a8d7f 100644 --- a/MessageCommunicator/_ByteStreamHandler/_Tcp/TcpPassiveByteStreamHandler.cs +++ b/MessageCommunicator/_ByteStreamHandler/_Tcp/TcpPassiveByteStreamHandler.cs @@ -22,6 +22,11 @@ public class TcpPassiveByteStreamHandler : TcpByteStreamHandler public ushort ListeningPort { get; } + /// + /// Gets the true listening port in case ListeningPort is set to 0. + /// + public ushort ActualListeningPort { get; private set; } + public override bool IsRunning => _isRunning; /// @@ -51,6 +56,7 @@ internal TcpPassiveByteStreamHandler( this.ListeningIPAddress = listeningIPAddress; this.ListeningPort = listeningPort; + this.ActualListeningPort = listeningPort; } /// @@ -122,11 +128,11 @@ private async void RunConnectionMainLoop(int loopId) CancellationTokenSource? lastCancelTokenSource = null; TcpListener? tcpListener = null; var reconnectErrorCount = 0; - var currentListenerPort = this.ListeningPort; while(loopId == _runningLoopCounter) { if (tcpListener == null) { + this.ActualListeningPort = this.ListeningPort; try { if (this.IsLoggerSet) @@ -137,7 +143,7 @@ private async void RunConnectionMainLoop(int loopId) } tcpListener = new TcpListener(this.ListeningIPAddress, this.ListeningPort); tcpListener.Start(); - currentListenerPort = (ushort)((IPEndPoint)tcpListener.LocalEndpoint).Port; + this.ActualListeningPort = (ushort)((IPEndPoint)tcpListener.LocalEndpoint).Port; reconnectErrorCount = 0; _currentListener = tcpListener; @@ -146,7 +152,7 @@ private async void RunConnectionMainLoop(int loopId) { this.Log( LoggingMessageType.Info, - StringBuffer.Format("TcpListener created for port {0}", currentListenerPort)); + StringBuffer.Format("TcpListener created for port {0}", this.ActualListeningPort)); } } catch (Exception ex) @@ -155,7 +161,7 @@ private async void RunConnectionMainLoop(int loopId) { this.Log( LoggingMessageType.Error, - StringBuffer.Format("Error while creating TcpListener for port {0}: {1}", currentListenerPort, ex.Message), + StringBuffer.Format("Error while creating TcpListener for port {0}: {1}", this.ActualListeningPort, ex.Message), exception: ex); } @@ -182,7 +188,7 @@ await this.WaitByReconnectWaitTimeAsync(reconnectErrorCount) this.Log( LoggingMessageType.Info, StringBuffer.Format("Listening for incoming connections on port {0}...", - currentListenerPort)); + this.ActualListeningPort)); } actTcpClient = await tcpListener.AcceptTcpClientAsync() @@ -196,7 +202,7 @@ await this.WaitByReconnectWaitTimeAsync(reconnectErrorCount) LoggingMessageType.Info, StringBuffer.Format( "Got new connection on listening port {0}. Connection established between {1} and {2}", - currentListenerPort, actLocalEndPoint.ToString(), actPartnerEndPoint.ToString())); + this.ActualListeningPort, actLocalEndPoint.ToString(), actPartnerEndPoint.ToString())); } } catch (ObjectDisposedException) @@ -212,7 +218,7 @@ await this.WaitByReconnectWaitTimeAsync(reconnectErrorCount) { this.Log( LoggingMessageType.Error, - StringBuffer.Format("Error while listening for incoming connections on port {0}: {1}", currentListenerPort, ex.Message), + StringBuffer.Format("Error while listening for incoming connections on port {0}: {1}", this.ActualListeningPort, ex.Message), exception: ex); }