From b95c9e5bbc45dfc54e6ba868297420431ac9d0ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Wed, 5 Oct 2016 14:09:51 +0200 Subject: [PATCH] Remove redundancies from API function names. --- examples/http-server-fibers/source/app.d | 8 +- examples/http-server/source/app.d | 12 +- source/eventcore/driver.d | 88 ++++++++------ source/eventcore/drivers/epoll.d | 2 +- source/eventcore/drivers/posix.d | 144 ++++++++++++----------- source/eventcore/drivers/select.d | 2 +- source/eventcore/drivers/timer.d | 18 +-- 7 files changed, 149 insertions(+), 125 deletions(-) diff --git a/examples/http-server-fibers/source/app.d b/examples/http-server-fibers/source/app.d index 17fab73..1362f68 100644 --- a/examples/http-server-fibers/source/app.d +++ b/examples/http-server-fibers/source/app.d @@ -89,7 +89,7 @@ struct StreamConnectionImpl { @property bool empty() { reader.start(); - eventDriver.sockets.waitSocketData(m_socket, &onData); + eventDriver.sockets.waitForData(m_socket, &onData); reader.wait(); return m_empty; } @@ -98,7 +98,7 @@ struct StreamConnectionImpl { { reader.start(); if (m_readBufferFill >= 2) onReadLineData(m_socket, IOStatus.ok, 0); - else eventDriver.sockets.readSocket(m_socket, m_readBuffer[m_readBufferFill .. $], IOMode.once, &onReadLineData); + else eventDriver.sockets.read(m_socket, m_readBuffer[m_readBufferFill .. $], IOMode.once, &onReadLineData); reader.wait(); auto ln = m_line; m_line = null; @@ -108,7 +108,7 @@ struct StreamConnectionImpl { void write(const(ubyte)[] data) { writer.start(); - eventDriver.sockets.writeSocket(m_socket, data, IOMode.all, &onWrite); + eventDriver.sockets.write(m_socket, data, IOMode.all, &onWrite); writer.wait(); } @@ -160,7 +160,7 @@ struct StreamConnectionImpl { reader.finish(); } else if (m_readBuffer.length - m_readBufferFill > 0) { - eventDriver.sockets.readSocket(m_socket, m_readBuffer[m_readBufferFill .. $], IOMode.once, &onReadLineData); + eventDriver.sockets.read(m_socket, m_readBuffer[m_readBufferFill .. $], IOMode.once, &onReadLineData); } else { reader.finish(exh); } diff --git a/examples/http-server/source/app.d b/examples/http-server/source/app.d index 1e64118..e7b00c1 100644 --- a/examples/http-server/source/app.d +++ b/examples/http-server/source/app.d @@ -49,7 +49,7 @@ struct ClientHandler { { onLine = on_line; if (linefill >= 2) onReadData(client, IOStatus.ok, 0); - else eventDriver.sockets.readSocket(client, linebuf[linefill .. $], IOMode.once, &onReadData); + else eventDriver.sockets.read(client, linebuf[linefill .. $], IOMode.once, &onReadData); } void onRequestLine(ubyte[] ln) @@ -57,7 +57,7 @@ struct ClientHandler { //print("Request: %s", cast(char[])ln); if (ln.length == 0) { //print("Error: empty request line"); - eventDriver.sockets.shutdownSocket(client); + eventDriver.sockets.shutdown(client); eventDriver.sockets.releaseRef(client); } @@ -68,7 +68,7 @@ struct ClientHandler { { if (ln.length == 0) { auto reply = cast(const(ubyte)[])"HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 13\r\nKeep-Alive: timeout=10\r\n\r\nHello, World!"; - eventDriver.sockets.writeSocket(client, reply, IOMode.all, &onWriteFinished); + eventDriver.sockets.write(client, reply, IOMode.all, &onWriteFinished); } else readLine(&onHeaderLine); } @@ -83,7 +83,7 @@ struct ClientHandler { if (status != IOStatus.ok) { print("Client disconnect"); - eventDriver.sockets.shutdownSocket(client); + eventDriver.sockets.shutdown(client); eventDriver.sockets.releaseRef(client); return; } @@ -102,11 +102,11 @@ struct ClientHandler { onLine(linebuf[linefill + idx + 2 .. linefill + idx + 2 + idx]); } else if (linebuf.length - linefill > 0) { - eventDriver.sockets.readSocket(client, linebuf[linefill .. $], IOMode.once, &onReadData); + eventDriver.sockets.read(client, linebuf[linefill .. $], IOMode.once, &onReadData); } else { // ERROR: header line too long print("Header line too long"); - eventDriver.sockets.shutdownSocket(client); + eventDriver.sockets.shutdown(client); eventDriver.sockets.releaseRef(client); } } diff --git a/source/eventcore/driver.d b/source/eventcore/driver.d index 6b47171..81ab5ae 100644 --- a/source/eventcore/driver.d +++ b/source/eventcore/driver.d @@ -10,7 +10,7 @@ interface EventDriver { @property EventDriverCore core(); @property EventDriverFiles files(); @property EventDriverSockets sockets(); - @property EventDriverTimers udp(); + @property EventDriverTimers timers(); @property EventDriverEvents events(); @property EventDriverSignals signals(); @property EventDriverWatchers watchers(); @@ -80,6 +80,18 @@ interface EventDriverCore { interface EventDriverSockets { @safe: /*@nogc:*/ nothrow: + StreamSocketFD connectStream(scope Address peer_address, ConnectCallback on_connect); + StreamListenSocketFD listenStream(scope Address bind_address, AcceptCallback on_accept); + void waitForConnections(StreamListenSocketFD sock, AcceptCallback on_accept); + ConnectionState getConnectionState(StreamSocketFD sock); + void setTCPNoDelay(StreamSocketFD socket, bool enable); + void read(StreamSocketFD socket, ubyte[] buffer, IOMode mode, IOCallback on_read_finish); + void write(StreamSocketFD socket, const(ubyte)[] buffer, IOMode mode, IOCallback on_write_finish); + void waitForData(StreamSocketFD socket, IOCallback on_data_available); + void shutdown(StreamSocketFD socket, bool shut_read = true, bool shut_write = true); + void cancelRead(StreamSocketFD socket); + void cancelWrite(StreamSocketFD socket); + /** Increments the reference count of the given resource. */ @@ -91,22 +103,17 @@ interface EventDriverSockets { freed and the resource descriptor gets invalidated. */ void releaseRef(SocketFD descriptor); - - StreamSocketFD connectStream(scope Address peer_address, ConnectCallback on_connect); - StreamListenSocketFD listenStream(scope Address bind_address, AcceptCallback on_accept); - void waitForConnections(StreamListenSocketFD sock, AcceptCallback on_accept); - ConnectionState getConnectionState(StreamSocketFD sock); - void setTCPNoDelay(StreamSocketFD socket, bool enable); - void readSocket(StreamSocketFD socket, ubyte[] buffer, IOMode mode, IOCallback on_read_finish); - void writeSocket(StreamSocketFD socket, const(ubyte)[] buffer, IOMode mode, IOCallback on_write_finish); - void waitSocketData(StreamSocketFD socket, IOCallback on_data_available); - void shutdownSocket(StreamSocketFD socket, bool shut_read = true, bool shut_write = true); - void cancelRead(StreamSocketFD socket); - void cancelWrite(StreamSocketFD socket); } interface EventDriverFiles { @safe: /*@nogc:*/ nothrow: + FileFD open(string path, FileOpenMode mode); + FileFD createTemp(); + void write(FileFD file, ulong offset, ubyte[] buffer, IOCallback on_write_finish); + void read(FileFD file, ulong offset, ubyte[] buffer, IOCallback on_read_finish); + void cancelWrite(FileFD file); + void cancelRead(FileFD file); + /** Increments the reference count of the given resource. */ @@ -118,17 +125,16 @@ interface EventDriverFiles { freed and the resource descriptor gets invalidated. */ void releaseRef(FileFD descriptor); - - FileFD openFile(string path, FileOpenMode mode); - FileFD createTempFile(); - void write(FileFD file, ulong offset, ubyte[] buffer, IOCallback on_write_finish); - void read(FileFD file, ulong offset, ubyte[] buffer, IOCallback on_read_finish); - void cancelWrite(FileFD file); - void cancelRead(FileFD file); } interface EventDriverEvents { @safe: /*@nogc:*/ nothrow: + EventID create(); + void trigger(EventID event, bool notify_all = true); + void trigger(EventID event, bool notify_all = true) shared; + void wait(EventID event, EventCallback on_event); + void cancelWait(EventID event, EventCallback on_event); + /** Increments the reference count of the given resource. */ @@ -140,22 +146,24 @@ interface EventDriverEvents { freed and the resource descriptor gets invalidated. */ void releaseRef(EventID descriptor); - - EventID createEvent(); - void triggerEvent(EventID event, bool notify_all = true); - void triggerEvent(EventID event, bool notify_all = true) shared; - void waitForEvent(EventID event, EventCallback on_event); - void cancelWaitForEvent(EventID event, EventCallback on_event); } interface EventDriverSignals { @safe: /*@nogc:*/ nothrow: - void waitForSignal(int sig, SignalCallback on_signal); - void cancelWaitForSignal(int sig); + void wait(int sig, SignalCallback on_signal); + void cancelWait(int sig); } interface EventDriverTimers { @safe: /*@nogc:*/ nothrow: + TimerID create(); + void set(TimerID timer, Duration timeout, Duration repeat = Duration.zero); + void stop(TimerID timer); + bool isPending(TimerID timer); + bool isPeriodic(TimerID timer); + void wait(TimerID timer, TimerCallback callback); + void cancelWait(TimerID timer, TimerCallback callback); + /** Increments the reference count of the given resource. */ @@ -167,21 +175,25 @@ interface EventDriverTimers { freed and the resource descriptor gets invalidated. */ void releaseRef(TimerID descriptor); - - TimerID createTimer(); - void setTimer(TimerID timer, Duration timeout, Duration repeat = Duration.zero); - void stopTimer(TimerID timer); - bool isTimerPending(TimerID timer); - bool isTimerPeriodic(TimerID timer); - void waitTimer(TimerID timer, TimerCallback callback); - void cancelTimerWait(TimerID timer, TimerCallback callback); } interface EventDriverWatchers { @safe: /*@nogc:*/ nothrow: WatcherID watchDirectory(string path, bool recursive); - void waitForChanges(WatcherID watcher, FileChangesCallback callback); - void cancelWaitForChanges(WatcherID watcher); + void wait(WatcherID watcher, FileChangesCallback callback); + void cancelWait(WatcherID watcher); + + /** + Increments the reference count of the given resource. + */ + void addRef(WatcherID descriptor); + /** + Decrements the reference count of the given resource. + + Once the reference count reaches zero, all associated resources will be + freed and the resource descriptor gets invalidated. + */ + void releaseRef(WatcherID descriptor); } diff --git a/source/eventcore/drivers/epoll.d b/source/eventcore/drivers/epoll.d index 7205555..3499b4b 100644 --- a/source/eventcore/drivers/epoll.d +++ b/source/eventcore/drivers/epoll.d @@ -33,7 +33,7 @@ final class EpollEventDriver : PosixEventDriver { override @property EpollEventDriver core() { return this; } override @property EpollEventDriver files() { return this; } override @property EpollEventDriver sockets() { return this; } - override @property EpollEventDriver udp() { return this; } + override @property EpollEventDriver timers() { return this; } override @property EpollEventDriver events() { return this; } override @property EpollEventDriver signals() { return this; } override @property EpollEventDriver watchers() { return this; } diff --git a/source/eventcore/drivers/posix.d b/source/eventcore/drivers/posix.d index e0ee9fd..f3c75f9 100644 --- a/source/eventcore/drivers/posix.d +++ b/source/eventcore/drivers/posix.d @@ -61,7 +61,7 @@ abstract class PosixEventDriver : EventDriver, abstract override @property PosixEventDriver core(); abstract override @property PosixEventDriver files(); abstract override @property PosixEventDriver sockets(); - abstract override @property PosixEventDriver udp(); + abstract override @property PosixEventDriver timers(); abstract override @property PosixEventDriver events(); abstract override @property PosixEventDriver signals(); abstract override @property PosixEventDriver watchers(); @@ -244,7 +244,7 @@ abstract class PosixEventDriver : EventDriver, () @trusted { setsockopt(socket, IPPROTO_TCP, TCP_NODELAY, cast(char*)&opt, opt.sizeof); } (); } - final override void readSocket(StreamSocketFD socket, ubyte[] buffer, IOMode mode, IOCallback on_read_finish) + final override void read(StreamSocketFD socket, ubyte[] buffer, IOMode mode, IOCallback on_read_finish) { if (buffer.length == 0) { on_read_finish(socket, IOStatus.ok, 0); @@ -340,7 +340,7 @@ abstract class PosixEventDriver : EventDriver, } } - final override void writeSocket(StreamSocketFD socket, const(ubyte)[] buffer, IOMode mode, IOCallback on_write_finish) + final override void write(StreamSocketFD socket, const(ubyte)[] buffer, IOMode mode, IOCallback on_write_finish) { if (buffer.length == 0) { on_write_finish(socket, IOStatus.ok, 0); @@ -432,7 +432,7 @@ abstract class PosixEventDriver : EventDriver, } } - final override void waitSocketData(StreamSocketFD socket, IOCallback on_data_available) + final override void waitForData(StreamSocketFD socket, IOCallback on_data_available) { sizediff_t ret; ubyte dummy; @@ -489,17 +489,35 @@ abstract class PosixEventDriver : EventDriver, } else finalize(ret ? IOStatus.ok : IOStatus.disconnected); } - final override void shutdownSocket(StreamSocketFD socket, bool shut_read, bool shut_write) + final override void shutdown(StreamSocketFD socket, bool shut_read, bool shut_write) { // TODO! } - final override FileFD openFile(string path, FileOpenMode mode) + final override void addRef(SocketFD fd) + { + auto pfd = &m_fds[fd]; + assert(pfd.refCount > 0, "Adding reference to unreferenced socket FD."); + m_fds[fd].refCount++; + } + + final override void releaseRef(SocketFD fd) + { + auto pfd = &m_fds[fd]; + assert(pfd.refCount > 0, "Releasing reference to unreferenced socket FD."); + if (--m_fds[fd].refCount == 0) { + unregisterFD(fd); + clearFD(fd); + closeSocket(fd); + } + } + + final override FileFD open(string path, FileOpenMode mode) { assert(false, "TODO!"); } - final override FileFD createTempFile() + final override FileFD createTemp() { assert(false, "TODO!"); } @@ -524,7 +542,18 @@ abstract class PosixEventDriver : EventDriver, assert(false, "TODO!"); } - final override EventID createEvent() + final override void addRef(FileFD descriptor) + { + assert(false); + } + + final override void releaseRef(FileFD descriptor) + { + assert(false); + } + + + final override EventID create() { auto id = cast(EventID)eventfd(0, EFD_NONBLOCK); initFD(id); @@ -534,7 +563,7 @@ abstract class PosixEventDriver : EventDriver, return id; } - final override void triggerEvent(EventID event, bool notify_all = true) + final override void trigger(EventID event, bool notify_all = true) { assert(event < m_fds.length, "Invalid event ID passed to triggerEvent."); if (notify_all) { @@ -549,7 +578,7 @@ abstract class PosixEventDriver : EventDriver, } } - final override void triggerEvent(EventID event, bool notify_all = true) + final override void trigger(EventID event, bool notify_all = true) shared @trusted { import core.atomic : atomicStore; auto thisus = cast(PosixEventDriver)this; @@ -560,13 +589,13 @@ abstract class PosixEventDriver : EventDriver, () @trusted { .write(event, &one, one.sizeof); } (); } - final override void waitForEvent(EventID event, EventCallback on_event) + final override void wait(EventID event, EventCallback on_event) { assert(event < m_fds.length, "Invalid event ID passed to waitForEvent."); return m_fds[event].waiters.put(on_event); } - final override void cancelWaitForEvent(EventID event, EventCallback on_event) + final override void cancelWait(EventID event, EventCallback on_event) { import std.algorithm.searching : countUntil; import std.algorithm.mutation : remove; @@ -581,44 +610,7 @@ abstract class PosixEventDriver : EventDriver, () @trusted { .read(event, &cnt, cnt.sizeof); } (); import core.atomic : cas; auto all = cas(&m_fds[event].triggerAll, true, false); - triggerEvent(cast(EventID)event, all); - } - - final override void waitForSignal(int sig, SignalCallback on_signal) - { - assert(false, "TODO!"); - } - - final override void cancelWaitForSignal(int sig) - { - assert(false, "TODO!"); - } - - final override WatcherID watchDirectory(string path, bool recursive) - { - assert(false, "TODO!"); - } - - final override void waitForChanges(WatcherID watcher, FileChangesCallback callback) - { - assert(false, "TODO!"); - } - - final override void cancelWaitForChanges(WatcherID watcher) - { - assert(false, "TODO!"); - } - - final override void addRef(SocketFD fd) - { - auto pfd = &m_fds[fd]; - assert(pfd.refCount > 0, "Adding reference to unreferenced socket FD."); - m_fds[fd].refCount++; - } - - final override void addRef(FileFD descriptor) - { - assert(false); + trigger(cast(EventID)event, all); } final override void addRef(EventID descriptor) @@ -628,22 +620,6 @@ abstract class PosixEventDriver : EventDriver, m_fds[descriptor].refCount++; } - final override void releaseRef(SocketFD fd) - { - auto pfd = &m_fds[fd]; - assert(pfd.refCount > 0, "Releasing reference to unreferenced socket FD."); - if (--m_fds[fd].refCount == 0) { - unregisterFD(fd); - clearFD(fd); - closeSocket(fd); - } - } - - final override void releaseRef(FileFD descriptor) - { - assert(false); - } - final override void releaseRef(EventID descriptor) { auto pfd = &m_fds[descriptor]; @@ -655,6 +631,42 @@ abstract class PosixEventDriver : EventDriver, } } + + final override void wait(int sig, SignalCallback on_signal) + { + assert(false, "TODO!"); + } + + final override void cancelWait(int sig) + { + assert(false, "TODO!"); + } + + final override WatcherID watchDirectory(string path, bool recursive) + { + assert(false, "TODO!"); + } + + final override void wait(WatcherID watcher, FileChangesCallback callback) + { + assert(false, "TODO!"); + } + + final override void cancelWait(WatcherID watcher) + { + assert(false, "TODO!"); + } + + final override void addRef(WatcherID descriptor) + { + assert(false, "TODO!"); + } + + final override void releaseRef(WatcherID descriptor) + { + assert(false, "TODO!"); + } + final override void* rawUserData(StreamSocketFD descriptor, size_t size, DataInitializer initialize, DataInitializer destroy) @system { FDSlot* fds = &m_fds[descriptor]; diff --git a/source/eventcore/drivers/select.d b/source/eventcore/drivers/select.d index ede2a54..1c53ecc 100644 --- a/source/eventcore/drivers/select.d +++ b/source/eventcore/drivers/select.d @@ -27,7 +27,7 @@ final class SelectEventDriver : PosixEventDriver { override @property SelectEventDriver core() { return this; } override @property SelectEventDriver files() { return this; } override @property SelectEventDriver sockets() { return this; } - override @property SelectEventDriver udp() { return this; } + override @property SelectEventDriver timers() { return this; } override @property SelectEventDriver events() { return this; } override @property SelectEventDriver signals() { return this; } override @property SelectEventDriver watchers() { return this; } diff --git a/source/eventcore/drivers/timer.d b/source/eventcore/drivers/timer.d index f800dfb..2f4820f 100644 --- a/source/eventcore/drivers/timer.d +++ b/source/eventcore/drivers/timer.d @@ -70,7 +70,7 @@ mixin template DefaultTimerImpl() { return any_fired; } - final override TimerID createTimer() + final override TimerID create() @trusted { auto id = cast(TimerID)(++m_lastTimerID); TimerSlot* tm; @@ -83,11 +83,11 @@ mixin template DefaultTimerImpl() { return id; } - final override void setTimer(TimerID timer, Duration timeout, Duration repeat) + final override void set(TimerID timer, Duration timeout, Duration repeat) @trusted { scope (failure) assert(false); auto tm = m_timers[timer]; - if (tm.pending) stopTimer(timer); + if (tm.pending) stop(timer); tm.timeout = Clock.currStdTime + timeout.total!"hnsecs"; tm.repeatDuration = repeat.total!"hnsecs"; tm.pending = true; @@ -97,7 +97,7 @@ mixin template DefaultTimerImpl() { catch (Exception e) { print("Failed to insert timer: %s", e.msg); } } - final override void stopTimer(TimerID timer) + final override void stop(TimerID timer) @trusted { auto tm = m_timers[timer]; if (!tm.pending) return; @@ -118,22 +118,22 @@ mixin template DefaultTimerImpl() { } } - final override bool isTimerPending(TimerID descriptor) + final override bool isPending(TimerID descriptor) { return m_timers[descriptor].pending; } - final override bool isTimerPeriodic(TimerID descriptor) + final override bool isPeriodic(TimerID descriptor) { return m_timers[descriptor].repeatDuration > 0; } - final override void waitTimer(TimerID timer, TimerCallback callback) + final override void wait(TimerID timer, TimerCallback callback) { m_timers[timer].callbacks ~= callback; } - final override void cancelTimerWait(TimerID timer, TimerCallback callback) + final override void cancelWait(TimerID timer, TimerCallback callback) { import std.algorithm.mutation : remove; import std.algorithm.searching : countUntil; @@ -160,7 +160,7 @@ mixin template DefaultTimerImpl() { auto tm = m_timers[descriptor]; if (!--tm.refCount) { - if (tm.pending) stopTimer(tm.id); + if (tm.pending) stop(tm.id); m_timers.remove(descriptor); () @trusted { scope (failure) assert(false); ms_allocator.dispose(tm); } (); }