Skip to content

Commit 819b5e5

Browse files
author
Troy Willmot
committed
Some code clean ups/reductions in allocations.
Add test to call client multiple times in quick succesion to try to reproduce issue. Add logic to detect zero offset returned or zero bytes transferred (with success status) and report as error via error handler. Ignore nuget.exe and config.
1 parent a0dc9de commit 819b5e5

File tree

5 files changed

+47
-7
lines changed

5 files changed

+47
-7
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,4 @@ GeneratedArtifacts/
212212
_Pvt_Extensions/
213213
ModelManifest.xml
214214
/src/Yort.Ntp.Android/Yort.Ntp.Android.csproj.bak
215+
/.nuget

src/Yort.Ntp.Net40.Tests/NtpClientTests.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,33 @@ public void NtpClient_DefaultServer_GetsNonNullResponse()
2323
client.ErrorOccurred += Client_ErrorOccurred;
2424
client.BeginRequestTime();
2525
_GotResultSignal.WaitOne(1000);
26-
Assert.AreNotEqual(null, _Result);
26+
Assert.IsNotNull(_Result);
27+
}
28+
finally
29+
{
30+
client.TimeReceived -= this.Client_TimeReceived;
31+
client.ErrorOccurred -= this.Client_ErrorOccurred;
32+
}
33+
}
34+
35+
[TestMethod]
36+
[TestCategory("NetworkRequiredTests")]
37+
public void NtpClient_DefaultServer_GetsValidResponsesOverMultipleRequests()
38+
{
39+
var ntpEpoch = new DateTime(1900, 01, 01, 0, 0, 0, DateTimeKind.Utc);
40+
_GotResultSignal = new System.Threading.AutoResetEvent(false);
41+
var client = new Yort.Ntp.NtpClient();
42+
try
43+
{
44+
client.TimeReceived += Client_TimeReceived;
45+
client.ErrorOccurred += Client_ErrorOccurred;
46+
for (int cnt = 0; cnt < 60; cnt++)
47+
{
48+
client.BeginRequestTime();
49+
_GotResultSignal.WaitOne(1000);
50+
Assert.IsNotNull(_Result);
51+
Assert.AreNotEqual(ntpEpoch, _Result);
52+
}
2753
}
2854
finally
2955
{
@@ -34,6 +60,7 @@ public void NtpClient_DefaultServer_GetsNonNullResponse()
3460

3561
private void Client_ErrorOccurred(object sender, NtpNetworkErrorEventArgs e)
3662
{
63+
Assert.Fail(e.Exception.Message);
3764
_GotResultSignal.Set();
3865
}
3966

src/Yort.Ntp.Shared.StandardSockets/NtpClientStandardSockets.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,12 @@ private void Socket_Completed_ProcessResult(object sender, SocketAsyncEventArgs
173173
socket = ((System.Net.Sockets.Socket)sender);
174174

175175
if (e.SocketError == SocketError.Success)
176-
ConvertBufferToCurrentTime(e.Buffer);
176+
{
177+
if (e.BytesTransferred == 0)
178+
OnErrorOccurred(new NtpNetworkException("No data received."));
179+
else
180+
ConvertBufferToCurrentTime(e.Buffer);
181+
}
177182
else
178183
OnErrorOccurred(NtpNetworkExceptionFromSocketArgs(e));
179184
}

src/Yort.Ntp.SharedImplementation/AssemblyInfoCommon.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#endif
1414
[assembly: AssemblyCompany("Yort")]
1515
[assembly: AssemblyProduct("Yort.Ntp")]
16-
[assembly: AssemblyCopyright("Copyright © 2017.")]
16+
[assembly: AssemblyCopyright("Copyright © 2018.")]
1717
[assembly: AssemblyCulture("")]
1818

1919
// Setting ComVisible to false makes the types in this assembly not visible
@@ -32,5 +32,5 @@
3232
// Build Number
3333
// Revision
3434
//
35-
[assembly: AssemblyVersion("1.0.12.0")]
36-
[assembly: AssemblyFileVersion("1.0.12.0")]
35+
[assembly: AssemblyVersion("1.0.13.0")]
36+
[assembly: AssemblyFileVersion("1.0.13.0")]

src/Yort.Ntp.SharedImplementation/NtpClient.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public partial class NtpClient
1515

1616
private readonly string _ServerAddress;
1717

18+
private static readonly DateTime NtpEpoch = new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Utc);
19+
1820
#endregion
1921

2022
#region Events
@@ -199,9 +201,14 @@ private void ConvertBufferToCurrentTime(byte[] buffer)
199201
}
200202

201203
ulong milliseconds = (hTime * 1000 + (lTime * 1000) / 0x100000000L);
204+
if (milliseconds == 0)
205+
{
206+
OnErrorOccurred(new NtpNetworkException("Incomplete or invalid data received."));
207+
return;
208+
}
202209

203-
var timeSpan = TimeSpan.FromTicks((long)milliseconds * TimeSpan.TicksPerMillisecond);
204-
var currentTime = new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Utc) + timeSpan;
210+
var timeSpan = TimeSpan.FromMilliseconds(milliseconds);
211+
var currentTime = NtpEpoch + timeSpan;
205212

206213
OnTimeReceived(currentTime);
207214
}

0 commit comments

Comments
 (0)