private static IObservable <PingReply> CreatePingObservable(Ping ping, Action <object> start) { Contract.Requires(ping != null); Contract.Requires(start != null); Contract.Ensures(Contract.Result <IObservable <PingReply> >() != null); return(Observable2.FromEventBasedAsyncPattern <PingCompletedEventHandler, PingCompletedEventArgs>( h => h.Invoke, h => ping.PingCompleted += h, h => ping.PingCompleted -= h, start, ping.SendAsyncCancel) .Select(e => e.EventArgs.Reply)); }
/// <summary> /// Sends the specified e-mail message to an SMTP server for delivery. /// </summary> /// <param name="client">The object that sends the e-mail message.</param> /// <param name="message">A <see cref="MailMessage"/> that contains the message to send.</param> /// <returns>A singleton observable that contains the cached result of sending the specified <paramref name="message"/>.</returns> public static IObservable <EventPattern <AsyncCompletedEventArgs> > SendObservable( this SmtpClient client, MailMessage message) { Contract.Requires(client != null); Contract.Requires(message != null); Contract.Ensures(Contract.Result <IObservable <EventPattern <AsyncCompletedEventArgs> > >() != null); return(Observable2.FromEventBasedAsyncPattern <SendCompletedEventHandler>( d => d.Invoke, eh => client.SendCompleted += eh, eh => client.SendCompleted -= eh, token => client.SendAsync(message, token), client.SendAsyncCancel)); }
/// <summary> /// Downloads the specified resource as a <see cref="Byte"/> array. /// </summary> /// <param name="client">The object that downloads the resource.</param> /// <param name="address">A <see cref="Uri"/> containing the URI to download.</param> /// <returns>An observable that caches the result of the download and replays it to observers.</returns> public static IObservable <byte[]> DownloadDataObservable( this WebClient client, Uri address) { Contract.Requires(client != null); Contract.Requires(address != null); Contract.Ensures(Contract.Result <IObservable <byte[]> >() != null); return(Observable2.FromEventBasedAsyncPattern <DownloadDataCompletedEventHandler, DownloadDataCompletedEventArgs>( handler => handler.Invoke, handler => client.DownloadDataCompleted += handler, handler => client.DownloadDataCompleted -= handler, token => client.DownloadDataAsync(address, token), client.CancelAsync) .Select(e => e.EventArgs.Result)); }
/// <summary> /// Opens a writeable stream to the specified resource. /// </summary> /// <param name="client">The object that uploads to the resource.</param> /// <param name="address">The URI of the resource to receive the stream.</param> /// <param name="method">The HTTP method used to send data to the resource. If <see langword="null"/>, the default is POST for HTTP and STOR for FTP.</param> /// <returns>An observable containing the writeable stream that sends data to the resource.</returns> public static IObservable <Stream> OpenWriteObservable( this WebClient client, Uri address, string method) { Contract.Requires(client != null); Contract.Requires(address != null); Contract.Requires(method != null); Contract.Ensures(Contract.Result <IObservable <Stream> >() != null); return(Observable2.FromEventBasedAsyncPattern <OpenWriteCompletedEventHandler, OpenWriteCompletedEventArgs>( handler => handler.Invoke, handler => client.OpenWriteCompleted += handler, handler => client.OpenWriteCompleted -= handler, token => client.OpenWriteAsync(address, method, token), client.CancelAsync) .Select(e => e.EventArgs.Result)); }
/// <summary> /// Downloads the specified resource as a file. /// </summary> /// <param name="client">The object that downloads the resource.</param> /// <param name="address">A <see cref="Uri"/> containing the URI to download.</param> /// <param name="fileName">The file to create or overwrite with the resource.</param> /// <returns>An observable that caches the result of the download and replays it to observers.</returns> public static IObservable <string> DownloadFileObservable( this WebClient client, Uri address, string fileName) { Contract.Requires(client != null); Contract.Requires(address != null); Contract.Requires(fileName != null); Contract.Ensures(Contract.Result <IObservable <string> >() != null); return(Observable2.FromEventBasedAsyncPattern <AsyncCompletedEventHandler, AsyncCompletedEventArgs>( handler => handler.Invoke, handler => client.DownloadFileCompleted += handler, handler => client.DownloadFileCompleted -= handler, token => client.DownloadFileAsync(address, fileName, token), client.CancelAsync) .Select(e => fileName)); }
/// <summary> /// Uploads data to the specified resource. /// </summary> /// <param name="client">The object that uploads to the resource.</param> /// <param name="address">The URI of the resource to receive the collection.</param> /// <param name="method">The HTTP method used to send data to the resource. If <see langword="null"/>, the default is POST for HTTP and STOR for FTP.</param> /// <param name="values">The collection of data as name/value pairs to send to the resource.</param> /// <returns>An observable that caches the response from the server and replays it to observers.</returns> public static IObservable <byte[]> UploadValuesObservable( this WebClient client, Uri address, string method, NameValueCollection values) { Contract.Requires(client != null); Contract.Requires(address != null); Contract.Requires(values != null); Contract.Ensures(Contract.Result <IObservable <byte[]> >() != null); return(Observable2.FromEventBasedAsyncPattern <UploadValuesCompletedEventHandler, UploadValuesCompletedEventArgs>( handler => handler.Invoke, handler => client.UploadValuesCompleted += handler, handler => client.UploadValuesCompleted -= handler, token => client.UploadValuesAsync(address, method, values, token), client.CancelAsync) .Select(e => e.EventArgs.Result)); }
/// <summary> /// Sends an e-mail message to an SMTP server for delivery. /// </summary> /// <param name="client">The object that sends the e-mail message.</param> /// <param name="from">Contains the address information of the message sender.</param> /// <param name="recipients">Contains the addresses that the message is sent to.</param> /// <param name="subject">Contains the subject line for the message.</param> /// <param name="body">Contains the message body.</param> /// <returns>A singleton observable that contains the cached result of sending the message.</returns> public static IObservable <EventPattern <AsyncCompletedEventArgs> > SendObservable( this SmtpClient client, string from, string recipients, string subject, string body) { Contract.Requires(client != null); Contract.Requires(!string.IsNullOrEmpty(from)); Contract.Requires(!string.IsNullOrEmpty(recipients)); Contract.Ensures(Contract.Result <IObservable <EventPattern <AsyncCompletedEventArgs> > >() != null); return(Observable2.FromEventBasedAsyncPattern <SendCompletedEventHandler>( d => d.Invoke, eh => client.SendCompleted += eh, eh => client.SendCompleted -= eh, token => client.SendAsync(from, recipients, subject, body, token), client.SendAsyncCancel)); }
/// <summary> /// Uploads a string to the specified resource. /// </summary> /// <param name="client">The object that uploads to the resource.</param> /// <param name="address">The URI of the resource to receive the string.</param> /// <param name="method">The HTTP method used to send data to the resource. If <see langword="null"/>, the default is POST for HTTP and STOR for FTP.</param> /// <param name="data">The string to send to the resource.</param> /// <returns>An observable that caches the response from the server and replays it to observers.</returns> public static IObservable <string> UploadStringObservable( this WebClient client, Uri address, string method, string data) { Contract.Requires(client != null); Contract.Requires(address != null); Contract.Requires(method != null); Contract.Ensures(Contract.Result <IObservable <string> >() != null); return(Observable2.FromEventBasedAsyncPattern <UploadStringCompletedEventHandler, UploadStringCompletedEventArgs>( handler => handler.Invoke, handler => client.UploadStringCompleted += handler, handler => client.UploadStringCompleted -= handler, token => client.UploadStringAsync(address, method, data, token), client.CancelAsync) .Select(e => e.EventArgs.Result)); }
/// <summary> /// Downloads the specified resource as a <see cref="Byte"/> array and includes a channel for progress notifications. /// </summary> /// <param name="client">The object that downloads the resource.</param> /// <param name="address">A <see cref="Uri"/> containing the URI to download.</param> /// <returns>An observable that contains progress notifications in the left channel and the data in the right channel.</returns> public static IObservable <Either <DownloadProgressChangedEventArgs, byte[]> > DownloadDataWithProgress( this WebClient client, Uri address) { Contract.Requires(client != null); Contract.Requires(address != null); Contract.Ensures(Contract.Result <IObservable <Either <DownloadProgressChangedEventArgs, byte[]> > >() != null); return(Observable2.FromEventBasedAsyncPattern <DownloadDataCompletedEventHandler, DownloadDataCompletedEventArgs, DownloadProgressChangedEventHandler, DownloadProgressChangedEventArgs>( handler => handler.Invoke, handler => client.DownloadDataCompleted += handler, handler => client.DownloadDataCompleted -= handler, handler => handler.Invoke, handler => client.DownloadProgressChanged += handler, handler => client.DownloadProgressChanged -= handler, token => client.DownloadDataAsync(address, token), client.CancelAsync) .Select( left => left.EventArgs, right => right.EventArgs.Result)); }
/// <summary> /// Uploads a file to the specified resource and includes a channel for progress notifications. /// </summary> /// <param name="client">The object that uploads to the resource.</param> /// <param name="address">The URI of the resource to receive the file.</param> /// <param name="method">The HTTP method used to send data to the resource. If <see langword="null"/>, the default is POST for HTTP and STOR for FTP.</param> /// <param name="fileName">The file to upload to the resource.</param> /// <returns>An observable that contains progress notifications in the left channel and the server's response in the right channel.</returns> public static IObservable <Either <UploadProgressChangedEventArgs, byte[]> > UploadFileWithProgress( this WebClient client, Uri address, string method, string fileName) { Contract.Requires(client != null); Contract.Requires(address != null); Contract.Requires(fileName != null); Contract.Ensures(Contract.Result <IObservable <Either <UploadProgressChangedEventArgs, byte[]> > >() != null); return(Observable2.FromEventBasedAsyncPattern <UploadFileCompletedEventHandler, UploadFileCompletedEventArgs, UploadProgressChangedEventHandler, UploadProgressChangedEventArgs>( handler => handler.Invoke, handler => client.UploadFileCompleted += handler, handler => client.UploadFileCompleted -= handler, handler => handler.Invoke, handler => client.UploadProgressChanged += handler, handler => client.UploadProgressChanged -= handler, token => client.UploadFileAsync(address, method, fileName, token), client.CancelAsync) .Select( left => left.EventArgs, right => right.EventArgs.Result)); }