Skip to content

Commit

Permalink
version -> static if
Browse files Browse the repository at this point in the history
  • Loading branch information
trikko committed Apr 30, 2024
1 parent f763ecf commit 889a350
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 35 deletions.
22 changes: 22 additions & 0 deletions source/serverino/common.d
Expand Up @@ -27,6 +27,28 @@ module serverino.common;

import std.datetime : MonoTimeImpl, ClockType;

// Serverino can be built using two different backends: select or epoll
public enum BackendType
{
select,
epoll
}

// The backend is selected using the version directive or by checking the OS
version(use_select) { enum Backend = BackendType.select; }
else version(use_epoll) { enum Backend = BackendType.epoll; }
else {
version(linux) enum Backend = BackendType.epoll;
else enum Backend = BackendType.select;
}

static if(Backend == BackendType.epoll)
{
version(linux) { }
else static assert(false, "epoll backend is only available on Linux");
}

// The time type used in the serverino library
alias CoarseTime = MonoTimeImpl!(ClockType.coarse);

// Serverino version
Expand Down
11 changes: 2 additions & 9 deletions source/serverino/communicator.d
Expand Up @@ -25,13 +25,6 @@ OTHER DEALINGS IN THE SOFTWARE.

module serverino.communicator;

version(use_select) { version=with_select; }
else version(use_epoll) { version=with_epoll; }
else {
version(linux) version=with_epoll;
else version=with_select;
}

import serverino.common;
import serverino.databuffer;
import serverino.daemon : WorkerInfo, now;
Expand Down Expand Up @@ -144,7 +137,7 @@ package class Communicator
{
import serverino.daemon : Daemon;

version(with_epoll)
static if (serverino.common.Backend == BackendType.epoll)
{
import core.sys.linux.epoll : EPOLLIN, EPOLLOUT;
Daemon.epollRemoveSocket(clientSkt);
Expand Down Expand Up @@ -188,7 +181,7 @@ package class Communicator
s.blocking = false;
this.clientSkt = s;

version(with_epoll)
static if (serverino.common.Backend == BackendType.epoll)
{
import serverino.daemon : Daemon;
import core.sys.linux.epoll : EPOLLIN, EPOLLOUT;
Expand Down
29 changes: 11 additions & 18 deletions source/serverino/daemon.d
Expand Up @@ -25,13 +25,6 @@ OTHER DEALINGS IN THE SOFTWARE.

module serverino.daemon;

version(use_select) { version=with_select; }
else version(use_epoll) { version=with_epoll; }
else {
version(linux) version=with_epoll;
else version=with_select;
}

import serverino.common;
import serverino.communicator;
import serverino.config;
Expand All @@ -46,7 +39,7 @@ import std.socket;
import std.algorithm : filter;
import std.datetime : SysTime, Clock, seconds;

version(with_epoll) import core.sys.linux.epoll;
static if (serverino.common.Backend == BackendType.epoll) import core.sys.linux.epoll;


// The class WorkerInfo is used to keep track of the workers.
Expand Down Expand Up @@ -130,7 +123,7 @@ package class WorkerInfo
ubyte[1] data;
accepted.receive(data);

version(with_epoll)
static if (serverino.common.Backend == BackendType.epoll)
{
import serverino.daemon : Daemon;
import core.sys.linux.epoll : EPOLLIN, EPOLLOUT;
Expand All @@ -153,7 +146,7 @@ package class WorkerInfo

if (this.unixSocket)
{
version(with_epoll)
static if (serverino.common.Backend == BackendType.epoll)
{
import serverino.daemon : Daemon;
Daemon.epollRemoveSocket(unixSocket);
Expand Down Expand Up @@ -407,7 +400,7 @@ package:

tryInit!Modules();

version(with_epoll) epoll = epoll_create1(0);
static if (serverino.common.Backend == BackendType.epoll) epoll = epoll_create1(0);

// Starting all the listeners.
foreach(ref listener; config.listeners)
Expand Down Expand Up @@ -442,7 +435,7 @@ package:
exit(EXIT_FAILURE);
}

version(with_epoll) epollAddSocket(listener.socket, EPOLLIN, cast(void*)listener);
static if (serverino.common.Backend == BackendType.epoll) epollAddSocket(listener.socket, EPOLLIN, cast(void*)listener);
}

// Create all workers and start the ones that are required.
Expand All @@ -457,7 +450,7 @@ package:
foreach(idx; 0..512)
new Communicator(config);

version(with_select)
static if (serverino.common.Backend == BackendType.select)
{
// We use a socketset to check for updates
SocketSet ssRead = new SocketSet(config.listeners.length + WorkerInfo.instances.length);
Expand All @@ -470,7 +463,7 @@ package:
{

// We have to reset and fill the socketSet every time!
version(with_select)
static if (serverino.common.Backend == BackendType.select)
{
ssRead.reset();
ssWrite.reset();
Expand Down Expand Up @@ -501,7 +494,7 @@ package:
warning("Exception: ", se.msg);
}
}
else version(with_epoll)
else static if (serverino.common.Backend == BackendType.epoll)
{
enum MAX_EPOLL_EVENTS = 1500;
epoll_event[MAX_EPOLL_EVENTS] events = void;
Expand Down Expand Up @@ -584,7 +577,7 @@ package:
// Select version main loop
// ------------------------

version(with_select)
static if (serverino.common.Backend == BackendType.select)
{
// Check the workers for updates
foreach(ref worker; WorkerInfo.alive)
Expand Down Expand Up @@ -650,7 +643,7 @@ package:
// epoll version main loop
// ------------------------

else version(with_epoll)
else static if (serverino.common.Backend == BackendType.epoll)
{
foreach(ref epoll_event e; events[0..updates])
{
Expand Down Expand Up @@ -752,7 +745,7 @@ package:
}


version(with_epoll)
static if (serverino.common.Backend == BackendType.epoll)
{

void epollAddSocket(Socket s, int events, void* ptr)
Expand Down
11 changes: 3 additions & 8 deletions source/serverino/versionlog.d
Expand Up @@ -24,13 +24,8 @@ OTHER DEALINGS IN THE SOFTWARE.
*/

// This module is used to check and display the version of the serverino library during compilation
version(use_select) { version=with_select; }
else version(use_epoll) { version=with_epoll; }
else {
version(linux) version=with_epoll;
else version=with_select;
}
import serverino.common;

version(with_select) pragma(msg, " Building serverino using select/socketset backend.");
else version(with_epoll) pragma(msg, " Building serverino using epoll backend.");
static if (serverino.common.Backend == BackendType.select) pragma(msg, " Building serverino using select/socketset backend.");
else static if (serverino.common.Backend == BackendType.epoll) pragma(msg, " Building serverino using epoll backend.");
else static assert(false, "No backend selected. Please, use -version=use_select or -version=use_epoll.");

0 comments on commit 889a350

Please sign in to comment.