Add Windows IOCP and Kqueue implementations for the event loop
This commit is contained in:
parent
154e2f2ff7
commit
6b093cd5fa
178
source/tanya/async/event/epoll.d
Normal file
178
source/tanya/async/event/epoll.d
Normal file
@ -0,0 +1,178 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* Copyright: Eugene Wissner 2016.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
|
||||
*/
|
||||
module tanya.async.event.epoll;
|
||||
|
||||
version (linux):
|
||||
|
||||
public import core.sys.linux.epoll;
|
||||
import tanya.async.protocol;
|
||||
import tanya.async.event.selector;
|
||||
import tanya.async.loop;
|
||||
import tanya.async.transport;
|
||||
import tanya.async.watcher;
|
||||
import tanya.memory;
|
||||
import tanya.memory.mmappool;
|
||||
import tanya.network.socket;
|
||||
import core.stdc.errno;
|
||||
import core.sys.posix.unistd;
|
||||
import core.time;
|
||||
import std.algorithm.comparison;
|
||||
|
||||
class EpollLoop : SelectorLoop
|
||||
{
|
||||
protected int fd;
|
||||
private epoll_event[] events;
|
||||
|
||||
/**
|
||||
* Initializes the loop.
|
||||
*/
|
||||
this()
|
||||
{
|
||||
if ((fd = epoll_create1(EPOLL_CLOEXEC)) < 0)
|
||||
{
|
||||
throw MmapPool.instance.make!BadLoopException("epoll initialization failed");
|
||||
}
|
||||
super();
|
||||
events = MmapPool.instance.makeArray!epoll_event(maxEvents);
|
||||
}
|
||||
|
||||
/**
|
||||
* Free loop internals.
|
||||
*/
|
||||
~this()
|
||||
{
|
||||
MmapPool.instance.dispose(events);
|
||||
close(fd);
|
||||
}
|
||||
|
||||
/**
|
||||
* Should be called if the backend configuration changes.
|
||||
*
|
||||
* Params:
|
||||
* watcher = Watcher.
|
||||
* oldEvents = The events were already set.
|
||||
* events = The events should be set.
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if the operation was successful.
|
||||
*/
|
||||
protected override bool reify(ConnectionWatcher watcher, EventMask oldEvents, EventMask events)
|
||||
in
|
||||
{
|
||||
assert(watcher !is null);
|
||||
}
|
||||
body
|
||||
{
|
||||
int op = EPOLL_CTL_DEL;
|
||||
epoll_event ev;
|
||||
|
||||
if (events == oldEvents)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (events && oldEvents)
|
||||
{
|
||||
op = EPOLL_CTL_MOD;
|
||||
}
|
||||
else if (events && !oldEvents)
|
||||
{
|
||||
op = EPOLL_CTL_ADD;
|
||||
}
|
||||
|
||||
ev.data.fd = watcher.socket.handle;
|
||||
ev.events = (events & (Event.read | Event.accept) ? EPOLLIN | EPOLLPRI : 0)
|
||||
| (events & Event.write ? EPOLLOUT : 0)
|
||||
| EPOLLET;
|
||||
|
||||
return epoll_ctl(fd, op, watcher.socket.handle, &ev) == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Does the actual polling.
|
||||
*/
|
||||
protected override void poll()
|
||||
{
|
||||
// Don't block
|
||||
immutable timeout = cast(immutable int) blockTime.total!"msecs";
|
||||
auto eventCount = epoll_wait(fd, events.ptr, maxEvents, timeout);
|
||||
|
||||
if (eventCount < 0)
|
||||
{
|
||||
if (errno != EINTR)
|
||||
{
|
||||
throw defaultAllocator.make!BadLoopException();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto i = 0; i < eventCount; ++i)
|
||||
{
|
||||
auto io = cast(IOWatcher) connections[events[i].data.fd];
|
||||
|
||||
if (io is null)
|
||||
{
|
||||
acceptConnections(connections[events[i].data.fd]);
|
||||
}
|
||||
else if (events[i].events & EPOLLERR)
|
||||
{
|
||||
kill(io, null);
|
||||
}
|
||||
else if (events[i].events & (EPOLLIN | EPOLLPRI | EPOLLHUP))
|
||||
{
|
||||
auto transport = cast(SelectorStreamTransport) io.transport;
|
||||
assert(transport !is null);
|
||||
|
||||
SocketException exception;
|
||||
try
|
||||
{
|
||||
ptrdiff_t received;
|
||||
do
|
||||
{
|
||||
received = transport.socket.receive(io.output[]);
|
||||
io.output += received;
|
||||
}
|
||||
while (received);
|
||||
}
|
||||
catch (SocketException e)
|
||||
{
|
||||
exception = e;
|
||||
}
|
||||
if (transport.socket.disconnected)
|
||||
{
|
||||
kill(io, exception);
|
||||
}
|
||||
else if (io.output.length)
|
||||
{
|
||||
swapPendings.insertBack(io);
|
||||
}
|
||||
}
|
||||
else if (events[i].events & EPOLLOUT)
|
||||
{
|
||||
auto transport = cast(SelectorStreamTransport) io.transport;
|
||||
assert(transport !is null);
|
||||
|
||||
transport.writeReady = true;
|
||||
if (transport.input.length)
|
||||
{
|
||||
feed(transport);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns: The blocking time.
|
||||
*/
|
||||
override protected @property inout(Duration) blockTime()
|
||||
inout @safe pure nothrow
|
||||
{
|
||||
return min(super.blockTime, 1.dur!"seconds");
|
||||
}
|
||||
}
|
294
source/tanya/async/event/iocp.d
Normal file
294
source/tanya/async/event/iocp.d
Normal file
@ -0,0 +1,294 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* Copyright: Eugene Wissner 2016.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
|
||||
*/
|
||||
module tanya.async.event.iocp;
|
||||
|
||||
version (Windows):
|
||||
|
||||
import tanya.container.buffer;
|
||||
import tanya.async.loop;
|
||||
import tanya.async.protocol;
|
||||
import tanya.async.transport;
|
||||
import tanya.async.watcher;
|
||||
import tanya.memory;
|
||||
import tanya.memory.mmappool;
|
||||
import tanya.network.socket;
|
||||
import core.sys.windows.basetyps;
|
||||
import core.sys.windows.mswsock;
|
||||
import core.sys.windows.winbase;
|
||||
import core.sys.windows.windef;
|
||||
import core.sys.windows.winsock2;
|
||||
|
||||
class IOCPStreamTransport : StreamTransport
|
||||
{
|
||||
private OverlappedConnectedSocket socket_;
|
||||
|
||||
private WriteBuffer input;
|
||||
|
||||
/**
|
||||
* Creates new completion port transport.
|
||||
* Params:
|
||||
* socket = Socket.
|
||||
*/
|
||||
this(OverlappedConnectedSocket socket)
|
||||
in
|
||||
{
|
||||
assert(socket !is null);
|
||||
}
|
||||
body
|
||||
{
|
||||
socket_ = socket;
|
||||
input = MmapPool.instance.make!WriteBuffer();
|
||||
}
|
||||
|
||||
~this()
|
||||
{
|
||||
MmapPool.instance.dispose(input);
|
||||
}
|
||||
|
||||
@property inout(OverlappedConnectedSocket) socket() inout pure nothrow @safe @nogc
|
||||
{
|
||||
return socket_;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write some data to the transport.
|
||||
*
|
||||
* Params:
|
||||
* data = Data to send.
|
||||
*/
|
||||
void write(ubyte[] data)
|
||||
{
|
||||
immutable empty = input.length == 0;
|
||||
input ~= data;
|
||||
if (empty)
|
||||
{
|
||||
SocketState overlapped;
|
||||
try
|
||||
{
|
||||
overlapped = MmapPool.instance.make!SocketState;
|
||||
socket.beginSend(input[], overlapped);
|
||||
}
|
||||
catch (SocketException e)
|
||||
{
|
||||
MmapPool.instance.dispose(overlapped);
|
||||
MmapPool.instance.dispose(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class IOCPLoop : Loop
|
||||
{
|
||||
protected HANDLE completionPort;
|
||||
|
||||
protected OVERLAPPED overlap;
|
||||
|
||||
/**
|
||||
* Initializes the loop.
|
||||
*/
|
||||
this()
|
||||
{
|
||||
super();
|
||||
|
||||
completionPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
|
||||
if (!completionPort)
|
||||
{
|
||||
throw defaultAllocator.make!BadLoopException("Creating completion port failed");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Should be called if the backend configuration changes.
|
||||
*
|
||||
* Params:
|
||||
* watcher = Watcher.
|
||||
* oldEvents = The events were already set.
|
||||
* events = The events should be set.
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if the operation was successful.
|
||||
*/
|
||||
override protected bool reify(ConnectionWatcher watcher,
|
||||
EventMask oldEvents,
|
||||
EventMask events)
|
||||
{
|
||||
SocketState overlapped;
|
||||
if (!(oldEvents & Event.accept) && (events & Event.accept))
|
||||
{
|
||||
auto socket = cast(OverlappedStreamSocket) watcher.socket;
|
||||
assert(socket !is null);
|
||||
|
||||
if (CreateIoCompletionPort(cast(HANDLE) socket.handle,
|
||||
completionPort,
|
||||
cast(ULONG_PTR) (cast(void*) watcher),
|
||||
0) !is completionPort)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
overlapped = MmapPool.instance.make!SocketState;
|
||||
socket.beginAccept(overlapped);
|
||||
}
|
||||
catch (SocketException e)
|
||||
{
|
||||
MmapPool.instance.dispose(overlapped);
|
||||
defaultAllocator.dispose(e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (!(oldEvents & Event.read) && (events & Event.read)
|
||||
|| !(oldEvents & Event.write) && (events & Event.write))
|
||||
{
|
||||
auto io = cast(IOWatcher) watcher;
|
||||
assert(io !is null);
|
||||
|
||||
auto transport = cast(IOCPStreamTransport) io.transport;
|
||||
assert(transport !is null);
|
||||
|
||||
if (CreateIoCompletionPort(cast(HANDLE) transport.socket.handle,
|
||||
completionPort,
|
||||
cast(ULONG_PTR) (cast(void*) watcher),
|
||||
0) !is completionPort)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Begin to read
|
||||
if (!(oldEvents & Event.read) && (events & Event.read))
|
||||
{
|
||||
try
|
||||
{
|
||||
overlapped = MmapPool.instance.make!SocketState;
|
||||
transport.socket.beginReceive(io.output[], overlapped);
|
||||
}
|
||||
catch (SocketException e)
|
||||
{
|
||||
MmapPool.instance.dispose(overlapped);
|
||||
defaultAllocator.dispose(e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Does the actual polling.
|
||||
*/
|
||||
override protected void poll()
|
||||
{
|
||||
DWORD lpNumberOfBytes;
|
||||
ULONG_PTR key;
|
||||
LPOVERLAPPED overlap;
|
||||
immutable timeout = cast(immutable int) blockTime.total!"msecs";
|
||||
|
||||
auto result = GetQueuedCompletionStatus(completionPort,
|
||||
&lpNumberOfBytes,
|
||||
&key,
|
||||
&overlap,
|
||||
timeout);
|
||||
if (result == FALSE && overlap == NULL)
|
||||
{
|
||||
return; // Timeout
|
||||
}
|
||||
|
||||
auto overlapped = (cast(SocketState) ((cast(void*) overlap) - 8));
|
||||
assert(overlapped !is null);
|
||||
scope (failure)
|
||||
{
|
||||
MmapPool.instance.dispose(overlapped);
|
||||
}
|
||||
|
||||
switch (overlapped.event)
|
||||
{
|
||||
case OverlappedSocketEvent.accept:
|
||||
auto connection = cast(ConnectionWatcher) (cast(void*) key);
|
||||
assert(connection !is null);
|
||||
|
||||
auto listener = cast(OverlappedStreamSocket) connection.socket;
|
||||
assert(listener !is null);
|
||||
|
||||
auto socket = listener.endAccept(overlapped);
|
||||
auto transport = MmapPool.instance.make!IOCPStreamTransport(socket);
|
||||
auto io = MmapPool.instance.make!IOWatcher(transport, connection.protocol);
|
||||
|
||||
connection.incoming.insertBack(io);
|
||||
|
||||
reify(io, EventMask(Event.none), EventMask(Event.read, Event.write));
|
||||
|
||||
swapPendings.insertBack(connection);
|
||||
listener.beginAccept(overlapped);
|
||||
break;
|
||||
case OverlappedSocketEvent.read:
|
||||
auto io = cast(IOWatcher) (cast(void*) key);
|
||||
assert(io !is null);
|
||||
if (!io.active)
|
||||
{
|
||||
MmapPool.instance.dispose(io);
|
||||
MmapPool.instance.dispose(overlapped);
|
||||
return;
|
||||
}
|
||||
|
||||
auto transport = cast(IOCPStreamTransport) io.transport;
|
||||
assert(transport !is null);
|
||||
|
||||
int received;
|
||||
SocketException exception;
|
||||
try
|
||||
{
|
||||
received = transport.socket.endReceive(overlapped);
|
||||
}
|
||||
catch (SocketException e)
|
||||
{
|
||||
exception = e;
|
||||
}
|
||||
if (transport.socket.disconnected)
|
||||
{
|
||||
// We want to get one last notification to destroy the watcher
|
||||
transport.socket.beginReceive(io.output[], overlapped);
|
||||
kill(io, exception);
|
||||
}
|
||||
else if (received > 0)
|
||||
{
|
||||
immutable full = io.output.free == received;
|
||||
|
||||
io.output += received;
|
||||
// Receive was interrupted because the buffer is full. We have to continue
|
||||
if (full)
|
||||
{
|
||||
transport.socket.beginReceive(io.output[], overlapped);
|
||||
}
|
||||
swapPendings.insertBack(io);
|
||||
}
|
||||
break;
|
||||
case OverlappedSocketEvent.write:
|
||||
auto io = cast(IOWatcher) (cast(void*) key);
|
||||
assert(io !is null);
|
||||
|
||||
auto transport = cast(IOCPStreamTransport) io.transport;
|
||||
assert(transport !is null);
|
||||
|
||||
transport.input += transport.socket.endSend(overlapped);
|
||||
if (transport.input.length)
|
||||
{
|
||||
transport.socket.beginSend(transport.input[], overlapped);
|
||||
}
|
||||
else
|
||||
{
|
||||
transport.socket.beginReceive(io.output[], overlapped);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
assert(false, "Unknown event");
|
||||
}
|
||||
}
|
||||
}
|
349
source/tanya/async/event/kqueue.d
Normal file
349
source/tanya/async/event/kqueue.d
Normal file
@ -0,0 +1,349 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* Copyright: Eugene Wissner 2016.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
|
||||
*/
|
||||
module tanya.async.event.kqueue;
|
||||
|
||||
version (OSX)
|
||||
{
|
||||
version = MissingKevent;
|
||||
}
|
||||
else version (iOS)
|
||||
{
|
||||
version = MissingKevent;
|
||||
}
|
||||
else version (TVOS)
|
||||
{
|
||||
version = MissingKevent;
|
||||
}
|
||||
else version (WatchOS)
|
||||
{
|
||||
version = MissingKevent;
|
||||
}
|
||||
else version (OpenBSD)
|
||||
{
|
||||
version = MissingKevent;
|
||||
}
|
||||
else version (DragonFlyBSD)
|
||||
{
|
||||
version = MissingKevent;
|
||||
}
|
||||
|
||||
version (MissingKevent)
|
||||
{
|
||||
extern (C):
|
||||
nothrow:
|
||||
@nogc:
|
||||
|
||||
import core.stdc.stdint; // intptr_t, uintptr_t
|
||||
import core.sys.posix.time; // timespec
|
||||
|
||||
enum : short
|
||||
{
|
||||
EVFILT_READ = -1,
|
||||
EVFILT_WRITE = -2,
|
||||
EVFILT_AIO = -3, /* attached to aio requests */
|
||||
EVFILT_VNODE = -4, /* attached to vnodes */
|
||||
EVFILT_PROC = -5, /* attached to struct proc */
|
||||
EVFILT_SIGNAL = -6, /* attached to struct proc */
|
||||
EVFILT_TIMER = -7, /* timers */
|
||||
EVFILT_MACHPORT = -8, /* Mach portsets */
|
||||
EVFILT_FS = -9, /* filesystem events */
|
||||
EVFILT_USER = -10, /* User events */
|
||||
EVFILT_VM = -12, /* virtual memory events */
|
||||
EVFILT_SYSCOUNT = 11
|
||||
}
|
||||
|
||||
extern(D) void EV_SET(kevent_t* kevp, typeof(kevent_t.tupleof) args)
|
||||
{
|
||||
*kevp = kevent_t(args);
|
||||
}
|
||||
|
||||
struct kevent_t
|
||||
{
|
||||
uintptr_t ident; /* identifier for this event */
|
||||
short filter; /* filter for event */
|
||||
ushort flags;
|
||||
uint fflags;
|
||||
intptr_t data;
|
||||
void *udata; /* opaque user data identifier */
|
||||
}
|
||||
|
||||
enum
|
||||
{
|
||||
/* actions */
|
||||
EV_ADD = 0x0001, /* add event to kq (implies enable) */
|
||||
EV_DELETE = 0x0002, /* delete event from kq */
|
||||
EV_ENABLE = 0x0004, /* enable event */
|
||||
EV_DISABLE = 0x0008, /* disable event (not reported) */
|
||||
|
||||
/* flags */
|
||||
EV_ONESHOT = 0x0010, /* only report one occurrence */
|
||||
EV_CLEAR = 0x0020, /* clear event state after reporting */
|
||||
EV_RECEIPT = 0x0040, /* force EV_ERROR on success, data=0 */
|
||||
EV_DISPATCH = 0x0080, /* disable event after reporting */
|
||||
|
||||
EV_SYSFLAGS = 0xF000, /* reserved by system */
|
||||
EV_FLAG1 = 0x2000, /* filter-specific flag */
|
||||
|
||||
/* returned values */
|
||||
EV_EOF = 0x8000, /* EOF detected */
|
||||
EV_ERROR = 0x4000, /* error, data contains errno */
|
||||
}
|
||||
|
||||
int kqueue();
|
||||
int kevent(int kq, const kevent_t *changelist, int nchanges,
|
||||
kevent_t *eventlist, int nevents,
|
||||
const timespec *timeout);
|
||||
}
|
||||
|
||||
version (OSX)
|
||||
{
|
||||
version = MacBSD;
|
||||
}
|
||||
else version (iOS)
|
||||
{
|
||||
version = MacBSD;
|
||||
}
|
||||
else version (FreeBSD)
|
||||
{
|
||||
version = MacBSD;
|
||||
public import core.sys.freebsd.sys.event;
|
||||
}
|
||||
else version (OpenBSD)
|
||||
{
|
||||
version = MacBSD;
|
||||
}
|
||||
else version (DragonFlyBSD)
|
||||
{
|
||||
version = MacBSD;
|
||||
}
|
||||
|
||||
version (MacBSD):
|
||||
|
||||
import dlib.async.event.selector;
|
||||
import dlib.async.loop;
|
||||
import dlib.async.transport;
|
||||
import dlib.async.watcher;
|
||||
import dlib.memory;
|
||||
import dlib.memory.mmappool;
|
||||
import dlib.network.socket;
|
||||
import core.stdc.errno;
|
||||
import core.sys.posix.unistd;
|
||||
import core.sys.posix.sys.time;
|
||||
import core.time;
|
||||
import std.algorithm.comparison;
|
||||
|
||||
class KqueueLoop : SelectorLoop
|
||||
{
|
||||
protected int fd;
|
||||
private kevent_t[] events;
|
||||
private kevent_t[] changes;
|
||||
private size_t changeCount;
|
||||
|
||||
/**
|
||||
* Returns: Maximal event count can be got at a time
|
||||
* (should be supported by the backend).
|
||||
*/
|
||||
override protected @property inout(uint) maxEvents() inout const pure nothrow @safe @nogc
|
||||
{
|
||||
return cast(uint) events.length;
|
||||
}
|
||||
|
||||
this()
|
||||
{
|
||||
super();
|
||||
|
||||
if ((fd = kqueue()) == -1)
|
||||
{
|
||||
throw MmapPool.instance.make!BadLoopException("epoll initialization failed");
|
||||
}
|
||||
events = MmapPool.instance.makeArray!kevent_t(64);
|
||||
changes = MmapPool.instance.makeArray!kevent_t(64);
|
||||
}
|
||||
|
||||
/**
|
||||
* Free loop internals.
|
||||
*/
|
||||
~this()
|
||||
{
|
||||
MmapPool.instance.dispose(events);
|
||||
MmapPool.instance.dispose(changes);
|
||||
close(fd);
|
||||
}
|
||||
|
||||
private void set(socket_t socket, short filter, ushort flags)
|
||||
{
|
||||
if (changes.length <= changeCount)
|
||||
{
|
||||
MmapPool.instance.resizeArray(changes, changeCount + maxEvents);
|
||||
}
|
||||
EV_SET(&changes[changeCount],
|
||||
cast(ulong) socket,
|
||||
filter,
|
||||
flags,
|
||||
0U,
|
||||
0L,
|
||||
null);
|
||||
++changeCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Should be called if the backend configuration changes.
|
||||
*
|
||||
* Params:
|
||||
* watcher = Watcher.
|
||||
* oldEvents = The events were already set.
|
||||
* events = The events should be set.
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if the operation was successful.
|
||||
*/
|
||||
override protected bool reify(ConnectionWatcher watcher,
|
||||
EventMask oldEvents,
|
||||
EventMask events)
|
||||
{
|
||||
if (events != oldEvents)
|
||||
{
|
||||
if (oldEvents & Event.read || oldEvents & Event.accept)
|
||||
{
|
||||
set(watcher.socket.handle, EVFILT_READ, EV_DELETE);
|
||||
}
|
||||
if (oldEvents & Event.write)
|
||||
{
|
||||
set(watcher.socket.handle, EVFILT_WRITE, EV_DELETE);
|
||||
}
|
||||
}
|
||||
if (events & (Event.read | events & Event.accept))
|
||||
{
|
||||
set(watcher.socket.handle, EVFILT_READ, EV_ADD | EV_ENABLE);
|
||||
}
|
||||
if (events & Event.write)
|
||||
{
|
||||
set(watcher.socket.handle, EVFILT_WRITE, EV_ADD | EV_DISPATCH);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Does the actual polling.
|
||||
*/
|
||||
protected override void poll()
|
||||
{
|
||||
timespec ts;
|
||||
blockTime.split!("seconds", "nsecs")(ts.tv_sec, ts.tv_nsec);
|
||||
|
||||
if (changeCount > maxEvents)
|
||||
{
|
||||
MmapPool.instance.resizeArray(events, changes.length);
|
||||
}
|
||||
|
||||
auto eventCount = kevent(fd, changes.ptr, cast(int) changeCount, events.ptr, maxEvents, &ts);
|
||||
changeCount = 0;
|
||||
|
||||
if (eventCount < 0)
|
||||
{
|
||||
if (errno != EINTR)
|
||||
{
|
||||
throw defaultAllocator.make!BadLoopException();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i; i < eventCount; ++i)
|
||||
{
|
||||
assert(connections.length > events[i].ident);
|
||||
|
||||
IOWatcher io = cast(IOWatcher) connections[events[i].ident];
|
||||
// If it is a ConnectionWatcher. Accept connections.
|
||||
if (io is null)
|
||||
{
|
||||
acceptConnections(connections[events[i].ident]);
|
||||
}
|
||||
else if (events[i].flags & EV_ERROR)
|
||||
{
|
||||
kill(io, null);
|
||||
}
|
||||
else if (events[i].filter == EVFILT_READ)
|
||||
{
|
||||
auto transport = cast(SelectorStreamTransport) io.transport;
|
||||
assert(transport !is null);
|
||||
|
||||
SocketException exception;
|
||||
try
|
||||
{
|
||||
ptrdiff_t received;
|
||||
do
|
||||
{
|
||||
received = transport.socket.receive(io.output[]);
|
||||
io.output += received;
|
||||
}
|
||||
while (received);
|
||||
}
|
||||
catch (SocketException e)
|
||||
{
|
||||
exception = e;
|
||||
}
|
||||
if (transport.socket.disconnected)
|
||||
{
|
||||
kill(io, exception);
|
||||
}
|
||||
else if (io.output.length)
|
||||
{
|
||||
swapPendings.insertBack(io);
|
||||
}
|
||||
}
|
||||
else if (events[i].filter == EVFILT_WRITE)
|
||||
{
|
||||
auto transport = cast(SelectorStreamTransport) io.transport;
|
||||
assert(transport !is null);
|
||||
|
||||
transport.writeReady = true;
|
||||
if (transport.input.length)
|
||||
{
|
||||
feed(transport);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns: The blocking time.
|
||||
*/
|
||||
override protected @property inout(Duration) blockTime()
|
||||
inout @safe pure nothrow
|
||||
{
|
||||
return min(super.blockTime, 1.dur!"seconds");
|
||||
}
|
||||
|
||||
/**
|
||||
* If the transport couldn't send the data, the further sending should
|
||||
* be handled by the event loop.
|
||||
*
|
||||
* Params:
|
||||
* transport = Transport.
|
||||
* exception = Exception thrown on sending.
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if the operation could be successfully
|
||||
* completed or scheduled, $(D_KEYWORD false) otherwise (the
|
||||
* transport is be destroyed then).
|
||||
*/
|
||||
protected override bool feed(SelectorStreamTransport transport, SocketException exception = null)
|
||||
{
|
||||
if (!super.feed(transport, exception))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (!transport.writeReady)
|
||||
{
|
||||
set(transport.socket.handle, EVFILT_WRITE, EV_DISPATCH);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
266
source/tanya/async/event/selector.d
Normal file
266
source/tanya/async/event/selector.d
Normal file
@ -0,0 +1,266 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* Copyright: Eugene Wissner 2016.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
|
||||
*/
|
||||
module tanya.async.event.selector;
|
||||
|
||||
version (Posix):
|
||||
|
||||
import tanya.async.loop;
|
||||
import tanya.async.transport;
|
||||
import tanya.async.watcher;
|
||||
import tanya.container.buffer;
|
||||
import tanya.memory;
|
||||
import tanya.memory.mmappool;
|
||||
import tanya.network.socket;
|
||||
import core.sys.posix.netinet.in_;
|
||||
import core.stdc.errno;
|
||||
|
||||
/**
|
||||
* Transport for stream sockets.
|
||||
*/
|
||||
class SelectorStreamTransport : StreamTransport
|
||||
{
|
||||
private ConnectedSocket socket_;
|
||||
|
||||
/// Input buffer.
|
||||
package WriteBuffer input;
|
||||
|
||||
private SelectorLoop loop;
|
||||
|
||||
/// Received notification that the underlying socket is write-ready.
|
||||
package bool writeReady;
|
||||
|
||||
/**
|
||||
* Params:
|
||||
* loop = Event loop.
|
||||
* socket = Socket.
|
||||
*/
|
||||
this(SelectorLoop loop, ConnectedSocket socket)
|
||||
{
|
||||
socket_ = socket;
|
||||
this.loop = loop;
|
||||
input = MmapPool.instance.make!WriteBuffer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the transport and deallocate the data buffers.
|
||||
*/
|
||||
~this()
|
||||
{
|
||||
MmapPool.instance.dispose(input);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns: Transport socket.
|
||||
*/
|
||||
inout(ConnectedSocket) socket() inout pure nothrow @safe @nogc
|
||||
{
|
||||
return socket_;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write some data to the transport.
|
||||
*
|
||||
* Params:
|
||||
* data = Data to send.
|
||||
*/
|
||||
void write(ubyte[] data)
|
||||
{
|
||||
if (!data.length)
|
||||
{
|
||||
return;
|
||||
}
|
||||
// Try to write if the socket is write ready.
|
||||
if (writeReady)
|
||||
{
|
||||
ptrdiff_t sent;
|
||||
SocketException exception;
|
||||
try
|
||||
{
|
||||
sent = socket.send(data);
|
||||
if (sent == 0)
|
||||
{
|
||||
writeReady = false;
|
||||
}
|
||||
}
|
||||
catch (SocketException e)
|
||||
{
|
||||
writeReady = false;
|
||||
exception = e;
|
||||
}
|
||||
if (sent < data.length)
|
||||
{
|
||||
input ~= data[sent..$];
|
||||
loop.feed(this, exception);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
input ~= data;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
abstract class SelectorLoop : Loop
|
||||
{
|
||||
/// Pending connections.
|
||||
protected ConnectionWatcher[] connections;
|
||||
|
||||
this()
|
||||
{
|
||||
super();
|
||||
connections = MmapPool.instance.makeArray!ConnectionWatcher(maxEvents);
|
||||
}
|
||||
|
||||
~this()
|
||||
{
|
||||
foreach (ref connection; connections)
|
||||
{
|
||||
// We want to free only IOWatchers. ConnectionWatcher are created by the
|
||||
// user and should be freed by himself.
|
||||
auto io = cast(IOWatcher) connection;
|
||||
if (io !is null)
|
||||
{
|
||||
MmapPool.instance.dispose(io);
|
||||
connection = null;
|
||||
}
|
||||
}
|
||||
MmapPool.instance.dispose(connections);
|
||||
}
|
||||
|
||||
/**
|
||||
* If the transport couldn't send the data, the further sending should
|
||||
* be handled by the event loop.
|
||||
*
|
||||
* Params:
|
||||
* transport = Transport.
|
||||
* exception = Exception thrown on sending.
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if the operation could be successfully
|
||||
* completed or scheduled, $(D_KEYWORD false) otherwise (the
|
||||
* transport will be destroyed then).
|
||||
*/
|
||||
protected bool feed(SelectorStreamTransport transport, SocketException exception = null)
|
||||
{
|
||||
while (transport.input.length && transport.writeReady)
|
||||
{
|
||||
try
|
||||
{
|
||||
ptrdiff_t sent = transport.socket.send(transport.input[]);
|
||||
if (sent == 0)
|
||||
{
|
||||
transport.writeReady = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
transport.input += sent;
|
||||
}
|
||||
}
|
||||
catch (SocketException e)
|
||||
{
|
||||
exception = e;
|
||||
transport.writeReady = false;
|
||||
}
|
||||
}
|
||||
if (exception !is null)
|
||||
{
|
||||
auto watcher = cast(IOWatcher) connections[transport.socket.handle];
|
||||
assert(watcher !is null);
|
||||
|
||||
kill(watcher, exception);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Start watching.
|
||||
*
|
||||
* Params:
|
||||
* watcher = Watcher.
|
||||
*/
|
||||
override void start(ConnectionWatcher watcher)
|
||||
{
|
||||
if (watcher.active)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (connections.length <= watcher.socket)
|
||||
{
|
||||
MmapPool.instance.resizeArray(connections, watcher.socket.handle + maxEvents / 2);
|
||||
}
|
||||
connections[watcher.socket.handle] = watcher;
|
||||
|
||||
super.start(watcher);
|
||||
}
|
||||
|
||||
/**
|
||||
* Accept incoming connections.
|
||||
*
|
||||
* Params:
|
||||
* connection = Connection watcher ready to accept.
|
||||
*/
|
||||
package void acceptConnections(ConnectionWatcher connection)
|
||||
in
|
||||
{
|
||||
assert(connection !is null);
|
||||
}
|
||||
body
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
ConnectedSocket client;
|
||||
try
|
||||
{
|
||||
client = (cast(StreamSocket) connection.socket).accept();
|
||||
}
|
||||
catch (SocketException e)
|
||||
{
|
||||
defaultAllocator.dispose(e);
|
||||
break;
|
||||
}
|
||||
if (client is null)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
IOWatcher io;
|
||||
auto transport = MmapPool.instance.make!SelectorStreamTransport(this, client);
|
||||
|
||||
if (connections.length >= client.handle)
|
||||
{
|
||||
io = cast(IOWatcher) connections[client.handle];
|
||||
}
|
||||
else
|
||||
{
|
||||
MmapPool.instance.resizeArray(connections, client.handle + maxEvents / 2);
|
||||
}
|
||||
if (io is null)
|
||||
{
|
||||
io = MmapPool.instance.make!IOWatcher(transport,
|
||||
connection.protocol);
|
||||
connections[client.handle] = io;
|
||||
}
|
||||
else
|
||||
{
|
||||
io(transport, connection.protocol);
|
||||
}
|
||||
|
||||
reify(io, EventMask(Event.none), EventMask(Event.read, Event.write));
|
||||
connection.incoming.insertBack(io);
|
||||
}
|
||||
|
||||
if (!connection.incoming.empty)
|
||||
{
|
||||
swapPendings.insertBack(connection);
|
||||
}
|
||||
}
|
||||
}
|
493
source/tanya/async/loop.d
Normal file
493
source/tanya/async/loop.d
Normal file
@ -0,0 +1,493 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* Copyright: Eugene Wissner 2016.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
|
||||
*
|
||||
* ---
|
||||
* import tanya.async;
|
||||
* import tanya.network.socket;
|
||||
*
|
||||
* class EchoProtocol : TransmissionControlProtocol
|
||||
* {
|
||||
* private DuplexTransport transport;
|
||||
*
|
||||
* void received(ubyte[] data)
|
||||
* {
|
||||
* transport.write(data);
|
||||
* }
|
||||
*
|
||||
* void connected(DuplexTransport transport)
|
||||
* {
|
||||
* this.transport = transport;
|
||||
* }
|
||||
*
|
||||
* void disconnected(SocketException exception = null)
|
||||
* {
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* void main()
|
||||
* {
|
||||
* auto address = new InternetAddress("127.0.0.1", cast(ushort) 8192);
|
||||
* version (Windows)
|
||||
* {
|
||||
* auto sock = new OverlappedStreamSocket(AddressFamily.INET)
|
||||
* }
|
||||
* else
|
||||
* {
|
||||
* auto sock = new StreamSocket(AddressFamily.INET)
|
||||
* sock.blocking = false;
|
||||
* }
|
||||
*
|
||||
* sock.bind(address);
|
||||
* sock.listen(5);
|
||||
*
|
||||
* auto io = new ConnectionWatcher(sock);
|
||||
* io.setProtocol!EchoProtocol;
|
||||
*
|
||||
* defaultLoop.start(io);
|
||||
* defaultLoop.run();
|
||||
*
|
||||
* sock.shutdown();
|
||||
* }
|
||||
* ---
|
||||
*/
|
||||
module tanya.async.loop;
|
||||
|
||||
import tanya.async.protocol;
|
||||
import tanya.async.transport;
|
||||
import tanya.async.watcher;
|
||||
import tanya.container.buffer;
|
||||
import tanya.memory;
|
||||
import tanya.memory.mmappool;
|
||||
import tanya.network.socket;
|
||||
import core.time;
|
||||
import std.algorithm.iteration;
|
||||
import std.algorithm.mutation;
|
||||
import std.typecons;
|
||||
|
||||
version (DisableBackends)
|
||||
{
|
||||
}
|
||||
else version (linux)
|
||||
{
|
||||
import tanya.async.event.epoll;
|
||||
version = Epoll;
|
||||
}
|
||||
else version (Windows)
|
||||
{
|
||||
import tanya.async.event.iocp;
|
||||
version = IOCP;
|
||||
}
|
||||
else version (OSX)
|
||||
{
|
||||
version = Kqueue;
|
||||
}
|
||||
else version (iOS)
|
||||
{
|
||||
version = Kqueue;
|
||||
}
|
||||
else version (FreeBSD)
|
||||
{
|
||||
version = Kqueue;
|
||||
}
|
||||
else version (OpenBSD)
|
||||
{
|
||||
version = Kqueue;
|
||||
}
|
||||
else version (DragonFlyBSD)
|
||||
{
|
||||
version = Kqueue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Events.
|
||||
*/
|
||||
enum Event : uint
|
||||
{
|
||||
none = 0x00, /// No events.
|
||||
read = 0x01, /// Non-blocking read call.
|
||||
write = 0x02, /// Non-blocking write call.
|
||||
accept = 0x04, /// Connection made.
|
||||
error = 0x80000000, /// Sent when an error occurs.
|
||||
}
|
||||
|
||||
alias EventMask = BitFlags!Event;
|
||||
|
||||
/**
|
||||
* Tries to set $(D_PSYMBOL MmapPool) to the default allocator.
|
||||
*/
|
||||
shared static this()
|
||||
{
|
||||
if (allocator is null)
|
||||
{
|
||||
allocator = MmapPool.instance;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Event loop.
|
||||
*/
|
||||
abstract class Loop
|
||||
{
|
||||
/// Pending watchers.
|
||||
protected PendingQueue!Watcher pendings;
|
||||
|
||||
protected PendingQueue!Watcher swapPendings;
|
||||
|
||||
/**
|
||||
* Returns: Maximal event count can be got at a time
|
||||
* (should be supported by the backend).
|
||||
*/
|
||||
protected @property inout(uint) maxEvents() inout const pure nothrow @safe @nogc
|
||||
{
|
||||
return 128U;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the loop.
|
||||
*/
|
||||
this()
|
||||
{
|
||||
pendings = MmapPool.instance.make!(PendingQueue!Watcher);
|
||||
swapPendings = MmapPool.instance.make!(PendingQueue!Watcher);
|
||||
}
|
||||
|
||||
/**
|
||||
* Frees loop internals.
|
||||
*/
|
||||
~this()
|
||||
{
|
||||
MmapPool.instance.dispose(pendings);
|
||||
MmapPool.instance.dispose(swapPendings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the loop.
|
||||
*/
|
||||
void run()
|
||||
{
|
||||
done_ = false;
|
||||
do
|
||||
{
|
||||
poll();
|
||||
|
||||
// Invoke pendings
|
||||
swapPendings.each!((ref p) => p.invoke());
|
||||
|
||||
swap(pendings, swapPendings);
|
||||
}
|
||||
while (!done_);
|
||||
}
|
||||
|
||||
/**
|
||||
* Break out of the loop.
|
||||
*/
|
||||
void unloop() @safe pure nothrow
|
||||
{
|
||||
done_ = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Start watching.
|
||||
*
|
||||
* Params:
|
||||
* watcher = Watcher.
|
||||
*/
|
||||
void start(ConnectionWatcher watcher)
|
||||
{
|
||||
if (watcher.active)
|
||||
{
|
||||
return;
|
||||
}
|
||||
watcher.active = true;
|
||||
reify(watcher, EventMask(Event.none), EventMask(Event.accept));
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop watching.
|
||||
*
|
||||
* Params:
|
||||
* watcher = Watcher.
|
||||
*/
|
||||
void stop(ConnectionWatcher watcher)
|
||||
{
|
||||
if (!watcher.active)
|
||||
{
|
||||
return;
|
||||
}
|
||||
watcher.active = false;
|
||||
|
||||
reify(watcher, EventMask(Event.accept), EventMask(Event.none));
|
||||
}
|
||||
|
||||
/**
|
||||
* Should be called if the backend configuration changes.
|
||||
*
|
||||
* Params:
|
||||
* watcher = Watcher.
|
||||
* oldEvents = The events were already set.
|
||||
* events = The events should be set.
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if the operation was successful.
|
||||
*/
|
||||
abstract protected bool reify(ConnectionWatcher watcher,
|
||||
EventMask oldEvents,
|
||||
EventMask events);
|
||||
|
||||
/**
|
||||
* Returns: The blocking time.
|
||||
*/
|
||||
protected @property inout(Duration) blockTime()
|
||||
inout @safe pure nothrow
|
||||
{
|
||||
// Don't block if we have to do.
|
||||
return swapPendings.empty ? blockTime_ : Duration.zero;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the blocking time for IO watchers.
|
||||
*
|
||||
* Params:
|
||||
* blockTime = The blocking time. Cannot be larger than
|
||||
* $(D_PSYMBOL maxBlockTime).
|
||||
*/
|
||||
protected @property void blockTime(in Duration blockTime) @safe pure nothrow
|
||||
in
|
||||
{
|
||||
assert(blockTime <= 1.dur!"hours", "Too long to wait.");
|
||||
assert(!blockTime.isNegative);
|
||||
}
|
||||
body
|
||||
{
|
||||
blockTime_ = blockTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Kills the watcher and closes the connection.
|
||||
*/
|
||||
protected void kill(IOWatcher watcher, SocketException exception)
|
||||
{
|
||||
watcher.socket.shutdown();
|
||||
defaultAllocator.dispose(watcher.socket);
|
||||
MmapPool.instance.dispose(watcher.transport);
|
||||
watcher.exception = exception;
|
||||
swapPendings.insertBack(watcher);
|
||||
}
|
||||
|
||||
/**
|
||||
* Does the actual polling.
|
||||
*/
|
||||
abstract protected void poll();
|
||||
|
||||
/// Whether the event loop should be stopped.
|
||||
private bool done_;
|
||||
|
||||
/// Maximal block time.
|
||||
protected Duration blockTime_ = 1.dur!"minutes";
|
||||
}
|
||||
|
||||
/**
|
||||
* Exception thrown on errors in the event loop.
|
||||
*/
|
||||
class BadLoopException : Exception
|
||||
{
|
||||
@nogc:
|
||||
/**
|
||||
* Params:
|
||||
* file = The file where the exception occurred.
|
||||
* line = The line number where the exception occurred.
|
||||
* next = The previous exception in the chain of exceptions, if any.
|
||||
*/
|
||||
this(string file = __FILE__, size_t line = __LINE__, Throwable next = null)
|
||||
pure @safe nothrow const
|
||||
{
|
||||
super("Event loop cannot be initialized.", file, line, next);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* Returns: The default event loop.
|
||||
*/
|
||||
@property Loop defaultLoop()
|
||||
{
|
||||
if (defaultLoop_ !is null)
|
||||
{
|
||||
return defaultLoop_;
|
||||
}
|
||||
version (Epoll)
|
||||
{
|
||||
defaultLoop_ = MmapPool.instance.make!EpollLoop;
|
||||
}
|
||||
else version (IOCP)
|
||||
{
|
||||
defaultLoop_ = MmapPool.instance.make!IOCPLoop;
|
||||
}
|
||||
else version (Kqueue)
|
||||
{
|
||||
import tanya.async.event.kqueue;
|
||||
defaultLoop_ = MmapPool.instance.make!KqueueLoop;
|
||||
}
|
||||
return defaultLoop_;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the default event loop.
|
||||
*
|
||||
* This property makes it possible to implement your own backends or event
|
||||
* loops, for example, if the system is not supported or if you want to
|
||||
* extend the supported implementation. Just extend $(D_PSYMBOL Loop) and pass
|
||||
* your implementation to this property.
|
||||
*
|
||||
* Params:
|
||||
* loop = The event loop.
|
||||
*/
|
||||
@property void defaultLoop(Loop loop)
|
||||
in
|
||||
{
|
||||
assert(loop !is null);
|
||||
}
|
||||
body
|
||||
{
|
||||
defaultLoop_ = loop;
|
||||
}
|
||||
|
||||
private Loop defaultLoop_;
|
||||
|
||||
/**
|
||||
* Queue.
|
||||
*
|
||||
* Params:
|
||||
* T = Content type.
|
||||
*/
|
||||
class PendingQueue(T)
|
||||
{
|
||||
/**
|
||||
* Creates a new $(D_PSYMBOL Queue).
|
||||
*/
|
||||
this()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all elements from the queue.
|
||||
*/
|
||||
~this()
|
||||
{
|
||||
foreach (e; this)
|
||||
{
|
||||
MmapPool.instance.dispose(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns: First element.
|
||||
*/
|
||||
@property ref T front()
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
}
|
||||
body
|
||||
{
|
||||
return first.next.content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts a new element.
|
||||
*
|
||||
* Params:
|
||||
* x = New element.
|
||||
*
|
||||
* Returns: $(D_KEYWORD this).
|
||||
*/
|
||||
typeof(this) insertBack(T x)
|
||||
{
|
||||
Entry* temp = MmapPool.instance.make!Entry;
|
||||
|
||||
temp.content = x;
|
||||
|
||||
if (empty)
|
||||
{
|
||||
first.next = rear = temp;
|
||||
}
|
||||
else
|
||||
{
|
||||
rear.next = temp;
|
||||
rear = rear.next;
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
alias insert = insertBack;
|
||||
|
||||
/**
|
||||
* Inserts a new element.
|
||||
*
|
||||
* Params:
|
||||
* x = New element.
|
||||
*
|
||||
* Returns: $(D_KEYWORD this).
|
||||
*/
|
||||
typeof(this) opOpAssign(string Op)(ref T x)
|
||||
if (Op == "~")
|
||||
{
|
||||
return insertBack(x);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns: $(D_KEYWORD true) if the queue is empty.
|
||||
*/
|
||||
@property bool empty() const @safe pure nothrow
|
||||
{
|
||||
return first.next is null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Move position to the next element.
|
||||
*
|
||||
* Returns: $(D_KEYWORD this).
|
||||
*/
|
||||
typeof(this) popFront()
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
}
|
||||
body
|
||||
{
|
||||
auto n = first.next.next;
|
||||
|
||||
MmapPool.instance.dispose(first.next);
|
||||
first.next = n;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Queue entry.
|
||||
*/
|
||||
protected struct Entry
|
||||
{
|
||||
/// Queue item content.
|
||||
T content;
|
||||
|
||||
/// Next list item.
|
||||
Entry* next;
|
||||
}
|
||||
|
||||
/// The first element of the list.
|
||||
protected Entry first;
|
||||
|
||||
/// The last element of the list.
|
||||
protected Entry* rear;
|
||||
}
|
@ -1,26 +1,19 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* Copyright: Eugene Wissner 2016.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
|
||||
*/
|
||||
module tanya.event.config;
|
||||
|
||||
package version (DisableBackends)
|
||||
{
|
||||
}
|
||||
else
|
||||
{
|
||||
version (linux)
|
||||
{
|
||||
enum UseEpoll = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
enum UseEpoll = false;
|
||||
}
|
||||
}
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* Copyright: Eugene Wissner 2016.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
|
||||
*/
|
||||
module tanya.async;
|
||||
|
||||
public
|
||||
{
|
||||
import tanya.async.loop;
|
||||
import tanya.async.protocol;
|
||||
import tanya.async.transport;
|
||||
import tanya.async.watcher;
|
||||
}
|
50
source/tanya/async/protocol.d
Normal file
50
source/tanya/async/protocol.d
Normal file
@ -0,0 +1,50 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* Copyright: Eugene Wissner 2016.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
|
||||
*/
|
||||
module tanya.async.protocol;
|
||||
|
||||
import tanya.network.socket;
|
||||
import tanya.async.transport;
|
||||
|
||||
/**
|
||||
* Common protocol interface.
|
||||
*/
|
||||
interface Protocol
|
||||
{
|
||||
/**
|
||||
* Params:
|
||||
* data = Read data.
|
||||
*/
|
||||
void received(ubyte[] data);
|
||||
|
||||
/**
|
||||
* Called when a connection is made.
|
||||
*
|
||||
* Params:
|
||||
* transport = Protocol transport.
|
||||
*/
|
||||
void connected(DuplexTransport transport);
|
||||
|
||||
/**
|
||||
* Called when a connection is lost.
|
||||
*
|
||||
* Params:
|
||||
* exception = $(D_PSYMBOL Exception) if an error caused
|
||||
* the disconnect, $(D_KEYWORD null) otherwise.
|
||||
*/
|
||||
void disconnected(SocketException exception = null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface for TCP.
|
||||
*/
|
||||
interface TransmissionControlProtocol : Protocol
|
||||
{
|
||||
}
|
63
source/tanya/async/transport.d
Normal file
63
source/tanya/async/transport.d
Normal file
@ -0,0 +1,63 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* Copyright: Eugene Wissner 2016.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
|
||||
*/
|
||||
module tanya.async.transport;
|
||||
|
||||
import tanya.network.socket;
|
||||
|
||||
/**
|
||||
* Base transport interface.
|
||||
*/
|
||||
interface Transport
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface for read-only transports.
|
||||
*/
|
||||
interface ReadTransport : Transport
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface for write-only transports.
|
||||
*/
|
||||
interface WriteTransport : Transport
|
||||
{
|
||||
/**
|
||||
* Write some data to the transport.
|
||||
*
|
||||
* Params:
|
||||
* data = Data to send.
|
||||
*/
|
||||
void write(ubyte[] data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a bidirectional transport.
|
||||
*/
|
||||
interface DuplexTransport : ReadTransport, WriteTransport
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a socket transport.
|
||||
*/
|
||||
interface SocketTransport : Transport
|
||||
{
|
||||
@property inout(Socket) socket() inout pure nothrow @safe @nogc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a connection-oriented socket transport.
|
||||
*/
|
||||
package interface StreamTransport : DuplexTransport, SocketTransport
|
||||
{
|
||||
}
|
242
source/tanya/async/watcher.d
Normal file
242
source/tanya/async/watcher.d
Normal file
@ -0,0 +1,242 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* Copyright: Eugene Wissner 2016.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
|
||||
*/
|
||||
module tanya.async.watcher;
|
||||
|
||||
import tanya.async.loop;
|
||||
import tanya.async.protocol;
|
||||
import tanya.async.transport;
|
||||
import tanya.container.buffer;
|
||||
import tanya.memory;
|
||||
import tanya.memory.mmappool;
|
||||
import tanya.network.socket;
|
||||
import std.functional;
|
||||
import std.exception;
|
||||
|
||||
version (Windows)
|
||||
{
|
||||
import core.sys.windows.basetyps;
|
||||
import core.sys.windows.mswsock;
|
||||
import core.sys.windows.winbase;
|
||||
import core.sys.windows.windef;
|
||||
import core.sys.windows.winsock2;
|
||||
}
|
||||
|
||||
/**
|
||||
* A watcher is an opaque structure that you allocate and register to record
|
||||
* your interest in some event.
|
||||
*/
|
||||
abstract class Watcher
|
||||
{
|
||||
/// Whether the watcher is active.
|
||||
bool active;
|
||||
|
||||
/**
|
||||
* Invoke some action on event.
|
||||
*/
|
||||
void invoke();
|
||||
}
|
||||
|
||||
class ConnectionWatcher : Watcher
|
||||
{
|
||||
/// Watched socket.
|
||||
private Socket socket_;
|
||||
|
||||
/// Protocol factory.
|
||||
protected Protocol delegate() protocolFactory;
|
||||
|
||||
package PendingQueue!IOWatcher incoming;
|
||||
|
||||
/**
|
||||
* Params:
|
||||
* socket = Socket.
|
||||
*/
|
||||
this(Socket socket)
|
||||
{
|
||||
socket_ = socket;
|
||||
incoming = MmapPool.instance.make!(PendingQueue!IOWatcher);
|
||||
}
|
||||
|
||||
/// Ditto.
|
||||
protected this()
|
||||
{
|
||||
}
|
||||
|
||||
~this()
|
||||
{
|
||||
MmapPool.instance.dispose(incoming);
|
||||
}
|
||||
|
||||
/*
|
||||
* Params:
|
||||
* P = Protocol should be used.
|
||||
*/
|
||||
void setProtocol(P : Protocol)()
|
||||
{
|
||||
this.protocolFactory = () => cast(Protocol) MmapPool.instance.make!P;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns: Socket.
|
||||
*/
|
||||
@property inout(Socket) socket() inout pure nothrow @nogc
|
||||
{
|
||||
return socket_;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns: New protocol instance.
|
||||
*/
|
||||
@property Protocol protocol()
|
||||
in
|
||||
{
|
||||
assert(protocolFactory !is null, "Protocol isn't set.");
|
||||
}
|
||||
body
|
||||
{
|
||||
return protocolFactory();
|
||||
}
|
||||
|
||||
/**
|
||||
* Invokes new connection callback.
|
||||
*/
|
||||
override void invoke()
|
||||
{
|
||||
foreach (io; incoming)
|
||||
{
|
||||
io.protocol.connected(cast(DuplexTransport) io.transport);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Contains a pending watcher with the invoked events or a transport can be
|
||||
* read from.
|
||||
*/
|
||||
class IOWatcher : ConnectionWatcher
|
||||
{
|
||||
/// If an exception was thrown the transport should be already invalid.
|
||||
private union
|
||||
{
|
||||
StreamTransport transport_;
|
||||
SocketException exception_;
|
||||
}
|
||||
|
||||
private Protocol protocol_;
|
||||
|
||||
/**
|
||||
* Returns: Underlying output buffer.
|
||||
*/
|
||||
package ReadBuffer output;
|
||||
|
||||
/**
|
||||
* Params:
|
||||
* transport = Transport.
|
||||
* protocol = New instance of the application protocol.
|
||||
*/
|
||||
this(StreamTransport transport, Protocol protocol)
|
||||
in
|
||||
{
|
||||
assert(transport !is null);
|
||||
assert(protocol !is null);
|
||||
}
|
||||
body
|
||||
{
|
||||
super();
|
||||
transport_ = transport;
|
||||
protocol_ = protocol;
|
||||
output = MmapPool.instance.make!ReadBuffer();
|
||||
active = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroys the watcher.
|
||||
*/
|
||||
protected ~this()
|
||||
{
|
||||
MmapPool.instance.dispose(output);
|
||||
MmapPool.instance.dispose(protocol_);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assigns a transport.
|
||||
*
|
||||
* Params:
|
||||
* transport = Transport.
|
||||
* protocol = Application protocol.
|
||||
*
|
||||
* Returns: $(D_KEYWORD this).
|
||||
*/
|
||||
IOWatcher opCall(StreamTransport transport, Protocol protocol) pure nothrow @safe @nogc
|
||||
in
|
||||
{
|
||||
assert(transport !is null);
|
||||
assert(protocol !is null);
|
||||
}
|
||||
body
|
||||
{
|
||||
transport_ = transport;
|
||||
protocol_ = protocol;
|
||||
active = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns: Transport used by this watcher.
|
||||
*/
|
||||
@property inout(StreamTransport) transport() inout pure nothrow @nogc
|
||||
{
|
||||
return transport_;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets an exception occurred during a read/write operation.
|
||||
*
|
||||
* Params:
|
||||
* exception = Thrown exception.
|
||||
*/
|
||||
@property void exception(SocketException exception) pure nothrow @nogc
|
||||
{
|
||||
exception_ = exception;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns: Application protocol.
|
||||
*/
|
||||
override @property Protocol protocol() pure nothrow @safe @nogc
|
||||
{
|
||||
return protocol_;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns: Socket.
|
||||
*/
|
||||
override @property inout(Socket) socket() inout pure nothrow @nogc
|
||||
{
|
||||
return transport.socket;
|
||||
}
|
||||
|
||||
/**
|
||||
* Invokes the watcher callback.
|
||||
*/
|
||||
override void invoke()
|
||||
{
|
||||
if (output.length)
|
||||
{
|
||||
protocol.received(output[0..$]);
|
||||
output.clear();
|
||||
}
|
||||
else
|
||||
{
|
||||
protocol.disconnected(exception_);
|
||||
active = false;
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -1,223 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* Copyright: Eugene Wissner 2016.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
|
||||
*/
|
||||
module tanya.event.internal.epoll;
|
||||
|
||||
import tanya.event.config;
|
||||
|
||||
static if (UseEpoll):
|
||||
|
||||
public import core.sys.linux.epoll;
|
||||
import tanya.event.internal.selector;
|
||||
import tanya.event.protocol;
|
||||
import tanya.event.transport;
|
||||
import tanya.event.watcher;
|
||||
import tanya.event.loop;
|
||||
import tanya.container.list;
|
||||
import tanya.memory;
|
||||
import core.stdc.errno;
|
||||
import core.sys.posix.fcntl;
|
||||
import core.sys.posix.netinet.in_;
|
||||
import core.time;
|
||||
import std.algorithm.comparison;
|
||||
|
||||
extern (C) nothrow
|
||||
{ // TODO: Make a pull request for Phobos to mark this extern functions as @nogc.
|
||||
int epoll_create1(int __flags);
|
||||
int epoll_ctl(int __epfd, int __op, int __fd, epoll_event *__event);
|
||||
int epoll_wait(int __epfd, epoll_event *__events, int __maxevents, int __timeout);
|
||||
int accept4(int, sockaddr*, socklen_t*, int flags);
|
||||
}
|
||||
|
||||
private enum maxEvents = 128;
|
||||
|
||||
class EpollLoop : Loop
|
||||
{
|
||||
/**
|
||||
* Initializes the loop.
|
||||
*/
|
||||
this()
|
||||
{
|
||||
super();
|
||||
|
||||
if ((fd = epoll_create1(EPOLL_CLOEXEC)) < 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
epollEvents = makeArray!epoll_event(defaultAllocator, maxEvents).ptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Frees loop internals.
|
||||
*/
|
||||
~this()
|
||||
{
|
||||
dispose(defaultAllocator, epollEvents);
|
||||
}
|
||||
|
||||
/**
|
||||
* Should be called if the backend configuration changes.
|
||||
*
|
||||
* Params:
|
||||
* socket = Socket.
|
||||
* oldEvents = The events were already set.
|
||||
* events = The events should be set.
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if the operation was successful.
|
||||
*/
|
||||
protected override bool modify(int socket, EventMask oldEvents, EventMask events)
|
||||
{
|
||||
int op = EPOLL_CTL_DEL;
|
||||
epoll_event ev;
|
||||
|
||||
if (events == oldEvents)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (events && oldEvents)
|
||||
{
|
||||
op = EPOLL_CTL_MOD;
|
||||
}
|
||||
else if (events && !oldEvents)
|
||||
{
|
||||
op = EPOLL_CTL_ADD;
|
||||
}
|
||||
|
||||
ev.data.fd = socket;
|
||||
ev.events = (events & (Event.read | Event.accept) ? EPOLLIN | EPOLLPRI : 0)
|
||||
| (events & Event.write ? EPOLLOUT : 0)
|
||||
| EPOLLET;
|
||||
|
||||
return epoll_ctl(fd, op, socket, &ev) == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Accept incoming connections.
|
||||
*
|
||||
* Params:
|
||||
* protocolFactory = Protocol factory.
|
||||
* socket = Socket.
|
||||
*/
|
||||
protected override void acceptConnection(Protocol delegate() protocolFactory,
|
||||
int socket)
|
||||
{
|
||||
sockaddr_in client_addr;
|
||||
socklen_t client_len = client_addr.sizeof;
|
||||
int client = accept4(socket,
|
||||
cast(sockaddr *)&client_addr,
|
||||
&client_len,
|
||||
O_NONBLOCK);
|
||||
while (client >= 0)
|
||||
{
|
||||
auto transport = make!SocketTransport(defaultAllocator, this, client);
|
||||
IOWatcher connection;
|
||||
|
||||
if (connections.length > client)
|
||||
{
|
||||
connection = cast(IOWatcher) connections[client];
|
||||
// If it is a ConnectionWatcher
|
||||
if (connection is null && connections[client] !is null)
|
||||
{
|
||||
dispose(defaultAllocator, connections[client]);
|
||||
connections[client] = null;
|
||||
}
|
||||
}
|
||||
if (connection !is null)
|
||||
{
|
||||
connection(protocolFactory, transport);
|
||||
}
|
||||
else
|
||||
{
|
||||
connections[client] = make!IOWatcher(defaultAllocator,
|
||||
protocolFactory,
|
||||
transport);
|
||||
}
|
||||
|
||||
modify(client, EventMask(Event.none), EventMask(Event.read, Event.write));
|
||||
|
||||
swapPendings.insertBack(connections[client]);
|
||||
|
||||
client = accept4(socket,
|
||||
cast(sockaddr *)&client_addr,
|
||||
&client_len,
|
||||
O_NONBLOCK);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Does the actual polling.
|
||||
*/
|
||||
protected override void poll()
|
||||
{
|
||||
// Don't block
|
||||
immutable timeout = cast(immutable int) blockTime.total!"msecs";
|
||||
auto eventCount = epoll_wait(fd, epollEvents, maxEvents, timeout);
|
||||
|
||||
if (eventCount < 0)
|
||||
{
|
||||
if (errno != EINTR)
|
||||
{
|
||||
throw make!BadLoopException(defaultAllocator);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto i = 0; i < eventCount; ++i)
|
||||
{
|
||||
epoll_event *ev = epollEvents + i;
|
||||
auto connection = cast(IOWatcher) connections[ev.data.fd];
|
||||
|
||||
if (connection is null)
|
||||
{
|
||||
swapPendings.insertBack(connections[ev.data.fd]);
|
||||
// acceptConnection(connections[ev.data.fd].protocol,
|
||||
// connections[ev.data.fd].socket);
|
||||
}
|
||||
else
|
||||
{
|
||||
auto transport = cast(SocketTransport) connection.transport;
|
||||
assert(transport !is null);
|
||||
|
||||
if (ev.events & (EPOLLIN | EPOLLPRI | EPOLLERR | EPOLLHUP))
|
||||
{
|
||||
try
|
||||
{
|
||||
while (!transport.receive())
|
||||
{
|
||||
}
|
||||
swapPendings.insertBack(connection);
|
||||
}
|
||||
catch (TransportException e)
|
||||
{
|
||||
swapPendings.insertBack(connection);
|
||||
dispose(defaultAllocator, e);
|
||||
}
|
||||
}
|
||||
else if (ev.events & (EPOLLOUT | EPOLLERR | EPOLLHUP))
|
||||
{
|
||||
transport.writeReady = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns: The blocking time.
|
||||
*/
|
||||
override protected @property inout(Duration) blockTime()
|
||||
inout @safe pure nothrow
|
||||
{
|
||||
return min(super.blockTime, 1.dur!"seconds");
|
||||
}
|
||||
|
||||
private int fd;
|
||||
private epoll_event* epollEvents;
|
||||
}
|
@ -1,216 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* Copyright: Eugene Wissner 2016.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
|
||||
*/
|
||||
module tanya.event.internal.selector;
|
||||
|
||||
import tanya.memory;
|
||||
import tanya.container.buffer;
|
||||
import tanya.event.loop;
|
||||
import tanya.event.protocol;
|
||||
import tanya.event.transport;
|
||||
import core.stdc.errno;
|
||||
import core.sys.posix.netinet.in_;
|
||||
import core.sys.posix.unistd;
|
||||
|
||||
/**
|
||||
* Transport for stream sockets.
|
||||
*/
|
||||
class SocketTransport : DuplexTransport
|
||||
{
|
||||
private int socket_ = -1;
|
||||
|
||||
private Protocol protocol_;
|
||||
|
||||
/// Input buffer.
|
||||
private WriteBuffer input_;
|
||||
|
||||
/// Output buffer.
|
||||
private ReadBuffer output_;
|
||||
|
||||
private Loop loop;
|
||||
|
||||
private bool disconnected_;
|
||||
|
||||
package bool writeReady;
|
||||
|
||||
/**
|
||||
* Params:
|
||||
* loop = Event loop.
|
||||
* socket = Socket.
|
||||
* protocol = Protocol.
|
||||
*/
|
||||
this(Loop loop, int socket, Protocol protocol = null)
|
||||
{
|
||||
socket_ = socket;
|
||||
protocol_ = protocol;
|
||||
this.loop = loop;
|
||||
input_ = make!WriteBuffer(defaultAllocator);
|
||||
output_ = make!ReadBuffer(defaultAllocator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the transport and deallocate the data buffers.
|
||||
*/
|
||||
~this()
|
||||
{
|
||||
close(socket);
|
||||
dispose(defaultAllocator, input_);
|
||||
dispose(defaultAllocator, output_);
|
||||
dispose(defaultAllocator, protocol_);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns: Transport socket.
|
||||
*/
|
||||
int socket() const @safe pure nothrow
|
||||
{
|
||||
return socket_;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns: Protocol.
|
||||
*/
|
||||
@property Protocol protocol() @safe pure nothrow
|
||||
{
|
||||
return protocol_;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns: $(D_KEYWORD true) if the remote peer closed the connection,
|
||||
* $(D_KEYWORD false) otherwise.
|
||||
*/
|
||||
@property immutable(bool) disconnected() const @safe pure nothrow
|
||||
{
|
||||
return disconnected_;
|
||||
}
|
||||
|
||||
/**
|
||||
* Params:
|
||||
* protocol = Application protocol.
|
||||
*/
|
||||
@property void protocol(Protocol protocol) @safe pure nothrow
|
||||
{
|
||||
protocol_ = protocol;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns: Application protocol.
|
||||
*/
|
||||
@property inout(Protocol) protocol() inout @safe pure nothrow
|
||||
{
|
||||
return protocol_;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write some data to the transport.
|
||||
*
|
||||
* Params:
|
||||
* data = Data to send.
|
||||
*/
|
||||
void write(ubyte[] data)
|
||||
{
|
||||
// If the buffer wasn't empty the transport should be already there.
|
||||
if (!input.length && data.length)
|
||||
{
|
||||
loop.feed(this);
|
||||
}
|
||||
input ~= data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns: Input buffer.
|
||||
*/
|
||||
@property WriteBuffer input() @safe pure nothrow
|
||||
{
|
||||
return input_;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns: Output buffer.
|
||||
*/
|
||||
@property ReadBuffer output() @safe pure nothrow
|
||||
{
|
||||
return output_;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read data from the socket. Returns $(D_KEYWORD true) if the reading
|
||||
* is completed. In the case that the peer closed the connection, returns
|
||||
* $(D_KEYWORD true) aswell.
|
||||
*
|
||||
* Returns: Whether the reading is completed.
|
||||
*
|
||||
* Throws: $(D_PSYMBOL TransportException) if a read error is occured.
|
||||
*/
|
||||
bool receive()
|
||||
{
|
||||
auto readCount = recv(socket, output.buffer, output.free, 0);
|
||||
|
||||
if (readCount > 0)
|
||||
{
|
||||
output_ ~= output.buffer[0..readCount];
|
||||
return false;
|
||||
}
|
||||
else if (readCount == 0)
|
||||
{
|
||||
disconnected_ = true;
|
||||
return true;
|
||||
}
|
||||
else if (errno == EAGAIN || errno == EWOULDBLOCK)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
disconnected_ = true;
|
||||
throw make!TransportException(defaultAllocator,
|
||||
"Read from the socket failed.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns: Whether the writing is completed.
|
||||
*
|
||||
* Throws: $(D_PSYMBOL TransportException) if a read error is occured.
|
||||
*/
|
||||
bool send()
|
||||
{
|
||||
auto sentCount = core.sys.posix.netinet.in_.send(socket,
|
||||
input.buffer,
|
||||
input.length,
|
||||
0);
|
||||
|
||||
input.written = sentCount;
|
||||
if (input.length == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else if (sentCount >= 0)
|
||||
{
|
||||
loop.feed(this);
|
||||
|
||||
return false;
|
||||
}
|
||||
else if (errno == EAGAIN || errno == EWOULDBLOCK)
|
||||
{
|
||||
writeReady = false;
|
||||
loop.feed(this);
|
||||
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
disconnected_ = true;
|
||||
loop.feed(this);
|
||||
throw make!TransportException(defaultAllocator,
|
||||
"Write to the socket failed.");
|
||||
}
|
||||
}
|
||||
}
|
@ -1,275 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* Copyright: Eugene Wissner 2016.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
|
||||
*/
|
||||
module tanya.event.loop;
|
||||
|
||||
import tanya.memory;
|
||||
import tanya.container.queue;
|
||||
import tanya.container.vector;
|
||||
import tanya.event.config;
|
||||
import tanya.event.protocol;
|
||||
import tanya.event.transport;
|
||||
import tanya.event.watcher;
|
||||
import tanya.container.buffer;
|
||||
import core.thread;
|
||||
import core.time;
|
||||
import std.algorithm.iteration;
|
||||
import std.algorithm.mutation;
|
||||
import std.typecons;
|
||||
|
||||
static if (UseEpoll)
|
||||
{
|
||||
import tanya.event.internal.epoll;
|
||||
}
|
||||
|
||||
/**
|
||||
* Events.
|
||||
*/
|
||||
enum Event : uint
|
||||
{
|
||||
none = 0x00, /// No events.
|
||||
read = 0x01, /// Non-blocking read call.
|
||||
write = 0x02, /// Non-blocking write call.
|
||||
accept = 0x04, /// Connection made.
|
||||
}
|
||||
|
||||
alias EventMask = BitFlags!Event;
|
||||
|
||||
/**
|
||||
* Event loop.
|
||||
*/
|
||||
abstract class Loop
|
||||
{
|
||||
/// Pending watchers.
|
||||
protected Queue!Watcher pendings;
|
||||
|
||||
protected Queue!Watcher swapPendings;
|
||||
|
||||
/// Pending connections.
|
||||
protected Vector!ConnectionWatcher connections;
|
||||
|
||||
/**
|
||||
* Initializes the loop.
|
||||
*/
|
||||
this()
|
||||
{
|
||||
connections = make!(Vector!ConnectionWatcher)(defaultAllocator);
|
||||
pendings = make!(Queue!Watcher)(defaultAllocator);
|
||||
swapPendings = make!(Queue!Watcher)(defaultAllocator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Frees loop internals.
|
||||
*/
|
||||
~this()
|
||||
{
|
||||
dispose(defaultAllocator, connections);
|
||||
dispose(defaultAllocator, pendings);
|
||||
dispose(defaultAllocator, swapPendings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the loop.
|
||||
*/
|
||||
void run()
|
||||
{
|
||||
done_ = false;
|
||||
do
|
||||
{
|
||||
poll();
|
||||
|
||||
// Invoke pendings
|
||||
swapPendings.each!((ref p) => p.invoke());
|
||||
|
||||
swap(pendings, swapPendings);
|
||||
}
|
||||
while (!done_);
|
||||
}
|
||||
|
||||
/**
|
||||
* Break out of the loop.
|
||||
*/
|
||||
void unloop() @safe pure nothrow
|
||||
{
|
||||
done_ = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Start watching.
|
||||
*
|
||||
* Params:
|
||||
* watcher = Watcher.
|
||||
*/
|
||||
void start(ConnectionWatcher watcher)
|
||||
{
|
||||
if (watcher.active)
|
||||
{
|
||||
return;
|
||||
}
|
||||
watcher.active = true;
|
||||
watcher.accept = &acceptConnection;
|
||||
connections[watcher.socket] = watcher;
|
||||
|
||||
modify(watcher.socket, EventMask(Event.none), EventMask(Event.accept));
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop watching.
|
||||
*
|
||||
* Params:
|
||||
* watcher = Watcher.
|
||||
*/
|
||||
void stop(ConnectionWatcher watcher)
|
||||
{
|
||||
if (!watcher.active)
|
||||
{
|
||||
return;
|
||||
}
|
||||
watcher.active = false;
|
||||
|
||||
modify(watcher.socket, EventMask(Event.accept), EventMask(Event.none));
|
||||
}
|
||||
|
||||
/**
|
||||
* Feeds the given event set into the event loop, as if the specified event
|
||||
* had happened for the specified watcher.
|
||||
*
|
||||
* Params:
|
||||
* transport = Affected transport.
|
||||
*/
|
||||
void feed(DuplexTransport transport)
|
||||
{
|
||||
pendings.insertBack(connections[transport.socket]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Should be called if the backend configuration changes.
|
||||
*
|
||||
* Params:
|
||||
* socket = Socket.
|
||||
* oldEvents = The events were already set.
|
||||
* events = The events should be set.
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if the operation was successful.
|
||||
*/
|
||||
protected bool modify(int socket, EventMask oldEvents, EventMask events);
|
||||
|
||||
/**
|
||||
* Returns: The blocking time.
|
||||
*/
|
||||
protected @property inout(Duration) blockTime()
|
||||
inout @safe pure nothrow
|
||||
{
|
||||
// Don't block if we have to do.
|
||||
return swapPendings.empty ? blockTime_ : Duration.zero;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the blocking time for IO watchers.
|
||||
*
|
||||
* Params:
|
||||
* blockTime = The blocking time. Cannot be larger than
|
||||
* $(D_PSYMBOL maxBlockTime).
|
||||
*/
|
||||
protected @property void blockTime(in Duration blockTime) @safe pure nothrow
|
||||
in
|
||||
{
|
||||
assert(blockTime <= 1.dur!"hours", "Too long to wait.");
|
||||
assert(!blockTime.isNegative);
|
||||
}
|
||||
body
|
||||
{
|
||||
blockTime_ = blockTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Does the actual polling.
|
||||
*/
|
||||
protected void poll();
|
||||
|
||||
/**
|
||||
* Accept incoming connections.
|
||||
*
|
||||
* Params:
|
||||
* protocolFactory = Protocol factory.
|
||||
* socket = Socket.
|
||||
*/
|
||||
protected void acceptConnection(Protocol delegate() protocolFactory,
|
||||
int socket);
|
||||
|
||||
/// Whether the event loop should be stopped.
|
||||
private bool done_;
|
||||
|
||||
/// Maximal block time.
|
||||
protected Duration blockTime_ = 1.dur!"minutes";
|
||||
}
|
||||
|
||||
/**
|
||||
* Exception thrown on errors in the event loop.
|
||||
*/
|
||||
class BadLoopException : Exception
|
||||
{
|
||||
/**
|
||||
* Params:
|
||||
* file = The file where the exception occurred.
|
||||
* line = The line number where the exception occurred.
|
||||
* next = The previous exception in the chain of exceptions, if any.
|
||||
*/
|
||||
this(string file = __FILE__, size_t line = __LINE__, Throwable next = null)
|
||||
pure @safe nothrow const @nogc
|
||||
{
|
||||
super("Event loop cannot be initialized.", file, line, next);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the event loop used by default. If an event loop wasn't set with
|
||||
* $(D_PSYMBOL defaultLoop) before, $(D_PSYMBOL getDefaultLoop()) will try to
|
||||
* choose an event loop supported on the system.
|
||||
*
|
||||
* Returns: The default event loop.
|
||||
*/
|
||||
Loop getDefaultLoop()
|
||||
{
|
||||
if (_defaultLoop !is null)
|
||||
{
|
||||
return _defaultLoop;
|
||||
}
|
||||
|
||||
static if (UseEpoll)
|
||||
{
|
||||
_defaultLoop = make!EpollLoop(defaultAllocator);
|
||||
}
|
||||
|
||||
return _defaultLoop;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the default event loop.
|
||||
*
|
||||
* This property makes it possible to implement your own backends or event
|
||||
* loops, for example, if the system is not supported or if you want to
|
||||
* extend the supported implementation. Just extend $(D_PSYMBOL Loop) and pass
|
||||
* your implementation to this property.
|
||||
*
|
||||
* Params:
|
||||
* loop = The event loop.
|
||||
*/
|
||||
@property void defaultLoop(Loop loop)
|
||||
in
|
||||
{
|
||||
assert(loop !is null);
|
||||
}
|
||||
body
|
||||
{
|
||||
_defaultLoop = loop;
|
||||
}
|
||||
|
||||
private Loop _defaultLoop;
|
@ -1,16 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* Copyright: Eugene Wissner 2016.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
|
||||
*/
|
||||
module tanya.event;
|
||||
|
||||
public import tanya.event.loop;
|
||||
public import tanya.event.protocol;
|
||||
public import tanya.event.transport;
|
||||
public import tanya.event.watcher;
|
@ -1,46 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* Copyright: Eugene Wissner 2016.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
|
||||
*/
|
||||
module tanya.event.protocol;
|
||||
|
||||
import tanya.event.transport;
|
||||
|
||||
/**
|
||||
* Common protocol interface.
|
||||
*/
|
||||
interface Protocol
|
||||
{
|
||||
@nogc:
|
||||
/**
|
||||
* Params:
|
||||
* data = Read data.
|
||||
*/
|
||||
void received(ubyte[] data);
|
||||
|
||||
/**
|
||||
* Called when a connection is made.
|
||||
*
|
||||
* Params:
|
||||
* transport = Protocol transport.
|
||||
*/
|
||||
void connected(DuplexTransport transport);
|
||||
|
||||
/**
|
||||
* Called when a connection is lost.
|
||||
*/
|
||||
void disconnected();
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface for TCP.
|
||||
*/
|
||||
interface TransmissionControlProtocol : Protocol
|
||||
{
|
||||
}
|
@ -1,134 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* Copyright: Eugene Wissner 2016.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
|
||||
*/
|
||||
module tanya.event.transport;
|
||||
|
||||
import tanya.container.buffer;
|
||||
import tanya.event.protocol;
|
||||
|
||||
/**
|
||||
* Exception thrown on read/write errors.
|
||||
*/
|
||||
class TransportException : Exception
|
||||
{
|
||||
/**
|
||||
* Params:
|
||||
* msg = Message to output.
|
||||
* file = The file where the exception occurred.
|
||||
* line = The line number where the exception occurred.
|
||||
* next = The previous exception in the chain of exceptions, if any.
|
||||
*/
|
||||
this(string msg,
|
||||
string file = __FILE__,
|
||||
size_t line = __LINE__,
|
||||
Throwable next = null) pure @safe nothrow const @nogc
|
||||
{
|
||||
super(msg, file, line, next);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Base transport interface.
|
||||
*/
|
||||
interface Transport
|
||||
{
|
||||
/**
|
||||
* Returns: Protocol.
|
||||
*/
|
||||
@property Protocol protocol() @safe pure nothrow;
|
||||
|
||||
/**
|
||||
* Returns: $(D_KEYWORD true) if the peer closed the connection,
|
||||
* $(D_KEYWORD false) otherwise.
|
||||
*/
|
||||
@property immutable(bool) disconnected() const @safe pure nothrow;
|
||||
|
||||
/**
|
||||
* Params:
|
||||
* protocol = Application protocol.
|
||||
*/
|
||||
@property void protocol(Protocol protocol) @safe pure nothrow
|
||||
in
|
||||
{
|
||||
assert(protocol !is null, "protocolConnected cannot be unset.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns: Application protocol.
|
||||
*/
|
||||
@property inout(Protocol) protocol() inout @safe pure nothrow;
|
||||
|
||||
/**
|
||||
* Returns: Transport socket.
|
||||
*/
|
||||
int socket() const @safe pure nothrow;
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface for read-only transports.
|
||||
*/
|
||||
interface ReadTransport : Transport
|
||||
{
|
||||
/**
|
||||
* Returns: Underlying output buffer.
|
||||
*/
|
||||
@property ReadBuffer output();
|
||||
|
||||
/**
|
||||
* Reads data into the buffer.
|
||||
*
|
||||
* Returns: Whether the reading is completed.
|
||||
*
|
||||
* Throws: $(D_PSYMBOL TransportException) if a read error is occured.
|
||||
*/
|
||||
bool receive()
|
||||
in
|
||||
{
|
||||
assert(!disconnected);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface for write-only transports.
|
||||
*/
|
||||
interface WriteTransport : Transport
|
||||
{
|
||||
/**
|
||||
* Returns: Underlying input buffer.
|
||||
*/
|
||||
@property WriteBuffer input();
|
||||
|
||||
/**
|
||||
* Write some data to the transport.
|
||||
*
|
||||
* Params:
|
||||
* data = Data to send.
|
||||
*/
|
||||
void write(ubyte[] data);
|
||||
|
||||
/**
|
||||
* Returns: Whether the writing is completed.
|
||||
*
|
||||
* Throws: $(D_PSYMBOL TransportException) if a read error is occured.
|
||||
*/
|
||||
bool send()
|
||||
in
|
||||
{
|
||||
assert(input.length);
|
||||
assert(!disconnected);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a bidirectional transport.
|
||||
*/
|
||||
abstract class DuplexTransport : ReadTransport, WriteTransport
|
||||
{
|
||||
}
|
@ -1,207 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* Copyright: Eugene Wissner 2016.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
|
||||
*/
|
||||
module tanya.event.watcher;
|
||||
|
||||
import tanya.event.protocol;
|
||||
import tanya.event.transport;
|
||||
import tanya.memory;
|
||||
import std.functional;
|
||||
|
||||
/**
|
||||
* A watcher is an opaque structure that you allocate and register to record
|
||||
* your interest in some event.
|
||||
*/
|
||||
abstract class Watcher
|
||||
{
|
||||
/// Whether the watcher is active.
|
||||
bool active;
|
||||
|
||||
/**
|
||||
* Invoke some action on event.
|
||||
*/
|
||||
void invoke();
|
||||
}
|
||||
|
||||
class ConnectionWatcher : Watcher
|
||||
{
|
||||
/// Watched file descriptor.
|
||||
private int socket_;
|
||||
|
||||
/// Protocol factory.
|
||||
protected Protocol delegate() protocolFactory;
|
||||
|
||||
/// Callback.
|
||||
package void delegate(Protocol delegate() protocolFactory,
|
||||
int socket) accept;
|
||||
|
||||
invariant
|
||||
{
|
||||
assert(socket_ >= 0, "Called with negative file descriptor.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Params:
|
||||
* protocolFactory = Function returning a new $(D_PSYMBOL Protocol) instance.
|
||||
* socket = Socket.
|
||||
*/
|
||||
this(Protocol function() protocolFactory, int socket)
|
||||
{
|
||||
this.protocolFactory = toDelegate(protocolFactory);
|
||||
socket_ = socket;
|
||||
}
|
||||
|
||||
/// Ditto.
|
||||
this(Protocol delegate() protocolFactory, int socket)
|
||||
{
|
||||
this.protocolFactory = protocolFactory;
|
||||
socket_ = socket;
|
||||
}
|
||||
|
||||
/// Ditto.
|
||||
protected this(Protocol function() protocolFactory)
|
||||
{
|
||||
this.protocolFactory = toDelegate(protocolFactory);
|
||||
}
|
||||
|
||||
/// Ditto.
|
||||
protected this(Protocol delegate() protocolFactory)
|
||||
{
|
||||
this.protocolFactory = protocolFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns: Socket.
|
||||
*/
|
||||
@property inout(int) socket() inout @safe pure nothrow
|
||||
{
|
||||
return socket_;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns: Application protocol factory.
|
||||
*/
|
||||
@property inout(Protocol delegate()) protocol() inout
|
||||
{
|
||||
return protocolFactory;
|
||||
}
|
||||
|
||||
override void invoke()
|
||||
{
|
||||
accept(protocol, socket);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Contains a pending watcher with the invoked events or a transport can be
|
||||
* read from.
|
||||
*/
|
||||
class IOWatcher : ConnectionWatcher
|
||||
{
|
||||
/// References a watcher or a transport.
|
||||
DuplexTransport transport_;
|
||||
|
||||
/**
|
||||
* Params:
|
||||
* protocolFactory = Function returning application specific protocol.
|
||||
* transport = Transport.
|
||||
*/
|
||||
this(Protocol delegate() protocolFactory,
|
||||
DuplexTransport transport)
|
||||
in
|
||||
{
|
||||
assert(transport !is null);
|
||||
assert(protocolFactory !is null);
|
||||
}
|
||||
body
|
||||
{
|
||||
super(protocolFactory);
|
||||
this.transport_ = transport;
|
||||
}
|
||||
|
||||
~this()
|
||||
{
|
||||
dispose(defaultAllocator, transport_);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assigns a transport.
|
||||
*
|
||||
* Params:
|
||||
* protocolFactory = Function returning application specific protocol.
|
||||
* transport = Transport.
|
||||
*
|
||||
* Returns: $(D_KEYWORD this).
|
||||
*/
|
||||
IOWatcher opCall(Protocol delegate() protocolFactory,
|
||||
DuplexTransport transport) @safe pure nothrow
|
||||
in
|
||||
{
|
||||
assert(transport !is null);
|
||||
assert(protocolFactory !is null);
|
||||
}
|
||||
body
|
||||
{
|
||||
this.protocolFactory = protocolFactory;
|
||||
this.transport_ = transport;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns: Transport used by this watcher.
|
||||
*/
|
||||
@property inout(DuplexTransport) transport() inout @safe pure nothrow
|
||||
{
|
||||
return transport_;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns: Socket.
|
||||
*/
|
||||
override @property inout(int) socket() inout @safe pure nothrow
|
||||
{
|
||||
return transport.socket;
|
||||
}
|
||||
|
||||
/**
|
||||
* Invokes the watcher callback.
|
||||
*
|
||||
* Finalizes the transport on disconnect.
|
||||
*/
|
||||
override void invoke()
|
||||
{
|
||||
if (transport.protocol is null)
|
||||
{
|
||||
transport.protocol = protocolFactory();
|
||||
transport.protocol.connected(transport);
|
||||
}
|
||||
else if (transport.disconnected)
|
||||
{
|
||||
transport.protocol.disconnected();
|
||||
dispose(defaultAllocator, transport_);
|
||||
protocolFactory = null;
|
||||
}
|
||||
else if (transport.output.length)
|
||||
{
|
||||
transport.protocol.received(transport.output[]);
|
||||
}
|
||||
else if (transport.input.length)
|
||||
{
|
||||
try
|
||||
{
|
||||
transport.send();
|
||||
}
|
||||
catch (TransportException e)
|
||||
{
|
||||
dispose(defaultAllocator, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user