ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/cfperl.h
(Generate patch)

Comparing deliantra/server/include/cfperl.h (file contents):
Revision 1.41 by root, Mon Dec 25 11:25:49 2006 UTC vs.
Revision 1.50 by root, Tue Jan 9 21:32:42 2007 UTC

11#include <EXTERN.h> 11#include <EXTERN.h>
12#include <perl.h> 12#include <perl.h>
13#include <XSUB.h> 13#include <XSUB.h>
14 14
15#include <EventAPI.h> 15#include <EventAPI.h>
16#include <CoroAPI.h>
16 17
18#include "util.h"
19#include "keyword.h"
20#include "dynbuf.h"
17#include "callback.h" 21#include "callback.h"
22
23//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
18 24
19// optimisations/workaround for functions requiring my_perl in scope (anti-bloat) 25// optimisations/workaround for functions requiring my_perl in scope (anti-bloat)
20#undef localtime 26#undef localtime
21#undef srand48 27#undef srand48
22#undef drand48 28#undef drand48
28#undef strerror 34#undef strerror
29 35
30// same here, massive symbol spamming 36// same here, massive symbol spamming
31#undef do_open 37#undef do_open
32#undef do_close 38#undef do_close
39#undef ref
40#undef seed
33 41
34// perl bug #40256: perl does overwrite those with reentrant versions 42// perl bug #40256: perl does overwrite those with reentrant versions
35// but does not initialise their state structures. 43// but does not initialise their state structures.
36#undef random 44#undef random
37#undef crypt 45#undef crypt
38 46
47//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
48
49// some macros to simplify perl in-calls
50
51#define CHECK_ERROR \
52 if (SvTRUE (ERRSV)) \
53 LOG (llevError, "runtime error in %s: %s\n", __func__, SvPVutf8_nolen (ERRSV));
54
55#define CALL_BEGIN(args) dSP; ENTER; SAVETMPS; PUSHMARK (SP); EXTEND (SP, args)
56#define CALL_ARG_SV(sv) PUSHs (sv_2mortal (sv)) // separate because no refcount inc
57#define CALL_ARG(expr) PUSHs (sv_2mortal (to_sv (expr)))
58#define CALL_CALL(name, flags) PUTBACK; int count = call_pv (name, (flags) | G_EVAL); SPAGAIN;
59#define CALL_END CHECK_ERROR; FREETMPS; LEAVE
60
61//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
62
39void cfperl_init (); 63void cfperl_init ();
40void cfperl_main (); 64void cfperl_main ();
65void cfperl_emergency_save ();
66
67//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
68
69// virtual server time, excluding time jumps and lag
70extern double runtime;
41 71
42enum event_klass 72enum event_klass
43{ 73{
44 KLASS_NONE, 74 KLASS_NONE,
45 KLASS_GLOBAL, 75 KLASS_GLOBAL,
89#define RESULT_INT(idx) RESULT(idx, INT) 119#define RESULT_INT(idx) RESULT(idx, INT)
90 120
91double cfperl_result_DOUBLE (int idx); 121double cfperl_result_DOUBLE (int idx);
92int cfperl_result_INT (int idx); 122int cfperl_result_INT (int idx);
93 123
124//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
125
126INTERFACE_CLASS (attachable)
94struct attachable 127struct attachable
95{ 128{
129 static MGVTBL vtbl;
130
96 static unordered_vector<attachable *> mortals; 131 static unordered_vector<attachable *> mortals;
97 static void check_mortals (); 132 MTH static void check_mortals ();
98 133
99 enum { 134 enum {
100 F_DESTROYED = 0x01, 135 F_DESTROYED = 0x01,
101 F_BORROWED = 0x02, 136 F_BORROWED = 0x02,
102 }; 137 };
103 138
104 // object is delete'd after the refcount reaches 0 139 // object is delete'd after the refcount reaches 0
105 mutable int refcnt; 140 mutable int ACC (RW, refcnt);
106 int flags; 141 int ACC (RW, flags);
107 142
108 void refcnt_inc () const { ++refcnt; } 143 MTH void refcnt_inc () const { ++refcnt; }
109 void refcnt_dec () const { --refcnt; } 144 MTH void refcnt_dec () const { --refcnt; }
110 145
146 MTH int refcnt_cnt () const;
111 // check wether the object has died and destroy 147 // check wether the object has died and destroy
112 void refcnt_chk () { if (refcnt <= 0) do_check (); } 148 MTH void refcnt_chk () { if (refcnt <= 0) do_check (); }
113 149
114 // destroy the object unless it was already destroyed 150 // destroy the object unless it was already destroyed
115 // this politely asks everybody interested the reduce 151 // this politely asks everybody interested the reduce
116 // the refcount to 0 as soon as possible. 152 // the refcount to 0 as soon as possible.
117 void destroy (); 153 MTH void destroy ();
118 154
119 // return wether an object was destroyed already 155 // return wether an object was destroyed already
120 bool destroyed () const { return flags & F_DESTROYED; } 156 MTH bool destroyed () const { return flags & F_DESTROYED; }
121 157
122 virtual void gather_callbacks (AV *&callbacks, event_type event) const; 158 virtual void gather_callbacks (AV *&callbacks, event_type event) const;
123 159
124#if 0 160#if 0
125private: 161private:
146 virtual ~attachable (); 182 virtual ~attachable ();
147 183
148 attachable &operator =(const attachable &src); 184 attachable &operator =(const attachable &src);
149 185
150 bool invoke (event_type event, ...); 186 bool invoke (event_type event, ...);
151 void instantiate (); 187 MTH void instantiate ();
152 void reattach (); 188 void reattach ();
153 189
154protected: 190protected:
155 // do the real refcount checking work 191 // do the real refcount checking work
156 void do_check (); 192 void do_check ();
164{ 200{
165 void gather_callbacks (AV *&callbacks, event_type event) const; 201 void gather_callbacks (AV *&callbacks, event_type event) const;
166}; 202};
167 203
168extern struct global gbl_ev; 204extern struct global gbl_ev;
205
206//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
169 207
170struct object_freezer : dynbuf 208struct object_freezer : dynbuf
171{ 209{
172 AV *av; 210 AV *av;
173 211
213 } 251 }
214 252
215 *p = '\n'; 253 *p = '\n';
216 } 254 }
217 255
218 void put (keyword k, const char *v) 256 void put (keyword k, const char *v = 0)
219 { 257 {
220 put_string (k, v); 258 put_string (k, v);
221 } 259 }
222 260
223 void put (keyword k, const shstr &v) 261 void put (keyword k, const shstr &v)
288 void put (keyword k, const refptr<T> &v) 326 void put (keyword k, const refptr<T> &v)
289 { 327 {
290 put (k, (T *)v); 328 put (k, (T *)v);
291 } 329 }
292 330
293 bool save (const char *filename); 331 bool save (const char *path);
294 char *as_string (); // like strdup 332 char *as_string (); // like strdup
295 333
296 operator bool () { return !!av; } 334 operator bool () { return !!av; }
297}; 335};
298 336
303struct object_thawer 341struct object_thawer
304{ 342{
305 SV *text; // text part 343 SV *text; // text part
306 AV *av; // perl part 344 AV *av; // perl part
307 char *line; // current beginning of line 345 char *line; // current beginning of line
308 char *last_value; 346 char *last_keyword, *last_value;
347 const char *name;
309 348
310 operator bool () { return !!text; } 349 operator bool () { return !!text; }
311 350
312 object_thawer (const char *filename = 0); 351 object_thawer (const char *path = 0);
313 object_thawer (const char *data, AV *perlav); 352 object_thawer (const char *data, AV *perlav);
314 ~object_thawer (); 353 ~object_thawer ();
315 354
316 void get (attachable *obj, int oid); 355 void get (attachable *obj, int oid);
317 356
318 keyword get_kv (); // also parse value for later use 357 keyword get_kv (); // also parse value for later use
358 void skip_kv (keyword kw);
319 359
320 const char *get_str () { return last_value; } // may be 0 360 const char *get_str () { return last_value; } // may be 0
321 361
322 void get (shstr &sh) const; 362 void get (shstr &sh) const;
323 void get_ornull (shstr &sh) const { sh = last_value; } 363 void get_ornull (shstr &sh) const { sh = last_value; }
325 365
326 sint32 get_sint32 () const; 366 sint32 get_sint32 () const;
327 sint64 get_sint64 () const; 367 sint64 get_sint64 () const;
328 double get_double () const; 368 double get_double () const;
329 369
330 void get (float &v) { v = get_double (); } 370 void get (float &v) { v = get_double (); }
331 void get (double &v) { v = get_double (); } 371 void get (double &v) { v = get_double (); }
332 372
373 void get (bool &i) { i = get_sint32 (); }
333 void get (sint8 &i) { i = get_sint32 (); } 374 void get (sint8 &i) { i = get_sint32 (); }
334 void get (uint8 &i) { i = get_sint32 (); } 375 void get (uint8 &i) { i = get_sint32 (); }
335 void get (sint16 &i) { i = get_sint32 (); } 376 void get (sint16 &i) { i = get_sint32 (); }
336 void get (uint16 &i) { i = get_sint32 (); } 377 void get (uint16 &i) { i = get_sint32 (); }
337 void get (sint32 &i) { i = get_sint32 (); } 378 void get (sint32 &i) { i = get_sint32 (); }
340 void get (sint64 &i) { i = get_sint64 (); } 381 void get (sint64 &i) { i = get_sint64 (); }
341}; 382};
342 383
343char *fgets (char *s, int n, object_thawer &thawer); 384char *fgets (char *s, int n, object_thawer &thawer);
344 385
386//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
387
388struct coroapi {
389 static struct CoroAPI *GCoroAPI;
390
391 static int nready () { return CORO_NREADY; }
392 static int cede () { return CORO_CEDE ; }
393
394 static int cede_counter;
395
396 static void cede_every (int count)
397 {
398 if (++cede_counter >= count)
399 {
400 cede_counter = 0;
401
402 if (coroapi::nready ())
403 coroapi::cede ();
404 }
405 }
406};
407
345struct watcher_base 408struct watcher_base
346{ 409{
347 static struct EventAPI *GEventAPI; 410 static struct EventAPI *GEventAPI;
348}; 411};
349 412

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines