Module 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 = defaultAllocator.make!InternetAddress("127.0.0.1", 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(address);
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);
defaultAllocator.dispose(address);
}
Functions
Name | Description |
defaultLoop()
|
Returns the event loop used by default. If an event loop wasn't set with
defaultLoop before, defaultLoop will try to
choose an event loop supported on the system.
|
defaultLoop(loop)
|
Sets the default event loop.
|
Enums
Name | Description |
Event
|
Events.
|