Skip to content

Commit 156950b

Browse files
committed
Upgrades library to use some C# 7 features.
1 parent da9972d commit 156950b

File tree

16 files changed

+114
-209
lines changed

16 files changed

+114
-209
lines changed

NetworkTables.sln

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,37 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetworkTables.Core.Test", "
1616
EndProject
1717
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetworkTables.Test", "test\NetworkTables.Test\NetworkTables.Test.csproj", "{06EBE437-BE46-4225-95DA-250472C7BEA3}"
1818
EndProject
19+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Shared", "Shared", "{88646071-EBE3-46F7-9DA6-64FF32464975}"
20+
ProjectSection(SolutionItems) = preProject
21+
src\Shared\ConnectionInfo.cs = src\Shared\ConnectionInfo.cs
22+
src\Shared\Delegates.cs = src\Shared\Delegates.cs
23+
src\Shared\EntryInfo.cs = src\Shared\EntryInfo.cs
24+
src\Shared\Enumerations.cs = src\Shared\Enumerations.cs
25+
src\Shared\Wire\FastBitConverterBE.cs = src\Shared\Wire\FastBitConverterBE.cs
26+
src\Shared\Wire\FastBitConverterCache.cs = src\Shared\Wire\FastBitConverterCache.cs
27+
src\Shared\Wire\FastBitConverterLE.cs = src\Shared\Wire\FastBitConverterLE.cs
28+
src\Shared\Wire\IFastBitConverter.cs = src\Shared\Wire\IFastBitConverter.cs
29+
src\Shared\Tables\IRemote.cs = src\Shared\Tables\IRemote.cs
30+
src\Shared\Tables\IRemoteConnectionListener.cs = src\Shared\Tables\IRemoteConnectionListener.cs
31+
src\Shared\Tables\ITable.cs = src\Shared\Tables\ITable.cs
32+
src\Shared\Tables\ITableListener.cs = src\Shared\Tables\ITableListener.cs
33+
src\Shared\Wire\Leb128.cs = src\Shared\Wire\Leb128.cs
34+
src\Shared\ListStream.cs = src\Shared\ListStream.cs
35+
src\Shared\NetworkTable.cs = src\Shared\NetworkTable.cs
36+
src\Shared\NtCore.cs = src\Shared\NtCore.cs
37+
src\Shared\NtIPAddress.cs = src\Shared\NtIPAddress.cs
38+
src\Shared\NtType.cs = src\Shared\NtType.cs
39+
src\Shared\Exceptions\PersistentException.cs = src\Shared\Exceptions\PersistentException.cs
40+
src\Shared\RemoteProcedureCall.cs = src\Shared\RemoteProcedureCall.cs
41+
src\Shared\RpcStructs.cs = src\Shared\RpcStructs.cs
42+
src\Shared\StaticRemote.cs = src\Shared\StaticRemote.cs
43+
src\Shared\Exceptions\TableKeyDifferentTypeException.cs = src\Shared\Exceptions\TableKeyDifferentTypeException.cs
44+
src\Shared\Exceptions\TableKeyNotDefinedException.cs = src\Shared\Exceptions\TableKeyNotDefinedException.cs
45+
src\Shared\Value.cs = src\Shared\Value.cs
46+
src\Shared\Wire\WireDecoder.cs = src\Shared\Wire\WireDecoder.cs
47+
src\Shared\Wire\WireEncoder.cs = src\Shared\Wire\WireEncoder.cs
48+
EndProjectSection
49+
EndProject
1950
Global
2051
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2152
Debug|Any CPU = Debug|Any CPU
@@ -96,5 +127,6 @@ Global
96127
{EE176693-9FBA-43C6-B97A-D7A79DEE16B6} = {9C2F4909-9BE2-437D-862C-4E23155C2A7A}
97128
{070CCC8C-ECEE-4B59-A94A-A4BD1E330803} = {44BF4692-6DAD-4EEA-9A02-63920115AF2B}
98129
{06EBE437-BE46-4225-95DA-250472C7BEA3} = {44BF4692-6DAD-4EEA-9A02-63920115AF2B}
130+
{88646071-EBE3-46F7-9DA6-64FF32464975} = {9C2F4909-9BE2-437D-862C-4E23155C2A7A}
99131
EndGlobalSection
100132
EndGlobal

