1 | package cf; |
1 | package cf; |
|
|
2 | |
|
|
3 | use utf8; |
|
|
4 | use strict; |
2 | |
5 | |
3 | use Symbol; |
6 | use Symbol; |
4 | use List::Util; |
7 | use List::Util; |
5 | use Storable; |
8 | use Storable; |
6 | use Opcode; |
9 | use Opcode; |
7 | use Safe; |
10 | use Safe; |
8 | use Safe::Hole; |
11 | use Safe::Hole; |
9 | |
12 | |
|
|
13 | use Coro 3.3; |
|
|
14 | use Coro::Event; |
|
|
15 | use Coro::Timer; |
|
|
16 | use Coro::Signal; |
|
|
17 | use Coro::Semaphore; |
|
|
18 | use Coro::AIO; |
|
|
19 | |
|
|
20 | use Fcntl; |
10 | use IO::AIO (); |
21 | use IO::AIO 2.31 (); |
11 | use YAML::Syck (); |
22 | use YAML::Syck (); |
12 | use Time::HiRes; |
23 | use Time::HiRes; |
13 | use Event; |
24 | |
14 | $Event::Eval = 1; # no idea why this is required, but it is |
25 | use Event; $Event::Eval = 1; # no idea why this is required, but it is |
15 | |
26 | |
16 | # work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? |
27 | # work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? |
17 | $YAML::Syck::ImplicitUnicode = 1; |
28 | $YAML::Syck::ImplicitUnicode = 1; |
18 | |
29 | |
19 | use strict; |
30 | $Coro::main->prio (2); # run main coroutine ("the server") with very high priority |
20 | |
31 | |
21 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
32 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
22 | |
33 | |
23 | our %COMMAND = (); |
34 | our %COMMAND = (); |
24 | our %COMMAND_TIME = (); |
35 | our %COMMAND_TIME = (); |
25 | our %EXTCMD = (); |
36 | our %EXTCMD = (); |
26 | |
37 | |
27 | _init_vars; |
|
|
28 | |
|
|
29 | our @EVENT; |
38 | our @EVENT; |
30 | our $LIBDIR = datadir . "/ext"; |
39 | our $LIBDIR = datadir . "/ext"; |
31 | |
40 | |
32 | our $TICK = MAX_TIME * 1e-6; |
41 | our $TICK = MAX_TIME * 1e-6; |
33 | our $TICK_WATCHER; |
42 | our $TICK_WATCHER; |
34 | our $NEXT_TICK; |
43 | our $NEXT_TICK; |
|
|
44 | our $NOW; |
35 | |
45 | |
36 | our %CFG; |
46 | our %CFG; |
37 | |
47 | |
38 | our $UPTIME; $UPTIME ||= time; |
48 | our $UPTIME; $UPTIME ||= time; |
|
|
49 | our $RUNTIME; |
|
|
50 | |
|
|
51 | our %MAP; # all maps |
|
|
52 | our $LINK_MAP; # the special {link} map |
|
|
53 | our $FREEZE; |
|
|
54 | |
|
|
55 | binmode STDOUT; |
|
|
56 | binmode STDERR; |
|
|
57 | |
|
|
58 | # read virtual server time, if available |
|
|
59 | unless ($RUNTIME || !-e cf::localdir . "/runtime") { |
|
|
60 | open my $fh, "<", cf::localdir . "/runtime" |
|
|
61 | or die "unable to read runtime file: $!"; |
|
|
62 | $RUNTIME = <$fh> + 0.; |
|
|
63 | } |
|
|
64 | |
|
|
65 | mkdir cf::localdir; |
|
|
66 | mkdir cf::localdir . "/" . cf::playerdir; |
|
|
67 | mkdir cf::localdir . "/" . cf::tmpdir; |
|
|
68 | mkdir cf::localdir . "/" . cf::uniquedir; |
|
|
69 | |
|
|
70 | our %EXT_CORO; |
39 | |
71 | |
40 | ############################################################################# |
72 | ############################################################################# |
41 | |
73 | |
42 | =head2 GLOBAL VARIABLES |
74 | =head2 GLOBAL VARIABLES |
43 | |
75 | |
44 | =over 4 |
76 | =over 4 |
45 | |
77 | |
46 | =item $cf::UPTIME |
78 | =item $cf::UPTIME |
47 | |
79 | |
48 | The timestamp of the server start (so not actually an uptime). |
80 | The timestamp of the server start (so not actually an uptime). |
|
|
81 | |
|
|
82 | =item $cf::RUNTIME |
|
|
83 | |
|
|
84 | The time this server has run, starts at 0 and is increased by $cf::TICK on |
|
|
85 | every server tick. |
49 | |
86 | |
50 | =item $cf::LIBDIR |
87 | =item $cf::LIBDIR |
51 | |
88 | |
52 | The perl library directory, where extensions and cf-specific modules can |
89 | The perl library directory, where extensions and cf-specific modules can |
53 | be found. It will be added to C<@INC> automatically. |
90 | be found. It will be added to C<@INC> automatically. |
|
|
91 | |
|
|
92 | =item $cf::NOW |
|
|
93 | |
|
|
94 | The time of the last (current) server tick. |
54 | |
95 | |
55 | =item $cf::TICK |
96 | =item $cf::TICK |
56 | |
97 | |
57 | The interval between server ticks, in seconds. |
98 | The interval between server ticks, in seconds. |
58 | |
99 | |
… | |
… | |
66 | =cut |
107 | =cut |
67 | |
108 | |
68 | BEGIN { |
109 | BEGIN { |
69 | *CORE::GLOBAL::warn = sub { |
110 | *CORE::GLOBAL::warn = sub { |
70 | my $msg = join "", @_; |
111 | my $msg = join "", @_; |
|
|
112 | utf8::encode $msg; |
|
|
113 | |
71 | $msg .= "\n" |
114 | $msg .= "\n" |
72 | unless $msg =~ /\n$/; |
115 | unless $msg =~ /\n$/; |
73 | |
116 | |
74 | print STDERR "cfperl: $msg"; |
|
|
75 | LOG llevError, "cfperl: $msg"; |
117 | LOG llevError, "cfperl: $msg"; |
76 | }; |
118 | }; |
77 | } |
119 | } |
78 | |
120 | |
79 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
121 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
… | |
… | |
84 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
126 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
85 | |
127 | |
86 | # we bless all objects into (empty) derived classes to force a method lookup |
128 | # we bless all objects into (empty) derived classes to force a method lookup |
87 | # within the Safe compartment. |
129 | # within the Safe compartment. |
88 | for my $pkg (qw( |
130 | for my $pkg (qw( |
89 | cf::global |
131 | cf::global cf::attachable |
90 | cf::object cf::object::player |
132 | cf::object cf::object::player |
91 | cf::client cf::player |
133 | cf::client cf::player |
92 | cf::arch cf::living |
134 | cf::arch cf::living |
93 | cf::map cf::party cf::region |
135 | cf::map cf::party cf::region |
94 | )) { |
136 | )) { |
… | |
… | |
132 | sub to_json($) { |
174 | sub to_json($) { |
133 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
175 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
134 | JSON::Syck::Dump $_[0] |
176 | JSON::Syck::Dump $_[0] |
135 | } |
177 | } |
136 | |
178 | |
|
|
179 | =item cf::sync_job { BLOCK } |
|
|
180 | |
|
|
181 | The design of crossfire+ requires that the main coro ($Coro::main) is |
|
|
182 | always able to handle events or runnable, as crossfire+ is only partly |
|
|
183 | reentrant. Thus "blocking" it by e.g. waiting for I/O is not acceptable. |
|
|
184 | |
|
|
185 | If it must be done, put the blocking parts into C<sync_job>. This will run |
|
|
186 | the given BLOCK in another coroutine while waiting for the result. The |
|
|
187 | server will be frozen during this time, so the block should either finish |
|
|
188 | fast or be very important. |
|
|
189 | |
|
|
190 | =cut |
|
|
191 | |
|
|
192 | sub sync_job(&) { |
|
|
193 | my ($job) = @_; |
|
|
194 | |
|
|
195 | my $busy = 1; |
|
|
196 | my @res; |
|
|
197 | |
|
|
198 | # TODO: use suspend/resume instead |
|
|
199 | local $FREEZE = 1; |
|
|
200 | |
|
|
201 | my $coro = Coro::async { |
|
|
202 | @res = eval { $job->() }; |
|
|
203 | warn $@ if $@; |
|
|
204 | undef $busy; |
|
|
205 | }; |
|
|
206 | |
|
|
207 | if ($Coro::current == $Coro::main) { |
|
|
208 | $coro->prio (Coro::PRIO_MAX); |
|
|
209 | while ($busy) { |
|
|
210 | Coro::cede_notself; |
|
|
211 | Event::one_event unless Coro::nready; |
|
|
212 | } |
|
|
213 | } else { |
|
|
214 | $coro->join; |
|
|
215 | } |
|
|
216 | |
|
|
217 | wantarray ? @res : $res[0] |
|
|
218 | } |
|
|
219 | |
|
|
220 | =item $coro = cf::coro { BLOCK } |
|
|
221 | |
|
|
222 | Creates and returns a new coro. This coro is automcatially being canceled |
|
|
223 | when the extension calling this is being unloaded. |
|
|
224 | |
|
|
225 | =cut |
|
|
226 | |
|
|
227 | sub coro(&) { |
|
|
228 | my $cb = shift; |
|
|
229 | |
|
|
230 | my $coro; $coro = async { |
|
|
231 | eval { |
|
|
232 | $cb->(); |
|
|
233 | }; |
|
|
234 | warn $@ if $@; |
|
|
235 | }; |
|
|
236 | |
|
|
237 | $coro->on_destroy (sub { |
|
|
238 | delete $EXT_CORO{$coro+0}; |
|
|
239 | }); |
|
|
240 | $EXT_CORO{$coro+0} = $coro; |
|
|
241 | |
|
|
242 | $coro |
|
|
243 | } |
|
|
244 | |
137 | =back |
245 | =back |
138 | |
246 | |
139 | =cut |
247 | =cut |
140 | |
248 | |
141 | ############################################################################# |
249 | ############################################################################# |
142 | |
250 | |
143 | =head2 ATTACHABLE OBJECTS |
251 | =head2 ATTACHABLE OBJECTS |
144 | |
252 | |
145 | You can define and attach attachments to each "attachable" object in |
253 | Many objects in crossfire are so-called attachable objects. That means you can |
146 | crossfire+ (objects, players, clients, maps and the special "global" |
254 | attach callbacks/event handlers (a collection of which is called an "attachment") |
|
|
255 | to it. All such attachable objects support the following methods. |
|
|
256 | |
147 | class). In the following description, CLASS can be any of C<global>, |
257 | In the following description, CLASS can be any of C<global>, C<object> |
148 | C<object> C<player>, C<client> or C<map>. |
258 | C<player>, C<client> or C<map> (i.e. the attachable objects in |
|
|
259 | crossfire+). |
149 | |
260 | |
150 | =over 4 |
261 | =over 4 |
151 | |
262 | |
|
|
263 | =item $attachable->attach ($attachment, key => $value...) |
|
|
264 | |
|
|
265 | =item $attachable->detach ($attachment) |
|
|
266 | |
|
|
267 | Attach/detach a pre-registered attachment to a specific object and give it |
|
|
268 | the specified key/value pairs as arguments. |
|
|
269 | |
|
|
270 | Example, attach a minesweeper attachment to the given object, making it a |
|
|
271 | 10x10 minesweeper game: |
|
|
272 | |
|
|
273 | $obj->attach (minesweeper => width => 10, height => 10); |
|
|
274 | |
|
|
275 | =item $bool = $attachable->attached ($name) |
|
|
276 | |
|
|
277 | Checks wether the named attachment is currently attached to the object. |
|
|
278 | |
|
|
279 | =item cf::CLASS->attach ... |
|
|
280 | |
|
|
281 | =item cf::CLASS->detach ... |
|
|
282 | |
|
|
283 | Define an anonymous attachment and attach it to all objects of the given |
|
|
284 | CLASS. See the next function for an explanation of its arguments. |
|
|
285 | |
|
|
286 | You can attach to global events by using the C<cf::global> class. |
|
|
287 | |
|
|
288 | Example, log all player logins: |
|
|
289 | |
|
|
290 | cf::player->attach ( |
|
|
291 | on_login => sub { |
|
|
292 | my ($pl) = @_; |
|
|
293 | ... |
|
|
294 | }, |
|
|
295 | ); |
|
|
296 | |
|
|
297 | Example, attach to the jeweler skill: |
|
|
298 | |
|
|
299 | cf::object->attach ( |
|
|
300 | type => cf::SKILL, |
|
|
301 | subtype => cf::SK_JEWELER, |
|
|
302 | on_use_skill => sub { |
|
|
303 | my ($sk, $ob, $part, $dir, $msg) = @_; |
|
|
304 | ... |
|
|
305 | }, |
|
|
306 | ); |
|
|
307 | |
152 | =item cf::CLASS::attachment $name, ... |
308 | =item cf::CLASS::attachment $name, ... |
153 | |
309 | |
154 | Register an attachment by name through which attachable objects can refer |
310 | Register an attachment by C<$name> through which attachable objects of the |
155 | to this attachment. |
311 | given CLASS can refer to this attachment. |
156 | |
312 | |
157 | =item $bool = $attachable->attached ($name) |
313 | Some classes such as crossfire maps and objects can specify attachments |
|
|
314 | that are attached at load/instantiate time, thus the need for a name. |
158 | |
315 | |
159 | Checks wether the named attachment is currently attached to the object. |
|
|
160 | |
|
|
161 | =item $attachable->attach ($attachment, key => $value...) |
|
|
162 | |
|
|
163 | =item $attachable->detach ($attachment) |
|
|
164 | |
|
|
165 | Attach/detach a pre-registered attachment either to a specific object |
|
|
166 | (C<$attachable>) or all objects of the given class (if C<$attachable> is a |
|
|
167 | class in a static method call). |
|
|
168 | |
|
|
169 | You can attach to global events by using the C<cf::global> class. |
|
|
170 | |
|
|
171 | These method calls expect any number of the following handler/hook |
316 | These calls expect any number of the following handler/hook descriptions: |
172 | descriptions: |
|
|
173 | |
317 | |
174 | =over 4 |
318 | =over 4 |
175 | |
319 | |
176 | =item prio => $number |
320 | =item prio => $number |
177 | |
321 | |
… | |
… | |
203 | package and register them. Only handlers for eevents supported by the |
347 | package and register them. Only handlers for eevents supported by the |
204 | object/class are recognised. |
348 | object/class are recognised. |
205 | |
349 | |
206 | =back |
350 | =back |
207 | |
351 | |
|
|
352 | Example, define an attachment called "sockpuppet" that calls the given |
|
|
353 | event handler when a monster attacks: |
|
|
354 | |
|
|
355 | cf::object::attachment sockpuppet => |
|
|
356 | on_skill_attack => sub { |
|
|
357 | my ($self, $victim) = @_; |
|
|
358 | ... |
|
|
359 | } |
|
|
360 | } |
|
|
361 | |
|
|
362 | =item $attachable->valid |
|
|
363 | |
|
|
364 | Just because you have a perl object does not mean that the corresponding |
|
|
365 | C-level object still exists. If you try to access an object that has no |
|
|
366 | valid C counterpart anymore you get an exception at runtime. This method |
|
|
367 | can be used to test for existence of the C object part without causing an |
|
|
368 | exception. |
|
|
369 | |
208 | =cut |
370 | =cut |
209 | |
371 | |
210 | # the following variables are defined in .xs and must not be re-created |
372 | # the following variables are defined in .xs and must not be re-created |
211 | our @CB_GLOBAL = (); # registry for all global events |
373 | our @CB_GLOBAL = (); # registry for all global events |
|
|
374 | our @CB_ATTACHABLE = (); # registry for all attachables |
212 | our @CB_OBJECT = (); # all objects (should not be used except in emergency) |
375 | our @CB_OBJECT = (); # all objects (should not be used except in emergency) |
213 | our @CB_PLAYER = (); |
376 | our @CB_PLAYER = (); |
214 | our @CB_CLIENT = (); |
377 | our @CB_CLIENT = (); |
215 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
378 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
216 | our @CB_MAP = (); |
379 | our @CB_MAP = (); |
217 | |
380 | |
218 | my %attachment; |
381 | my %attachment; |
219 | |
382 | |
220 | sub _attach_cb($$$$) { |
383 | sub _attach_cb($$$$) { |
221 | my ($registry, $event, $prio, $cb) = @_; |
384 | my ($registry, $event, $prio, $cb) = @_; |
… | |
… | |
226 | |
389 | |
227 | @{$registry->[$event]} = sort |
390 | @{$registry->[$event]} = sort |
228 | { $a->[0] cmp $b->[0] } |
391 | { $a->[0] cmp $b->[0] } |
229 | @{$registry->[$event] || []}, $cb; |
392 | @{$registry->[$event] || []}, $cb; |
230 | } |
393 | } |
|
|
394 | |
|
|
395 | # hack |
|
|
396 | my %attachable_klass = map +($_ => 1), KLASS_OBJECT, KLASS_CLIENT, KLASS_PLAYER, KLASS_MAP; |
231 | |
397 | |
232 | # attach handles attaching event callbacks |
398 | # attach handles attaching event callbacks |
233 | # the only thing the caller has to do is pass the correct |
399 | # the only thing the caller has to do is pass the correct |
234 | # registry (== where the callback attaches to). |
400 | # registry (== where the callback attaches to). |
235 | sub _attach { |
401 | sub _attach { |
… | |
… | |
237 | |
403 | |
238 | my $object_type; |
404 | my $object_type; |
239 | my $prio = 0; |
405 | my $prio = 0; |
240 | my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; |
406 | my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; |
241 | |
407 | |
|
|
408 | #TODO: get rid of this hack |
|
|
409 | if ($attachable_klass{$klass}) { |
|
|
410 | %cb_id = (%cb_id, map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == KLASS_ATTACHABLE, 0 .. $#EVENT); |
|
|
411 | } |
|
|
412 | |
242 | while (@arg) { |
413 | while (@arg) { |
243 | my $type = shift @arg; |
414 | my $type = shift @arg; |
244 | |
415 | |
245 | if ($type eq "prio") { |
416 | if ($type eq "prio") { |
246 | $prio = shift @arg; |
417 | $prio = shift @arg; |
… | |
… | |
321 | my ($obj, $name) = @_; |
492 | my ($obj, $name) = @_; |
322 | |
493 | |
323 | exists $obj->{_attachment}{$name} |
494 | exists $obj->{_attachment}{$name} |
324 | } |
495 | } |
325 | |
496 | |
326 | for my $klass (qw(GLOBAL OBJECT PLAYER CLIENT MAP)) { |
497 | for my $klass (qw(ATTACHABLE GLOBAL OBJECT PLAYER CLIENT MAP)) { |
327 | eval "#line " . __LINE__ . " 'cf.pm' |
498 | eval "#line " . __LINE__ . " 'cf.pm' |
328 | sub cf::\L$klass\E::_attach_registry { |
499 | sub cf::\L$klass\E::_attach_registry { |
329 | (\\\@CB_$klass, KLASS_$klass) |
500 | (\\\@CB_$klass, KLASS_$klass) |
330 | } |
501 | } |
331 | |
502 | |
… | |
… | |
367 | } |
538 | } |
368 | |
539 | |
369 | 0 |
540 | 0 |
370 | } |
541 | } |
371 | |
542 | |
372 | =item $bool = cf::invoke EVENT_GLOBAL_XXX, ... |
543 | =item $bool = cf::global::invoke (EVENT_CLASS_XXX, ...) |
373 | |
544 | |
374 | =item $bool = $object->invoke (EVENT_OBJECT_XXX, ...) |
|
|
375 | |
|
|
376 | =item $bool = $player->invoke (EVENT_PLAYER_XXX, ...) |
|
|
377 | |
|
|
378 | =item $bool = $client->invoke (EVENT_CLIENT_XXX, ...) |
545 | =item $bool = $attachable->invoke (EVENT_CLASS_XXX, ...) |
379 | |
546 | |
380 | =item $bool = $map->invoke (EVENT_MAP_XXX, ...) |
|
|
381 | |
|
|
382 | Generate a global/object/player/map-specific event with the given arguments. |
547 | Generate an object-specific event with the given arguments. |
383 | |
548 | |
384 | This API is preliminary (most likely, the EVENT_KLASS_xxx prefix will be |
549 | This API is preliminary (most likely, the EVENT_CLASS_xxx prefix will be |
385 | removed in future versions), and there is no public API to access override |
550 | removed in future versions), and there is no public API to access override |
386 | results (if you must, access C<@cf::invoke_results> directly). |
551 | results (if you must, access C<@cf::invoke_results> directly). |
387 | |
552 | |
388 | =back |
553 | =back |
389 | |
554 | |
390 | =cut |
555 | =cut |
391 | |
556 | |
392 | ############################################################################# |
557 | ############################################################################# |
393 | |
|
|
394 | =head2 METHODS VALID FOR ALL ATTACHABLE OBJECTS |
|
|
395 | |
|
|
396 | Attachable objects includes objects, players, clients and maps. |
|
|
397 | |
|
|
398 | =over 4 |
|
|
399 | |
|
|
400 | =item $object->valid |
|
|
401 | |
|
|
402 | Just because you have a perl object does not mean that the corresponding |
|
|
403 | C-level object still exists. If you try to access an object that has no |
|
|
404 | valid C counterpart anymore you get an exception at runtime. This method |
|
|
405 | can be used to test for existence of the C object part without causing an |
|
|
406 | exception. |
|
|
407 | |
|
|
408 | =back |
|
|
409 | |
|
|
410 | =cut |
|
|
411 | |
|
|
412 | ############################################################################# |
|
|
413 | # object support |
558 | # object support |
414 | |
559 | |
415 | sub instantiate { |
|
|
416 | my ($obj, $data) = @_; |
|
|
417 | |
|
|
418 | $data = from_json $data; |
|
|
419 | |
|
|
420 | for (@$data) { |
|
|
421 | my ($name, $args) = @$_; |
|
|
422 | |
|
|
423 | $obj->attach ($name, %{$args || {} }); |
|
|
424 | } |
|
|
425 | } |
|
|
426 | |
|
|
427 | # basically do the same as instantiate, without calling instantiate |
|
|
428 | sub reattach { |
560 | sub reattach { |
|
|
561 | # basically do the same as instantiate, without calling instantiate |
429 | my ($obj) = @_; |
562 | my ($obj) = @_; |
|
|
563 | |
430 | my $registry = $obj->registry; |
564 | my $registry = $obj->registry; |
431 | |
565 | |
432 | @$registry = (); |
566 | @$registry = (); |
433 | |
567 | |
434 | delete $obj->{_attachment} unless scalar keys %{ $obj->{_attachment} || {} }; |
568 | delete $obj->{_attachment} unless scalar keys %{ $obj->{_attachment} || {} }; |
… | |
… | |
443 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
577 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
444 | } |
578 | } |
445 | } |
579 | } |
446 | } |
580 | } |
447 | |
581 | |
448 | sub object_freezer_save { |
582 | cf::attachable->attach ( |
449 | my ($filename, $rdata, $objs) = @_; |
|
|
450 | |
|
|
451 | if (length $$rdata) { |
|
|
452 | warn sprintf "saving %s (%d,%d)\n", |
|
|
453 | $filename, length $$rdata, scalar @$objs; |
|
|
454 | |
|
|
455 | if (open my $fh, ">:raw", "$filename~") { |
|
|
456 | chmod SAVE_MODE, $fh; |
|
|
457 | syswrite $fh, $$rdata; |
|
|
458 | close $fh; |
|
|
459 | |
|
|
460 | if (@$objs && open my $fh, ">:raw", "$filename.pst~") { |
|
|
461 | chmod SAVE_MODE, $fh; |
|
|
462 | syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; |
|
|
463 | close $fh; |
|
|
464 | rename "$filename.pst~", "$filename.pst"; |
|
|
465 | } else { |
|
|
466 | unlink "$filename.pst"; |
|
|
467 | } |
|
|
468 | |
|
|
469 | rename "$filename~", $filename; |
|
|
470 | } else { |
|
|
471 | warn "FATAL: $filename~: $!\n"; |
|
|
472 | } |
|
|
473 | } else { |
|
|
474 | unlink $filename; |
|
|
475 | unlink "$filename.pst"; |
|
|
476 | } |
|
|
477 | } |
|
|
478 | |
|
|
479 | sub object_freezer_as_string { |
|
|
480 | my ($rdata, $objs) = @_; |
|
|
481 | |
|
|
482 | use Data::Dumper; |
|
|
483 | |
|
|
484 | $$rdata . Dumper $objs |
|
|
485 | } |
|
|
486 | |
|
|
487 | sub object_thawer_load { |
|
|
488 | my ($filename) = @_; |
|
|
489 | |
|
|
490 | local $/; |
|
|
491 | |
|
|
492 | my $av; |
|
|
493 | |
|
|
494 | #TODO: use sysread etc. |
|
|
495 | if (open my $data, "<:raw:perlio", $filename) { |
|
|
496 | $data = <$data>; |
|
|
497 | if (open my $pst, "<:raw:perlio", "$filename.pst") { |
|
|
498 | $av = eval { (Storable::thaw <$pst>)->{objs} }; |
|
|
499 | } |
|
|
500 | return ($data, $av); |
|
|
501 | } |
|
|
502 | |
|
|
503 | () |
|
|
504 | } |
|
|
505 | |
|
|
506 | cf::object->attach ( |
|
|
507 | prio => -1000000, |
583 | prio => -1000000, |
|
|
584 | on_instantiate => sub { |
|
|
585 | my ($obj, $data) = @_; |
|
|
586 | |
|
|
587 | $data = from_json $data; |
|
|
588 | |
|
|
589 | for (@$data) { |
|
|
590 | my ($name, $args) = @$_; |
|
|
591 | |
|
|
592 | $obj->attach ($name, %{$args || {} }); |
|
|
593 | } |
|
|
594 | }, |
|
|
595 | on_reattach => \&reattach, |
508 | on_clone => sub { |
596 | on_clone => sub { |
509 | my ($src, $dst) = @_; |
597 | my ($src, $dst) = @_; |
510 | |
598 | |
511 | @{$dst->registry} = @{$src->registry}; |
599 | @{$dst->registry} = @{$src->registry}; |
512 | |
600 | |
… | |
… | |
514 | |
602 | |
515 | %{$dst->{_attachment}} = %{$src->{_attachment}} |
603 | %{$dst->{_attachment}} = %{$src->{_attachment}} |
516 | if exists $src->{_attachment}; |
604 | if exists $src->{_attachment}; |
517 | }, |
605 | }, |
518 | ); |
606 | ); |
|
|
607 | |
|
|
608 | sub object_freezer_save { |
|
|
609 | my ($filename, $rdata, $objs) = @_; |
|
|
610 | |
|
|
611 | sync_job { |
|
|
612 | if (length $$rdata) { |
|
|
613 | warn sprintf "saving %s (%d,%d)\n", |
|
|
614 | $filename, length $$rdata, scalar @$objs; |
|
|
615 | |
|
|
616 | if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { |
|
|
617 | chmod SAVE_MODE, $fh; |
|
|
618 | aio_write $fh, 0, (length $$rdata), $$rdata, 0; |
|
|
619 | aio_fsync $fh; |
|
|
620 | close $fh; |
|
|
621 | |
|
|
622 | if (@$objs) { |
|
|
623 | if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) { |
|
|
624 | chmod SAVE_MODE, $fh; |
|
|
625 | my $data = Storable::nfreeze { version => 1, objs => $objs }; |
|
|
626 | aio_write $fh, 0, (length $data), $data, 0; |
|
|
627 | aio_fsync $fh; |
|
|
628 | close $fh; |
|
|
629 | aio_rename "$filename.pst~", "$filename.pst"; |
|
|
630 | } |
|
|
631 | } else { |
|
|
632 | aio_unlink "$filename.pst"; |
|
|
633 | } |
|
|
634 | |
|
|
635 | aio_rename "$filename~", $filename; |
|
|
636 | } else { |
|
|
637 | warn "FATAL: $filename~: $!\n"; |
|
|
638 | } |
|
|
639 | } else { |
|
|
640 | aio_unlink $filename; |
|
|
641 | aio_unlink "$filename.pst"; |
|
|
642 | } |
|
|
643 | } |
|
|
644 | } |
|
|
645 | |
|
|
646 | sub object_freezer_as_string { |
|
|
647 | my ($rdata, $objs) = @_; |
|
|
648 | |
|
|
649 | use Data::Dumper; |
|
|
650 | |
|
|
651 | $$rdata . Dumper $objs |
|
|
652 | } |
|
|
653 | |
|
|
654 | sub object_thawer_load { |
|
|
655 | my ($filename) = @_; |
|
|
656 | |
|
|
657 | my ($data, $av); |
|
|
658 | |
|
|
659 | (aio_load $filename, $data) >= 0 |
|
|
660 | or return; |
|
|
661 | |
|
|
662 | unless (aio_stat "$filename.pst") { |
|
|
663 | (aio_load "$filename.pst", $av) >= 0 |
|
|
664 | or return; |
|
|
665 | $av = eval { (Storable::thaw <$av>)->{objs} }; |
|
|
666 | } |
|
|
667 | |
|
|
668 | return ($data, $av); |
|
|
669 | } |
519 | |
670 | |
520 | ############################################################################# |
671 | ############################################################################# |
521 | # command handling &c |
672 | # command handling &c |
522 | |
673 | |
523 | =item cf::register_command $name => \&callback($ob,$args); |
674 | =item cf::register_command $name => \&callback($ob,$args); |
… | |
… | |
712 | |
863 | |
713 | =head2 CORE EXTENSIONS |
864 | =head2 CORE EXTENSIONS |
714 | |
865 | |
715 | Functions and methods that extend core crossfire objects. |
866 | Functions and methods that extend core crossfire objects. |
716 | |
867 | |
|
|
868 | =head3 cf::player |
|
|
869 | |
717 | =over 4 |
870 | =over 4 |
718 | |
871 | |
719 | =item cf::player::exists $login |
872 | =item cf::player::exists $login |
720 | |
873 | |
721 | Returns true when the given account exists. |
874 | Returns true when the given account exists. |
… | |
… | |
725 | sub cf::player::exists($) { |
878 | sub cf::player::exists($) { |
726 | cf::player::find $_[0] |
879 | cf::player::find $_[0] |
727 | or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; |
880 | or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; |
728 | } |
881 | } |
729 | |
882 | |
|
|
883 | =item $player->ext_reply ($msgid, $msgtype, %msg) |
|
|
884 | |
|
|
885 | Sends an ext reply to the player. |
|
|
886 | |
|
|
887 | =cut |
|
|
888 | |
|
|
889 | sub cf::player::ext_reply($$$%) { |
|
|
890 | my ($self, $id, %msg) = @_; |
|
|
891 | |
|
|
892 | $msg{msgid} = $id; |
|
|
893 | |
|
|
894 | $self->send ("ext " . to_json \%msg); |
|
|
895 | } |
|
|
896 | |
|
|
897 | =back |
|
|
898 | |
|
|
899 | =head3 cf::object::player |
|
|
900 | |
|
|
901 | =over 4 |
|
|
902 | |
730 | =item $player_object->reply ($npc, $msg[, $flags]) |
903 | =item $player_object->reply ($npc, $msg[, $flags]) |
731 | |
904 | |
732 | Sends a message to the player, as if the npc C<$npc> replied. C<$npc> |
905 | Sends a message to the player, as if the npc C<$npc> replied. C<$npc> |
733 | can be C<undef>. Does the right thing when the player is currently in a |
906 | can be C<undef>. Does the right thing when the player is currently in a |
734 | dialogue with the given NPC character. |
907 | dialogue with the given NPC character. |
735 | |
908 | |
736 | =cut |
909 | =cut |
737 | |
910 | |
738 | # rough implementation of a future "reply" method that works |
911 | # rough implementation of a future "reply" method that works |
739 | # with dialog boxes. |
912 | # with dialog boxes. |
|
|
913 | #TODO: the first argument must go, split into a $npc->reply_to ( method |
740 | sub cf::object::player::reply($$$;$) { |
914 | sub cf::object::player::reply($$$;$) { |
741 | my ($self, $npc, $msg, $flags) = @_; |
915 | my ($self, $npc, $msg, $flags) = @_; |
742 | |
916 | |
743 | $flags = cf::NDI_BROWN | cf::NDI_UNIQUE unless @_ >= 4; |
917 | $flags = cf::NDI_BROWN | cf::NDI_UNIQUE unless @_ >= 4; |
744 | |
918 | |
… | |
… | |
748 | $msg = $npc->name . " says: $msg" if $npc; |
922 | $msg = $npc->name . " says: $msg" if $npc; |
749 | $self->message ($msg, $flags); |
923 | $self->message ($msg, $flags); |
750 | } |
924 | } |
751 | } |
925 | } |
752 | |
926 | |
753 | =item $player->ext_reply ($msgid, $msgtype, %msg) |
|
|
754 | |
|
|
755 | Sends an ext reply to the player. |
|
|
756 | |
|
|
757 | =cut |
|
|
758 | |
|
|
759 | sub cf::player::ext_reply($$$%) { |
|
|
760 | my ($self, $id, %msg) = @_; |
|
|
761 | |
|
|
762 | $msg{msgid} = $id; |
|
|
763 | |
|
|
764 | $self->send ("ext " . to_json \%msg); |
|
|
765 | } |
|
|
766 | |
|
|
767 | =item $player_object->may ("access") |
927 | =item $player_object->may ("access") |
768 | |
928 | |
769 | Returns wether the given player is authorized to access resource "access" |
929 | Returns wether the given player is authorized to access resource "access" |
770 | (e.g. "command_wizcast"). |
930 | (e.g. "command_wizcast"). |
771 | |
931 | |
… | |
… | |
778 | (ref $cf::CFG{"may_$access"} |
938 | (ref $cf::CFG{"may_$access"} |
779 | ? scalar grep $self->name eq $_, @{$cf::CFG{"may_$access"}} |
939 | ? scalar grep $self->name eq $_, @{$cf::CFG{"may_$access"}} |
780 | : $cf::CFG{"may_$access"}) |
940 | : $cf::CFG{"may_$access"}) |
781 | } |
941 | } |
782 | |
942 | |
783 | =cut |
943 | =head3 cf::client |
784 | |
944 | |
785 | ############################################################################# |
945 | =over 4 |
|
|
946 | |
|
|
947 | =item $client->send_drawinfo ($text, $flags) |
|
|
948 | |
|
|
949 | Sends a drawinfo packet to the client. Circumvents output buffering so |
|
|
950 | should not be used under normal circumstances. |
|
|
951 | |
|
|
952 | =cut |
|
|
953 | |
|
|
954 | sub cf::client::send_drawinfo { |
|
|
955 | my ($self, $text, $flags) = @_; |
|
|
956 | |
|
|
957 | utf8::encode $text; |
|
|
958 | $self->send_packet (sprintf "drawinfo %d %s", $flags, $text); |
|
|
959 | } |
|
|
960 | |
|
|
961 | |
|
|
962 | =item $success = $client->query ($flags, "text", \&cb) |
|
|
963 | |
|
|
964 | Queues a query to the client, calling the given callback with |
|
|
965 | the reply text on a reply. flags can be C<cf::CS_QUERY_YESNO>, |
|
|
966 | C<cf::CS_QUERY_SINGLECHAR> or C<cf::CS_QUERY_HIDEINPUT> or C<0>. |
|
|
967 | |
|
|
968 | Queries can fail, so check the return code. Or don't, as queries will become |
|
|
969 | reliable at some point in the future. |
|
|
970 | |
|
|
971 | =cut |
|
|
972 | |
|
|
973 | sub cf::client::query { |
|
|
974 | my ($self, $flags, $text, $cb) = @_; |
|
|
975 | |
|
|
976 | return unless $self->state == ST_PLAYING |
|
|
977 | || $self->state == ST_SETUP |
|
|
978 | || $self->state == ST_CUSTOM; |
|
|
979 | |
|
|
980 | $self->state (ST_CUSTOM); |
|
|
981 | |
|
|
982 | utf8::encode $text; |
|
|
983 | push @{ $self->{query_queue} }, [(sprintf "query %d %s", $flags, $text), $cb]; |
|
|
984 | |
|
|
985 | $self->send_packet ($self->{query_queue}[0][0]) |
|
|
986 | if @{ $self->{query_queue} } == 1; |
|
|
987 | } |
|
|
988 | |
|
|
989 | cf::client->attach ( |
|
|
990 | on_reply => sub { |
|
|
991 | my ($ns, $msg) = @_; |
|
|
992 | |
|
|
993 | # this weird shuffling is so that direct followup queries |
|
|
994 | # get handled first |
|
|
995 | my $queue = delete $ns->{query_queue}; |
|
|
996 | |
|
|
997 | (shift @$queue)->[1]->($msg); |
|
|
998 | |
|
|
999 | push @{ $ns->{query_queue} }, @$queue; |
|
|
1000 | |
|
|
1001 | if (@{ $ns->{query_queue} } == @$queue) { |
|
|
1002 | if (@$queue) { |
|
|
1003 | $ns->send_packet ($ns->{query_queue}[0][0]); |
|
|
1004 | } else { |
|
|
1005 | $ns->state (ST_PLAYING) if $ns->state == ST_CUSTOM; |
|
|
1006 | } |
|
|
1007 | } |
|
|
1008 | }, |
|
|
1009 | ); |
|
|
1010 | |
|
|
1011 | =item $client->coro (\&cb) |
|
|
1012 | |
|
|
1013 | Create a new coroutine, running the specified callback. The coroutine will |
|
|
1014 | be automatically cancelled when the client gets destroyed (e.g. on logout, |
|
|
1015 | or loss of connection). |
|
|
1016 | |
|
|
1017 | =cut |
|
|
1018 | |
|
|
1019 | sub cf::client::coro { |
|
|
1020 | my ($self, $cb) = @_; |
|
|
1021 | |
|
|
1022 | my $coro; $coro = async { |
|
|
1023 | eval { |
|
|
1024 | $cb->(); |
|
|
1025 | }; |
|
|
1026 | warn $@ if $@; |
|
|
1027 | }; |
|
|
1028 | |
|
|
1029 | $coro->on_destroy (sub { |
|
|
1030 | delete $self->{_coro}{$coro+0}; |
|
|
1031 | }); |
|
|
1032 | |
|
|
1033 | $self->{_coro}{$coro+0} = $coro; |
|
|
1034 | |
|
|
1035 | $coro |
|
|
1036 | } |
|
|
1037 | |
|
|
1038 | cf::client->attach ( |
|
|
1039 | on_destroy => sub { |
|
|
1040 | my ($ns) = @_; |
|
|
1041 | |
|
|
1042 | $_->cancel for values %{ (delete $ns->{_coro}) || {} }; |
|
|
1043 | }, |
|
|
1044 | ); |
|
|
1045 | |
|
|
1046 | =back |
|
|
1047 | |
786 | |
1048 | |
787 | =head2 SAFE SCRIPTING |
1049 | =head2 SAFE SCRIPTING |
788 | |
1050 | |
789 | Functions that provide a safe environment to compile and execute |
1051 | Functions that provide a safe environment to compile and execute |
790 | snippets of perl code without them endangering the safety of the server |
1052 | snippets of perl code without them endangering the safety of the server |
… | |
… | |
1019 | } |
1281 | } |
1020 | |
1282 | |
1021 | ############################################################################# |
1283 | ############################################################################# |
1022 | # initialisation |
1284 | # initialisation |
1023 | |
1285 | |
1024 | sub _perl_reload(&) { |
1286 | sub perl_reload() { |
1025 | my ($msg) = @_; |
1287 | # can/must only be called in main |
|
|
1288 | if ($Coro::current != $Coro::main) { |
|
|
1289 | warn "can only reload from main coroutine\n"; |
|
|
1290 | return; |
|
|
1291 | } |
1026 | |
1292 | |
1027 | $msg->("reloading..."); |
1293 | warn "reloading..."; |
|
|
1294 | |
|
|
1295 | local $FREEZE = 1; |
|
|
1296 | cf::emergency_save; |
1028 | |
1297 | |
1029 | eval { |
1298 | eval { |
|
|
1299 | # if anything goes wrong in here, we should simply crash as we already saved |
|
|
1300 | |
1030 | # cancel all watchers |
1301 | # cancel all watchers |
1031 | for (Event::all_watchers) { |
1302 | for (Event::all_watchers) { |
1032 | $_->cancel if $_->data & WF_AUTOCANCEL; |
1303 | $_->cancel if $_->data & WF_AUTOCANCEL; |
1033 | } |
1304 | } |
1034 | |
1305 | |
|
|
1306 | # cancel all extension coros |
|
|
1307 | $_->cancel for values %EXT_CORO; |
|
|
1308 | %EXT_CORO = (); |
|
|
1309 | |
1035 | # unload all extensions |
1310 | # unload all extensions |
1036 | for (@exts) { |
1311 | for (@exts) { |
1037 | $msg->("unloading <$_>"); |
1312 | warn "unloading <$_>"; |
1038 | unload_extension $_; |
1313 | unload_extension $_; |
1039 | } |
1314 | } |
1040 | |
1315 | |
1041 | # unload all modules loaded from $LIBDIR |
1316 | # unload all modules loaded from $LIBDIR |
1042 | while (my ($k, $v) = each %INC) { |
1317 | while (my ($k, $v) = each %INC) { |
1043 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
1318 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
1044 | |
1319 | |
1045 | $msg->("removing <$k>"); |
1320 | warn "removing <$k>"; |
1046 | delete $INC{$k}; |
1321 | delete $INC{$k}; |
1047 | |
1322 | |
1048 | $k =~ s/\.pm$//; |
1323 | $k =~ s/\.pm$//; |
1049 | $k =~ s/\//::/g; |
1324 | $k =~ s/\//::/g; |
1050 | |
1325 | |
… | |
… | |
1055 | Symbol::delete_package $k; |
1330 | Symbol::delete_package $k; |
1056 | } |
1331 | } |
1057 | |
1332 | |
1058 | # sync database to disk |
1333 | # sync database to disk |
1059 | cf::db_sync; |
1334 | cf::db_sync; |
|
|
1335 | IO::AIO::flush; |
1060 | |
1336 | |
1061 | # get rid of safe::, as good as possible |
1337 | # get rid of safe::, as good as possible |
1062 | Symbol::delete_package "safe::$_" |
1338 | Symbol::delete_package "safe::$_" |
1063 | for qw(cf::object cf::object::player cf::player cf::map cf::party cf::region); |
1339 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
1064 | |
1340 | |
1065 | # remove register_script_function callbacks |
1341 | # remove register_script_function callbacks |
1066 | # TODO |
1342 | # TODO |
1067 | |
1343 | |
1068 | # unload cf.pm "a bit" |
1344 | # unload cf.pm "a bit" |
… | |
… | |
1071 | # don't, removes xs symbols, too, |
1347 | # don't, removes xs symbols, too, |
1072 | # and global variables created in xs |
1348 | # and global variables created in xs |
1073 | #Symbol::delete_package __PACKAGE__; |
1349 | #Symbol::delete_package __PACKAGE__; |
1074 | |
1350 | |
1075 | # reload cf.pm |
1351 | # reload cf.pm |
1076 | $msg->("reloading cf.pm"); |
1352 | warn "reloading cf.pm"; |
1077 | require cf; |
1353 | require cf; |
|
|
1354 | cf::_connect_to_perl; # nominally unnecessary, but cannot hurt |
1078 | |
1355 | |
1079 | # load config and database again |
1356 | # load config and database again |
1080 | cf::cfg_load; |
1357 | cf::cfg_load; |
1081 | cf::db_load; |
1358 | cf::db_load; |
1082 | |
1359 | |
1083 | # load extensions |
1360 | # load extensions |
1084 | $msg->("load extensions"); |
1361 | warn "load extensions"; |
1085 | cf::load_extensions; |
1362 | cf::load_extensions; |
1086 | |
1363 | |
1087 | # reattach attachments to objects |
1364 | # reattach attachments to objects |
1088 | $msg->("reattach"); |
1365 | warn "reattach"; |
1089 | _global_reattach; |
1366 | _global_reattach; |
1090 | }; |
1367 | }; |
1091 | $msg->($@) if $@; |
|
|
1092 | |
1368 | |
1093 | $msg->("reloaded"); |
1369 | if ($@) { |
|
|
1370 | warn $@; |
|
|
1371 | warn "error while reloading, exiting."; |
|
|
1372 | exit 1; |
|
|
1373 | } |
|
|
1374 | |
|
|
1375 | warn "reloaded successfully"; |
1094 | }; |
1376 | }; |
1095 | |
|
|
1096 | sub perl_reload() { |
|
|
1097 | _perl_reload { |
|
|
1098 | warn $_[0]; |
|
|
1099 | print "$_[0]\n"; |
|
|
1100 | }; |
|
|
1101 | } |
|
|
1102 | |
1377 | |
1103 | register "<global>", __PACKAGE__; |
1378 | register "<global>", __PACKAGE__; |
1104 | |
1379 | |
1105 | register_command "perl-reload" => sub { |
1380 | register_command "perl-reload" => sub { |
1106 | my ($who, $arg) = @_; |
1381 | my ($who, $arg) = @_; |
1107 | |
1382 | |
1108 | if ($who->flag (FLAG_WIZ)) { |
1383 | if ($who->flag (FLAG_WIZ)) { |
|
|
1384 | $who->message ("start of reload."); |
1109 | _perl_reload { |
1385 | perl_reload; |
1110 | warn $_[0]; |
1386 | $who->message ("end of reload."); |
1111 | $who->message ($_[0]); |
|
|
1112 | }; |
|
|
1113 | } |
1387 | } |
1114 | }; |
1388 | }; |
1115 | |
1389 | |
1116 | unshift @INC, $LIBDIR; |
1390 | unshift @INC, $LIBDIR; |
1117 | |
1391 | |
1118 | $TICK_WATCHER = Event->timer ( |
1392 | $TICK_WATCHER = Event->timer ( |
|
|
1393 | reentrant => 0, |
1119 | prio => 0, |
1394 | prio => 0, |
1120 | at => $NEXT_TICK || 1, |
1395 | at => $NEXT_TICK || $TICK, |
1121 | data => WF_AUTOCANCEL, |
1396 | data => WF_AUTOCANCEL, |
1122 | cb => sub { |
1397 | cb => sub { |
|
|
1398 | unless ($FREEZE) { |
1123 | cf::server_tick; # one server iteration |
1399 | cf::server_tick; # one server iteration |
|
|
1400 | $RUNTIME += $TICK; |
|
|
1401 | } |
1124 | |
1402 | |
1125 | my $NOW = Event::time; |
|
|
1126 | $NEXT_TICK += $TICK; |
1403 | $NEXT_TICK += $TICK; |
1127 | |
1404 | |
1128 | # if we are delayed by four ticks or more, skip them all |
1405 | # if we are delayed by four ticks or more, skip them all |
1129 | $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4; |
1406 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
1130 | |
1407 | |
1131 | $TICK_WATCHER->at ($NEXT_TICK); |
1408 | $TICK_WATCHER->at ($NEXT_TICK); |
1132 | $TICK_WATCHER->start; |
1409 | $TICK_WATCHER->start; |
1133 | }, |
1410 | }, |
1134 | ); |
1411 | ); |
… | |
… | |
1139 | poll => 'r', |
1416 | poll => 'r', |
1140 | prio => 5, |
1417 | prio => 5, |
1141 | data => WF_AUTOCANCEL, |
1418 | data => WF_AUTOCANCEL, |
1142 | cb => \&IO::AIO::poll_cb); |
1419 | cb => \&IO::AIO::poll_cb); |
1143 | |
1420 | |
|
|
1421 | # we must not ever block the main coroutine |
|
|
1422 | $Coro::idle = sub { |
|
|
1423 | #Carp::cluck "FATAL: Coro::idle was called, major BUG\n";#d# |
|
|
1424 | warn "FATAL: Coro::idle was called, major BUG\n"; |
|
|
1425 | (Coro::unblock_sub { |
|
|
1426 | Event::one_event; |
|
|
1427 | })->(); |
|
|
1428 | }; |
|
|
1429 | |
1144 | 1 |
1430 | 1 |
1145 | |
1431 | |