C# Class Yort.Ntp.NtpClient

Afficher le fichier Open project: Yortw/Yort.Ntp Class Usage Examples

Méthodes publiques

Méthode 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.

Méthodes protégées

Méthode 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

Méthode 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 méthode

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
Résultat void

NtpClient() public méthode

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

NtpClient() public méthode

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

OnErrorOccurred() protected méthode

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.
Résultat void

OnTimeReceived() protected méthode

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.
Résultat void

RequestTimeAsync() public méthode

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
Résultat System.Threading.Tasks.Task