vibe_tcp_echo.dhover×80all
#!/usr/bin/env dub
/+ dub.sdl:
    name "vibe_tcp_echo"
    dependency "vibe-core" version="2.14.0"
    buildType "checked" {
        buildOptions "optimize" "inline" "debugInfo"
    }
    platforms "linux"
+/
import 
(package) std
std
.
(module) std.stdio
Category Symbols
File handles _popen File isFileHandle openNetwork stderr stdin stdout
Reading chunks lines readf readfln readln
Writing toFile write writef writefln writeln
Misc KeepTerminator LockType StdioException

Standard I/O functions that extend core.stdc.stdio. core.stdc.stdio is publically imported when importing std.stdio.

There are three layers of I/O:

  1. The lowest layer is the operating system layer. The two main schemes are Windows and Posix.

  2. C's stdio.h which unifies the two operating system schemes.

  3. std.stdio, this module, unifies the various stdio.h implementations into a high level package for D programs.

Source

std/stdio.d

@copyrightCopyright The D Language Foundation 2007-.@licenseBoost License 1.0.@authorsWalter Bright, Andrei Alexandrescu, Alex Rønne Petersen
stdio
:
(alias template) vibe_tcp_echo.writeln = std.stdio.writeln(T...)(T args)

Equivalent to write(args, '\n'). Calling writeln without arguments is valid and just prints a newline to the standard output.

Example

Reads stdin and writes it to stdout with an argument counter.

import std.stdio;

void main()
{
    string line;

    for (size_t count = 0; (line = readln) !is null; count++)
    {
         writeln("Input ", count, ": ", line);
    }
}
@paramargs the items to write to stdout@throwsIn case of an I/O error, throws an StdioException.
writeln
;
import
(package) vibe
vibe
.
(package) vibe.core
core
.
(module) vibe.core.net

TCP/UDP connection and server handling.

@copyright© 2012-2016 Sönke Ludwig@authorsSönke Ludwig@licenseSubject to the terms of the MIT license, as written in the included LICENSE.txt file.
net
:
(alias) vibe_tcp_echo.connectTCP = vibe.core.net.TCPConnection vibe.core.net.connectTCP(string host, ushort port, string bind_interface = null, ushort bind_port = cast(ushort)0u, core.time.Duration timeout = max()) @safe

Establishes a connection to the given host/port.

connectTCP
,
(alias) vibe_tcp_echo.listenTCP = vibe.core.net.TCPListener[] vibe.core.net.listenTCP(ushort port, void delegate(vibe.core.net.TCPConnection stream) nothrow @safe connection_callback, vibe.core.net.TCPListenOptions options = TCPListenOptions.reuseAddress) @safe

Starts listening on the specified port.

'connection_callback' will be called for each client that connects to the server socket. Each new connection gets its own fiber. The stream parameter then allows to perform blocking I/O on the client socket.

The address parameter can be used to specify the network interface on which the server socket is supposed to listen for connections. By default, all IPv4 and IPv6 interfaces will be used.

listenTCP
,
(struct) vibe.core.net.TCPConnection

Represents a single TCP connection.

TCPConnection
,
(struct) vibe.core.net.TCPListener

Represents a listening TCP socket.

TCPListener
;
import
(package) vibe
vibe
.
(package) vibe.core
core
.
(module) vibe.core.stream

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.

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).

