… | |
… | |
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; |
… | |
… | |
71 | mkdir cf::localdir . "/" . cf::uniquedir; |
71 | mkdir cf::localdir . "/" . cf::uniquedir; |
72 | mkdir $RANDOM_MAPS; |
72 | mkdir $RANDOM_MAPS; |
73 | |
73 | |
74 | # a special map that is always available |
74 | # a special map that is always available |
75 | our $LINK_MAP; |
75 | our $LINK_MAP; |
76 | |
76 | our $EMERGENCY_POSITION; |
77 | our $EMERGENCY_POSITION = $cf::CFG{emergency_position} || ["/world/world_105_115", 5, 37]; |
|
|
78 | |
77 | |
79 | ############################################################################# |
78 | ############################################################################# |
80 | |
79 | |
81 | =head2 GLOBAL VARIABLES |
80 | =head2 GLOBAL VARIABLES |
82 | |
81 | |
… | |
… | |
245 | # wake up all waiters, to be on the safe side |
244 | # wake up all waiters, to be on the safe side |
246 | $_->ready for @{ delete $LOCK{$key} }; |
245 | $_->ready for @{ delete $LOCK{$key} }; |
247 | } |
246 | } |
248 | } |
247 | } |
249 | |
248 | |
|
|
249 | =item cf::async { BLOCK } |
|
|
250 | |
|
|
251 | Like C<Coro::async>, but runs the given BLOCK in an eval and only logs the |
|
|
252 | error instead of exiting the server in case of a problem. |
|
|
253 | |
|
|
254 | =cut |
|
|
255 | |
|
|
256 | sub async(&) { |
|
|
257 | my ($cb) = @_; |
|
|
258 | |
|
|
259 | Coro::async { |
|
|
260 | eval { $cb->() }; |
|
|
261 | warn $@ if $@; |
|
|
262 | } |
|
|
263 | } |
|
|
264 | |
250 | =item cf::sync_job { BLOCK } |
265 | =item cf::sync_job { BLOCK } |
251 | |
266 | |
252 | The design of crossfire+ requires that the main coro ($Coro::main) is |
267 | 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 |
268 | 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. |
269 | reentrant. Thus "blocking" it by e.g. waiting for I/O is not acceptable. |
… | |
… | |
301 | =cut |
316 | =cut |
302 | |
317 | |
303 | sub coro(&) { |
318 | sub coro(&) { |
304 | my $cb = shift; |
319 | my $cb = shift; |
305 | |
320 | |
306 | my $coro; $coro = async { |
321 | my $coro = &cf::async ($cb); |
307 | eval { |
|
|
308 | $cb->(); |
|
|
309 | }; |
|
|
310 | warn $@ if $@; |
|
|
311 | }; |
|
|
312 | |
322 | |
313 | $coro->on_destroy (sub { |
323 | $coro->on_destroy (sub { |
314 | delete $EXT_CORO{$coro+0}; |
324 | delete $EXT_CORO{$coro+0}; |
315 | }); |
325 | }); |
316 | $EXT_CORO{$coro+0} = $coro; |
326 | $EXT_CORO{$coro+0} = $coro; |
… | |
… | |
1311 | } |
1321 | } |
1312 | |
1322 | |
1313 | sub swap_out { |
1323 | sub swap_out { |
1314 | my ($self) = @_; |
1324 | my ($self) = @_; |
1315 | |
1325 | |
|
|
1326 | # save first because save cedes |
|
|
1327 | $self->save; |
|
|
1328 | |
1316 | return if $self->players; |
1329 | return if $self->players; |
1317 | return if $self->in_memory != cf::MAP_IN_MEMORY; |
1330 | return if $self->in_memory != cf::MAP_IN_MEMORY; |
1318 | return if $self->{deny_save}; |
1331 | return if $self->{deny_save}; |
1319 | |
1332 | |
1320 | $self->save; |
|
|
1321 | $self->clear; |
1333 | $self->clear; |
1322 | $self->in_memory (cf::MAP_SWAPPED); |
1334 | $self->in_memory (cf::MAP_SWAPPED); |
1323 | } |
1335 | } |
1324 | |
1336 | |
1325 | sub reset_at { |
1337 | sub reset_at { |
… | |
… | |
1538 | |
1550 | |
1539 | # try to abort aborted map switching on player login :) |
1551 | # try to abort aborted map switching on player login :) |
1540 | # should happen only on crashes |
1552 | # should happen only on crashes |
1541 | if ($pl->ob->{_link_pos}) { |
1553 | if ($pl->ob->{_link_pos}) { |
1542 | $pl->ob->enter_link; |
1554 | $pl->ob->enter_link; |
1543 | Coro::async { |
1555 | cf::async { |
1544 | # we need this sleep as the login has a concurrent enter_exit running |
1556 | # 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 |
1557 | # and this sleep increases chances of the player not ending up in scorn |
1546 | Coro::Timer::sleep 1; |
1558 | Coro::Timer::sleep 1; |
1547 | $pl->ob->leave_link; |
1559 | $pl->ob->leave_link; |
1548 | }; |
1560 | }; |
… | |
… | |
1557 | sub cf::object::player::goto_map { |
1569 | sub cf::object::player::goto_map { |
1558 | my ($self, $path, $x, $y) = @_; |
1570 | my ($self, $path, $x, $y) = @_; |
1559 | |
1571 | |
1560 | $self->enter_link; |
1572 | $self->enter_link; |
1561 | |
1573 | |
1562 | (Coro::async { |
1574 | (cf::async { |
1563 | $path = new cf::path $path; |
1575 | $path = new cf::path $path; |
1564 | |
1576 | |
1565 | my $map = cf::map::find_map $path->as_string; |
1577 | my $map = cf::map::find_map $path->as_string; |
1566 | $map = $map->customise_for ($self) if $map; |
1578 | $map = $map->customise_for ($self) if $map; |
1567 | |
1579 | |
… | |
… | |
1629 | |
1641 | |
1630 | return unless $self->type == cf::PLAYER; |
1642 | return unless $self->type == cf::PLAYER; |
1631 | |
1643 | |
1632 | $self->enter_link; |
1644 | $self->enter_link; |
1633 | |
1645 | |
1634 | (Coro::async { |
1646 | (cf::async { |
1635 | unless (eval { |
1647 | unless (eval { |
1636 | |
|
|
1637 | prepare_random_map $exit |
1648 | prepare_random_map $exit |
1638 | if $exit->slaying eq "/!"; |
1649 | if $exit->slaying eq "/!"; |
1639 | |
1650 | |
1640 | my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path; |
1651 | my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path; |
1641 | $self->goto_map ($path, $exit->stats->hp, $exit->stats->sp); |
1652 | $self->goto_map ($path, $exit->stats->hp, $exit->stats->sp); |
… | |
… | |
1703 | on_reply => sub { |
1714 | on_reply => sub { |
1704 | my ($ns, $msg) = @_; |
1715 | my ($ns, $msg) = @_; |
1705 | |
1716 | |
1706 | # this weird shuffling is so that direct followup queries |
1717 | # this weird shuffling is so that direct followup queries |
1707 | # get handled first |
1718 | # get handled first |
1708 | my $queue = delete $ns->{query_queue}; |
1719 | my $queue = delete $ns->{query_queue} |
|
|
1720 | or return; # be conservative, not sure how that can happen, but we saw a crash here |
1709 | |
1721 | |
1710 | (shift @$queue)->[1]->($msg); |
1722 | (shift @$queue)->[1]->($msg); |
1711 | |
1723 | |
1712 | push @{ $ns->{query_queue} }, @$queue; |
1724 | push @{ $ns->{query_queue} }, @$queue; |
1713 | |
1725 | |
… | |
… | |
1730 | =cut |
1742 | =cut |
1731 | |
1743 | |
1732 | sub cf::client::coro { |
1744 | sub cf::client::coro { |
1733 | my ($self, $cb) = @_; |
1745 | my ($self, $cb) = @_; |
1734 | |
1746 | |
1735 | my $coro; $coro = async { |
1747 | my $coro = &cf::async ($cb); |
1736 | eval { |
|
|
1737 | $cb->(); |
|
|
1738 | }; |
|
|
1739 | warn $@ if $@; |
|
|
1740 | }; |
|
|
1741 | |
1748 | |
1742 | $coro->on_destroy (sub { |
1749 | $coro->on_destroy (sub { |
1743 | delete $self->{_coro}{$coro+0}; |
1750 | delete $self->{_coro}{$coro+0}; |
1744 | }); |
1751 | }); |
1745 | |
1752 | |
… | |
… | |
1980 | open my $fh, "<:utf8", cf::confdir . "/config" |
1987 | open my $fh, "<:utf8", cf::confdir . "/config" |
1981 | or return; |
1988 | or return; |
1982 | |
1989 | |
1983 | local $/; |
1990 | local $/; |
1984 | *CFG = YAML::Syck::Load <$fh>; |
1991 | *CFG = YAML::Syck::Load <$fh>; |
|
|
1992 | |
|
|
1993 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_105_115", 5, 37]; |
|
|
1994 | |
|
|
1995 | if (exists $CFG{mlockall}) { |
|
|
1996 | eval { |
|
|
1997 | $CFG{mlockall} ? &mlockall : &munlockall |
|
|
1998 | and die "WARNING: m(un)lockall failed: $!\n"; |
|
|
1999 | }; |
|
|
2000 | warn $@ if $@; |
|
|
2001 | } |
1985 | } |
2002 | } |
1986 | |
2003 | |
1987 | sub main { |
2004 | sub main { |
1988 | # we must not ever block the main coroutine |
2005 | # we must not ever block the main coroutine |
1989 | local $Coro::idle = sub { |
2006 | local $Coro::idle = sub { |