Skip to content

Commit

Permalink
Added new project for .Net Compact Framework 3.5.
Browse files Browse the repository at this point in the history
Cloned original GuerrillaNtp project and made nessisary changes.

New project was built using VS2008, latest version of VS compatible is CF.
  • Loading branch information
Matthew Hertzfeld committed Mar 2, 2020
1 parent a4898c9 commit c5c95b2
Show file tree
Hide file tree
Showing 7 changed files with 687 additions and 0 deletions.
75 changes: 75 additions & 0 deletions GuerrillaNtp_CF/GuerrillaNtp_CF.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.30729</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{6A920E41-4597-4312-A5D0-43CC0DE70EBC}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>GuerrillaNtp</RootNamespace>
<AssemblyName>GuerrillaNtp_CF</AssemblyName>
<ProjectTypeGuids>{4D628B5B-2FBC-4AA6-8C16-197242AEB884};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<PlatformFamilyName>PocketPC</PlatformFamilyName>
<PlatformID>b2c48bd2-963d-4549-9169-1fa021dce484</PlatformID>
<OSVersion>5.2</OSVersion>
<DeployDirSuffix>GuerrillaNtp_CF</DeployDirSuffix>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<NativePlatformName>Windows Mobile 6 Professional SDK</NativePlatformName>
<FormFactorID>
</FormFactorID>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;$(PlatformFamilyName)</DefineConstants>
<NoStdLib>true</NoStdLib>
<NoConfig>true</NoConfig>
<ErrorReport>prompt</ErrorReport>
<FileAlignment>512</FileAlignment>
<WarningLevel>4</WarningLevel>
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE;$(PlatformFamilyName)</DefineConstants>
<NoStdLib>true</NoStdLib>
<NoConfig>true</NoConfig>
<ErrorReport>prompt</ErrorReport>
<FileAlignment>512</FileAlignment>
<WarningLevel>4</WarningLevel>
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
</PropertyGroup>
<ItemGroup>
<Reference Include="mscorlib" />
<Reference Include="System" />
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<Compile Include="NtpClient.cs" />
<Compile Include="NtpException.cs" />
<Compile Include="NtpLeapIndicator.cs" />
<Compile Include="NtpMode.cs" />
<Compile Include="NtpPacket.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CompactFramework.CSharp.targets" />
<ProjectExtensions>
<VisualStudio>
<FlavorProperties GUID="{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}">
<HostingProcess disable="1" />
</FlavorProperties>
</VisualStudio>
</ProjectExtensions>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
144 changes: 144 additions & 0 deletions GuerrillaNtp_CF/NtpClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// Part of GuerrillaNtp: https://guerrillantp.machinezoo.com
using System;
using System.Net;
using System.Net.Sockets;

