Skip to content

Commit 9d85a86

Browse files
committed
Switches connections to attempt to connect to IP addresses in parallel
Needs to be tested on linux.
1 parent 156950b commit 9d85a86

File tree

11 files changed

+100
-451
lines changed

11 files changed

+100
-451
lines changed

src/FRC.NetworkTables/Dispatcher.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using NetworkTables.Interfaces;
55
using NetworkTables.TcpSockets;
66
using NetworkTables.Logging;
7+
using System;
78

89
namespace NetworkTables
910
{
@@ -45,22 +46,23 @@ public void StartServer(string persistentFilename, string listenAddress, int por
4546

4647
public void SetServer(string serverName, int port)
4748
{
48-
SetConnector(() => TcpConnector.Connect(serverName, port, Logger.Instance, 1));
49+
SetConnector(() => TcpConnector.Connect(new List<(string server, int port)> { (serverName, port) }, Logger.Instance, TimeSpan.FromSeconds(1)));
4950
}
5051

5152
public void SetServer(IList<NtIPAddress> servers)
5253
{
53-
List<Connector> connectors = new List<Connector>();
54+
List<(string server, int port)> addresses = new List<(string server, int port)>(servers.Count);
5455
foreach (var server in servers)
5556
{
56-
connectors.Add(() => TcpConnector.Connect(server.IpAddress, server.Port, Logger.Instance, 1));
57+
addresses.Add(server);
5758
}
58-
SetConnector(connectors);
59+
60+
SetConnector(() => TcpConnector.Connect(addresses, Logger.Instance, TimeSpan.FromSeconds(1)));
5961
}
6062

6163
public void SetServerOverride(IPAddress address, int port)
6264
{
63-
SetConnectorOverride(() => TcpConnector.Connect(address.ToString(), port, Logger.Instance, 1));
65+
SetConnectorOverride(() => TcpConnector.Connect(new List<(string server, int port)> { (address.ToString(), port) }, Logger.Instance, TimeSpan.FromSeconds(1)));
6466
}
6567

6668
public void ClearServerOverride()

src/FRC.NetworkTables/DispatcherBase.cs

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
using System.Threading.Tasks;
99
using Nito.AsyncEx.Synchronous;
1010
using NetworkTables.Logging;
11+
using System.Net.Sockets;
1112

1213
namespace NetworkTables
1314
{
1415
internal class DispatcherBase : IDisposable
1516
{
16-
public delegate NtTcpClient Connector();
17+
public delegate TcpClient Connector();
1718

1819
public const double MinimumUpdateTime = 0.01; //100ms
1920
public const double MaximumUpdateTime = 1.0; //1 second
@@ -38,7 +39,7 @@ internal class DispatcherBase : IDisposable
3839
private bool m_doReconnect = true;
3940
private string m_identity = "";
4041

41-
private IList<Connector> m_clientConnectors = new List<Connector>();
42+
private Connector m_clientConnector;
4243

4344
private DateTime m_lastFlush;
4445

@@ -153,15 +154,10 @@ public void StartClient()
153154
}
154155

155156
public void SetConnector(Connector connector)
156-
{
157-
SetConnector(new List<Connector>() { connector });
158-
}
159-
160-
public void SetConnector(IList<Connector> connectors)
161157
{
162158
lock (m_userMutex)
163159
{
164-
m_clientConnectors = connectors;
160+
m_clientConnector = connector;
165161
}
166162
}
167163

@@ -191,7 +187,7 @@ public void Stop()
191187
//Wake up client thread with a reconnect
192188
lock (m_userMutex)
193189
{
194-
m_clientConnectors.Clear();
190+
m_clientConnector = null;
195191
}
196192
ClientReconnect();
197193

@@ -360,7 +356,7 @@ private void ServerThreadMain()
360356
}
361357
if (!m_active) return;
362358

363-
if (stream.RemoteEndPoint is IPEndPoint ipEp)
359+
if (stream.Client.RemoteEndPoint is IPEndPoint ipEp)
364360
{
365361
Debug(Logger.Instance, $"server: client connection from {ipEp.Address} port {ipEp.Port.ToString()}");
366362
}
@@ -397,7 +393,6 @@ private void ServerThreadMain()
397393

398394
private void ClientThreadMain()
399395
{
400-
int i = 0;
401396
while (m_active)
402397
{
403398
//Sleep between retries
@@ -407,16 +402,7 @@ private void ClientThreadMain()
407402

408403
lock (m_userMutex)
409404
{
410-
if (m_clientConnectorOverride != null)
411-
{
412-
connect = m_clientConnectorOverride;
413-
}
414-
else
415-
{
416-
if (m_clientConnectors.Count == 0) continue;
417-
if (i >= m_clientConnectors.Count) i = 0;
418-
connect = m_clientConnectors[i++];
419-
}
405+
connect = m_clientConnectorOverride ?? m_clientConnector;
420406
}
421407

422408
Debug(Logger.Instance, "client trying to connect");

src/FRC.NetworkTables/NetworkConnection.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
using System.Threading;
66
using NetworkTables.Streams;
77
using NetworkTables.Support;
8-
using NetworkTables.TcpSockets;
98
using NetworkTables.Wire;
109
using static NetworkTables.Logging.Logger;
1110
using System.IO;
1211
using System.Net;
1312
using System.Threading.Tasks;
1413
using Nito.AsyncEx.Synchronous;
1514
using NetworkTables.Logging;
15+
using System.Net.Sockets;
1616

1717
namespace NetworkTables
1818
{
@@ -32,7 +32,7 @@ public enum State { Created, Init, Handshake, Synchronized, Active, Dead };
3232
private readonly Stream m_stream;
3333

3434
// ReSharper disable once PrivateFieldCanBeConvertedToLocalVariable
35-
private readonly IClient m_client;
35+
private readonly TcpClient m_client;
3636

3737
public int PeerPort { get; }
3838
public string PeerIP { get; }
@@ -64,7 +64,7 @@ public enum State { Created, Init, Handshake, Synchronized, Active, Dead };
6464

6565
private readonly List<(int First, int Second)> m_pendingUpdate = new List<(int First, int Second)>();
6666

67-
public NetworkConnection(IClient client, Notifier notifier, HandshakeFunc handshake,
67+
public NetworkConnection(TcpClient client, Notifier notifier, HandshakeFunc handshake,
6868
Message.GetEntryTypeFunc getEntryType)
6969
{
7070
Uid = (uint)Interlocked.Increment(ref s_uid) - 1;
@@ -79,7 +79,7 @@ public NetworkConnection(IClient client, Notifier notifier, HandshakeFunc handsh
7979
m_state = State.Created;
8080
LastUpdate = 0;
8181

82-
if (m_client.RemoteEndPoint is IPEndPoint ipEp)
82+
if (m_client.Client.RemoteEndPoint is IPEndPoint ipEp)
8383
{
8484
PeerIP = ipEp.Address.ToString();
8585
PeerPort = ipEp.Port;

src/FRC.NetworkTables/TcpSockets/IClient.cs

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
using System;
2+
using System.Net.Sockets;
23

34
namespace NetworkTables.TcpSockets
45
{
56
internal interface INetworkAcceptor: IDisposable
67
{
78
int Start();
89
void Shutdown();
9-
IClient Accept();
10+
TcpClient Accept();
1011
}
1112
}

0 commit comments

Comments
 (0)