… | |
… | |
80 | our $NEXT_TICK; |
80 | our $NEXT_TICK; |
81 | our $NOW; |
81 | our $NOW; |
82 | our $USE_FSYNC = 1; # use fsync to write maps - default off |
82 | our $USE_FSYNC = 1; # use fsync to write maps - default off |
83 | |
83 | |
84 | our $BDB_POLL_WATCHER; |
84 | our $BDB_POLL_WATCHER; |
|
|
85 | our $BDB_CHECKPOINT_WATCHER; |
|
|
86 | our $BDB_TRICKLE_WATCHER; |
85 | our $DB_ENV; |
87 | our $DB_ENV; |
86 | |
88 | |
87 | our %CFG; |
89 | our %CFG; |
88 | |
90 | |
89 | our $UPTIME; $UPTIME ||= time; |
91 | our $UPTIME; $UPTIME ||= time; |
… | |
… | |
187 | $msg .= "\n" |
189 | $msg .= "\n" |
188 | unless $msg =~ /\n$/; |
190 | unless $msg =~ /\n$/; |
189 | |
191 | |
190 | $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; |
192 | $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; |
191 | |
193 | |
192 | utf8::encode $msg; |
|
|
193 | LOG llevError, $msg; |
194 | LOG llevError, $msg; |
194 | }; |
195 | }; |
195 | } |
196 | } |
196 | |
197 | |
197 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
198 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
… | |
… | |
508 | =item cf::db_put $family => $key => $value |
509 | =item cf::db_put $family => $key => $value |
509 | |
510 | |
510 | Stores the given C<$value> in the family. It can currently store binary |
511 | Stores the given C<$value> in the family. It can currently store binary |
511 | data only (use Compress::LZF::sfreeze_cr/sthaw to convert to/from binary). |
512 | data only (use Compress::LZF::sfreeze_cr/sthaw to convert to/from binary). |
512 | |
513 | |
|
|
514 | =item $db = cf::db_table "name" |
|
|
515 | |
|
|
516 | Create and/or open a new database table. The string must not be "db" and must be unique |
|
|
517 | within each server. |
|
|
518 | |
513 | =cut |
519 | =cut |
|
|
520 | |
|
|
521 | sub db_table($) { |
|
|
522 | my ($name) = @_; |
|
|
523 | my $db = BDB::db_create $DB_ENV; |
|
|
524 | |
|
|
525 | eval { |
|
|
526 | $db->set_flags (BDB::CHKSUM); |
|
|
527 | |
|
|
528 | utf8::encode $name; |
|
|
529 | BDB::db_open $db, undef, $name, undef, BDB::BTREE, |
|
|
530 | BDB::CREATE | BDB::AUTO_COMMIT, 0666; |
|
|
531 | cf::cleanup "db_open(db): $!" if $!; |
|
|
532 | }; |
|
|
533 | cf::cleanup "db_open(db): $@" if $@; |
|
|
534 | |
|
|
535 | $db |
|
|
536 | } |
514 | |
537 | |
515 | our $DB; |
538 | our $DB; |
516 | |
539 | |
517 | sub db_init { |
540 | sub db_init { |
518 | unless ($DB) { |
|
|
519 | $DB = BDB::db_create $DB_ENV; |
|
|
520 | |
|
|
521 | cf::sync_job { |
541 | cf::sync_job { |
522 | eval { |
542 | $DB ||= db_table "db"; |
523 | $DB->set_flags (BDB::CHKSUM); |
|
|
524 | |
|
|
525 | BDB::db_open $DB, undef, "db", undef, BDB::BTREE, |
|
|
526 | BDB::CREATE | BDB::AUTO_COMMIT, 0666; |
|
|
527 | cf::cleanup "db_open(db): $!" if $!; |
|
|
528 | }; |
|
|
529 | cf::cleanup "db_open(db): $@" if $@; |
|
|
530 | }; |
|
|
531 | } |
543 | }; |
532 | } |
544 | } |
533 | |
545 | |
534 | sub db_get($$) { |
546 | sub db_get($$) { |
535 | my $key = "$_[0]/$_[1]"; |
547 | my $key = "$_[0]/$_[1]"; |
536 | |
548 | |
… | |
… | |
1017 | |
1029 | |
1018 | my $guard = cf::lock_acquire "io"; |
1030 | my $guard = cf::lock_acquire "io"; |
1019 | |
1031 | |
1020 | sync_job { |
1032 | sync_job { |
1021 | if (length $$rdata) { |
1033 | if (length $$rdata) { |
|
|
1034 | utf8::decode (my $decname = $filename); |
1022 | warn sprintf "saving %s (%d,%d)\n", |
1035 | warn sprintf "saving %s (%d,%d)\n", |
1023 | $filename, length $$rdata, scalar @$objs; |
1036 | $decname, length $$rdata, scalar @$objs; |
1024 | |
1037 | |
1025 | if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { |
1038 | if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { |
1026 | chmod SAVE_MODE, $fh; |
1039 | chmod SAVE_MODE, $fh; |
1027 | aio_write $fh, 0, (length $$rdata), $$rdata, 0; |
1040 | aio_write $fh, 0, (length $$rdata), $$rdata, 0; |
1028 | aio_fsync $fh if $cf::USE_FSYNC; |
1041 | aio_fsync $fh if $cf::USE_FSYNC; |
… | |
… | |
1078 | |
1091 | |
1079 | undef $guard; |
1092 | undef $guard; |
1080 | $av = eval { (Storable::thaw $av)->{objs} }; |
1093 | $av = eval { (Storable::thaw $av)->{objs} }; |
1081 | } |
1094 | } |
1082 | |
1095 | |
|
|
1096 | utf8::decode (my $decname = $filename); |
1083 | warn sprintf "loading %s (%d)\n", |
1097 | warn sprintf "loading %s (%d,%d)\n", |
1084 | $filename, length $data, scalar @{$av || []}; |
1098 | $decname, length $data, scalar @{$av || []}; |
1085 | |
1099 | |
1086 | ($data, $av) |
1100 | ($data, $av) |
1087 | } |
1101 | } |
1088 | |
1102 | |
1089 | =head2 COMMAND CALLBACKS |
1103 | =head2 COMMAND CALLBACKS |
… | |
… | |
1276 | use Coro::AIO; |
1290 | use Coro::AIO; |
1277 | |
1291 | |
1278 | =head3 cf::player |
1292 | =head3 cf::player |
1279 | |
1293 | |
1280 | =over 4 |
1294 | =over 4 |
|
|
1295 | |
|
|
1296 | =item cf::player::num_playing |
|
|
1297 | |
|
|
1298 | Returns the official number of playing players, as per the Crossfire metaserver rules. |
|
|
1299 | |
|
|
1300 | =cut |
|
|
1301 | |
|
|
1302 | sub num_playing { |
|
|
1303 | scalar grep |
|
|
1304 | $_->ob->map |
|
|
1305 | && !$_->hidden |
|
|
1306 | && !$_->ob->flag (cf::FLAG_WIZ), |
|
|
1307 | cf::player::list |
|
|
1308 | } |
1281 | |
1309 | |
1282 | =item cf::player::find $login |
1310 | =item cf::player::find $login |
1283 | |
1311 | |
1284 | Returns the given player object, loading it if necessary (might block). |
1312 | Returns the given player object, loading it if necessary (might block). |
1285 | |
1313 | |
… | |
… | |
3498 | |
3526 | |
3499 | }, |
3527 | }, |
3500 | ); |
3528 | ); |
3501 | |
3529 | |
3502 | { |
3530 | { |
|
|
3531 | BDB::min_parallel 8; |
3503 | BDB::max_poll_time $TICK * 0.1; |
3532 | BDB::max_poll_time $TICK * 0.1; |
3504 | $BDB_POLL_WATCHER = Event->io ( |
3533 | $BDB_POLL_WATCHER = Event->io ( |
3505 | reentrant => 0, |
3534 | reentrant => 0, |
3506 | fd => BDB::poll_fileno, |
3535 | fd => BDB::poll_fileno, |
3507 | poll => 'r', |
3536 | poll => 'r', |
3508 | prio => 0, |
3537 | prio => 0, |
3509 | data => WF_AUTOCANCEL, |
3538 | data => WF_AUTOCANCEL, |
3510 | cb => \&BDB::poll_cb, |
3539 | cb => \&BDB::poll_cb, |
3511 | ); |
3540 | ); |
3512 | BDB::min_parallel 8; |
|
|
3513 | |
3541 | |
3514 | BDB::set_sync_prepare { |
3542 | BDB::set_sync_prepare { |
3515 | my $status; |
3543 | my $status; |
3516 | my $current = $Coro::current; |
3544 | my $current = $Coro::current; |
3517 | ( |
3545 | ( |
… | |
… | |
3538 | | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE, |
3566 | | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE, |
3539 | 0666; |
3567 | 0666; |
3540 | |
3568 | |
3541 | cf::cleanup "db_env_open($BDBDIR): $!" if $!; |
3569 | cf::cleanup "db_env_open($BDBDIR): $!" if $!; |
3542 | |
3570 | |
3543 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC, 1); |
3571 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC |
|
|
3572 | | BDB::LOG_AUTOREMOVE, 1); |
3544 | $DB_ENV->set_lk_detect; |
3573 | $DB_ENV->set_lk_detect; |
3545 | }; |
3574 | }; |
3546 | |
3575 | |
3547 | cf::cleanup "db_env_open(db): $@" if $@; |
3576 | cf::cleanup "db_env_open(db): $@" if $@; |
3548 | }; |
3577 | }; |
3549 | } |
3578 | } |
|
|
3579 | |
|
|
3580 | $BDB_CHECKPOINT_WATCHER = Event->timer ( |
|
|
3581 | after => 11, |
|
|
3582 | interval => 60, |
|
|
3583 | hard => 1, |
|
|
3584 | prio => 0, |
|
|
3585 | data => WF_AUTOCANCEL, |
|
|
3586 | cb => sub { |
|
|
3587 | BDB::db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { }; |
|
|
3588 | }, |
|
|
3589 | ); |
|
|
3590 | $BDB_TRICKLE_WATCHER = Event->timer ( |
|
|
3591 | after => 5, |
|
|
3592 | interval => 10, |
|
|
3593 | hard => 1, |
|
|
3594 | prio => 0, |
|
|
3595 | data => WF_AUTOCANCEL, |
|
|
3596 | cb => sub { |
|
|
3597 | BDB::db_env_memp_trickle $DB_ENV, 20, 0, sub { }; |
|
|
3598 | }, |
|
|
3599 | ); |
3550 | } |
3600 | } |
3551 | |
3601 | |
3552 | { |
3602 | { |
3553 | IO::AIO::min_parallel 8; |
3603 | IO::AIO::min_parallel 8; |
3554 | |
3604 | |