@copyright© 2012-2016 Sönke Ludwig@licenseSubject to the terms of the MIT license, as written in the included LICENSE.txt file.@authorsSönke Ludwig
stream
:
(enum) eventcore.driver.IOMode
IOMode
;
void
void vibe_tcp_echo.echo(vibe.core.net.TCPConnection conn) @safe
echo
(
(struct) vibe.core.net.TCPConnection

Represents a single TCP connection.

TCPConnection
(parameter) vibe.core.net.TCPConnection conn
conn
) @safe
{ scope (exit)
(parameter) vibe.core.net.TCPConnection conn
conn
.
void vibe.core.net.TCPConnection.close() nothrow @safe
close
();
ubyte[64]
(local variable) ubyte[64] buf
buf
;
(alias) object.size_t = ulong
size_t
(local variable) ulong total
total
;
while (
(local variable) ulong total
total
< 5)
{ const
(local variable) const(ulong) len
len
=
(parameter) vibe.core.net.TCPConnection conn
conn
.
ulong vibe.core.net.TCPConnection.read(scope ubyte[] dst, eventcore.driver.IOMode mode) @safe
read
(
(local variable) ubyte[64] buf
buf
[0 .. 5 -
(local variable) ulong total
total
],
(enum) eventcore.driver.IOMode
IOMode
.
(enum value) eventcore.driver.IOMode.once = 1

Process as much as possible with a single call

once
);
assert(
(local variable) const(ulong) len
len
> 0);
(parameter) vibe.core.net.TCPConnection conn
conn
.
void vibe.core.net.TCPConnection.write(scope const(ubyte)[] bytes) @safe
write
(
(local variable) ubyte[64] buf
buf
[0 ..
(local variable) const(ulong) len
len
]);
(local variable) ulong total
total
+=
(local variable) const(ulong) len
len
;
} } void
void D main()
main
()
{ auto
(local variable) vibe.core.net.TCPListener listener
listener
=
vibe.core.net.TCPListener vibe.core.net.listenTCP(ushort port, void delegate(vibe.core.net.TCPConnection stream) nothrow @safe connection_callback, string address, vibe.core.net.TCPListenOptions options = TCPListenOptions.reuseAddress) @safe

Starts listening on the specified port.

'connection_callback' will be called for each client that connects to the server socket. Each new connection gets its own fiber. The stream parameter then allows to perform blocking I/O on the client socket.

The address parameter can be used to specify the network interface on which the server socket is supposed to listen for connections. By default, all IPv4 and IPv6 interfaces will be used.

listenTCP
(0, (
(struct) vibe.core.net.TCPConnection

Represents a single TCP connection.

TCPConnection
(parameter) vibe.core.net.TCPConnection conn
conn
) nothrow @safe {
try
void vibe_tcp_echo.echo(vibe.core.net.TCPConnection conn) @safe
echo
(
(parameter) vibe.core.net.TCPConnection conn
conn
); catch (
(class) object.Exception

The base class of all errors that are safe to catch and handle.

In principle, only thrown objects derived from this class are safe to catch inside a catch block. Thrown objects not derived from Exception represent runtime errors that should not be caught, as certain runtime guarantees may not hold, making it unsafe to continue program execution.

Examples

bool gotCaught;
try
{
    throw new Exception("msg");
}
catch (Exception e)
{
    gotCaught = true;
    assert(e.msg == "msg");
}
assert(gotCaught);
Exception
(local variable) object.Exception error
error
) { assert(false,
(local variable) object.Exception error
error
.
(field) string object.Throwable.msg

A message describing the error.

msg
); }
}, "127.0.0.1"); scope (exit)
(local variable) vibe.core.net.TCPListener listener
listener
.
void vibe.core.net.TCPListener.stopListening() @safe

Stops listening and closes the socket.

stopListening
();
const
(local variable) const(ushort) port
port
=
(local variable) vibe.core.net.TCPListener listener
listener
.
vibe.core.net.NetworkAddress vibe.core.net.TCPListener.bindAddress() @property @safe

The local address at which TCP connections are accepted.

bindAddress
.
ushort vibe.core.net.NetworkAddress.port() const pure nothrow @property @safe

The port in host byte order.

port
;
auto
(local variable) vibe.core.net.TCPConnection client
client
=
vibe.core.net.TCPConnection vibe.core.net.connectTCP(string host, ushort port, string bind_interface = null, ushort bind_port = cast(ushort)0u, core.time.Duration timeout = max()) @safe

Establishes a connection to the given host/port.

connectTCP
("127.0.0.1",
(local variable) const(ushort) port
port
);
scope (exit)
(local variable) vibe.core.net.TCPConnection client
client
.
void vibe.core.net.TCPConnection.close() nothrow @safe
close
();
(local variable) vibe.core.net.TCPConnection client
client
.
void vibe.core.net.TCPConnection.write(scope const(ubyte)[] bytes) @safe
write
(cast(const(ubyte)[]) "hello");
ubyte[64]
(local variable) ubyte[64] back
back
;
(alias) object.size_t = ulong
size_t
(local variable) ulong len
len
;
while (
(local variable) ulong len
len
< 5)
{ const
(local variable) const(ulong) n
n
=
(local variable) vibe.core.net.TCPConnection client
client
.
ulong vibe.core.net.TCPConnection.read(scope ubyte[] dst, eventcore.driver.IOMode mode) @safe
read
(
(local variable) ubyte[64] back
back
[
(local variable) ulong len
len
.. 5],
(enum) eventcore.driver.IOMode
IOMode
.
(enum value) eventcore.driver.IOMode.once = 1

Process as much as possible with a single call

once
);
assert(
(local variable) const(ulong) n
n
> 0);
(local variable) ulong len
len
+=
(local variable) const(ulong) n
n
;
} assert(cast(const(char)[])
(local variable) ubyte[64] back
back
[0 ..
(local variable) ulong len
len
] == "hello");
void std.stdio.writeln!(string, const(char)[])(string __param_0, const(char)[] __param_1) @safe

Equivalent to write(args, '\n'). Calling writeln without arguments is valid and just prints a newline to the standard output.

Example

Reads stdin and writes it to stdout with an argument counter.

import std.stdio;

void main()
{
    string line;

    for (size_t count = 0; (line = readln) !is null; count++)
    {
         writeln("Input ", count, ": ", line);
    }
}
@paramargs the items to write to stdout@throwsIn case of an I/O error, throws an StdioException.
writeln
("echoed: ", cast(const(char)[])
(local variable) ubyte[64] back
back
[0 ..
(local variable) ulong len
len
]);
}