From cb727583e5a3ceadca3ba0ca00fd197e29b45b52 Mon Sep 17 00:00:00 2001 From: Lucas Trzesniewski Date: Wed, 8 Nov 2023 17:58:40 +0100 Subject: [PATCH] Simplify binding code This also removes the capability of choosing the network interface by its primary IP address, since we've never needed it and the code got too confusing. --- .../Transport/ZmqTransportTests.cs | 11 +++++------ src/Abc.Zebus/Transport/ZmqEndPoint.cs | 15 ++++++++++++++- src/Abc.Zebus/Transport/ZmqInboundSocket.cs | 19 ++++--------------- 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/Abc.Zebus.Tests/Transport/ZmqTransportTests.cs b/src/Abc.Zebus.Tests/Transport/ZmqTransportTests.cs index 8e0d3856..bf791386 100644 --- a/src/Abc.Zebus.Tests/Transport/ZmqTransportTests.cs +++ b/src/Abc.Zebus.Tests/Transport/ZmqTransportTests.cs @@ -270,16 +270,15 @@ public void should_support_peer_endpoint_modifications() var senderTransport = CreateAndStartZmqTransport(); var receivedMessages = new ConcurrentBag(); - var receiverTransport = CreateAndStartZmqTransport(onMessageReceived: receivedMessages.Add); - var receiver = receiverTransport.GetPeer(); + var receiverTransport1 = CreateAndStartZmqTransport(onMessageReceived: receivedMessages.Add); + var receiverTransport2 = CreateAndStartZmqTransport(onMessageReceived: receivedMessages.Add); + var receiver = receiverTransport1.GetPeer(); senderTransport.Send(new FakeCommand(0).ToTransportMessage(), new[] { receiver }); Wait.Until(() => receivedMessages.Count == 1, 2.Seconds()); - var newEndPoint = "tcp://127.0.0.1:" + TcpUtil.GetRandomUnusedPort(); - receiverTransport.Stop(); - receiverTransport = CreateAndStartZmqTransport(newEndPoint, receivedMessages.Add); - receiver.EndPoint = receiverTransport.InboundEndPoint; + receiverTransport1.Stop(); + receiver.EndPoint = receiverTransport2.InboundEndPoint; senderTransport.Send(new FakeCommand(0).ToTransportMessage(), new[] { receiver }); Wait.Until(() => receivedMessages.Count == 2, 2.Seconds(), "unable to receive message"); diff --git a/src/Abc.Zebus/Transport/ZmqEndPoint.cs b/src/Abc.Zebus/Transport/ZmqEndPoint.cs index ca48f7a6..159f0275 100644 --- a/src/Abc.Zebus/Transport/ZmqEndPoint.cs +++ b/src/Abc.Zebus/Transport/ZmqEndPoint.cs @@ -1,7 +1,12 @@ -namespace Abc.Zebus.Transport; +using System; +using System.Text.RegularExpressions; + +namespace Abc.Zebus.Transport; internal readonly struct ZmqEndPoint { + private static readonly Regex _endpointRegex = new(@"^tcp://(?\*|[0-9a-zA-Z_.-]+):(?\*|[0-9]+)/?$", RegexOptions.IgnoreCase); + private readonly string? _value; public ZmqEndPoint(string? value) @@ -9,4 +14,12 @@ public ZmqEndPoint(string? value) public override string ToString() => _value ?? "tcp://*:*"; + + public static (string host, string port) Parse(string? endpoint) + { + var match = _endpointRegex.Match(endpoint ?? string.Empty); + return match.Success + ? (match.Groups["host"].Value, match.Groups["port"].Value) + : throw new InvalidOperationException($"Invalid endpoint: {endpoint}"); + } } diff --git a/src/Abc.Zebus/Transport/ZmqInboundSocket.cs b/src/Abc.Zebus/Transport/ZmqInboundSocket.cs index 93530876..184e9f8c 100644 --- a/src/Abc.Zebus/Transport/ZmqInboundSocket.cs +++ b/src/Abc.Zebus/Transport/ZmqInboundSocket.cs @@ -1,6 +1,5 @@ using System; using System.Net; -using System.Text.RegularExpressions; using Abc.Zebus.Serialization.Protobuf; using Abc.Zebus.Transport.Zmq; using Microsoft.Extensions.Logging; @@ -11,9 +10,6 @@ internal class ZmqInboundSocket : IDisposable { private static readonly ILogger _logger = ZebusLogManager.GetLogger(typeof(ZmqInboundSocket)); - private static readonly Regex _endpointRegex = new(@"^tcp://(?\*|[0-9a-zA-Z_.-]+):(?\*|[0-9]+)/?$", RegexOptions.IgnoreCase); - private static readonly Regex _ipRegex = new(@"^(?:[0-9]+\.){3}[0-9]+$"); - private readonly ZmqContext _context; private readonly ZmqEndPoint _configuredEndPoint; private readonly ZmqSocketOptions _options; @@ -32,11 +28,12 @@ public ZmqEndPoint Bind() { _socket = CreateSocket(); - var (configuredHost, configuredPort) = ParseEndpoint(_configuredEndPoint.ToString()); + var (configuredHost, configuredPort) = ZmqEndPoint.Parse(_configuredEndPoint.ToString()); + + _socket.Bind($"tcp://*:{configuredPort}"); - _socket.Bind($"tcp://{(_ipRegex.IsMatch(configuredHost) ? configuredHost : "*")}:{configuredPort}"); + var (boundHost, boundPort) = ZmqEndPoint.Parse(_socket.GetOptionString(ZmqSocketOption.LAST_ENDPOINT)); - var (boundHost, boundPort) = ParseEndpoint(_socket.GetOptionString(ZmqSocketOption.LAST_ENDPOINT)); if (boundHost == "0.0.0.0") { // Use the hostname from the config when one is provided, or the FQDN otherwise @@ -49,14 +46,6 @@ public ZmqEndPoint Bind() _logger.LogInformation($"Socket bound, Inbound EndPoint: {socketEndPoint}"); return socketEndPoint; - - static (string host, string port) ParseEndpoint(string endpoint) - { - var match = _endpointRegex.Match(endpoint); - return match.Success - ? (match.Groups["host"].Value, match.Groups["port"].Value) - : throw new InvalidOperationException($"Invalid endpoint: {endpoint}"); - } } public void Dispose()