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.47 by root, Sun Jan 7 02:39:14 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 ();
65
66//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
67
68// virtual server time, excluding time jumps and lag
69extern double runtime;
41 70
42enum event_klass 71enum event_klass
43{ 72{
44 KLASS_NONE, 73 KLASS_NONE,
45 KLASS_GLOBAL, 74 KLASS_GLOBAL,
89#define RESULT_INT(idx) RESULT(idx, INT) 118#define RESULT_INT(idx) RESULT(idx, INT)
90 119
91double cfperl_result_DOUBLE (int idx); 120double cfperl_result_DOUBLE (int idx);
92int cfperl_result_INT (int idx); 121int cfperl_result_INT (int idx);
93 122
123//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
124
125INTERFACE_CLASS (attachable)
94struct attachable 126struct attachable
95{ 127{
128 static MGVTBL vtbl;
129
96 static unordered_vector<attachable *> mortals; 130 static unordered_vector<attachable *> mortals;
97 static void check_mortals (); 131 MTH static void check_mortals ();
98 132
99 enum { 133 enum {
100 F_DESTROYED = 0x01, 134 F_DESTROYED = 0x01,
101 F_BORROWED = 0x02, 135 F_BORROWED = 0x02,
102 }; 136 };
103 137
104 // object is delete'd after the refcount reaches 0 138 // object is delete'd after the refcount reaches 0
105 mutable int refcnt; 139 mutable int ACC (RW, refcnt);
106 int flags; 140 int ACC (RW, flags);
107 141
108 void refcnt_inc () const { ++refcnt; } 142 MTH void refcnt_inc () const { ++refcnt; }
109 void refcnt_dec () const { --refcnt; } 143 MTH void refcnt_dec () const { --refcnt; }
110 144
111 // check wether the object has died and destroy 145 // check wether the object has died and destroy
112 void refcnt_chk () { if (refcnt <= 0) do_check (); } 146 MTH void refcnt_chk () { if (refcnt <= 0) do_check (); }
113 147
114 // destroy the object unless it was already destroyed 148 // destroy the object unless it was already destroyed
115 // this politely asks everybody interested the reduce 149 // this politely asks everybody interested the reduce
116 // the refcount to 0 as soon as possible. 150 // the refcount to 0 as soon as possible.
117 void destroy (); 151 MTH void destroy ();
118 152
119 // return wether an object was destroyed already 153 // return wether an object was destroyed already
120 bool destroyed () const { return flags & F_DESTROYED; } 154 MTH bool destroyed () const { return flags & F_DESTROYED; }
121 155
122 virtual void gather_callbacks (AV *&callbacks, event_type event) const; 156 virtual void gather_callbacks (AV *&callbacks, event_type event) const;
123 157
124#if 0 158#if 0
125private: 159private:
165 void gather_callbacks (AV *&callbacks, event_type event) const; 199 void gather_callbacks (AV *&callbacks, event_type event) const;
166}; 200};
167 201
168extern struct global gbl_ev; 202extern struct global gbl_ev;
169 203
204//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
205
170struct object_freezer : dynbuf 206struct object_freezer : dynbuf
171{ 207{
172 AV *av; 208 AV *av;
173 209
174 object_freezer (); 210 object_freezer ();
213 } 249 }
214 250
215 *p = '\n'; 251 *p = '\n';
216 } 252 }
217 253
218 void put (keyword k, const char *v) 254 void put (keyword k, const char *v = 0)
219 { 255 {
220 put_string (k, v); 256 put_string (k, v);
221 } 257 }
222 258
223 void put (keyword k, const shstr &v) 259 void put (keyword k, const shstr &v)
288 void put (keyword k, const refptr<T> &v) 324 void put (keyword k, const refptr<T> &v)
289 { 325 {
290 put (k, (T *)v); 326 put (k, (T *)v);
291 } 327 }
292 328
293 bool save (const char *filename); 329 bool save (const char *path);
294 char *as_string (); // like strdup 330 char *as_string (); // like strdup
295 331
296 operator bool () { return !!av; } 332 operator bool () { return !!av; }
297}; 333};
298 334
303struct object_thawer 339struct object_thawer
304{ 340{
305 SV *text; // text part 341 SV *text; // text part
306 AV *av; // perl part 342 AV *av; // perl part
307 char *line; // current beginning of line 343 char *line; // current beginning of line
308 char *last_value; 344 char *last_keyword, *last_value;
345 const char *name;
309 346
310 operator bool () { return !!text; } 347 operator bool () { return !!text; }
311 348
312 object_thawer (const char *filename = 0); 349 object_thawer (const char *path = 0);
313 object_thawer (const char *data, AV *perlav); 350 object_thawer (const char *data, AV *perlav);
314 ~object_thawer (); 351 ~object_thawer ();
315 352
316 void get (attachable *obj, int oid); 353 void get (attachable *obj, int oid);
317 354
318 keyword get_kv (); // also parse value for later use 355 keyword get_kv (); // also parse value for later use
356 void skip_kv (keyword kw);
319 357
320 const char *get_str () { return last_value; } // may be 0 358 const char *get_str () { return last_value; } // may be 0
321 359
322 void get (shstr &sh) const; 360 void get (shstr &sh) const;
323 void get_ornull (shstr &sh) const { sh = last_value; } 361 void get_ornull (shstr &sh) const { sh = last_value; }
325 363
326 sint32 get_sint32 () const; 364 sint32 get_sint32 () const;
327 sint64 get_sint64 () const; 365 sint64 get_sint64 () const;
328 double get_double () const; 366 double get_double () const;
329 367
330 void get (float &v) { v = get_double (); } 368 void get (float &v) { v = get_double (); }
331 void get (double &v) { v = get_double (); } 369 void get (double &v) { v = get_double (); }
332 370
371 void get (bool &i) { i = get_sint32 (); }
333 void get (sint8 &i) { i = get_sint32 (); } 372 void get (sint8 &i) { i = get_sint32 (); }
334 void get (uint8 &i) { i = get_sint32 (); } 373 void get (uint8 &i) { i = get_sint32 (); }
335 void get (sint16 &i) { i = get_sint32 (); } 374 void get (sint16 &i) { i = get_sint32 (); }
336 void get (uint16 &i) { i = get_sint32 (); } 375 void get (uint16 &i) { i = get_sint32 (); }
337 void get (sint32 &i) { i = get_sint32 (); } 376 void get (sint32 &i) { i = get_sint32 (); }
340 void get (sint64 &i) { i = get_sint64 (); } 379 void get (sint64 &i) { i = get_sint64 (); }
341}; 380};
342 381
343char *fgets (char *s, int n, object_thawer &thawer); 382char *fgets (char *s, int n, object_thawer &thawer);
344 383
384//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
385
386struct coroapi {
387 static struct CoroAPI *GCoroAPI;
388
389 static int nready () { return CORO_NREADY; }
390 static int cede () { return CORO_CEDE ; }
391
392 static int cede_counter;
393
394 static void cede_every (int count)
395 {
396 if (++cede_counter >= count)
397 {
398 cede_counter = 0;
399
400 if (coroapi::nready ())
401 coroapi::cede ();
402 }
403 }
404};
405
345struct watcher_base 406struct watcher_base
346{ 407{
347 static struct EventAPI *GEventAPI; 408 static struct EventAPI *GEventAPI;
348}; 409};
349 410

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines