… | |
… | |
229 | my $guard = Coro::guard { $TICK_WATCHER->start }; |
229 | my $guard = Coro::guard { $TICK_WATCHER->start }; |
230 | $TICK_WATCHER->stop; |
230 | $TICK_WATCHER->stop; |
231 | $guard |
231 | $guard |
232 | } |
232 | } |
233 | |
233 | |
|
|
234 | =item cf::async { BLOCK } |
|
|
235 | |
|
|
236 | Currently the same as Coro::async_pool, meaning you cannot use |
|
|
237 | C<on_destroy>, C<join> or other gimmicks on these coroutines. The only |
|
|
238 | thing you are allowed to do is call C<prio> on it. |
|
|
239 | |
|
|
240 | =cut |
|
|
241 | |
|
|
242 | BEGIN { *async = \&Coro::async_pool } |
|
|
243 | |
234 | =item cf::sync_job { BLOCK } |
244 | =item cf::sync_job { BLOCK } |
235 | |
245 | |
236 | The design of crossfire+ requires that the main coro ($Coro::main) is |
246 | The design of crossfire+ requires that the main coro ($Coro::main) is |
237 | always able to handle events or runnable, as crossfire+ is only partly |
247 | always able to handle events or runnable, as crossfire+ is only partly |
238 | reentrant. Thus "blocking" it by e.g. waiting for I/O is not acceptable. |
248 | reentrant. Thus "blocking" it by e.g. waiting for I/O is not acceptable. |
… | |
… | |
256 | my $freeze_guard = freeze_mainloop; |
266 | my $freeze_guard = freeze_mainloop; |
257 | |
267 | |
258 | my $busy = 1; |
268 | my $busy = 1; |
259 | my @res; |
269 | my @res; |
260 | |
270 | |
261 | (Coro::async_pool { |
271 | (async { |
262 | @res = eval { $job->() }; |
272 | @res = eval { $job->() }; |
263 | warn $@ if $@; |
273 | warn $@ if $@; |
264 | undef $busy; |
274 | undef $busy; |
265 | })->prio (Coro::PRIO_MAX); |
275 | })->prio (Coro::PRIO_MAX); |
266 | |
276 | |
267 | while ($busy) { |
277 | while ($busy) { |
268 | unless (Coro::cede) { |
278 | Coro::cede or Event::one_event; |
269 | Coro::nready ? Event::one_event 0 : Event::one_event; |
|
|
270 | Coro::cede_notself unless Coro::cede; |
|
|
271 | } |
|
|
272 | } |
279 | } |
273 | |
280 | |
274 | wantarray ? @res : $res[0] |
281 | wantarray ? @res : $res[0] |
275 | } else { |
282 | } else { |
276 | # we are in another coroutine, how wonderful, everything just works |
283 | # we are in another coroutine, how wonderful, everything just works |
277 | |
284 | |
278 | $job->() |
285 | $job->() |
279 | } |
286 | } |
280 | } |
287 | } |
281 | |
288 | |
282 | =item $coro = cf::coro { BLOCK } |
289 | =item $coro = cf::async_ext { BLOCK } |
283 | |
290 | |
284 | Creates (and readies) and returns a new coro. This coro is automcatially |
291 | Like async, but this coro is automcatially being canceled when the |
285 | being canceled when the extension calling this is being unloaded. |
292 | extension calling this is being unloaded. |
286 | |
293 | |
287 | =cut |
294 | =cut |
288 | |
295 | |
289 | sub coro(&) { |
296 | sub async_ext(&) { |
290 | my $cb = shift; |
297 | my $cb = shift; |
291 | |
298 | |
292 | my $coro = &Coro::async_pool ($cb); |
299 | my $coro = &Coro::async ($cb); |
293 | |
300 | |
294 | $coro->on_destroy (sub { |
301 | $coro->on_destroy (sub { |
295 | delete $EXT_CORO{$coro+0}; |
302 | delete $EXT_CORO{$coro+0}; |
296 | }); |
303 | }); |
297 | $EXT_CORO{$coro+0} = $coro; |
304 | $EXT_CORO{$coro+0} = $coro; |
… | |
… | |
1270 | } |
1277 | } |
1271 | |
1278 | |
1272 | sub find_sync { |
1279 | sub find_sync { |
1273 | my ($path, $origin) = @_; |
1280 | my ($path, $origin) = @_; |
1274 | |
1281 | |
1275 | cf::sync_job { |
1282 | cf::sync_job { cf::map::find $path, $origin } |
1276 | my $map = cf::map::find $path, $origin; |
|
|
1277 | $map |
|
|
1278 | } |
|
|
1279 | } |
1283 | } |
1280 | |
1284 | |
1281 | sub do_load_sync { |
1285 | sub do_load_sync { |
1282 | my ($map) = @_; |
1286 | my ($map) = @_; |
1283 | |
1287 | |
… | |
… | |
1545 | # try to abort aborted map switching on player login :) |
1549 | # try to abort aborted map switching on player login :) |
1546 | # should happen only on crashes |
1550 | # should happen only on crashes |
1547 | if ($pl->ob->{_link_pos}) { |
1551 | if ($pl->ob->{_link_pos}) { |
1548 | |
1552 | |
1549 | $pl->ob->enter_link; |
1553 | $pl->ob->enter_link; |
1550 | (Coro::async_pool { |
1554 | (async { |
1551 | # we need this sleep as the login has a concurrent enter_exit running |
1555 | # we need this sleep as the login has a concurrent enter_exit running |
1552 | # and this sleep increases chances of the player not ending up in scorn |
1556 | # and this sleep increases chances of the player not ending up in scorn |
|
|
1557 | $pl->ob->reply (undef, |
|
|
1558 | "There was an internal problem at your last logout, " |
|
|
1559 | . "the server will try to bring you to your intended destination in a second.", |
|
|
1560 | cf::NDI_RED); |
1553 | Coro::Timer::sleep 1; |
1561 | Coro::Timer::sleep 1; |
1554 | $pl->ob->leave_link; |
1562 | $pl->ob->leave_link; |
1555 | })->prio (2); |
1563 | })->prio (2); |
1556 | } |
1564 | } |
1557 | }, |
1565 | }, |
… | |
… | |
1564 | sub cf::object::player::goto { |
1572 | sub cf::object::player::goto { |
1565 | my ($self, $path, $x, $y) = @_; |
1573 | my ($self, $path, $x, $y) = @_; |
1566 | |
1574 | |
1567 | $self->enter_link; |
1575 | $self->enter_link; |
1568 | |
1576 | |
1569 | (Coro::async_pool { |
1577 | (async { |
1570 | $path = new cf::path $path; |
1578 | $path = new cf::path $path; |
1571 | |
1579 | |
1572 | my $map = cf::map::find $path->as_string; |
1580 | my $map = cf::map::find $path->as_string; |
1573 | $map = $map->customise_for ($self) if $map; |
1581 | $map = $map->customise_for ($self) if $map; |
1574 | |
1582 | |
… | |
… | |
1636 | |
1644 | |
1637 | return unless $self->type == cf::PLAYER; |
1645 | return unless $self->type == cf::PLAYER; |
1638 | |
1646 | |
1639 | $self->enter_link; |
1647 | $self->enter_link; |
1640 | |
1648 | |
1641 | (Coro::async_pool { |
1649 | (async { |
1642 | $self->deactivate_recursive; # just to be sure |
1650 | $self->deactivate_recursive; # just to be sure |
1643 | unless (eval { |
1651 | unless (eval { |
1644 | prepare_random_map $exit |
1652 | prepare_random_map $exit |
1645 | if $exit->slaying eq "/!"; |
1653 | if $exit->slaying eq "/!"; |
1646 | |
1654 | |
… | |
… | |
1727 | } |
1735 | } |
1728 | } |
1736 | } |
1729 | }, |
1737 | }, |
1730 | ); |
1738 | ); |
1731 | |
1739 | |
1732 | =item $client->coro (\&cb) |
1740 | =item $client->async (\&cb) |
1733 | |
1741 | |
1734 | Create a new coroutine, running the specified callback. The coroutine will |
1742 | Create a new coroutine, running the specified callback. The coroutine will |
1735 | be automatically cancelled when the client gets destroyed (e.g. on logout, |
1743 | be automatically cancelled when the client gets destroyed (e.g. on logout, |
1736 | or loss of connection). |
1744 | or loss of connection). |
1737 | |
1745 | |
1738 | =cut |
1746 | =cut |
1739 | |
1747 | |
1740 | sub cf::client::coro { |
1748 | sub cf::client::async { |
1741 | my ($self, $cb) = @_; |
1749 | my ($self, $cb) = @_; |
1742 | |
1750 | |
1743 | my $coro = &Coro::async_pool ($cb); |
1751 | my $coro = &Coro::async ($cb); |
1744 | |
1752 | |
1745 | $coro->on_destroy (sub { |
1753 | $coro->on_destroy (sub { |
1746 | delete $self->{_coro}{$coro+0}; |
1754 | delete $self->{_coro}{$coro+0}; |
1747 | }); |
1755 | }); |
1748 | |
1756 | |
… | |
… | |
2002 | |
2010 | |
2003 | sub main { |
2011 | sub main { |
2004 | # we must not ever block the main coroutine |
2012 | # we must not ever block the main coroutine |
2005 | local $Coro::idle = sub { |
2013 | local $Coro::idle = sub { |
2006 | Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d# |
2014 | Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d# |
2007 | Coro::async_pool { Event::one_event }; |
2015 | async { Event::one_event }; |
2008 | }; |
2016 | }; |
2009 | |
2017 | |
2010 | cfg_load; |
2018 | cfg_load; |
2011 | db_load; |
2019 | db_load; |
2012 | load_extensions; |
2020 | load_extensions; |
… | |
… | |
2120 | $LINK_MAP->{path} = bless { path => "{link}" }, "cf::path"; |
2128 | $LINK_MAP->{path} = bless { path => "{link}" }, "cf::path"; |
2121 | $LINK_MAP->in_memory (MAP_IN_MEMORY); |
2129 | $LINK_MAP->in_memory (MAP_IN_MEMORY); |
2122 | |
2130 | |
2123 | # dirty hack because... archetypes are not yet loaded |
2131 | # dirty hack because... archetypes are not yet loaded |
2124 | Event->timer ( |
2132 | Event->timer ( |
2125 | after => 2, |
2133 | after => 10, |
2126 | cb => sub { |
2134 | cb => sub { |
2127 | $_[0]->w->cancel; |
2135 | $_[0]->w->cancel; |
2128 | |
2136 | |
2129 | # provide some exits "home" |
2137 | # provide some exits "home" |
2130 | my $exit = cf::object::new "exit"; |
2138 | my $exit = cf::object::new "exit"; |