byLineAsync

HTTP/FTP fetch content as a range of lines asynchronously.

A range of lines is returned immediately and the request that fetches the lines is performed in another thread. If the method or other request properties is to be customized then set the conn parameter with a HTTP/FTP instance that has these properties set.

If postData is non-_null the method will be set to post for HTTP requests.

The background thread will buffer up to transmitBuffers number of lines before it stops receiving data from network. When the main thread reads the lines from the range it frees up buffers and allows for the background thread to receive more data from the network.

If no data is available and the main thread accesses the range it will block until data becomes available. An exception to this is the wait(Duration) method on the LineInputRange. This method will wait at maximum for the specified duration and return true if data is available.

  1. auto byLineAsync(const(char)[] url, const(PostUnit)[] postData, KeepTerminator keepTerminator, Terminator terminator, size_t transmitBuffers, Conn conn)
  2. auto byLineAsync(const(char)[] url, KeepTerminator keepTerminator, Terminator terminator, size_t transmitBuffers, Conn conn)
    byLineAsync
    (
    Conn = AutoProtocol
    Terminator = char
    Char = char
    )
    (
    const(char)[] url
    ,
    KeepTerminator keepTerminator = No.keepTerminator
    ,
    Terminator terminator = '\n'
    ,
    size_t transmitBuffers = 10
    ,
    Conn conn = Conn()
    )

Parameters

url const(char)[]

The url to receive content from

keepTerminator KeepTerminator

Yes.keepTerminator signals that the line terminator should be returned as part of the lines in the range.

terminator Terminator

The character that terminates a line

transmitBuffers size_t

The number of lines buffered asynchronously

conn Conn

The connection to use e.g. HTTP or FTP.

Return Value

Type: auto

A range of Char[] with the content of the resource pointer to by the URL.

Examples

import std.net.curl, std.stdio;
// Get some pages in the background
auto range1 = byLineAsync("www.google.com");
auto range2 = byLineAsync("www.wikipedia.org");
foreach (line; byLineAsync("dlang.org"))
    writeln(line);

// Lines already fetched in the background and ready
foreach (line; range1) writeln(line);
foreach (line; range2) writeln(line);
import std.net.curl, std.stdio;
// Get a line in a background thread and wait in
// main thread for 2 seconds for it to arrive.
auto range3 = byLineAsync("dlang.com");
if (range3.wait(dur!"seconds"(2)))
    writeln(range3.front);
else
    writeln("No line received after 2 seconds!");

Meta