diff --git a/source/serverino/common.d b/source/serverino/common.d index ca161a7..64a8ed8 100644 --- a/source/serverino/common.d +++ b/source/serverino/common.d @@ -103,17 +103,33 @@ static if (Backend == BackendType.KQUEUE) enum EVFILT_SYSCOUNT = 11; - int kqueue(); - - pragma(mangle, "kevent") - int kevent_f( - int kq, - const kevent* changelist, - int nchanges, - kevent* eventlist, - int nevents, - const timespec* timeout - ); + version(linux) + { + int function() kqueue; + + int function( + int kq, + const kevent* changelist, + int nchanges, + kevent* eventlist, + int nevents, + const timespec* timeout + ) kevent_f; + } + else + { + int kqueue(); + + pragma(mangle, "kevent") + int kevent_f( + int kq, + const kevent* changelist, + int nchanges, + kevent* eventlist, + int nevents, + const timespec* timeout + ); + } } } else static assert(false, "kqueue backend is only available on Linux and BSD"); @@ -545,4 +561,39 @@ version(Posix) package void setProcessName(string[] names) pthread_setname_np(pthread_self(), names[0].toStringz); } -package immutable static DEFAULT_BUFFER_SIZE = 32*1024; \ No newline at end of file +package immutable static DEFAULT_BUFFER_SIZE = 32*1024; + + +version(Posix) +{ + static if (Backend == BackendType.KQUEUE) + { + + void* kqueue_handle = null; + + shared static this() + { + import core.sys.posix.dlfcn; + + extern (C) int dll(); + + kqueue_handle = dlopen("libkqueue.so", RTLD_LAZY); + if (!kqueue_handle) + { + assert(false, "Failed to load libkqueue.so"); + } + + kqueue = cast(typeof(kqueue))dlsym(kqueue_handle, "kqueue"); + kevent_f = cast(typeof(kevent_f))dlsym(kqueue_handle, "kevent"); + + assert(kqueue && kevent_f, "Failed to load kqueue or kevent functions"); + } + + shared static ~this() + { + import core.sys.posix.dlfcn; + dlclose(kqueue_handle); + } + } +} +