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.186 by root, Sat Jan 20 01:12:39 2007 UTC vs.
Revision 1.190 by root, Mon Jan 22 03:44:41 2007 UTC

1140 $rmp->{map_layout_style}, $rmp->{treasureoptions}, $rmp->{symmetry_used}, 1140 $rmp->{map_layout_style}, $rmp->{treasureoptions}, $rmp->{symmetry_used},
1141 (cf::region::find $rmp->{region}), $rmp->{custom} 1141 (cf::region::find $rmp->{region}), $rmp->{custom}
1142 ) 1142 )
1143} 1143}
1144 1144
1145=item cf::map->register ($regex, $prio)
1146
1147Register a handler for the map path matching the given regex at the
1148givne priority (higher is better, built-in handlers have priority 0, the
1149default).
1150
1151=cut
1152
1145sub register { 1153sub register {
1146 my (undef, $regex) = @_; 1154 my (undef, $regex, $prio) = @_;
1147 my $pkg = caller; 1155 my $pkg = caller;
1148 1156
1149 no strict; 1157 no strict;
1150 push @{"$pkg\::ISA"}, __PACKAGE__; 1158 push @{"$pkg\::ISA"}, __PACKAGE__;
1151 1159
1152 $EXT_MAP{$pkg} = qr<$regex>; 1160 $EXT_MAP{$pkg} = [$prio, qr<$regex>];
1153} 1161}
1154 1162
1155# also paths starting with '/' 1163# also paths starting with '/'
1156$EXT_MAP{"cf::map"} = qr{^(?=/)}; 1164$EXT_MAP{"cf::map"} = [0, qr{^(?=/)}];
1157 1165
1158sub thawer_merge { 1166sub thawer_merge {
1159 my ($self, $merge) = @_; 1167 my ($self, $merge) = @_;
1160 1168
1161 # we have to keep some variables in memory intact 1169 # we have to keep some variables in memory intact
1204 1212
1205 return $path if UNIVERSAL::isa $path, "cf::map"; # already a map object 1213 return $path if UNIVERSAL::isa $path, "cf::map"; # already a map object
1206 1214
1207 $path = normalise $path, $base; 1215 $path = normalise $path, $base;
1208 1216
1209 for my $pkg (keys %EXT_MAP) { 1217 for my $pkg (sort { $EXT_MAP{$b}[0] <=> $EXT_MAP{$a}[0] } keys %EXT_MAP) {
1210 if ($path =~ $EXT_MAP{$pkg}) { 1218 if ($path =~ $EXT_MAP{$pkg}[1]) {
1211 my $self = bless cf::map::new, $pkg; 1219 my $self = bless cf::map::new, $pkg;
1212 $self->{path} = $path; $self->path ($path); 1220 $self->{path} = $path; $self->path ($path);
1213 $self->init; # pass $1 etc. 1221 $self->init; # pass $1 etc.
1214 return $self; 1222 return $self;
1215 } 1223 }
1289 $self->{load_path} = $path; 1297 $self->{load_path} = $path;
1290 1298
1291 1 1299 1
1292} 1300}
1293 1301
1294sub load_orig { 1302sub load_header_orig {
1295 my ($self) = @_; 1303 my ($self) = @_;
1296 1304
1297 $self->load_header_from ($self->load_path) 1305 $self->load_header_from ($self->load_path)
1298} 1306}
1299 1307
1300sub load_temp { 1308sub load_header_temp {
1301 my ($self) = @_; 1309 my ($self) = @_;
1302 1310
1303 $self->load_header_from ($self->save_path) 1311 $self->load_header_from ($self->save_path)
1312}
1313
1314sub prepare_temp {
1315 my ($self) = @_;
1316
1317 $self->last_access ((delete $self->{last_access})
1318 || $cf::RUNTIME); #d#
1319 # safety
1320 $self->{instantiate_time} = $cf::RUNTIME
1321 if $self->{instantiate_time} > $cf::RUNTIME;
1322}
1323
1324sub prepare_orig {
1325 my ($self) = @_;
1326
1327 $self->{load_original} = 1;
1328 $self->{instantiate_time} = $cf::RUNTIME;
1329 $self->last_access ($cf::RUNTIME);
1330 $self->instantiate;
1304} 1331}
1305 1332
1306sub load_header { 1333sub load_header {
1307 my ($self) = @_; 1334 my ($self) = @_;
1308 1335
1309 if ($self->load_temp) { 1336 if ($self->load_header_temp) {
1310 $self->last_access ((delete $self->{last_access}) 1337 $self->prepare_temp;
1311 || $cf::RUNTIME); #d#
1312 # safety
1313 $self->{instantiate_time} = $cf::RUNTIME
1314 if $self->{instantiate_time} > $cf::RUNTIME;
1315 } else { 1338 } else {
1316 $self->load_orig 1339 $self->load_header_orig
1317 or return; 1340 or return;
1318 1341 $self->prepare_orig;
1319 $self->{load_original} = 1;
1320 $self->{instantiate_time} = $cf::RUNTIME;
1321 $self->last_access ($cf::RUNTIME);
1322 $self->instantiate;
1323 } 1342 }
1324 1343
1325 1 1344 1
1326} 1345}
1327 1346
1354 1373
1355 $cf::MAP{$path} = $map 1374 $cf::MAP{$path} = $map
1356 } 1375 }
1357} 1376}
1358 1377
1378sub pre_load { }
1379sub post_load { }
1380
1359sub load { 1381sub load {
1360 my ($self) = @_; 1382 my ($self) = @_;
1361 1383
1362 my $path = $self->{path}; 1384 my $path = $self->{path};
1363 my $guard = cf::lock_acquire "map_load:$path"; 1385 my $guard = cf::lock_acquire "map_load:$path";
1365 return if $self->in_memory != cf::MAP_SWAPPED; 1387 return if $self->in_memory != cf::MAP_SWAPPED;
1366 1388
1367 $self->in_memory (cf::MAP_LOADING); 1389 $self->in_memory (cf::MAP_LOADING);
1368 1390
1369 $self->alloc; 1391 $self->alloc;
1392
1393 $self->pre_load;
1394
1370 $self->_load_objects ($self->{load_path}, 1) 1395 $self->_load_objects ($self->{load_path}, 1)
1371 or return; 1396 or return;
1372 1397
1373 $self->set_object_flag (cf::FLAG_OBJ_ORIGINAL, 1) 1398 $self->set_object_flag (cf::FLAG_OBJ_ORIGINAL, 1)
1374 if delete $self->{load_original}; 1399 if delete $self->{load_original};
1396 unless $self->difficulty; 1421 unless $self->difficulty;
1397 Coro::cede; 1422 Coro::cede;
1398 $self->activate; 1423 $self->activate;
1399 } 1424 }
1400 1425
1426 $self->post_load;
1427
1401 $self->in_memory (cf::MAP_IN_MEMORY); 1428 $self->in_memory (cf::MAP_IN_MEMORY);
1402} 1429}
1403 1430
1404sub customise_for { 1431sub customise_for {
1405 my ($self, $ob) = @_; 1432 my ($self, $ob) = @_;
1469 delete $MAP_PREFETCH{$path}; 1496 delete $MAP_PREFETCH{$path};
1470 } 1497 }
1471 } 1498 }
1472 undef $MAP_PREFETCHER; 1499 undef $MAP_PREFETCHER;
1473 }; 1500 };
1501 $MAP_PREFETCHER->prio (6);
1474 1502
1475 () 1503 ()
1476} 1504}
1477 1505
1478sub save { 1506sub save {
2178 sub db_sync() { 2206 sub db_sync() {
2179 db_save if $dirty; 2207 db_save if $dirty;
2180 undef $dirty; 2208 undef $dirty;
2181 } 2209 }
2182 2210
2183 my $idle = Event->idle (min => 10, max => 20, repeat => 0, data => WF_AUTOCANCEL, cb => sub { 2211 my $idle = Event->idle (
2184 db_sync; 2212 reentrant => 0,
2213 min => 10,
2214 max => 20,
2215 repeat => 0,
2216 data => WF_AUTOCANCEL,
2217 cb => \&db_sync,
2185 }); 2218 );
2186 2219
2187 sub db_dirty() { 2220 sub db_dirty() {
2188 $dirty = 1; 2221 $dirty = 1;
2189 $idle->start; 2222 $idle->start;
2190 } 2223 }
2258 2291
2259# install some emergency cleanup handlers 2292# install some emergency cleanup handlers
2260BEGIN { 2293BEGIN {
2261 for my $signal (qw(INT HUP TERM)) { 2294 for my $signal (qw(INT HUP TERM)) {
2262 Event->signal ( 2295 Event->signal (
2296 reentrant => 0,
2263 data => WF_AUTOCANCEL, 2297 data => WF_AUTOCANCEL,
2264 signal => $signal, 2298 signal => $signal,
2265 cb => sub { 2299 cb => sub {
2266 cf::cleanup "SIG$signal"; 2300 cf::cleanup "SIG$signal";
2267 }, 2301 },
2268 ); 2302 );
2269 } 2303 }
2270} 2304}
2425 $who->message ("reloading server."); 2459 $who->message ("reloading server.");
2426 2460
2427 # doing reload synchronously and two reloads happen back-to-back, 2461 # doing reload synchronously and two reloads happen back-to-back,
2428 # coro crashes during coro_state_free->destroy here. 2462 # coro crashes during coro_state_free->destroy here.
2429 2463
2430 $RELOAD_WATCHER ||= Event->timer (after => 0, data => WF_AUTOCANCEL, cb => sub { 2464 $RELOAD_WATCHER ||= Event->timer (
2465 reentrant => 0,
2466 after => 0,
2467 data => WF_AUTOCANCEL,
2468 cb => sub {
2431 reload; 2469 reload;
2432 undef $RELOAD_WATCHER; 2470 undef $RELOAD_WATCHER;
2471 },
2433 }); 2472 );
2434 } 2473 }
2435}; 2474};
2436 2475
2437unshift @INC, $LIBDIR; 2476unshift @INC, $LIBDIR;
2438 2477
2439my $bug_warning = 0; 2478my $bug_warning = 0;
2440 2479
2441$TICK_WATCHER = Event->timer ( 2480$TICK_WATCHER = Event->timer (
2442 reentrant => 0, 2481 reentrant => 0,
2443 parked => 1, 2482 parked => 1,
2444 prio => 0, 2483 prio => -1,
2445 at => $NEXT_TICK || $TICK, 2484 at => $NEXT_TICK || $TICK,
2446 data => WF_AUTOCANCEL, 2485 data => WF_AUTOCANCEL,
2447 cb => sub { 2486 cb => sub {
2448 if ($Coro::current != $Coro::main) { 2487 if ($Coro::current != $Coro::main) {
2449 Carp::cluck "major BUG: server tick called outside of main coro, skipping it" 2488 Carp::cluck "major BUG: server tick called outside of main coro, skipping it"
2458 $NEXT_TICK += $TICK; 2497 $NEXT_TICK += $TICK;
2459 2498
2460 $WAIT_FOR_TICK->broadcast; 2499 $WAIT_FOR_TICK->broadcast;
2461 $WAIT_FOR_TICK_ONE->send if $WAIT_FOR_TICK_ONE->awaited; 2500 $WAIT_FOR_TICK_ONE->send if $WAIT_FOR_TICK_ONE->awaited;
2462 2501
2502 my $AFTER = Event::time;
2503 warn $AFTER - $NOW;#d#
2504
2463 # if we are delayed by four ticks or more, skip them all 2505 # if we are delayed by four ticks or more, skip them all
2464 $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; 2506 $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4;
2465 2507
2466 $TICK_WATCHER->at ($NEXT_TICK); 2508 $TICK_WATCHER->at ($NEXT_TICK);
2467 $TICK_WATCHER->start; 2509 $TICK_WATCHER->start;
2468 }, 2510 },
2469); 2511);
2470 2512
2471IO::AIO::max_poll_time $TICK * 0.2; 2513IO::AIO::max_poll_time $TICK * 0.2;
2472 2514
2515undef $Coro::AIO::WATCHER;
2473$AIO_POLL_WATCHER = Event->io ( 2516$AIO_POLL_WATCHER = Event->io (
2517 reentrant => 0,
2474 fd => IO::AIO::poll_fileno, 2518 fd => IO::AIO::poll_fileno,
2475 poll => 'r', 2519 poll => 'r',
2476 prio => 5, 2520 prio => 6,
2477 data => WF_AUTOCANCEL, 2521 data => WF_AUTOCANCEL,
2478 cb => \&IO::AIO::poll_cb, 2522 cb => \&IO::AIO::poll_cb,
2479); 2523);
2480 2524
2481$WRITE_RUNTIME_WATCHER = Event->timer ( 2525$WRITE_RUNTIME_WATCHER = Event->timer (
2526 reentrant => 0,
2482 data => WF_AUTOCANCEL, 2527 data => WF_AUTOCANCEL,
2483 after => 1, 2528 after => 1,
2484 interval => 10, 2529 interval => 10,
2485 prio => 6, # keep it lowest so it acts like a watchdog 2530 prio => 6, # keep it lowest so it acts like a watchdog
2486 cb => Coro::unblock_sub { 2531 cb => Coro::unblock_sub {
2487 write_runtime 2532 write_runtime
2488 or warn "ERROR: unable to write runtime file: $!"; 2533 or warn "ERROR: unable to write runtime file: $!";
2489 }, 2534 },
2490); 2535);
2491 2536

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines