--- deliantra/server/include/cfperl.h 2006/12/11 23:35:37 1.34 +++ deliantra/server/include/cfperl.h 2006/12/16 03:08:26 1.38 @@ -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 @@ -35,6 +39,7 @@ { KLASS_NONE, KLASS_GLOBAL, + KLASS_SOCKET, KLASS_PLAYER, KLASS_OBJECT, KLASS_MAP, @@ -61,6 +66,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) // the ", ## __VA_ARGS" is, unfortunately, a gnu-cpp extension @@ -70,6 +76,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_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__) @@ -84,8 +91,8 @@ struct attachable_base { - SV *self; - AV *cb; // CF+ perl self and callback + SV *self; // CF+ perl self + AV *cb; // CF+ callbacks shstr attach; // generic extension attachment information void clear (); // called when free'ing objects @@ -114,11 +121,11 @@ { } - attachable_base &operator = (const attachable_base &src) + attachable_base &operator =(const attachable_base &src) { - clear (); - attach = src.attach; - return *this; + clear (); + attach = src.attach; + return *this; } }; @@ -323,5 +330,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