Skip to content

Commit 84abd33

Browse files
committed
Added access to ByteStreamHandler and MessageRecognizer of MessageChannel
1 parent 95d0820 commit 84abd33

2 files changed

Lines changed: 38 additions & 7 deletions

File tree

MessageCommunicator/MessageChannel.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ public IMessageReceiveHandler? ReceiveHandler
2626
set => _messageRecognizer.ReceiveHandler = value;
2727
}
2828

29+
/// <summary>
30+
/// Access to internal objects.
31+
/// Be careful when using them, wrong method calls can cause unexpected state!
32+
/// </summary>
33+
public MessageChannelInternals Internals { get; }
34+
2935
public MessageChannel(
3036
ByteStreamHandlerSettings byteStreamHandlerSettings,
3137
MessageRecognizerSettings messageRecognizerSettings,
@@ -43,6 +49,8 @@ public MessageChannel(
4349
_messageRecognizer.ByteStreamHandler = _byteStreamHandler;
4450

4551
this.ReceiveHandler = receiveHandler;
52+
53+
this.Internals = new MessageChannelInternals(this);
4654
}
4755

4856
public MessageChannel(
@@ -90,5 +98,22 @@ public Task StopAsync()
9098
{
9199
return _byteStreamHandler.StopAsync();
92100
}
101+
102+
//*********************************************************************
103+
//*********************************************************************
104+
//*********************************************************************
105+
public class MessageChannelInternals
106+
{
107+
private MessageChannel _owner;
108+
109+
public MessageChannelInternals(MessageChannel owner)
110+
{
111+
_owner = owner;
112+
}
113+
114+
public IByteStreamHandler ByteStreamHandler => _owner._byteStreamHandler;
115+
116+
public IMessageRecognizer MessageRecognizer => _owner._messageRecognizer;
117+
}
93118
}
94119
}

MessageCommunicator/_ByteStreamHandler/_Tcp/TcpPassiveByteStreamHandler.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ public class TcpPassiveByteStreamHandler : TcpByteStreamHandler
2222

2323
public ushort ListeningPort { get; }
2424

25+
/// <summary>
26+
/// Gets the true listening port in case ListeningPort is set to 0.
27+
/// </summary>
28+
public ushort ActualListeningPort { get; private set; }
29+
2530
public override bool IsRunning => _isRunning;
2631

2732
/// <inheritdoc />
@@ -51,6 +56,7 @@ internal TcpPassiveByteStreamHandler(
5156

5257
this.ListeningIPAddress = listeningIPAddress;
5358
this.ListeningPort = listeningPort;
59+
this.ActualListeningPort = listeningPort;
5460
}
5561

5662
/// <inheritdoc />
@@ -122,11 +128,11 @@ private async void RunConnectionMainLoop(int loopId)
122128
CancellationTokenSource? lastCancelTokenSource = null;
123129
TcpListener? tcpListener = null;
124130
var reconnectErrorCount = 0;
125-
var currentListenerPort = this.ListeningPort;
126131
while(loopId == _runningLoopCounter)
127132
{
128133
if (tcpListener == null)
129134
{
135+
this.ActualListeningPort = this.ListeningPort;
130136
try
131137
{
132138
if (this.IsLoggerSet)
@@ -137,7 +143,7 @@ private async void RunConnectionMainLoop(int loopId)
137143
}
138144
tcpListener = new TcpListener(this.ListeningIPAddress, this.ListeningPort);
139145
tcpListener.Start();
140-
currentListenerPort = (ushort)((IPEndPoint)tcpListener.LocalEndpoint).Port;
146+
this.ActualListeningPort = (ushort)((IPEndPoint)tcpListener.LocalEndpoint).Port;
141147

142148
reconnectErrorCount = 0;
143149
_currentListener = tcpListener;
@@ -146,7 +152,7 @@ private async void RunConnectionMainLoop(int loopId)
146152
{
147153
this.Log(
148154
LoggingMessageType.Info,
149-
StringBuffer.Format("TcpListener created for port {0}", currentListenerPort));
155+
StringBuffer.Format("TcpListener created for port {0}", this.ActualListeningPort));
150156
}
151157
}
152158
catch (Exception ex)
@@ -155,7 +161,7 @@ private async void RunConnectionMainLoop(int loopId)
155161
{
156162
this.Log(
157163
LoggingMessageType.Error,
158-
StringBuffer.Format("Error while creating TcpListener for port {0}: {1}", currentListenerPort, ex.Message),
164+
StringBuffer.Format("Error while creating TcpListener for port {0}: {1}", this.ActualListeningPort, ex.Message),
159165
exception: ex);
160166
}
161167

@@ -182,7 +188,7 @@ await this.WaitByReconnectWaitTimeAsync(reconnectErrorCount)
182188
this.Log(
183189
LoggingMessageType.Info,
184190
StringBuffer.Format("Listening for incoming connections on port {0}...",
185-
currentListenerPort));
191+
this.ActualListeningPort));
186192
}
187193

188194
actTcpClient = await tcpListener.AcceptTcpClientAsync()
@@ -196,7 +202,7 @@ await this.WaitByReconnectWaitTimeAsync(reconnectErrorCount)
196202
LoggingMessageType.Info,
197203
StringBuffer.Format(
198204
"Got new connection on listening port {0}. Connection established between {1} and {2}",
199-
currentListenerPort, actLocalEndPoint.ToString(), actPartnerEndPoint.ToString()));
205+
this.ActualListeningPort, actLocalEndPoint.ToString(), actPartnerEndPoint.ToString()));
200206
}
201207
}
202208
catch (ObjectDisposedException)
@@ -212,7 +218,7 @@ await this.WaitByReconnectWaitTimeAsync(reconnectErrorCount)
212218
{
213219
this.Log(
214220
LoggingMessageType.Error,
215-
StringBuffer.Format("Error while listening for incoming connections on port {0}: {1}", currentListenerPort, ex.Message),
221+
StringBuffer.Format("Error while listening for incoming connections on port {0}: {1}", this.ActualListeningPort, ex.Message),
216222
exception: ex);
217223
}
218224

0 commit comments

Comments
 (0)