--- deliantra/server/lib/cf.pm 2007/02/13 19:25:44 1.212 +++ deliantra/server/lib/cf.pm 2007/03/14 04:12:29 1.226 @@ -6,11 +6,13 @@ use Symbol; use List::Util; use Storable; +use Event; use Opcode; use Safe; use Safe::Hole; -use Coro 3.5 (); +use Coro 3.52 (); +use Coro::State; use Coro::Event; use Coro::Timer; use Coro::Signal; @@ -26,9 +28,9 @@ use Time::HiRes; use Compress::LZF; +Coro::State::cctx_stacksize 256000; # 1-2MB stack, for deep recursions in maze generator Compress::LZF::sfreeze_cr { }; # prime Compress::LZF so it does not use require later - -use Event; $Event::Eval = 1; # no idea why this is required, but it is +$Event::Eval = 1; # no idea why this is required, but it is sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload @@ -45,13 +47,14 @@ our %EXT_CORO = (); # coroutines bound to extensions our %EXT_MAP = (); # pluggable maps +our $RELOAD; # number of reloads so far our @EVENT; our $LIBDIR = datadir . "/ext"; our $TICK = MAX_TIME * 1e-6; our $TICK_WATCHER; our $AIO_POLL_WATCHER; -our $WRITE_RUNTIME_WATCHER; +our $NEXT_RUNTIME_WRITE; # when should the runtime file be written our $NEXT_TICK; our $NOW; our $USE_FSYNC = 1; # use fsync to write maps - default off @@ -275,9 +278,7 @@ my $guard = Coro::guard { $TICK_WATCHER->start; - $WRITE_RUNTIME_WATCHER->start; }; - $WRITE_RUNTIME_WATCHER->stop; $TICK_WATCHER->stop; $guard } @@ -358,6 +359,8 @@ } sub write_runtime { + my $guard = cf::lock_acquire "write_runtime"; + my $runtime = cf::localdir . "/runtime"; my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644 @@ -789,12 +792,6 @@ aio_unlink $filename; aio_unlink "$filename.pst"; } - - #d##TODO# nuke non .map-files if exist - if ($filename =~ s/\.map$//) { - aio_unlink $filename; - aio_unlink "$filename.pst"; - } } } @@ -811,9 +808,6 @@ my ($data, $av); - #d#TODO remove .map if file does not exist - aio_stat $filename and $filename =~ s/\.map$//; - (aio_load $filename, $data) >= 0 or return; @@ -1041,6 +1035,8 @@ sub quit_character { my ($pl) = @_; + my $name = $pl->ob->name; + $pl->{deny_save} = 1; $pl->password ("*"); # this should lock out the player until we nuked the dir @@ -1054,9 +1050,29 @@ aio_rename $path, $temp; delete $cf::PLAYER{$pl->ob->name}; $pl->destroy; + + my $prefix = qr<^~\Q$name\E/>; + + # nuke player maps + $cf::MAP{$_}->nuke for grep /$prefix/, keys %cf::MAP; + IO::AIO::aio_rmtree $temp; } +=item $pl->kick + +Kicks a player out of the game. This destroys the connection. + +=cut + +sub kick { + my ($pl, $kicker) = @_; + + $pl->invoke (cf::EVENT_PLAYER_KICK, $kicker); + $pl->killer ("kicked"); + $pl->ns->destroy; +} + =item cf::player::list_logins Returns am arrayref of all valid playernames in the system, can take a @@ -1309,11 +1325,6 @@ utf8::encode (my $save = $self->save_path); IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink $save; IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink "$save.pst"; - - #d#TODO remove .map and also nuke - $save =~ s/\.map// or return;#d# - IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink $save;#d# - IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink "$save.pst";#d# } sub load_header_from($) { @@ -1822,6 +1833,8 @@ return unless $self->contr->active; $self->activate_recursive; + + local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext $self->enter_map ($map, $x, $y); } @@ -1844,12 +1857,12 @@ if ($pl->ob->{_link_pos}) { $pl->ob->enter_link; (async { - # we need this sleep as the login has a concurrent enter_exit running - # and this sleep increases chances of the player not ending up in scorn $pl->ob->reply (undef, "There was an internal problem at your last logout, " . "the server will try to bring you to your intended destination in a second.", cf::NDI_RED); + # we need this sleep as the login has a concurrent enter_exit running + # and this sleep increases chances of the player not ending up in scorn Coro::Timer::sleep 1; $pl->ob->leave_link; })->prio (2); @@ -1984,7 +1997,7 @@ my ($self, $text, $flags) = @_; utf8::encode $text; - $self->send_packet (sprintf "drawinfo %d %s", $flags, $text); + $self->send_packet (sprintf "drawinfo %d %s", $flags || cf::NDI_BLACK, $text); } @@ -2258,9 +2271,54 @@ ############################################################################# # the server's init and main functions -sub load_resources { - load_regions sprintf "%s/%s/regions", cf::datadir, cf::mapdir +sub load_faces { + my $path = sprintf "%s/faces", cf::datadir; + + warn "loading faces from $path\n"; + + my $faces; + 0 < aio_load $path, $faces + or die "$path: $!"; + + Coro::cede; + $faces = Storable::thaw $faces; + Coro::cede; + + my $meta = delete $faces->{""}; + $meta->{version} == 1 + or cf::cleanup "$path: version mismatch, cannot proceed."; + + while (my ($face, $info) = each %$faces) { + my $idx = (cf::face::find $face) || cf::face::alloc $face; + cf::face::set $idx, $info->{visibility}, $info->{magicmap}; + cf::face::set_data $idx, 0, $info->{data32}, $info->{chksum32}; + Coro::cede; + } + + while (my ($face, $info) = each %$faces) { + next unless $info->{smooth}; + my $idx = cf::face::find $face + or next; + if (my $smooth = cf::face::find $info->{smooth}) { + cf::face::set_smooth $idx, $smooth; + } else { + warn "smooth face '$info->{smooth}' not found for face '$face'"; + } + Coro::cede; + } + + 1 +} + +sub reload_resources { + load_resource_file sprintf "%s/%s/regions", cf::datadir, cf::mapdir or die "unable to load regions file\n";#d# + load_faces + or die "unable to load faces\n";#d# +} + +sub init { + reload_resources; } sub cfg_load { @@ -2284,10 +2342,6 @@ } } -sub init { - load_resources; -} - sub main { # we must not ever block the main coroutine local $Coro::idle = sub { @@ -2374,10 +2428,10 @@ warn "entering sync_job"; - sync_job { - write_runtime; + cf::sync_job { + cf::write_runtime; # external watchdog should not bark cf::emergency_save; - write_runtime; + cf::write_runtime; # external watchdog should not bark warn "syncing database to disk"; BDB::db_env_txn_checkpoint $DB_ENV; @@ -2398,6 +2452,8 @@ warn "iterate..."; } + ++$RELOAD; + warn "cancelling all extension coros"; $_->cancel for values %EXT_CORO; %EXT_CORO = (); @@ -2464,12 +2520,14 @@ cf::load_extensions; warn "reattaching attachments to objects/players"; - _global_reattach; + _global_reattach; # objects, sockets warn "reattaching attachments to maps"; reattach $_ for values %MAP; + warn "reattaching attachments to players"; + reattach $_ for values %PLAYER; warn "loading reloadable resources"; - load_resources; + reload_resources; warn "leaving sync_job"; @@ -2529,6 +2587,14 @@ $RUNTIME += $TICK; $NEXT_TICK += $TICK; + if ($NOW >= $NEXT_RUNTIME_WRITE) { + $NEXT_RUNTIME_WRITE = $NOW + 10; + Coro::async_pool { + write_runtime + or warn "ERROR: unable to write runtime file: $!"; + }; + } + $WAIT_FOR_TICK->broadcast; $WAIT_FOR_TICK_ONE->send if $WAIT_FOR_TICK_ONE->awaited; @@ -2600,31 +2666,14 @@ IO::AIO::max_poll_time $TICK * 0.1; $AIO_POLL_WATCHER = Event->io ( reentrant => 0, + data => WF_AUTOCANCEL, fd => IO::AIO::poll_fileno, poll => 'r', prio => 6, - data => WF_AUTOCANCEL, cb => \&IO::AIO::poll_cb, ); } -$WRITE_RUNTIME_WATCHER = Event->timer ( - reentrant => 0, - data => WF_AUTOCANCEL, - parked => 1, - after => 1, - interval => 10, - prio => 6, # keep it lowest so it acts like a watchdog - cb => sub { - $TICK_WATCHER->is_active or cf::cleanup "mainloop frozen but runtime active", 1; - - Coro::async_pool { - write_runtime - or warn "ERROR: unable to write runtime file: $!"; - }; - }, -); - END { cf::emergency_save } 1