tanya.async.loop

Interface for the event loop implementations and the default event loop chooser.

import tanya.async;
import tanya.memory;
import tanya.network.socket;

class EchoProtocol : TransmissionControlProtocol
{
    private DuplexTransport transport;

    void received(in ubyte[] data) @nogc
    {
        ubyte[512] buffer;
        buffer[0 .. data.length] = data;
        transport.write(buffer[]);
    }

    void connected(DuplexTransport transport) @nogc
    {
        this.transport = transport;
    }

    void disconnected(SocketException e) @nogc
    {
    }
}

void main()
{
    auto address = address4("127.0.0.1");
    auto endpoint = Endpoint(address.get, cast(ushort) 8192);
   
    version (Windows)
    {
        auto sock = defaultAllocator.make!OverlappedStreamSocket(AddressFamily.inet);
    }
    else
    {
        auto sock = defaultAllocator.make!StreamSocket(AddressFamily.inet);
        sock.blocking = false;
    }

    sock.bind(endpoint);
    sock.listen(5);
   
    auto io = defaultAllocator.make!ConnectionWatcher(sock);
    io.setProtocol!EchoProtocol;
   
    defaultLoop.start(io);
    defaultLoop.run();
   
    sock.shutdown();
    defaultAllocator.dispose(io);
    defaultAllocator.dispose(sock);
}

Members

Aliases

EventMask
alias EventMask = BitFlags!Event
Undocumented in source.

Classes

BadLoopException
class BadLoopException

Exception thrown on errors in the event loop.

Loop
class Loop

Event loop.

Enums

Event
enum Event

Events.

Properties

defaultLoop
Loop defaultLoop [@property getter]

Returns the event loop used by default. If an event loop wasn't set with $(D_PSYMBOL defaultLoop) before, $(D_PSYMBOL defaultLoop) will try to choose an event loop supported on the system.

defaultLoop
void defaultLoop [@property getter]

Sets the default event loop.

Meta