2016-10-26 11:11:28 +00:00
|
|
|
/**
|
|
|
|
Generic stream interface used by several stream-like classes.
|
|
|
|
|
|
|
|
This module defines the basic (buffered) stream primitives. For concrete stream types, take a
|
|
|
|
look at the `vibe.stream` package. The `vibe.stream.operations` module contains additional
|
|
|
|
high-level operations on streams, such as reading streams by line or as a whole.
|
|
|
|
|
2016-11-02 19:59:55 +00:00
|
|
|
Note that starting with vibe-core 1.0.0, streams can be of either `struct` or `class` type.
|
|
|
|
Any APIs that take streams as a parameter should use a template type parameter that is tested
|
|
|
|
using the appropriate trait (e.g. `isInputStream`) instead of assuming the specific interface
|
|
|
|
type (e.g. `InputStream`).
|
|
|
|
|
2020-01-27 18:20:52 +00:00
|
|
|
Copyright: © 2012-2016 Sönke Ludwig
|
2016-10-26 11:11:28 +00:00
|
|
|
License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
|
|
|
|
Authors: Sönke Ludwig
|
|
|
|
*/
|
2016-06-18 08:00:02 +00:00
|
|
|
module vibe.core.stream;
|
|
|
|
|
2016-10-26 11:11:28 +00:00
|
|
|
import vibe.internal.traits : checkInterfaceConformance, validateInterfaceConformance;
|
2017-01-31 15:48:04 +00:00
|
|
|
import vibe.internal.interfaceproxy;
|
2016-10-26 11:11:28 +00:00
|
|
|
import core.time;
|
|
|
|
import std.algorithm;
|
|
|
|
import std.conv;
|
|
|
|
|
2017-01-18 23:36:32 +00:00
|
|
|
public import eventcore.driver : IOMode;
|
|
|
|
|
|
|
|
|
2017-01-25 21:42:35 +00:00
|
|
|
/** Pipes an InputStream directly into this OutputStream.
|
|
|
|
|
2020-03-14 17:37:54 +00:00
|
|
|
The number of bytes written is either the whole input stream when
|
|
|
|
`nbytes == ulong.max`, or exactly `nbytes` for `nbytes < ulong.max`. If the
|
|
|
|
input stream contains less than `nbytes` of data, an exception is thrown.
|
2017-10-30 21:55:54 +00:00
|
|
|
|
|
|
|
Returns:
|
2020-03-14 17:37:54 +00:00
|
|
|
The actual number of bytes written is returned. If `nbytes` is given
|
2017-10-30 21:55:54 +00:00
|
|
|
and not equal to `ulong.max`, íts value will be returned.
|
2017-01-25 21:42:35 +00:00
|
|
|
*/
|
2017-10-30 21:55:54 +00:00
|
|
|
ulong pipe(InputStream, OutputStream)(InputStream source, OutputStream sink, ulong nbytes)
|
2017-01-25 21:42:35 +00:00
|
|
|
@blocking @trusted
|
|
|
|
if (isOutputStream!OutputStream && isInputStream!InputStream)
|
|
|
|
{
|
2018-01-14 03:22:31 +00:00
|
|
|
import vibe.internal.allocator : theAllocator, makeArray, dispose;
|
2017-01-25 21:42:35 +00:00
|
|
|
|
2018-01-14 03:22:31 +00:00
|
|
|
scope buffer = cast(ubyte[]) theAllocator.allocate(64*1024);
|
|
|
|
scope (exit) theAllocator.dispose(buffer);
|
2017-01-25 21:42:35 +00:00
|
|
|
|
|
|
|
//logTrace("default write %d bytes, empty=%s", nbytes, stream.empty);
|
2017-10-30 21:55:54 +00:00
|
|
|
ulong ret = 0;
|
2017-01-25 21:42:35 +00:00
|
|
|
if (nbytes == ulong.max) {
|
|
|
|
while (!source.empty) {
|
|
|
|
size_t chunk = min(source.leastSize, buffer.length);
|
|
|
|
assert(chunk > 0, "leastSize returned zero for non-empty stream.");
|
|
|
|
//logTrace("read pipe chunk %d", chunk);
|
|
|
|
source.read(buffer[0 .. chunk]);
|
|
|
|
sink.write(buffer[0 .. chunk]);
|
2017-10-30 21:55:54 +00:00
|
|
|
ret += chunk;
|
2017-01-25 21:42:35 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (nbytes > 0) {
|
|
|
|
size_t chunk = min(nbytes, buffer.length);
|
|
|
|
//logTrace("read pipe chunk %d", chunk);
|
|
|
|
source.read(buffer[0 .. chunk]);
|
|
|
|
sink.write(buffer[0 .. chunk]);
|
|
|
|
nbytes -= chunk;
|
2017-10-30 21:55:54 +00:00
|
|
|
ret += chunk;
|
2017-01-25 21:42:35 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-30 21:55:54 +00:00
|
|
|
return ret;
|
2017-01-25 21:42:35 +00:00
|
|
|
}
|
|
|
|
/// ditto
|
2017-10-30 21:55:54 +00:00
|
|
|
ulong pipe(InputStream, OutputStream)(InputStream source, OutputStream sink)
|
2017-01-25 21:42:35 +00:00
|
|
|
@blocking
|
|
|
|
if (isOutputStream!OutputStream && isInputStream!InputStream)
|
|
|
|
{
|
2017-10-30 21:55:54 +00:00
|
|
|
return pipe(source, sink, ulong.max);
|
2017-01-25 21:42:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-18 23:36:32 +00:00
|
|
|
/** Marks a function as blocking.
|
|
|
|
|
|
|
|
Blocking in this case means that it may contain an operation that needs to wait for
|
|
|
|
external events, such as I/O operations, and may result in other tasks in the same
|
|
|
|
threa being executed before it returns.
|
|
|
|
|
|
|
|
Currently this attribute serves only as a documentation aid and is not enforced
|
|
|
|
or used for deducation in any way.
|
|
|
|
*/
|
|
|
|
struct blocking {}
|
2016-10-26 11:11:28 +00:00
|
|
|
|
|
|
|
/**************************************************************************************************/
|
|
|
|
/* Public functions */
|
|
|
|
/**************************************************************************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
Returns a `NullOutputStream` instance.
|
|
|
|
|
|
|
|
The instance will only be created on the first request and gets reused for
|
|
|
|
all subsequent calls from the same thread.
|
|
|
|
*/
|
|
|
|
NullOutputStream nullSink() @safe nothrow
|
|
|
|
{
|
|
|
|
static NullOutputStream ret;
|
|
|
|
if (!ret) ret = new NullOutputStream;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**************************************************************************************************/
|
|
|
|
/* Public types */
|
|
|
|
/**************************************************************************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
Interface for all classes implementing readable streams.
|
|
|
|
*/
|
|
|
|
interface InputStream {
|
|
|
|
@safe:
|
|
|
|
|
|
|
|
/** Returns true $(I iff) the end of the input stream has been reached.
|
2017-01-18 23:36:32 +00:00
|
|
|
|
|
|
|
For connection oriented streams, this function will block until either
|
|
|
|
new data arrives or the connection got closed.
|
2016-10-26 11:11:28 +00:00
|
|
|
*/
|
2017-01-18 23:36:32 +00:00
|
|
|
@property bool empty() @blocking;
|
|
|
|
|
|
|
|
/** (Scheduled for deprecation) Returns the maximum number of bytes that are known to remain available for read.
|
2016-10-26 11:11:28 +00:00
|
|
|
|
2017-01-18 23:36:32 +00:00
|
|
|
After `leastSize()` bytes have been read, the stream will either have reached EOS
|
|
|
|
and `empty()` returns `true`, or `leastSize()` returns again a number greater than `0`.
|
2016-10-26 11:11:28 +00:00
|
|
|
*/
|
2017-01-18 23:36:32 +00:00
|
|
|
@property ulong leastSize() @blocking;
|
2016-10-26 11:11:28 +00:00
|
|
|
|
2017-01-18 23:36:32 +00:00
|
|
|
/** (Scheduled for deprecation) Queries if there is data available for immediate, non-blocking read.
|
2016-10-26 11:11:28 +00:00
|
|
|
*/
|
|
|
|
@property bool dataAvailableForRead();
|
|
|
|
|
|
|
|
/** Returns a temporary reference to the data that is currently buffered.
|
|
|
|
|
|
|
|
The returned slice typically has the size `leastSize()` or `0` if `dataAvailableForRead()`
|
|
|
|
returns `false`. Streams that don't have an internal buffer will always return an empty
|
|
|
|
slice.
|
|
|
|
|
|
|
|
Note that any method invocation on the same stream potentially invalidates the contents of
|
|
|
|
the returned buffer.
|
|
|
|
*/
|
|
|
|
const(ubyte)[] peek();
|
|
|
|
|
|
|
|
/** Fills the preallocated array 'bytes' with data from the stream.
|
|
|
|
|
2017-01-18 23:36:32 +00:00
|
|
|
This function will continue read from the stream until the buffer has
|
|
|
|
been fully filled.
|
|
|
|
|
|
|
|
Params:
|
|
|
|
dst = The buffer into which to write the data that was read
|
|
|
|
mode = Optional reading mode (defaults to `IOMode.all`).
|
|
|
|
|
|
|
|
Return:
|
|
|
|
Returns the number of bytes read. The `dst` buffer will be filled up
|
|
|
|
to this index. The return value is guaranteed to be `dst.length` for
|
|
|
|
`IOMode.all`.
|
|
|
|
|
2016-10-26 11:11:28 +00:00
|
|
|
Throws: An exception if the operation reads past the end of the stream
|
2017-01-18 23:36:32 +00:00
|
|
|
|
|
|
|
See_Also: `readOnce`, `tryRead`
|
2016-10-26 11:11:28 +00:00
|
|
|
*/
|
2017-01-18 23:36:32 +00:00
|
|
|
size_t read(scope ubyte[] dst, IOMode mode) @blocking;
|
|
|
|
/// ditto
|
|
|
|
final void read(scope ubyte[] dst) @blocking { auto n = read(dst, IOMode.all); assert(n == dst.length); }
|
2016-10-26 11:11:28 +00:00
|
|
|
}
|
|
|
|
|
2017-01-18 23:36:32 +00:00
|
|
|
|
2016-10-26 11:11:28 +00:00
|
|
|
/**
|
|
|
|
Interface for all classes implementing writeable streams.
|
|
|
|
*/
|
|
|
|
interface OutputStream {
|
|
|
|
@safe:
|
|
|
|
|
|
|
|
/** Writes an array of bytes to the stream.
|
|
|
|
*/
|
2017-01-18 23:36:32 +00:00
|
|
|
size_t write(in ubyte[] bytes, IOMode mode) @blocking;
|
|
|
|
/// ditto
|
|
|
|
final void write(in ubyte[] bytes) @blocking { auto n = write(bytes, IOMode.all); assert(n == bytes.length); }
|
|
|
|
/// ditto
|
|
|
|
final void write(in char[] bytes) @blocking { write(cast(const(ubyte)[])bytes); }
|
2016-10-26 11:11:28 +00:00
|
|
|
|
|
|
|
/** Flushes the stream and makes sure that all data is being written to the output device.
|
|
|
|
*/
|
2017-01-18 23:36:32 +00:00
|
|
|
void flush() @blocking;
|
2016-10-26 11:11:28 +00:00
|
|
|
|
|
|
|
/** Flushes and finalizes the stream.
|
|
|
|
|
|
|
|
Finalize has to be called on certain types of streams. No writes are possible after a
|
|
|
|
call to finalize().
|
|
|
|
*/
|
2017-01-18 23:36:32 +00:00
|
|
|
void finalize() @blocking;
|
2016-10-26 11:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Interface for all classes implementing readable and writable streams.
|
|
|
|
*/
|
|
|
|
interface Stream : InputStream, OutputStream {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Interface for streams based on a connection.
|
|
|
|
|
|
|
|
Connection streams are based on streaming socket connections, pipes and similar end-to-end
|
|
|
|
streams.
|
|
|
|
|
|
|
|
See_also: `vibe.core.net.TCPConnection`
|
|
|
|
*/
|
|
|
|
interface ConnectionStream : Stream {
|
|
|
|
@safe:
|
|
|
|
|
|
|
|
/** Determines The current connection status.
|
|
|
|
|
|
|
|
If `connected` is `false`, writing to the connection will trigger an exception. Reading may
|
|
|
|
still succeed as long as there is data left in the input buffer. Use `InputStream.empty`
|
|
|
|
instead to determine when to stop reading.
|
|
|
|
*/
|
|
|
|
@property bool connected() const;
|
|
|
|
|
|
|
|
/** Actively closes the connection and frees associated resources.
|
|
|
|
|
|
|
|
Note that close must always be called, even if the remote has already closed the connection.
|
|
|
|
Failure to do so will result in resource and memory leakage.
|
|
|
|
|
|
|
|
Closing a connection implies a call to `finalize`, so that it doesn't need to be called
|
|
|
|
explicitly (it will be a no-op in that case).
|
|
|
|
*/
|
2017-01-18 23:36:32 +00:00
|
|
|
void close() @blocking;
|
2016-10-26 11:11:28 +00:00
|
|
|
|
|
|
|
/** Blocks until data becomes available for read.
|
|
|
|
|
|
|
|
The maximum wait time can be customized with the `timeout` parameter. If there is already
|
|
|
|
data availabe for read, or if the connection is closed, the function will return immediately
|
|
|
|
without blocking.
|
|
|
|
|
|
|
|
Params:
|
|
|
|
timeout = Optional timeout, the default value of `Duration.max` waits without a timeout.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The function will return `true` if data becomes available before the timeout is reached.
|
|
|
|
If the connection gets closed, or the timeout gets reached, `false` is returned instead.
|
|
|
|
*/
|
2017-01-18 23:36:32 +00:00
|
|
|
bool waitForData(Duration timeout = Duration.max) @blocking;
|
2016-10-26 11:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Interface for all streams supporting random access.
|
|
|
|
*/
|
|
|
|
interface RandomAccessStream : Stream {
|
|
|
|
@safe:
|
|
|
|
|
|
|
|
/// Returns the total size of the file.
|
|
|
|
@property ulong size() const nothrow;
|
|
|
|
|
|
|
|
/// Determines if this stream is readable.
|
|
|
|
@property bool readable() const nothrow;
|
|
|
|
|
|
|
|
/// Determines if this stream is writable.
|
|
|
|
@property bool writable() const nothrow;
|
|
|
|
|
|
|
|
/// Seeks to a specific position in the file if supported by the stream.
|
2017-01-18 23:36:32 +00:00
|
|
|
void seek(ulong offset) @blocking;
|
2016-10-26 11:11:28 +00:00
|
|
|
|
|
|
|
/// Returns the current offset of the file pointer
|
|
|
|
ulong tell() nothrow;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Stream implementation acting as a sink with no function.
|
|
|
|
|
|
|
|
Any data written to the stream will be ignored and discarded. This stream type is useful if
|
|
|
|
the output of a particular stream is not needed but the stream needs to be drained.
|
|
|
|
*/
|
|
|
|
final class NullOutputStream : OutputStream {
|
2017-01-18 23:36:32 +00:00
|
|
|
size_t write(in ubyte[] bytes, IOMode) { return bytes.length; }
|
|
|
|
alias write = OutputStream.write;
|
2016-10-26 11:11:28 +00:00
|
|
|
void flush() {}
|
|
|
|
void finalize() {}
|
|
|
|
}
|
|
|
|
|
2017-01-31 15:48:04 +00:00
|
|
|
|
|
|
|
/// Generic storage for types that implement the `InputStream` interface
|
|
|
|
alias InputStreamProxy = InterfaceProxy!InputStream;
|
|
|
|
/// Generic storage for types that implement the `OutputStream` interface
|
|
|
|
alias OutputStreamProxy = InterfaceProxy!OutputStream;
|
|
|
|
/// Generic storage for types that implement the `Stream` interface
|
|
|
|
alias StreamProxy = InterfaceProxy!Stream;
|
|
|
|
/// Generic storage for types that implement the `ConnectionStream` interface
|
|
|
|
alias ConnectionStreamProxy = InterfaceProxy!ConnectionStream;
|
|
|
|
/// Generic storage for types that implement the `RandomAccessStream` interface
|
|
|
|
alias RandomAccessStreamProxy = InterfaceProxy!RandomAccessStream;
|
|
|
|
|
|
|
|
|
2016-11-02 19:59:55 +00:00
|
|
|
/** Tests if the given aggregate type is a valid input stream.
|
|
|
|
|
|
|
|
See_also: `validateInputStream`
|
|
|
|
*/
|
2016-10-26 11:11:28 +00:00
|
|
|
enum isInputStream(T) = checkInterfaceConformance!(T, InputStream) is null;
|
2016-11-02 19:59:55 +00:00
|
|
|
|
|
|
|
/** Tests if the given aggregate type is a valid output stream.
|
|
|
|
|
|
|
|
See_also: `validateOutputStream`
|
|
|
|
*/
|
2016-10-26 11:11:28 +00:00
|
|
|
enum isOutputStream(T) = checkInterfaceConformance!(T, OutputStream) is null;
|
2016-11-02 19:59:55 +00:00
|
|
|
|
|
|
|
/** Tests if the given aggregate type is a valid bidirectional stream.
|
|
|
|
|
|
|
|
See_also: `validateStream`
|
|
|
|
*/
|
|
|
|
enum isStream(T) = checkInterfaceConformance!(T, Stream) is null;
|
|
|
|
|
|
|
|
/** Tests if the given aggregate type is a valid connection stream.
|
|
|
|
|
|
|
|
See_also: `validateConnectionStream`
|
|
|
|
*/
|
2016-10-26 11:11:28 +00:00
|
|
|
enum isConnectionStream(T) = checkInterfaceConformance!(T, ConnectionStream) is null;
|
2016-11-02 19:59:55 +00:00
|
|
|
|
|
|
|
/** Tests if the given aggregate type is a valid random access stream.
|
|
|
|
|
|
|
|
See_also: `validateRandomAccessStream`
|
|
|
|
*/
|
2016-10-26 11:11:28 +00:00
|
|
|
enum isRandomAccessStream(T) = checkInterfaceConformance!(T, RandomAccessStream) is null;
|
|
|
|
|
2016-11-02 19:59:55 +00:00
|
|
|
/** Verifies that the given type is a valid input stream.
|
|
|
|
|
|
|
|
A valid input stream type must implement all methods of the `InputStream` interface. Inheriting
|
|
|
|
form `InputStream` is not strictly necessary, which also enables struct types to be considered
|
|
|
|
as stream implementations.
|
|
|
|
|
|
|
|
See_Also: `isInputStream`
|
|
|
|
*/
|
|
|
|
mixin template validateInputStream(T) { import vibe.internal.traits : validateInterfaceConformance; mixin validateInterfaceConformance!(T, .InputStream); }
|
|
|
|
|
|
|
|
/** Verifies that the given type is a valid output stream.
|
|
|
|
|
|
|
|
A valid output stream type must implement all methods of the `OutputStream` interface. Inheriting
|
|
|
|
form `OutputStream` is not strictly necessary, which also enables struct types to be considered
|
|
|
|
as stream implementations.
|
|
|
|
|
|
|
|
See_Also: `isOutputStream`
|
|
|
|
*/
|
|
|
|
mixin template validateOutputStream(T) { import vibe.internal.traits : validateInterfaceConformance; mixin validateInterfaceConformance!(T, .OutputStream); }
|
|
|
|
|
|
|
|
/** Verifies that the given type is a valid bidirectional stream.
|
|
|
|
|
|
|
|
A valid stream type must implement all methods of the `Stream` interface. Inheriting
|
|
|
|
form `Stream` is not strictly necessary, which also enables struct types to be considered
|
|
|
|
as stream implementations.
|
|
|
|
|
|
|
|
See_Also: `isStream`
|
|
|
|
*/
|
|
|
|
mixin template validateStream(T) { import vibe.internal.traits : validateInterfaceConformance; mixin validateInterfaceConformance!(T, .Stream); }
|
|
|
|
|
|
|
|
/** Verifies that the given type is a valid connection stream.
|
|
|
|
|
|
|
|
A valid connection stream type must implement all methods of the `ConnectionStream` interface.
|
|
|
|
Inheriting form `ConnectionStream` is not strictly necessary, which also enables struct types
|
|
|
|
to be considered as stream implementations.
|
|
|
|
|
|
|
|
See_Also: `isConnectionStream`
|
|
|
|
*/
|
|
|
|
mixin template validateConnectionStream(T) { import vibe.internal.traits : validateInterfaceConformance; mixin validateInterfaceConformance!(T, .ConnectionStream); }
|
|
|
|
|
|
|
|
/** Verifies that the given type is a valid random access stream.
|
|
|
|
|
|
|
|
A valid random access stream type must implement all methods of the `RandomAccessStream`
|
|
|
|
interface. Inheriting form `RandomAccessStream` is not strictly necessary, which also enables
|
|
|
|
struct types to be considered as stream implementations.
|
|
|
|
|
|
|
|
See_Also: `isRandomAccessStream`
|
|
|
|
*/
|
|
|
|
mixin template validateRandomAccessStream(T) { import vibe.internal.traits : validateInterfaceConformance; mixin validateInterfaceConformance!(T, .RandomAccessStream); }
|