… | |
… | |
8 | use Storable; |
8 | use Storable; |
9 | use Opcode; |
9 | use Opcode; |
10 | use Safe; |
10 | use Safe; |
11 | use Safe::Hole; |
11 | use Safe::Hole; |
12 | |
12 | |
13 | use Coro 3.3; |
13 | use Coro 3.3 (); |
14 | use Coro::Event; |
14 | use Coro::Event; |
15 | use Coro::Timer; |
15 | use Coro::Timer; |
16 | use Coro::Signal; |
16 | use Coro::Signal; |
17 | use Coro::Semaphore; |
17 | use Coro::Semaphore; |
18 | use Coro::AIO; |
18 | use Coro::AIO; |
… | |
… | |
245 | # wake up all waiters, to be on the safe side |
245 | # wake up all waiters, to be on the safe side |
246 | $_->ready for @{ delete $LOCK{$key} }; |
246 | $_->ready for @{ delete $LOCK{$key} }; |
247 | } |
247 | } |
248 | } |
248 | } |
249 | |
249 | |
|
|
250 | =item cf::async { BLOCK } |
|
|
251 | |
|
|
252 | Like C<Coro::async>, but runs the given BLOCK in an eval and only logs the |
|
|
253 | error instead of exiting the server in case of a problem. |
|
|
254 | |
|
|
255 | =cut |
|
|
256 | |
|
|
257 | sub async(&) { |
|
|
258 | my ($cb) = @_; |
|
|
259 | |
|
|
260 | Coro::async { |
|
|
261 | eval { $cb->() }; |
|
|
262 | warn $@ if $@; |
|
|
263 | } |
|
|
264 | } |
|
|
265 | |
250 | =item cf::sync_job { BLOCK } |
266 | =item cf::sync_job { BLOCK } |
251 | |
267 | |
252 | The design of crossfire+ requires that the main coro ($Coro::main) is |
268 | The design of crossfire+ requires that the main coro ($Coro::main) is |
253 | always able to handle events or runnable, as crossfire+ is only partly |
269 | always able to handle events or runnable, as crossfire+ is only partly |
254 | reentrant. Thus "blocking" it by e.g. waiting for I/O is not acceptable. |
270 | reentrant. Thus "blocking" it by e.g. waiting for I/O is not acceptable. |
… | |
… | |
301 | =cut |
317 | =cut |
302 | |
318 | |
303 | sub coro(&) { |
319 | sub coro(&) { |
304 | my $cb = shift; |
320 | my $cb = shift; |
305 | |
321 | |
306 | my $coro; $coro = async { |
322 | my $coro = &cf::async ($cb); |
307 | eval { |
|
|
308 | $cb->(); |
|
|
309 | }; |
|
|
310 | warn $@ if $@; |
|
|
311 | }; |
|
|
312 | |
323 | |
313 | $coro->on_destroy (sub { |
324 | $coro->on_destroy (sub { |
314 | delete $EXT_CORO{$coro+0}; |
325 | delete $EXT_CORO{$coro+0}; |
315 | }); |
326 | }); |
316 | $EXT_CORO{$coro+0} = $coro; |
327 | $EXT_CORO{$coro+0} = $coro; |
… | |
… | |
1538 | |
1549 | |
1539 | # try to abort aborted map switching on player login :) |
1550 | # try to abort aborted map switching on player login :) |
1540 | # should happen only on crashes |
1551 | # should happen only on crashes |
1541 | if ($pl->ob->{_link_pos}) { |
1552 | if ($pl->ob->{_link_pos}) { |
1542 | $pl->ob->enter_link; |
1553 | $pl->ob->enter_link; |
1543 | Coro::async { |
1554 | cf::async { |
1544 | # 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 |
1545 | # 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 |
1546 | Coro::Timer::sleep 1; |
1557 | Coro::Timer::sleep 1; |
1547 | $pl->ob->leave_link; |
1558 | $pl->ob->leave_link; |
1548 | }; |
1559 | }; |
… | |
… | |
1557 | sub cf::object::player::goto_map { |
1568 | sub cf::object::player::goto_map { |
1558 | my ($self, $path, $x, $y) = @_; |
1569 | my ($self, $path, $x, $y) = @_; |
1559 | |
1570 | |
1560 | $self->enter_link; |
1571 | $self->enter_link; |
1561 | |
1572 | |
1562 | (Coro::async { |
1573 | (cf::async { |
1563 | $path = new cf::path $path; |
1574 | $path = new cf::path $path; |
1564 | |
1575 | |
1565 | my $map = cf::map::find_map $path->as_string; |
1576 | my $map = cf::map::find_map $path->as_string; |
1566 | $map = $map->customise_for ($self) if $map; |
1577 | $map = $map->customise_for ($self) if $map; |
1567 | |
1578 | |
… | |
… | |
1629 | |
1640 | |
1630 | return unless $self->type == cf::PLAYER; |
1641 | return unless $self->type == cf::PLAYER; |
1631 | |
1642 | |
1632 | $self->enter_link; |
1643 | $self->enter_link; |
1633 | |
1644 | |
1634 | (Coro::async { |
1645 | (cf::async { |
1635 | unless (eval { |
1646 | unless (eval { |
1636 | |
|
|
1637 | prepare_random_map $exit |
1647 | prepare_random_map $exit |
1638 | if $exit->slaying eq "/!"; |
1648 | if $exit->slaying eq "/!"; |
1639 | |
1649 | |
1640 | my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path; |
1650 | my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path; |
1641 | $self->goto_map ($path, $exit->stats->hp, $exit->stats->sp); |
1651 | $self->goto_map ($path, $exit->stats->hp, $exit->stats->sp); |
… | |
… | |
1703 | on_reply => sub { |
1713 | on_reply => sub { |
1704 | my ($ns, $msg) = @_; |
1714 | my ($ns, $msg) = @_; |
1705 | |
1715 | |
1706 | # this weird shuffling is so that direct followup queries |
1716 | # this weird shuffling is so that direct followup queries |
1707 | # get handled first |
1717 | # get handled first |
1708 | my $queue = delete $ns->{query_queue}; |
1718 | my $queue = delete $ns->{query_queue} |
|
|
1719 | or return; # be conservative, not sure how that cna happen, but we saw a crash here |
1709 | |
1720 | |
1710 | (shift @$queue)->[1]->($msg); |
1721 | (shift @$queue)->[1]->($msg); |
1711 | |
1722 | |
1712 | push @{ $ns->{query_queue} }, @$queue; |
1723 | push @{ $ns->{query_queue} }, @$queue; |
1713 | |
1724 | |
… | |
… | |
1730 | =cut |
1741 | =cut |
1731 | |
1742 | |
1732 | sub cf::client::coro { |
1743 | sub cf::client::coro { |
1733 | my ($self, $cb) = @_; |
1744 | my ($self, $cb) = @_; |
1734 | |
1745 | |
1735 | my $coro; $coro = async { |
1746 | my $coro = &cf::async ($cb); |
1736 | eval { |
|
|
1737 | $cb->(); |
|
|
1738 | }; |
|
|
1739 | warn $@ if $@; |
|
|
1740 | }; |
|
|
1741 | |
1747 | |
1742 | $coro->on_destroy (sub { |
1748 | $coro->on_destroy (sub { |
1743 | delete $self->{_coro}{$coro+0}; |
1749 | delete $self->{_coro}{$coro+0}; |
1744 | }); |
1750 | }); |
1745 | |
1751 | |