Skip to content

Commit

Permalink
change connect strategy to support linux devices
Browse files Browse the repository at this point in the history
  • Loading branch information
mregen committed Jul 22, 2016
1 parent 0d200a1 commit 5733d9c
Showing 1 changed file with 33 additions and 25 deletions.
58 changes: 33 additions & 25 deletions Stack/Opc.Ua.Core/Stack/Tcp/TcpMessageSocket.cs
Expand Up @@ -131,16 +131,33 @@ public async Task<bool> BeginConnect(Uri endpointUrl, EventHandler<SocketAsyncEv
throw new InvalidOperationException("The socket is already connected.");
}

bool ipV6Required = false;
bool ipV4Required = false;
// Get DNS host information.
IPAddress[] hostAdresses = await Dns.GetHostAddressesAsync(endpointUrl.DnsSafeHost);

// need to check if an IP address was provided.
IPAddress address = null;
// try IPv4 and IPv6 address
IPAddress addressV4 = null;
IPAddress addressV6 = null;

if (IPAddress.TryParse(endpointUrl.DnsSafeHost, out address))
foreach (IPAddress address in hostAdresses)
{
ipV6Required = address.AddressFamily == AddressFamily.InterNetworkV6;
ipV4Required = address.AddressFamily == AddressFamily.InterNetwork;
if (address.AddressFamily == AddressFamily.InterNetworkV6)
{
if (addressV6 == null)
{
addressV6 = address;
}
}
if (address.AddressFamily == AddressFamily.InterNetwork)
{
if (addressV4 == null)
{
addressV4 = address;
}
}
if ((addressV4 != null) && (addressV6 != null))
{
break;
}
}

TaskCompletionSource<SocketError> tcs = new TaskCompletionSource<SocketError>();
Expand All @@ -151,18 +168,6 @@ public async Task<bool> BeginConnect(Uri endpointUrl, EventHandler<SocketAsyncEv

lock (m_socketLock)
{
// force sockets if IP address was provided
if (!ipV4Required)
{
m_socketV6 = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
m_socketResponses++;
}
if (!ipV6Required)
{
m_socketV4 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
m_socketResponses++;
}

// ensure a valid port.
int port = endpointUrl.Port;

Expand All @@ -171,15 +176,18 @@ public async Task<bool> BeginConnect(Uri endpointUrl, EventHandler<SocketAsyncEv
port = Utils.UaTcpDefaultPort;
}

if (address != null)
// create sockets if IP address was provided
if (addressV6 != null)
{
argsV4.RemoteEndPoint = new IPEndPoint(address, port);
argsV6.RemoteEndPoint = new IPEndPoint(address, port);
m_socketV6 = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
argsV6.RemoteEndPoint = new IPEndPoint(addressV6, port);
m_socketResponses++;
}
else
if (addressV4 != null)
{
argsV4.RemoteEndPoint = new DnsEndPoint(endpointUrl.DnsSafeHost, port);
argsV6.RemoteEndPoint = new DnsEndPoint(endpointUrl.DnsSafeHost, port);
m_socketV4 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
argsV4.RemoteEndPoint = new IPEndPoint(addressV4, port);
m_socketResponses++;
}

argsV4.Completed += (o, e) =>
Expand Down

0 comments on commit 5733d9c

Please sign in to comment.