byChunkAsync

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

A range of chunks is returned immediately and the request that fetches the chunks 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 chunks before is stops receiving data from network. When the main thread reads the chunks 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 access the range it will block until data becomes available. An exception to this is the wait(Duration) method on the ChunkInputRange. This method will wait at maximum for the specified duration and return true if data is available.

  1. auto byChunkAsync(const(char)[] url, const(PostUnit)[] postData, size_t chunkSize, size_t transmitBuffers, Conn conn)
    byChunkAsync
    (
    Conn = AutoProtocol
    PostUnit
    )
    (
    const(char)[] url
    ,
    const(PostUnit)[] postData
    ,
    size_t chunkSize = 1024
    ,
    size_t transmitBuffers = 10
    ,
    Conn conn = Conn()
    )
    if (
    isCurlConn!(Conn)
    )
  2. auto byChunkAsync(const(char)[] url, size_t chunkSize, size_t transmitBuffers, Conn conn)

Parameters

url const(char)[]

The url to receive content from

postData const(PostUnit)[]

Data to HTTP Post

chunkSize size_t

The size of the chunks

transmitBuffers size_t

The number of chunks buffered asynchronously

conn Conn

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

Return Value

Type: auto

A range of ubytechunkSize 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 = byChunkAsync("www.google.com", 100);
auto range2 = byChunkAsync("www.wikipedia.org");
foreach (chunk; byChunkAsync("dlang.org"))
    writeln(chunk); // chunk is ubyte[100]

// Chunks already fetched in the background and ready
foreach (chunk; range1) writeln(chunk);
foreach (chunk; range2) writeln(chunk);
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 = byChunkAsync("dlang.com", 10);
if (range3.wait(dur!"seconds"(2)))
    writeln(range3.front);
else
    writeln("No chunk received after 2 seconds!");

Meta