--- deliantra/server/include/cfperl.h 2006/08/27 17:59:26 1.10 +++ deliantra/server/include/cfperl.h 2006/12/31 10:28:36 1.44 @@ -4,40 +4,80 @@ #ifndef CFPERL_H__ #define CFPERL_H__ -struct pl; -struct object; -struct mapstruct; +#include + +using namespace std; + +#include +#include +#include + +#include +#include + +#include "util.h" +#include "keyword.h" +#include "dynbuf.h" +#include "callback.h" + +////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +// optimisations/workaround for functions requiring my_perl in scope (anti-bloat) +#undef localtime +#undef srand48 +#undef drand48 +#undef srandom +#undef readdir +#undef getprotobyname +#undef gethostbyname +#undef ctime +#undef strerror + +// same here, massive symbol spamming +#undef do_open +#undef do_close +#undef ref +#undef seed + +// perl bug #40256: perl does overwrite those with reentrant versions +// but does not initialise their state structures. +#undef random +#undef crypt + +////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +// some macros to simplify perl in-calls + +#define CHECK_ERROR \ + if (SvTRUE (ERRSV)) \ + LOG (llevError, "runtime error in %s: %s\n", __func__, SvPVutf8_nolen (ERRSV)); + +#define CALL_BEGIN(args) dSP; ENTER; SAVETMPS; PUSHMARK (SP); EXTEND (SP, args) +#define CALL_ARG_SV(sv) PUSHs (sv_2mortal (sv)) // separate because no refcount inc +#define CALL_ARG(expr) PUSHs (sv_2mortal (to_sv (expr))) +#define CALL_CALL(name, flags) PUTBACK; int count = call_pv (name, (flags) | G_EVAL); SPAGAIN; +#define CALL_END CHECK_ERROR; FREETMPS; LEAVE + +////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void cfperl_init (); void cfperl_main (); -// could have used templates, but a more traditional C api -// uses more explicit typing which is ok for this purpose. -enum data_type -{ - DT_END, // no further arguments - DT_INT, - DT_INT64, - DT_DOUBLE, - DT_STRING, // 0-terminated string - DT_DATA, // string + length - DT_OBJECT, - DT_MAP, - DT_PLAYER, - DT_ARCH, - DT_PARTY, - DT_REGION, - DT_AV, // av that needs to be flattened out +////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - NUM_DATA_TYPES -}; +// virtual server time, excluding time jumps and lag +extern double runtime; enum event_klass { + KLASS_NONE, KLASS_GLOBAL, + KLASS_ATTACHABLE, + KLASS_CLIENT, KLASS_PLAYER, KLASS_OBJECT, KLASS_MAP, + KLASS_COMMAND, }; enum event_type @@ -48,101 +88,361 @@ NUM_EVENT_TYPES }; -#define ARG_INT(v) DT_INT , static_cast (v) -#define ARG_INT64(v) DT_INT64 , static_cast (v) -#define ARG_DOUBLE(v) DT_DOUBLE, static_cast (v) -#define ARG_STRING(v) DT_STRING, static_cast (v) -#define ARG_DATA(s,l) DT_DATA , static_cast (s), (l) -#define ARG_OBJECT(o) DT_OBJECT, (void *)static_cast (o) -#define ARG_MAP(o) DT_MAP , (void *)static_cast (o) -#define ARG_PLAYER(o) DT_PLAYER, (void *)static_cast (o) -#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_AV(o) DT_AV , static_cast (o) +#define ARG_INT(v) DT_INT , static_cast (v) +#define ARG_INT64(v) DT_INT64 , static_cast (v) +#define ARG_DOUBLE(v) DT_DOUBLE, static_cast (v) +#define ARG_STRING(v) DT_STRING, static_cast (v) +#define ARG_DATA(s,l) DT_DATA , static_cast (s), int (l) +#define ARG_OBJECT(o) DT_OBJECT, (void *)(static_cast (o)) +#define ARG_MAP(o) DT_MAP , (void *)(static_cast (o)) +#define ARG_PLAYER(o) DT_PLAYER, (void *)(static_cast (o)) +#define ARG_ARCH(o) DT_ARCH , (void *)(static_cast (o)) +#define ARG_CLIENT(o) DT_CLIENT, (void *)(static_cast (o)) +#define ARG_PARTY(o) DT_PARTY , (void *)(static_cast (o)) +#define ARG_REGION(o) DT_REGION, (void *)(static_cast (o)) // the ", ## __VA_ARGS" is, unfortunately, a gnu-cpp extension // all these return true when the normal event processing should be skipped (if any) -#define INVOKE_(event, ...) cfperl_invoke (event, ## __VA_ARGS__, DT_END) - -#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_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__) +#define INVOKE_GLOBAL(event, ...) gbl_ev.invoke (EVENT_ ## GLOBAL ## _ ## event, ## __VA_ARGS__, DT_END) +#define INVOKE_ATTACHABLE(event, obj, ...) (obj)->invoke (EVENT_ ## ATTACHABLE ## _ ## event, ## __VA_ARGS__, DT_END) +#define INVOKE_OBJECT(event, obj, ...) (obj)->invoke (EVENT_ ## OBJECT ## _ ## event, ## __VA_ARGS__, DT_END) +#define INVOKE_CLIENT(event, obj, ...) (obj)->invoke (EVENT_ ## CLIENT ## _ ## event, ## __VA_ARGS__, DT_END) +#define INVOKE_PLAYER(event, obj, ...) (obj)->invoke (EVENT_ ## PLAYER ## _ ## event, ## __VA_ARGS__, DT_END) +#define INVOKE_MAP(event, obj, ...) (obj)->invoke (EVENT_ ## MAP ## _ ## event, ## __VA_ARGS__, DT_END) //TODO should index into @result #define RESULT(idx,type) cfperl_result_ ## type (idx) -#define RESULT_INT(idx) RESULT(idx, INT) +#define RESULT_DOUBLE(idx) RESULT(idx, DOUBLE) +#define RESULT_INT(idx) RESULT(idx, INT) -bool cfperl_invoke (event_type event, ...); +double cfperl_result_DOUBLE (int idx); int cfperl_result_INT (int idx); -struct attachable_base +////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +struct attachable { - void *self, *cb; // CF+ perl self and callback - const char *attach; // generic extension attachment information + static MGVTBL vtbl; + + static unordered_vector mortals; + static void check_mortals (); - void clear (); // called when free'ing objects - void optimise (); // possibly save some memory by destroying unneeded data + enum { + F_DESTROYED = 0x01, + F_BORROWED = 0x02, + }; - void attachable_init () + // object is delete'd after the refcount reaches 0 + mutable int refcnt; + int flags; + + void refcnt_inc () const { ++refcnt; } + void refcnt_dec () const { --refcnt; } + + // check wether the object has died and destroy + void refcnt_chk () { if (refcnt <= 0) do_check (); } + + // destroy the object unless it was already destroyed + // this politely asks everybody interested the reduce + // the refcount to 0 as soon as possible. + void destroy (); + + // return wether an object was destroyed already + bool destroyed () const { return flags & F_DESTROYED; } + + virtual void gather_callbacks (AV *&callbacks, event_type event) const; + +#if 0 +private: + static refcounted *rc_first; + refcounted *rc_next; +#endif + + HV *self; // CF+ perl self + AV *cb; // CF+ callbacks + shstr attach; // generic extension attachment information + + void optimise (); // possibly save some memory by destroying unneeded data + + attachable () + : flags (0), refcnt (0), self (0), cb (0), attach (0) { - self = cb = 0; - attach = 0; } -}; -// objects attachable from perl (or any other extension) should include or -// derive using the curiously recurring template pattern, to avoid -// virtual method calls etc. -template -struct attachable : attachable_base -{ - static data_type get_dt () + attachable (const attachable &src) + : flags (0), refcnt (0), self (0), cb (0), attach (src.attach) { - subclass::get_dt (); } + + virtual ~attachable (); + + attachable &operator =(const attachable &src); + + bool invoke (event_type event, ...); + void instantiate (); + void reattach (); + +protected: + // do the real refcount checking work + void do_check (); + + // the method that does the real destroy work + virtual void do_destroy (); }; -struct object_freezer +// the global object is a pseudo object that cares for the global events +struct global : attachable { - const char *filename; - void *av; - int idx; + void gather_callbacks (AV *&callbacks, event_type event) const; +}; + +extern struct global gbl_ev; + +////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - object_freezer (const char *filename); +struct object_freezer : dynbuf +{ + AV *av; + + object_freezer (); ~object_freezer (); - void put (attachable_base *ext); + void put (attachable *ext); + + // used only for user-defined key-value pairs + void put (const shstr &k, const shstr &v) + { + add (k); + + if (v) + add (' '), add (v); + + add ('\n'); + } + + //TODO//temporary, used only for saving body locations + void put (const char *k, int v) + { + add (k); + add (' '); + add (sint32 (v)); + add ('\n'); + } + + template + void put_string (keyword k, const T &v) + { + int klen = keyword_len [k]; + int vlen = v ? strlen (v) + 1 : 0; + + char *p = (char *)alloc (klen + vlen + 1); + + memcpy (p, keyword_str [k], klen); p += klen; + + if (v) + { + *p++ = ' '; vlen--; + memcpy (p, v, vlen); p += vlen; + } + + *p = '\n'; + } + + void put (keyword k, const char *v) + { + put_string (k, v); + } + + void put (keyword k, const shstr &v) + { + put_string (k, v); + } + + void put (keyword k, double v) + { + char buf [128]; + + snprintf (buf, 128, "%.7g", v); - template - void put (attachable *obj) + put (k, (const char *)buf); + } + + void put_(keyword k, sint64 v) + { + add (keyword_str [k], keyword_len [k]); + add (' '); + add (v); + add ('\n'); + } + + void put_(keyword k, sint32 v) + { + add (keyword_str [k], keyword_len [k]); + add (' '); + add (v); + add ('\n'); + } + + void put (keyword kbeg, keyword kend, const shstr &v) + { + add (keyword_str [kbeg], keyword_len [kbeg]); add ('\n'); + + if (v) + { + add (v); + add ('\n'); + } + + add (keyword_str [kend], keyword_len [kend]); add ('\n'); + } + + void put (keyword k, float v) { put (k, (double)v); } + void put (keyword k, signed char v) { put_(k, (sint32)v); } + void put (keyword k, unsigned char v) { put_(k, (sint32)v); } + void put (keyword k, signed short v) { put_(k, (sint32)v); } + void put (keyword k, unsigned short v) { put_(k, (sint32)v); } + void put (keyword k, signed int v) { put_(k, (sint32)v); } + void put (keyword k, unsigned int v) { put_(k, (sint64)v); } + void put (keyword k, signed long v) { put_(k, (sint64)v); } + void put (keyword k, unsigned long v) { put_(k, (sint64)v); } + void put (keyword k, signed long long v) { put_(k, (sint64)v); } + void put (keyword k, unsigned long long v) { put_(k, (sint64)v); } + + template + void put (keyword k, const T *v) { - put ((attachable_base *)obj); + if (v) + put (k, v->name); + else + put (k, (const char *)0); } - void finish (); + template + void put (keyword k, const refptr &v) + { + put (k, (T *)v); + } + + bool save (const char *filename); + char *as_string (); // like strdup + + operator bool () { return !!av; } }; +// compatibility support, should be removed when no longer needed +int fprintf (object_freezer &freezer, const char *format, ...); +int fputs (const char *s, object_freezer &freezer); + struct object_thawer { - void *av; - int idx; + SV *text; // text part + AV *av; // perl part + char *line; // current beginning of line + char *last_keyword, *last_value; + + operator bool () { return !!text; } object_thawer (const char *filename = 0); + object_thawer (const char *data, AV *perlav); ~object_thawer (); - void get (data_type type, void *obj, attachable_base *ext); + void get (attachable *obj, int oid); + + keyword get_kv (); // also parse value for later use + void skip_kv (keyword kw); - template - void get (attachable *obj) + const char *get_str () { return last_value; } // may be 0 + + void get (shstr &sh) const; + void get_ornull (shstr &sh) const { sh = last_value; } + void get_ml (keyword kend, shstr &sh); + + sint32 get_sint32 () const; + sint64 get_sint64 () const; + double get_double () const; + + void get (float &v) { v = get_double (); } + void get (double &v) { v = get_double (); } + + void get (bool &i) { i = get_sint32 (); } + void get (sint8 &i) { i = get_sint32 (); } + void get (uint8 &i) { i = get_sint32 (); } + void get (sint16 &i) { i = get_sint32 (); } + void get (uint16 &i) { i = get_sint32 (); } + void get (sint32 &i) { i = get_sint32 (); } + + void get (uint32 &i) { i = get_sint64 (); } + void get (sint64 &i) { i = get_sint64 (); } +}; + +char *fgets (char *s, int n, object_thawer &thawer); + +////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +struct coroapi { + static struct CoroAPI *GCoroAPI; + + static int nready () { return CORO_NREADY; } + static int cede () { return CORO_CEDE ; } +}; + +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 () { - if (av) - get (subclass::get_dt (), (subclass *)obj, obj); + cancel (); } - void finish (); +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