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.138 by root, Fri Jan 5 17:07:17 2007 UTC vs.
Revision 1.141 by root, Fri Jan 5 20:08:53 2007 UTC

26use Event; $Event::Eval = 1; # no idea why this is required, but it is 26use Event; $Event::Eval = 1; # no idea why this is required, but it is
27 27
28# work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? 28# work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode?
29$YAML::Syck::ImplicitUnicode = 1; 29$YAML::Syck::ImplicitUnicode = 1;
30 30
31$Coro::main->prio (2); # run main coroutine ("the server") with very high priority 31$Coro::main->prio (Coro::PRIO_MAX); # run main coroutine ("the server") with very high priority
32 32
33sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload 33sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload
34 34
35our %COMMAND = (); 35our %COMMAND = ();
36our %COMMAND_TIME = (); 36our %COMMAND_TIME = ();
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
236Currently the same as Coro::async_pool, meaning you cannot use
237C<on_destroy>, C<join> or other gimmicks on these coroutines. The only
238thing you are allowed to do is call C<prio> on it.
239
240=cut
241
242BEGIN { *async = \&Coro::async_pool }
243
234=item cf::sync_job { BLOCK } 244=item cf::sync_job { BLOCK }
235 245
236The design of crossfire+ requires that the main coro ($Coro::main) is 246The design of crossfire+ requires that the main coro ($Coro::main) is
237always able to handle events or runnable, as crossfire+ is only partly 247always able to handle events or runnable, as crossfire+ is only partly
238reentrant. Thus "blocking" it by e.g. waiting for I/O is not acceptable. 248reentrant. 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 Coro::cede_notself; 278 Coro::cede or Event::one_event;
269 Event::one_event unless Coro::nready;
270 } 279 }
271 280
272 wantarray ? @res : $res[0] 281 wantarray ? @res : $res[0]
273 } else { 282 } else {
274 # we are in another coroutine, how wonderful, everything just works 283 # we are in another coroutine, how wonderful, everything just works
275 284
276 $job->() 285 $job->()
277 } 286 }
278} 287}
279 288
280=item $coro = cf::coro { BLOCK } 289=item $coro = cf::async_ext { BLOCK }
281 290
282Creates (and readies) and returns a new coro. This coro is automcatially 291Like async, but this coro is automcatially being canceled when the
283being canceled when the extension calling this is being unloaded. 292extension calling this is being unloaded.
284 293
285=cut 294=cut
286 295
287sub coro(&) { 296sub async_ext(&) {
288 my $cb = shift; 297 my $cb = shift;
289 298
290 my $coro = &Coro::async_pool ($cb); 299 my $coro = &Coro::async ($cb);
291 300
292 $coro->on_destroy (sub { 301 $coro->on_destroy (sub {
293 delete $EXT_CORO{$coro+0}; 302 delete $EXT_CORO{$coro+0};
294 }); 303 });
295 $EXT_CORO{$coro+0} = $coro; 304 $EXT_CORO{$coro+0} = $coro;
1538 my ($pl) = @_; 1547 my ($pl) = @_;
1539 1548
1540 # try to abort aborted map switching on player login :) 1549 # try to abort aborted map switching on player login :)
1541 # should happen only on crashes 1550 # should happen only on crashes
1542 if ($pl->ob->{_link_pos}) { 1551 if ($pl->ob->{_link_pos}) {
1552
1543 $pl->ob->enter_link; 1553 $pl->ob->enter_link;
1544 Coro::async_pool { 1554 (async {
1545 # 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
1546 # 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);
1547 Coro::Timer::sleep 1; 1561 Coro::Timer::sleep 1;
1548 $pl->ob->leave_link; 1562 $pl->ob->leave_link;
1549 }; 1563 })->prio (2);
1550 } 1564 }
1551 }, 1565 },
1552); 1566);
1553 1567
1554=item $player_object->goto ($path, $x, $y) 1568=item $player_object->goto ($path, $x, $y)
1558sub cf::object::player::goto { 1572sub cf::object::player::goto {
1559 my ($self, $path, $x, $y) = @_; 1573 my ($self, $path, $x, $y) = @_;
1560 1574
1561 $self->enter_link; 1575 $self->enter_link;
1562 1576
1563 (Coro::async_pool { 1577 (async {
1564 $path = new cf::path $path; 1578 $path = new cf::path $path;
1565 1579
1566 my $map = cf::map::find $path->as_string; 1580 my $map = cf::map::find $path->as_string;
1567 $map = $map->customise_for ($self) if $map; 1581 $map = $map->customise_for ($self) if $map;
1568 1582
1630 1644
1631 return unless $self->type == cf::PLAYER; 1645 return unless $self->type == cf::PLAYER;
1632 1646
1633 $self->enter_link; 1647 $self->enter_link;
1634 1648
1635 (Coro::async_pool { 1649 (async {
1636 $self->deactivate_recursive; # just to be sure 1650 $self->deactivate_recursive; # just to be sure
1637 unless (eval { 1651 unless (eval {
1638 prepare_random_map $exit 1652 prepare_random_map $exit
1639 if $exit->slaying eq "/!"; 1653 if $exit->slaying eq "/!";
1640 1654
1721 } 1735 }
1722 } 1736 }
1723 }, 1737 },
1724); 1738);
1725 1739
1726=item $client->coro (\&cb) 1740=item $client->async (\&cb)
1727 1741
1728Create a new coroutine, running the specified callback. The coroutine will 1742Create a new coroutine, running the specified callback. The coroutine will
1729be automatically cancelled when the client gets destroyed (e.g. on logout, 1743be automatically cancelled when the client gets destroyed (e.g. on logout,
1730or loss of connection). 1744or loss of connection).
1731 1745
1732=cut 1746=cut
1733 1747
1734sub cf::client::coro { 1748sub cf::client::async {
1735 my ($self, $cb) = @_; 1749 my ($self, $cb) = @_;
1736 1750
1737 my $coro = &Coro::async_pool ($cb); 1751 my $coro = &Coro::async ($cb);
1738 1752
1739 $coro->on_destroy (sub { 1753 $coro->on_destroy (sub {
1740 delete $self->{_coro}{$coro+0}; 1754 delete $self->{_coro}{$coro+0};
1741 }); 1755 });
1742 1756
1980 local $/; 1994 local $/;
1981 *CFG = YAML::Syck::Load <$fh>; 1995 *CFG = YAML::Syck::Load <$fh>;
1982 1996
1983 $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_105_115", 5, 37]; 1997 $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_105_115", 5, 37];
1984 1998
1999 $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset};
2000 $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset};
2001
1985 if (exists $CFG{mlockall}) { 2002 if (exists $CFG{mlockall}) {
1986 eval { 2003 eval {
1987 $CFG{mlockall} ? &mlockall : &munlockall 2004 $CFG{mlockall} ? &mlockall : &munlockall
1988 and die "WARNING: m(un)lockall failed: $!\n"; 2005 and die "WARNING: m(un)lockall failed: $!\n";
1989 }; 2006 };
1993 2010
1994sub main { 2011sub main {
1995 # we must not ever block the main coroutine 2012 # we must not ever block the main coroutine
1996 local $Coro::idle = sub { 2013 local $Coro::idle = sub {
1997 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#
1998 (Coro::unblock_sub {
1999 Event::one_event; 2015 async { Event::one_event };
2000 })->();
2001 }; 2016 };
2002 2017
2003 cfg_load; 2018 cfg_load;
2004 db_load; 2019 db_load;
2005 load_extensions; 2020 load_extensions;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines