Exemplo n.º 1
0
        async partial void SendTimeRequest()
        {
            var            socket      = new Windows.Networking.Sockets.DatagramSocket();
            AsyncUdpResult asyncResult = null;

            try
            {
                var buffer = new byte[48];
                buffer[0] = 0x1B;

                socket.MessageReceived += Socket_Completed_Receive;
                asyncResult             = new AsyncUdpResult(socket);

                await socket.ConnectAsync(new Windows.Networking.HostName(_ServerAddress), "123").AsTask().ConfigureAwait(false);

                using (var udpWriter = new DataWriter(socket.OutputStream))
                {
                    udpWriter.WriteBytes(buffer);
                    await udpWriter.StoreAsync().AsTask().ConfigureAwait(false);

                    udpWriter.WriteBytes(buffer);
                    await udpWriter.StoreAsync().AsTask().ConfigureAwait(false);

                    asyncResult.Wait(OneSecond);
                }
            }
            catch (Exception ex)
            {
                try
                {
                    if (socket != null)
                    {
                        ExecuteWithSuppressedExceptions(() => socket.MessageReceived -= this.Socket_Completed_Receive);
                        ExecuteWithSuppressedExceptions(() => socket.Dispose());
                    }
                }
                finally
                {
                    OnErrorOccurred(ExceptionToNtpNetworkException(ex));
                }
            }
            finally
            {
                asyncResult?.Dispose();
            }
        }
Exemplo n.º 2
0
		async partial void SendTimeRequest()
		{
			var socket = new Windows.Networking.Sockets.DatagramSocket();
			AsyncUdpResult asyncResult = null;
			try
			{
				var buffer = new byte[48];
				buffer[0] = 0x1B;

				socket.MessageReceived += Socket_Completed_Receive;
				asyncResult = new AsyncUdpResult(socket);
			
				await socket.ConnectAsync(new Windows.Networking.HostName(_ServerAddress), "123").AsTask().ConfigureAwait(false);

				using (var udpWriter = new DataWriter(socket.OutputStream))
				{
					udpWriter.WriteBytes(buffer);
					await udpWriter.StoreAsync().AsTask().ConfigureAwait(false);

					udpWriter.WriteBytes(buffer);
					await udpWriter.StoreAsync().AsTask().ConfigureAwait(false);

					asyncResult.Wait(OneSecond);
				}
			}
			catch (Exception ex)
			{
				try
				{
					if (socket != null)
					{
						ExecuteWithSuppressedExceptions(() => socket.MessageReceived -= this.Socket_Completed_Receive);
						ExecuteWithSuppressedExceptions(() => socket.Dispose());
					}
				}
				finally
				{
					OnErrorOccurred(ExceptionToNtpNetworkException(ex));
				}
			}
			finally
			{
				asyncResult?.Dispose();
			}
		}