--- deliantra/server/include/cfperl.h 2006/12/14 05:09:32 1.35 +++ deliantra/server/include/cfperl.h 2006/12/19 04:58:04 1.40 @@ -12,6 +12,10 @@ #include #include +#include + +#include "callback.h" + // optimisations/workaround for functions requiring my_perl in scope (anti-bloat) #undef localtime #undef srand48 @@ -23,6 +27,10 @@ #undef ctime #undef strerror +// same here, massive symbol spamming +#undef do_open +#undef do_close + // perl bug #40256: perl does overwrite those with reentrant versions // but does not initialise their state structures. #undef random @@ -35,7 +43,7 @@ { KLASS_NONE, KLASS_GLOBAL, - KLASS_SOCKET, + KLASS_CLIENT, KLASS_PLAYER, KLASS_OBJECT, KLASS_MAP, @@ -62,7 +70,7 @@ #define ARG_ARCH(o) DT_ARCH , (void *)static_cast (o) #define ARG_PARTY(o) DT_PARTY , (void *)static_cast (o) #define ARG_REGION(o) DT_REGION, (void *)static_cast (o) -#define ARG_SOCKET(o) DT_SOCKET, (void *)static_cast (o) +#define ARG_CLIENT(o) DT_CLIENT, (void *)static_cast (o) // the ", ## __VA_ARGS" is, unfortunately, a gnu-cpp extension @@ -72,7 +80,7 @@ #define INVOKE(klass, event, ...) INVOKE_(EVENT_ ## klass ## _ ## event, ## __VA_ARGS__) #define INVOKE_GLOBAL(event, ...) INVOKE_(EVENT_ ## GLOBAL ## _ ## event, ## __VA_ARGS__) #define INVOKE_OBJECT(event, op, ...) INVOKE_(EVENT_ ## OBJECT ## _ ## event, ARG_OBJECT (op), ## __VA_ARGS__) -#define INVOKE_SOCKET(event, ns, ...) INVOKE_(EVENT_ ## SOCKET ## _ ## event, ARG_SOCKET (ns), ## __VA_ARGS__) +#define INVOKE_CLIENT(event, ns, ...) INVOKE_(EVENT_ ## CLIENT ## _ ## event, ARG_CLIENT (ns), ## __VA_ARGS__) #define INVOKE_PLAYER(event, pl, ...) INVOKE_(EVENT_ ## PLAYER ## _ ## event, ARG_PLAYER (pl), ## __VA_ARGS__) #define INVOKE_MAP(event, map, ...) INVOKE_(EVENT_ ## MAP ## _ ## event, ARG_MAP (map) , ## __VA_ARGS__) @@ -326,5 +334,62 @@ char *fgets (char *s, int n, object_thawer &thawer); +struct watcher_base +{ + static struct EventAPI *GEventAPI; +}; + +template +struct watcher : watcher_base +{ + base *pe; + + void start (bool repeat = false) { GEventAPI->start ((pe_watcher *)pe, repeat); } + void stop (bool cancel_events = false) { GEventAPI->stop ((pe_watcher *)pe, cancel_events); } + void now () { GEventAPI->now ((pe_watcher *)pe); } + void suspend () { GEventAPI->suspend ((pe_watcher *)pe); } + void resume () { GEventAPI->resume ((pe_watcher *)pe); } + + void prio (int new_prio) { ((pe_watcher *)pe)->prio = new_prio; } + + ~watcher () + { + cancel (); + } + +private: + void cancel () { GEventAPI->cancel ((pe_watcher *)pe); } // private +}; + +struct iw : watcher, callback +{ + template + iw (O object, M method) + : callback (object, method) + { + alloc (); + } + +private: + void alloc (); +}; + +struct iow : watcher, callback +{ + template + iow (O object, M method) + : callback (object, method) + { + alloc (); + } + + void fd (int fd); + int poll (); + void poll (int events); + +private: + void alloc (); +}; + #endif