namespace GuerrillaNtp
{
/// <summary>
/// Represents UDP socket used to communicate with RFC4330-compliant SNTP/NTP server.
/// </summary>
/// <remarks>
/// <para>
/// See <a href="https://guerrillantp.machinezoo.com/">project homepage</a> for guidance on how to use GuerrillaNtp.
/// Most applications should just call <see cref="M:GuerrillaNtp.NtpClient.GetCorrectionOffset" />
/// after instantiating this class. Method <see cref="M:GuerrillaNtp.NtpClient.Query" />
/// can be used to obtain additional details stored in reply <see cref="T:GuerrillaNtp.NtpPacket" />.
/// </para>
/// <para>
/// This class holds unmanaged resources (the socket) and callers are responsible
/// for calling <see cref="M:GuerrillaNtp.NtpClient.Dispose" /> when they are done,
/// perhaps by instantiating this class in <c>using</c> block.
/// </para>
/// <para>
/// It is application responsibility to be a good netizen,
/// which most importantly means using reasonable polling intervals
/// and exponential backoff when querying public NTP server.
/// </para>
/// </remarks>
public class NtpClient
{
readonly Socket socket;

/// <summary>
/// Creates new <see cref="T:GuerrillaNtp.NtpClient" /> from server endpoint.
/// </summary>
/// <param name="endpoint">Endpoint of the remote SNTP server.</param>
/// <seealso cref="M:GuerrillaNtp.NtpClient.#ctor(System.Net.IPAddress,System.Int32)" />
/// <seealso cref="M:GuerrillaNtp.NtpClient.Dispose" />
public NtpClient(IPEndPoint endpoint)
{
socket = new Socket(endpoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
try
{
socket.Connect(endpoint);
}
catch
{
throw;
}
}

/// <summary>
/// Creates new <see cref="T:GuerrillaNtp.NtpClient" /> from server's IP address and optional port.
/// </summary>
/// <param name="address">IP address of remote SNTP server, standard NTP port is used.</param>
/// <seealso cref="M:GuerrillaNtp.NtpClient.#ctor(System.Net.IPEndPoint)" />
/// <seealso cref="M:GuerrillaNtp.NtpClient.Dispose" />
public NtpClient(IPAddress address) : this(new IPEndPoint(address, 123)) { }

/// <summary>
/// Creates new <see cref="T:GuerrillaNtp.NtpClient" /> from server's IP address and optional port.
/// </summary>
/// <param name="address">IP address of remote SNTP server</param>
/// <param name="port">Port of remote SNTP server. Default is 123 (standard NTP port).</param>
/// <seealso cref="M:GuerrillaNtp.NtpClient.#ctor(System.Net.IPEndPoint)" />
/// <seealso cref="M:GuerrillaNtp.NtpClient.Dispose" />
public NtpClient(IPAddress address, int port) : this(new IPEndPoint(address, port)) { }

/// <summary>
/// Queries the SNTP server and returns correction offset.
/// </summary>
/// <remarks>
/// Use this method if you just want correction offset from the server.
/// Call <see cref="M:GuerrillaNtp.NtpClient.Query" /> to obtain <see cref="T:GuerrillaNtp.NtpPacket" />
/// with additional information besides <see cref="P:GuerrillaNtp.NtpPacket.CorrectionOffset" />.
/// </remarks>
/// <returns>
/// Offset that should be added to local time to match server time.
/// </returns>
/// <exception cref="T:GuerrillaNtp.NtpException">Thrown when the server responds with invalid reply packet.</exception>
/// <exception cref="T:System.Net.Sockets.SocketException">
/// Thrown when no reply is received before <see cref="P:GuerrillaNtp.NtpClient.Timeout" /> is reached
/// or when there is an error communicating with the server.
/// </exception>
/// <seealso cref="M:GuerrillaNtp.NtpClient.Query" />
/// <seealso cref="P:GuerrillaNtp.NtpPacket.CorrectionOffset" />
public TimeSpan GetCorrectionOffset() { return Query().CorrectionOffset; }

/// <summary>
/// Queries the SNTP server with configurable <see cref="T:GuerrillaNtp.NtpPacket" /> request.
/// </summary>
/// <param name="request">SNTP request packet to use when querying the network time server.</param>
/// <returns>SNTP reply packet returned by the server.</returns>
/// <remarks>
/// <see cref="M:GuerrillaNtp.NtpPacket.#ctor" /> constructor
/// creates valid request packet, which you can further customize.
/// If you don't need any customization of the request packet, call <see cref="M:GuerrillaNtp.NtpClient.Query" /> instead.
/// Returned <see cref="T:GuerrillaNtp.NtpPacket" /> contains correction offset in
/// <see cref="P:GuerrillaNtp.NtpPacket.CorrectionOffset" /> property.
/// </remarks>
/// <exception cref="T:GuerrillaNtp.NtpException">
/// Thrown when the request packet is invalid or when the server responds with invalid reply packet.
/// </exception>
/// <exception cref="T:System.Net.Sockets.SocketException">
/// Thrown when no reply is received before <see cref="P:GuerrillaNtp.NtpClient.Timeout" /> is reached
/// or when there is an error communicating with the server.
/// </exception>
/// <seealso cref="M:GuerrillaNtp.NtpClient.GetCorrectionOffset" />
/// <seealso cref="P:GuerrillaNtp.NtpPacket.CorrectionOffset" />
/// <seealso cref="M:GuerrillaNtp.NtpClient.Query" />
public NtpPacket Query(NtpPacket request)
{
request.ValidateRequest();
socket.Send(request.Bytes);
var response = new byte[160];
int received = socket.Receive(response);
var truncated = new byte[received];
Array.Copy(response, truncated, received);
NtpPacket reply = new NtpPacket(truncated) { DestinationTimestamp = DateTime.UtcNow };
reply.ValidateReply(request);
return reply;
}

/// <summary>
/// Queries the SNTP server with default options.
/// </summary>
/// <remarks>
/// Use this method to obtain additional details from the returned <see cref="T:GuerrillaNtp.NtpPacket" />
/// besides <see cref="P:GuerrillaNtp.NtpPacket.CorrectionOffset" />.
/// If you just need the correction offset, call <see cref="M:GuerrillaNtp.NtpClient.GetCorrectionOffset" /> instead.
/// You can customize request packed by calling <see cref="M:GuerrillaNtp.NtpClient.Query(GuerrillaNtp.NtpPacket)" />.
/// </remarks>
/// <returns>SNTP reply packet returned by the server.</returns>
/// <exception cref="T:GuerrillaNtp.NtpException">Thrown when the server responds with invalid reply packet.</exception>
/// <exception cref="T:System.Net.Sockets.SocketException">
/// Thrown when no reply is received before <see cref="P:GuerrillaNtp.NtpClient.Timeout" /> is reached
/// or when there is an error communicating with the server.
/// </exception>
/// <seealso cref="M:GuerrillaNtp.NtpClient.GetCorrectionOffset" />
/// <seealso cref="P:GuerrillaNtp.NtpPacket.CorrectionOffset" />
/// <seealso cref="M:GuerrillaNtp.NtpClient.Query(GuerrillaNtp.NtpPacket)" />
public NtpPacket Query() { return Query(new NtpPacket()); }
}
}
26 changes: 26 additions & 0 deletions GuerrillaNtp_CF/NtpException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Part of GuerrillaNtp: https://guerrillantp.machinezoo.com
using System;

namespace GuerrillaNtp
{
/// <summary>
/// Represents errors that occur in SNTP packets or during SNTP operation.
/// </summary>
public class NtpException : Exception
{
/// <summary>
/// Gets the SNTP packet that caused this exception, if any.
/// </summary>
/// <value>
/// SNTP packet that caused this exception, usually reply packet,
/// or <c>null</c> if the error is not specific to any packet.
/// </value>
public NtpPacket Packet { get; private set; }

internal NtpException(NtpPacket packet, String message)
: base(message)
{
Packet = packet;
}
}
}
30 changes: 30 additions & 0 deletions GuerrillaNtp_CF/NtpLeapIndicator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Part of GuerrillaNtp: https://guerrillantp.machinezoo.com
namespace GuerrillaNtp
{
/// <summary>
/// Represents leap second warning from the server that instructs the client to add or remove leap second.
/// </summary>
/// <seealso cref="P:GuerrillaNtp.NtpPacket.LeapIndicator" />
public enum NtpLeapIndicator
{
/// <summary>
/// No leap second warning. No action required.
/// </summary>
NoWarning,

/// <summary>
/// Warns the client that the last minute of the current day has 61 seconds.
/// </summary>
LastMinuteHas61Seconds,

/// <summary>
/// Warns the client that the last minute of the current day has 59 seconds.
/// </summary>
LastMinuteHas59Seconds,

/// <summary>
/// Special value indicating that the server clock is unsynchronized and the returned time is unreliable.
/// </summary>
AlarmCondition
}
}
20 changes: 20 additions & 0 deletions GuerrillaNtp_CF/NtpMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Part of GuerrillaNtp: https://guerrillantp.machinezoo.com
namespace GuerrillaNtp
{
/// <summary>
/// Describes SNTP packet mode, i.e. client or server.
/// </summary>
/// <seealso cref="P:GuerrillaNtp.NtpPacket.Mode" />
public enum NtpMode
{
/// <summary>
/// Identifies client-to-server SNTP packet.
/// </summary>
Client = 3,

/// <summary>
/// Identifies server-to-client SNTP packet.
/// </summary>
Server = 4,
}
}
Loading

0 comments on commit c5c95b2

Please sign in to comment.