src/FRC.NetworkTables.Core/Native/CoreMethods.cs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -970,8 +970,7 @@ internal static void CreateRpc(string name, IList<byte> def, RpcCallback callbac
970970
return retPtr;
971971
};
972972
var nameB = CreateCachedUTF8String(name);
973-
byte[] bArr = def as byte[];
974-
if (bArr != null)
973+
if (def is byte[] bArr)
975974
{
976975
Interop.NT_CreateRpc(nameB.Buffer, nameB.Length, bArr, (UIntPtr)def.Count, IntPtr.Zero, modCallback);
977976
}
@@ -986,8 +985,7 @@ internal static void CreateRpc(string name, IList<byte> def, RpcCallback callbac
986985
internal static void CreatePolledRpc(string name, IList<byte> def)
987986
{
988987
var nameB = CreateCachedUTF8String(name);
989-
byte[] bArr = def as byte[];
990-
if (bArr != null)
988+
if (def is byte[] bArr)
991989
{
992990
Interop.NT_CreatePolledRpc(nameB.Buffer, nameB.Length, bArr, (UIntPtr)def.Count);
993991
}
@@ -999,9 +997,7 @@ internal static void CreatePolledRpc(string name, IList<byte> def)
999997

1000998
internal static bool PollRpc(bool blocking, TimeSpan timeout, out RpcCallInfo callInfo)
1001999
{
1002-
NtRpcCallInfo nativeInfo;
1003-
int retVal = Interop.NT_PollRpcTimeout(blocking ? 1 : 0, timeout.TotalSeconds, out nativeInfo);
1004-
if (retVal == 0)
1000+
if (Interop.NT_PollRpcTimeout(blocking ? 1 : 0, timeout.TotalSeconds, out NtRpcCallInfo nativeInfo) == 0)
10051001
{
10061002
callInfo = new RpcCallInfo();
10071003
return false;
@@ -1012,8 +1008,7 @@ internal static bool PollRpc(bool blocking, TimeSpan timeout, out RpcCallInfo ca
10121008

10131009
internal static bool PollRpc(bool blocking, out RpcCallInfo callInfo)
10141010
{
1015-
NtRpcCallInfo nativeInfo;
1016-
int retVal = Interop.NT_PollRpc(blocking ? 1 : 0, out nativeInfo);
1011+
int retVal = Interop.NT_PollRpc(blocking ? 1 : 0, out var nativeInfo);
10171012
if (retVal == 0)
10181013
{
10191014
callInfo = new RpcCallInfo();
@@ -1025,8 +1020,7 @@ internal static bool PollRpc(bool blocking, out RpcCallInfo callInfo)
10251020

10261021
internal static void PostRpcResponse(long rpcId, long callUid, IList<byte> result)
10271022
{
1028-
var bArr = result as byte[];
1029-
if (bArr != null)
1023+
if (result is byte[] bArr)
10301024
{
10311025
Interop.NT_PostRpcResponse((uint)rpcId, (uint)callUid, bArr, (UIntPtr)result.Count);
10321026
}
@@ -1039,8 +1033,7 @@ internal static void PostRpcResponse(long rpcId, long callUid, IList<byte> resul
10391033
internal static long CallRpc(string name, IList<byte> param)
10401034
{
10411035
var nameB = CreateCachedUTF8String(name);
1042-
var bArr = param as byte[];
1043-
if (bArr != null)
1036+
if (param is byte[] bArr)
10441037
{
10451038
return Interop.NT_CallRpc(nameB.Buffer, nameB.Length, bArr, (UIntPtr)param.Count);
10461039
}
@@ -1062,8 +1055,7 @@ internal static async Task<byte[]> GetRpcResultAsync(long callUid, CancellationT
10621055
{
10631056
var result = await Task.Run(() =>
10641057
{
1065-
byte[] results;
1066-
bool success = GetRpcResult(true, callUid, out results);
1058+
bool success = GetRpcResult(true, callUid, out byte[] results);
10671059
if (success)
10681060
{
10691061
return results;

src/FRC.NetworkTables/DSClient.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,7 @@ public async Task ThreadMainAsync(CancellationToken token)
197197
Debug3(Logger.Instance, $"found robotIP={ipString}");
198198

199199
// Parse into number
200-
uint ip;
201-
if (!uint.TryParse(ipString, out ip)) continue;
200+
if (!uint.TryParse(ipString, out uint ip)) continue;
202201

203202
if (BitConverter.IsLittleEndian)
204203
{

src/FRC.NetworkTables/DispatcherBase.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,7 @@ private void ServerThreadMain()
360360
}
361361
if (!m_active) return;
362362

363-
IPEndPoint ipEp = stream.RemoteEndPoint as IPEndPoint;
364-
if (ipEp != null)
363+
if (stream.RemoteEndPoint is IPEndPoint ipEp)
365364
{
366365
Debug(Logger.Instance, $"server: client connection from {ipEp.Address} port {ipEp.Port.ToString()}");
367366
}

src/FRC.NetworkTables/FRC.NetworkTables.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<ItemGroup>
2828
<PackageReference Include="Nito.AsyncEx.Context" Version="1.1.0" />
2929
<PackageReference Include="Nito.AsyncEx.Coordination" Version="1.0.2" />
30-
30+
<PackageReference Include="System.ValueTuple" Version="4.3.1" />
3131
</ItemGroup>
3232
<ItemGroup Condition=" '$(TargetFramework)' == 'net46' ">
3333
<Reference Include="System" />

src/FRC.NetworkTables/ImmutablePair.cs

Lines changed: 0 additions & 46 deletions
This file was deleted.

src/FRC.NetworkTables/Independent/IndependentNetworkTable.cs

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,7 @@ public bool[] GetBooleanArray(string key, bool[] defaultValue)
416416
///<inheritdoc/>
417417
public void AddTableListenerEx(ITableListener listener, NotifyFlags flags)
418418
{
419-
List<int> adapters;
420-
if (!m_listenerMap.TryGetValue(listener, out adapters))
419+
if (!m_listenerMap.TryGetValue(listener, out List<int> adapters))
421420
{
422421
adapters = new List<int>();
423422
m_listenerMap.Add(listener, adapters);
@@ -442,8 +441,7 @@ public void AddTableListenerEx(ITableListener listener, NotifyFlags flags)
442441
///<inheritdoc/>
443442
public void AddTableListenerEx(string key, ITableListener listener, NotifyFlags flags)
444443
{
445-
List<int> adapters;
446-
if (!m_listenerMap.TryGetValue(listener, out adapters))
444+
if (!m_listenerMap.TryGetValue(listener, out List<int> adapters))
447445
{
448446
adapters = new List<int>();
449447
m_listenerMap.Add(listener, adapters);
@@ -465,8 +463,7 @@ public void AddTableListenerEx(string key, ITableListener listener, NotifyFlags
465463
///<inheritdoc/>
466464
public void AddSubTableListener(ITableListener listener, bool localNotify)
467465
{
468-
List<int> adapters;
469-
if (!m_listenerMap.TryGetValue(listener, out adapters))
466+
if (!m_listenerMap.TryGetValue(listener, out List<int> adapters))
470467
{
471468
adapters = new List<int>();
472469
m_listenerMap.Add(listener, adapters);
@@ -520,8 +517,7 @@ public void AddSubTableListener(ITableListener listener)
520517
///<inheritdoc/>
521518
public void RemoveTableListener(ITableListener listener)
522519
{
523-
List<int> adapters;
524-
if (m_listenerMap.TryGetValue(listener, out adapters))
520+
if (m_listenerMap.TryGetValue(listener, out List<int> adapters))
525521
{
526522
foreach (int t in adapters)
527523
{
@@ -535,8 +531,7 @@ public void RemoveTableListener(ITableListener listener)
535531
///<inheritdoc/>
536532
public void AddTableListenerEx(Action<ITable, string, Value, NotifyFlags> listenerDelegate, NotifyFlags flags)
537533
{
538-
List<int> adapters;
539-
if (!m_actionListenerMap.TryGetValue(listenerDelegate, out adapters))
534+
if (!m_actionListenerMap.TryGetValue(listenerDelegate, out List<int> adapters))
540535
{
541536
adapters = new List<int>();
542537
m_actionListenerMap.Add(listenerDelegate, adapters);
@@ -561,8 +556,7 @@ public void AddTableListenerEx(Action<ITable, string, Value, NotifyFlags> listen
561556
///<inheritdoc/>
562557
public void AddTableListenerEx(string key, Action<ITable, string, Value, NotifyFlags> listenerDelegate, NotifyFlags flags)
563558
{
564-
List<int> adapters;
565-
if (!m_actionListenerMap.TryGetValue(listenerDelegate, out adapters))
559+
if (!m_actionListenerMap.TryGetValue(listenerDelegate, out List<int> adapters))
566560
{
567561
adapters = new List<int>();
568562
m_actionListenerMap.Add(listenerDelegate, adapters);
@@ -584,8 +578,7 @@ public void AddTableListenerEx(string key, Action<ITable, string, Value, NotifyF
584578
///<inheritdoc/>
585579
public void AddSubTableListener(Action<ITable, string, Value, NotifyFlags> listenerDelegate, bool localNotify)
586580
{
587-
List<int> adapters;
588-
if (!m_actionListenerMap.TryGetValue(listenerDelegate, out adapters))
581+
if (!m_actionListenerMap.TryGetValue(listenerDelegate, out List<int> adapters))
589582
{
590583
adapters = new List<int>();
591584
m_actionListenerMap.Add(listenerDelegate, adapters);
@@ -639,8 +632,7 @@ public void AddSubTableListener(Action<ITable, string, Value, NotifyFlags> liste
639632
///<inheritdoc/>
640633
public void RemoveTableListener(Action<ITable, string, Value, NotifyFlags> listenerDelegate)
641634
{
642-
List<int> adapters;
643-
if (m_actionListenerMap.TryGetValue(listenerDelegate, out adapters))
635+
if (m_actionListenerMap.TryGetValue(listenerDelegate, out List<int> adapters))
644636
{
645637
foreach (int t in adapters)
646638
{
@@ -678,8 +670,7 @@ public void AddConnectionListener(IRemoteConnectionListener listener, bool immed
678670
///<inheritdoc/>
679671
public void RemoveConnectionListener(IRemoteConnectionListener listener)
680672
{
681-
int val;
682-
if (m_connectionListenerMap.TryGetValue(listener, out val))
673+
if (m_connectionListenerMap.TryGetValue(listener, out int val))
683674
{
684675
m_ntCore.RemoveConnectionListener(val);
685676
}
@@ -704,8 +695,7 @@ public void AddConnectionListener(Action<IRemote, ConnectionInfo, bool> listener
704695
/// <inheritdoc/>
705696
public void RemoveConnectionListener(Action<IRemote, ConnectionInfo, bool> listener)
706697
{
707-
int val;
708-
if (m_actionConnectionListenerMap.TryGetValue(listener, out val))
698+
if (m_actionConnectionListenerMap.TryGetValue(listener, out int val))
709699
{
710700
m_ntCore.RemoveConnectionListener(val);
711701
}

src/FRC.NetworkTables/NetworkConnection.cs

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,6 @@ namespace NetworkTables
1818
{
1919
internal class NetworkConnection : IDisposable
2020
{
21-
private struct PendingUpdateIds
22-
{
23-
public int First { get; private set; }
24-
public int Second { get; private set; }
25-
26-
public void SetFirst(int first)
27-
{
28-
First = first;
29-
}
30-
31-
public void SetSecond(int second)
32-
{
33-
Second = second;
34-
}
35-
}
36-
3721
public int ProtoRev { get; set; }
3822

3923

@@ -78,7 +62,7 @@ public enum State { Created, Init, Handshake, Synchronized, Active, Dead };
7862

7963
private readonly List<Message> m_pendingOutgoing = new List<Message>();
8064

81-
private readonly List<PendingUpdateIds> m_pendingUpdate = new List<PendingUpdateIds>();
65+
private readonly List<(int First, int Second)> m_pendingUpdate = new List<(int First, int Second)>();
8266

8367
public NetworkConnection(IClient client, Notifier notifier, HandshakeFunc handshake,
8468
Message.GetEntryTypeFunc getEntryType)
@@ -95,8 +79,7 @@ public NetworkConnection(IClient client, Notifier notifier, HandshakeFunc handsh
9579
m_state = State.Created;
9680
LastUpdate = 0;
9781

98-
IPEndPoint ipEp = m_client.RemoteEndPoint as IPEndPoint;
99-
if (ipEp != null)
82+
if (m_client.RemoteEndPoint is IPEndPoint ipEp)
10083
{
10184
PeerIP = ipEp.Address.ToString();
10285
PeerPort = ipEp.Port;
@@ -213,7 +196,7 @@ private void ResizePendingUpdate(int newSize)
213196
{
214197
if (newSize > m_pendingUpdate.Capacity)
215198
m_pendingUpdate.Capacity = newSize;
216-
m_pendingUpdate.AddRange(Enumerable.Repeat(default(PendingUpdateIds), newSize - currentSize));
199+
m_pendingUpdate.AddRange(Enumerable.Repeat(default((int First, int Second)), newSize - currentSize));
217200
}
218201
}
219202

@@ -258,7 +241,7 @@ public void QueueOutgoing(Message msg)
258241
int pos = m_pendingOutgoing.Count;
259242
m_pendingOutgoing.Add(msg);
260243
if (id >= m_pendingUpdate.Count) ResizePendingUpdate(id + 1);
261-
m_pendingUpdate[id].SetFirst(pos + 1);
244+
m_pendingUpdate[id] = (pos + 1, m_pendingUpdate[id].Second);
262245
}
263246
break;
264247
}
@@ -277,12 +260,12 @@ public void QueueOutgoing(Message msg)
277260
if (m_pendingUpdate[id].First != 0)
278261
{
279262
m_pendingOutgoing[m_pendingUpdate[id].First - 1] = new Message();
280-
m_pendingUpdate[id].SetFirst(0);
263+
m_pendingUpdate[id] = (0, m_pendingUpdate[id].Second);
281264
}
282265
if (m_pendingUpdate[id].Second != 0)
283266
{
284267
m_pendingOutgoing[m_pendingUpdate[id].Second - 1] = new Message();
285-
m_pendingUpdate[id].SetSecond(0);
268+
m_pendingUpdate[id] = (m_pendingUpdate[id].First, 0);
286269
}
287270
}
288271
//Add deletion
@@ -309,7 +292,7 @@ public void QueueOutgoing(Message msg)
309292
int pos = m_pendingOutgoing.Count;
310293
m_pendingOutgoing.Add(msg);
311294
if (id > m_pendingUpdate.Count) ResizePendingUpdate(id + 1);
312-
m_pendingUpdate[id].SetSecond(pos + 1);
295+
m_pendingUpdate[id] = (m_pendingUpdate[id].First, pos + 1);
313296

314297
}
315298
break;

0 commit comments

Comments
 (0)