C# Class Yort.Ntp.NtpClient

Exibir arquivo Open project: Yortw/Yort.Ntp Class Usage Examples

Public Methods

Method Description
BeginRequestTime ( ) : void

Asynchronously requests a time from the NTP server specified in the constructor. When a time is received the TimeReceived event is raised with the result, otherwise the ErrorOccurred event should be raised containing details of the failure.

Note, events raised by this class may not (and probably will not) occur on the same thread that called this method. If the event handlers call UI components, dispatched invoke may be required.

This method may throw exceptions (most likely a NtpNetworkException if an error occurs trying to connect/bind to the network endpoint. Exception handling in client code is recommended.

NtpClient ( ) : System

Default constructor. Uses the KnownNtpServers.TimeANist server as a default.

NtpClient ( string serverAddress ) : System

Full constructor.

RequestTimeAsync ( ) : System.Threading.Tasks.Task

Returns an awaitable task whose result is the current time from the NTP server specified in the constructor.

This method may throw exceptions (most likely a NtpNetworkException if an error occurs trying to connect/bind to the network endpoint. Exception handling in client code is recommended.

Protected Methods

Method Description
OnErrorOccurred ( Exception exception ) : void

Raises the ErrorOccurred event.

This event may be raised on a different thread than called the BeginRequestTime method. If the event handler refers to UI, COM or other components that require thread affinity then dispatched invoke may be required.

OnTimeReceived ( System.DateTime time ) : void

Raises the TimeReceived event.

This event may be raised on a different thread than called the BeginRequestTime method. If the event handler refers to UI, COM or other components that require thread affinity then dispatched invoke may be required.

The time returned is a UTC time.

Private Methods

Method Description
ConvertBufferToCurrentTime ( byte buffer ) : void
ExceptionToNtpNetworkException ( Exception ex ) : NtpNetworkException
ExecuteWithSuppressedExceptions ( System.Action work ) : void
NtpNetworkExceptionFromSocketArgs ( SocketAsyncEventArgs e ) : NtpNetworkException
OnErrorOccurredAndDisposeSocket ( Exception exception, System.Net.Socket socket ) : void
SendTimeRequest ( ) : void
Socket_Completed_ProcessResult ( object sender, SocketAsyncEventArgs e ) : void
Socket_Completed_Receive ( Windows sender, Windows args ) : void
Socket_Completed_Receive ( object sender, SocketAsyncEventArgs e ) : void
Socket_Completed_SendAgain ( object sender, SocketAsyncEventArgs e ) : void
sArgs_Completed ( object sender, SocketAsyncEventArgs e ) : void

Method Details

BeginRequestTime() public method

Asynchronously requests a time from the NTP server specified in the constructor. When a time is received the TimeReceived event is raised with the result, otherwise the ErrorOccurred event should be raised containing details of the failure.

Note, events raised by this class may not (and probably will not) occur on the same thread that called this method. If the event handlers call UI components, dispatched invoke may be required.

This method may throw exceptions (most likely a NtpNetworkException if an error occurs trying to connect/bind to the network endpoint. Exception handling in client code is recommended.

public BeginRequestTime ( ) : void
return void

NtpClient() public method

Default constructor. Uses the KnownNtpServers.TimeANist server as a default.
public NtpClient ( ) : System
return System

NtpClient() public method

Full constructor.
public NtpClient ( string serverAddress ) : System
serverAddress string the name or address the NTP server to be used.
return System

OnErrorOccurred() protected method

Raises the ErrorOccurred event.

This event may be raised on a different thread than called the BeginRequestTime method. If the event handler refers to UI, COM or other components that require thread affinity then dispatched invoke may be required.

protected OnErrorOccurred ( Exception exception ) : void
exception System.Exception A derived instance describing the error.
return void

OnTimeReceived() protected method

Raises the TimeReceived event.

This event may be raised on a different thread than called the BeginRequestTime method. If the event handler refers to UI, COM or other components that require thread affinity then dispatched invoke may be required.

The time returned is a UTC time.

protected OnTimeReceived ( System.DateTime time ) : void
time System.DateTime The date and time received from the NTP server.
return void

RequestTimeAsync() public method

Returns an awaitable task whose result is the current time from the NTP server specified in the constructor.

This method may throw exceptions (most likely a NtpNetworkException if an error occurs trying to connect/bind to the network endpoint. Exception handling in client code is recommended.

public RequestTimeAsync ( ) : System.Threading.Tasks.Task
return System.Threading.Tasks.Task