ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/lib/cf.pm
(Generate patch)

Comparing deliantra/server/lib/cf.pm (file contents):
Revision 1.124 by root, Tue Jan 2 22:55:05 2007 UTC vs.
Revision 1.130 by root, Thu Jan 4 00:08:08 2007 UTC

8use Storable; 8use Storable;
9use Opcode; 9use Opcode;
10use Safe; 10use Safe;
11use Safe::Hole; 11use Safe::Hole;
12 12
13use Coro 3.3; 13use Coro 3.3 ();
14use Coro::Event; 14use Coro::Event;
15use Coro::Timer; 15use Coro::Timer;
16use Coro::Signal; 16use Coro::Signal;
17use Coro::Semaphore; 17use Coro::Semaphore;
18use Coro::AIO; 18use 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
252Like C<Coro::async>, but runs the given BLOCK in an eval and only logs the
253error instead of exiting the server in case of a problem.
254
255=cut
256
257sub 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
252The design of crossfire+ requires that the main coro ($Coro::main) is 268The design of crossfire+ requires that the main coro ($Coro::main) is
253always able to handle events or runnable, as crossfire+ is only partly 269always able to handle events or runnable, as crossfire+ is only partly
254reentrant. Thus "blocking" it by e.g. waiting for I/O is not acceptable. 270reentrant. Thus "blocking" it by e.g. waiting for I/O is not acceptable.
301=cut 317=cut
302 318
303sub coro(&) { 319sub 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;
1175 $map->{load_path} = $path; 1186 $map->{load_path} = $path;
1176 1187
1177 $map 1188 $map
1178} 1189}
1179 1190
1191sub find_map;
1180sub find_map { 1192sub find_map {
1181 my ($path, $origin) = @_; 1193 my ($path, $origin) = @_;
1182 1194
1183 #warn "find_map<$path,$origin>\n";#d# 1195 #warn "find_map<$path,$origin>\n";#d#
1184 1196
1220 $map->last_access ($cf::RUNTIME); 1232 $map->last_access ($cf::RUNTIME);
1221 1233
1222 if ($map->should_reset) { 1234 if ($map->should_reset) {
1223 $map->reset; 1235 $map->reset;
1224 undef $guard; 1236 undef $guard;
1225 $map = cf::map::find_map ($path) 1237 $map = find_map $path
1226 or return; 1238 or return;
1227 } 1239 }
1228 1240
1229 $cf::MAP{$key} = $map 1241 $cf::MAP{$key} = $map
1230 } 1242 }
1310} 1322}
1311 1323
1312sub swap_out { 1324sub swap_out {
1313 my ($self) = @_; 1325 my ($self) = @_;
1314 1326
1327 # save first because save cedes
1328 $self->save;
1329
1315 return if $self->players; 1330 return if $self->players;
1316 return if $self->in_memory != cf::MAP_IN_MEMORY; 1331 return if $self->in_memory != cf::MAP_IN_MEMORY;
1317 return if $self->{deny_save}; 1332 return if $self->{deny_save};
1318 1333
1319 $self->save;
1320 $self->clear; 1334 $self->clear;
1321 $self->in_memory (cf::MAP_SWAPPED); 1335 $self->in_memory (cf::MAP_SWAPPED);
1322} 1336}
1323 1337
1324sub reset_at { 1338sub reset_at {
1537 1551
1538 # try to abort aborted map switching on player login :) 1552 # try to abort aborted map switching on player login :)
1539 # should happen only on crashes 1553 # should happen only on crashes
1540 if ($pl->ob->{_link_pos}) { 1554 if ($pl->ob->{_link_pos}) {
1541 $pl->ob->enter_link; 1555 $pl->ob->enter_link;
1542 Coro::async { 1556 cf::async {
1543 # we need this sleep as the login has a concurrent enter_exit running 1557 # we need this sleep as the login has a concurrent enter_exit running
1544 # and this sleep increases chances of the player not ending up in scorn 1558 # and this sleep increases chances of the player not ending up in scorn
1545 Coro::Timer::sleep 1; 1559 Coro::Timer::sleep 1;
1546 $pl->ob->leave_link; 1560 $pl->ob->leave_link;
1547 }; 1561 };
1556sub cf::object::player::goto_map { 1570sub cf::object::player::goto_map {
1557 my ($self, $path, $x, $y) = @_; 1571 my ($self, $path, $x, $y) = @_;
1558 1572
1559 $self->enter_link; 1573 $self->enter_link;
1560 1574
1561 (Coro::async { 1575 (cf::async {
1562 $path = new cf::path $path; 1576 $path = new cf::path $path;
1563 1577
1564 my $map = cf::map::find_map $path->as_string; 1578 my $map = cf::map::find_map $path->as_string;
1565 $map = $map->customise_for ($self) if $map; 1579 $map = $map->customise_for ($self) if $map;
1566 1580
1628 1642
1629 return unless $self->type == cf::PLAYER; 1643 return unless $self->type == cf::PLAYER;
1630 1644
1631 $self->enter_link; 1645 $self->enter_link;
1632 1646
1633 (Coro::async { 1647 (cf::async {
1634 unless (eval { 1648 unless (eval {
1635
1636 prepare_random_map $exit 1649 prepare_random_map $exit
1637 if $exit->slaying eq "/!"; 1650 if $exit->slaying eq "/!";
1638 1651
1639 my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path; 1652 my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path;
1640 $self->goto_map ($path, $exit->stats->hp, $exit->stats->sp); 1653 $self->goto_map ($path, $exit->stats->hp, $exit->stats->sp);
1702 on_reply => sub { 1715 on_reply => sub {
1703 my ($ns, $msg) = @_; 1716 my ($ns, $msg) = @_;
1704 1717
1705 # this weird shuffling is so that direct followup queries 1718 # this weird shuffling is so that direct followup queries
1706 # get handled first 1719 # get handled first
1707 my $queue = delete $ns->{query_queue}; 1720 my $queue = delete $ns->{query_queue}
1721 or return; # be conservative, not sure how that can happen, but we saw a crash here
1708 1722
1709 (shift @$queue)->[1]->($msg); 1723 (shift @$queue)->[1]->($msg);
1710 1724
1711 push @{ $ns->{query_queue} }, @$queue; 1725 push @{ $ns->{query_queue} }, @$queue;
1712 1726
1729=cut 1743=cut
1730 1744
1731sub cf::client::coro { 1745sub cf::client::coro {
1732 my ($self, $cb) = @_; 1746 my ($self, $cb) = @_;
1733 1747
1734 my $coro; $coro = async { 1748 my $coro = &cf::async ($cb);
1735 eval {
1736 $cb->();
1737 };
1738 warn $@ if $@;
1739 };
1740 1749
1741 $coro->on_destroy (sub { 1750 $coro->on_destroy (sub {
1742 delete $self->{_coro}{$coro+0}; 1751 delete $self->{_coro}{$coro+0};
1743 }); 1752 });
1744 1753
2191 or warn "ERROR: unable to write runtime file: $!"; 2200 or warn "ERROR: unable to write runtime file: $!";
2192 })->(); 2201 })->();
2193 }, 2202 },
2194); 2203);
2195 2204
2205END { cf::emergency_save }
2206
21961 22071
2197 2208

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines