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.114 by root, Mon Jan 1 16:00:10 2007 UTC vs.
Revision 1.132 by root, Thu Jan 4 01:35:56 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;
71mkdir cf::localdir . "/" . cf::uniquedir; 71mkdir cf::localdir . "/" . cf::uniquedir;
72mkdir $RANDOM_MAPS; 72mkdir $RANDOM_MAPS;
73 73
74# a special map that is always available 74# a special map that is always available
75our $LINK_MAP; 75our $LINK_MAP;
76 76our $EMERGENCY_POSITION;
77our $EMERGENCY_POSITION = $cf::CFG{emergency_position} || ["/world/world_105_115", 5, 37];
78 77
79############################################################################# 78#############################################################################
80 79
81=head2 GLOBAL VARIABLES 80=head2 GLOBAL VARIABLES
82 81
181sub to_json($) { 180sub to_json($) {
182 $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs 181 $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs
183 JSON::Syck::Dump $_[0] 182 JSON::Syck::Dump $_[0]
184} 183}
185 184
185=item my $guard = cf::guard { BLOCK }
186
187Run the given callback when the guard object gets destroyed (useful for
188coroutine cancellations).
189
190You can call C<< ->cancel >> on the guard object to stop the block from
191being executed.
192
193=cut
194
195sub guard(&) {
196 bless \(my $cb = $_[0]), cf::guard::;
197}
198
199sub cf::guard::cancel {
200 ${$_[0]} = sub { };
201}
202
203sub cf::guard::DESTROY {
204 ${$_[0]}->();
205}
206
207=item cf::lock_wait $string
208
209Wait until the given lock is available. See cf::lock_acquire.
210
211=item my $lock = cf::lock_acquire $string
212
213Wait until the given lock is available and then acquires it and returns
214a guard object. If the guard object gets destroyed (goes out of scope,
215for example when the coroutine gets canceled), the lock is automatically
216returned.
217
218Lock names should begin with a unique identifier (for example, find_map
219uses map_find and load_map uses map_load).
220
221=cut
222
223our %LOCK;
224
225sub lock_wait($) {
226 my ($key) = @_;
227
228 # wait for lock, if any
229 while ($LOCK{$key}) {
230 push @{ $LOCK{$key} }, $Coro::current;
231 Coro::schedule;
232 }
233}
234
235sub lock_acquire($) {
236 my ($key) = @_;
237
238 # wait, to be sure we are not locked
239 lock_wait $key;
240
241 $LOCK{$key} = [];
242
243 cf::guard {
244 # wake up all waiters, to be on the safe side
245 $_->ready for @{ delete $LOCK{$key} };
246 }
247}
248
249=item cf::async { BLOCK }
250
251Like C<Coro::async>, but runs the given BLOCK in an eval and only logs the
252error instead of exiting the server in case of a problem.
253
254=cut
255
256sub async(&) {
257 my ($cb) = @_;
258
259 Coro::async {
260 eval { $cb->() };
261 warn $@ if $@;
262 }
263}
264
186=item cf::sync_job { BLOCK } 265=item cf::sync_job { BLOCK }
187 266
188The design of crossfire+ requires that the main coro ($Coro::main) is 267The design of crossfire+ requires that the main coro ($Coro::main) is
189always able to handle events or runnable, as crossfire+ is only partly 268always able to handle events or runnable, as crossfire+ is only partly
190reentrant. Thus "blocking" it by e.g. waiting for I/O is not acceptable. 269reentrant. Thus "blocking" it by e.g. waiting for I/O is not acceptable.
237=cut 316=cut
238 317
239sub coro(&) { 318sub coro(&) {
240 my $cb = shift; 319 my $cb = shift;
241 320
242 my $coro; $coro = async { 321 my $coro = &cf::async ($cb);
243 eval {
244 $cb->();
245 };
246 warn $@ if $@;
247 };
248 322
249 $coro->on_destroy (sub { 323 $coro->on_destroy (sub {
250 delete $EXT_CORO{$coro+0}; 324 delete $EXT_CORO{$coro+0};
251 }); 325 });
252 $EXT_CORO{$coro+0} = $coro; 326 $EXT_CORO{$coro+0} = $coro;
817 (aio_load "$filename.pst", $av) >= 0 891 (aio_load "$filename.pst", $av) >= 0
818 or return; 892 or return;
819 $av = eval { (Storable::thaw $av)->{objs} }; 893 $av = eval { (Storable::thaw $av)->{objs} };
820 } 894 }
821 895
896 warn sprintf "loading %s (%d)\n",
897 $filename, length $data, scalar @{$av || []};#d#
822 return ($data, $av); 898 return ($data, $av);
823} 899}
824 900
825############################################################################# 901#############################################################################
826# command handling &c 902# command handling &c
1087# and all this just because we cannot iterate over 1163# and all this just because we cannot iterate over
1088# all maps in C++... 1164# all maps in C++...
1089sub change_all_map_light { 1165sub change_all_map_light {
1090 my ($change) = @_; 1166 my ($change) = @_;
1091 1167
1092 $_->change_map_light ($change) for values %cf::MAP; 1168 $_->change_map_light ($change)
1169 for grep $_->outdoor, values %cf::MAP;
1093} 1170}
1094 1171
1095sub try_load_header($) { 1172sub try_load_header($) {
1096 my ($path) = @_; 1173 my ($path) = @_;
1097 1174
1108 $map->{load_path} = $path; 1185 $map->{load_path} = $path;
1109 1186
1110 $map 1187 $map
1111} 1188}
1112 1189
1190sub find_map;
1113sub find_map { 1191sub find_map {
1114 my ($path, $origin) = @_; 1192 my ($path, $origin) = @_;
1115 1193
1116 #warn "find_map<$path,$origin>\n";#d# 1194 #warn "find_map<$path,$origin>\n";#d#
1117 1195
1118 $path = new cf::path $path, $origin && $origin->path; 1196 $path = new cf::path $path, $origin && $origin->path;
1119 my $key = $path->as_string; 1197 my $key = $path->as_string;
1120 1198
1199 cf::lock_wait "map_find:$key";
1200
1121 $cf::MAP{$key} || do { 1201 $cf::MAP{$key} || do {
1202 my $guard = cf::lock_acquire "map_find:$key";
1203
1122 # do it the slow way 1204 # do it the slow way
1123 my $map = try_load_header $path->save_path; 1205 my $map = try_load_header $path->save_path;
1124 1206
1125 if ($map) { 1207 if ($map) {
1208 $map->last_access ((delete $map->{last_access})
1209 || $cf::RUNTIME); #d#
1126 # safety 1210 # safety
1127 $map->{instantiate_time} = $cf::RUNTIME 1211 $map->{instantiate_time} = $cf::RUNTIME
1128 if $map->{instantiate_time} > $cf::RUNTIME; 1212 if $map->{instantiate_time} > $cf::RUNTIME;
1129 } else { 1213 } else {
1130 if (my $rmp = $path->random_map_params) { 1214 if (my $rmp = $path->random_map_params) {
1135 1219
1136 $map or return; 1220 $map or return;
1137 1221
1138 $map->{load_original} = 1; 1222 $map->{load_original} = 1;
1139 $map->{instantiate_time} = $cf::RUNTIME; 1223 $map->{instantiate_time} = $cf::RUNTIME;
1224 $map->last_access ($cf::RUNTIME);
1140 $map->instantiate; 1225 $map->instantiate;
1141 1226
1142 # per-player maps become, after loading, normal maps 1227 # per-player maps become, after loading, normal maps
1143 $map->per_player (0) if $path->{user_rel}; 1228 $map->per_player (0) if $path->{user_rel};
1144 } 1229 }
1145 1230
1146 $map->path ($key); 1231 $map->path ($key);
1147 $map->{path} = $path; 1232 $map->{path} = $path;
1148 $map->last_access ($cf::RUNTIME); 1233 $map->{last_save} = $cf::RUNTIME;
1149 1234
1150 if ($map->should_reset) { 1235 if ($map->should_reset) {
1151 $map->reset; 1236 $map->reset;
1237 undef $guard;
1152 $map = find_map $path; 1238 $map = find_map $path
1239 or return;
1153 } 1240 }
1154 1241
1155 $cf::MAP{$key} = $map 1242 $cf::MAP{$key} = $map
1156 } 1243 }
1157} 1244}
1158 1245
1159sub load { 1246sub load {
1160 my ($self) = @_; 1247 my ($self) = @_;
1161 1248
1249 my $path = $self->{path};
1250 my $guard = cf::lock_acquire "map_load:" . $path->as_string;
1251
1162 return if $self->in_memory != cf::MAP_SWAPPED; 1252 return if $self->in_memory != cf::MAP_SWAPPED;
1163 1253
1164 $self->in_memory (cf::MAP_LOADING); 1254 $self->in_memory (cf::MAP_LOADING);
1165
1166 my $path = $self->{path};
1167 1255
1168 $self->alloc; 1256 $self->alloc;
1169 $self->load_objects ($self->{load_path}, 1) 1257 $self->load_objects ($self->{load_path}, 1)
1170 or return; 1258 or return;
1171 1259
1213} 1301}
1214 1302
1215sub save { 1303sub save {
1216 my ($self) = @_; 1304 my ($self) = @_;
1217 1305
1306 $self->{last_save} = $cf::RUNTIME;
1307
1308 return unless $self->dirty;
1309
1218 my $save = $self->{path}->save_path; utf8::encode $save; 1310 my $save = $self->{path}->save_path; utf8::encode $save;
1219 my $uniq = $self->{path}->uniq_path; utf8::encode $uniq; 1311 my $uniq = $self->{path}->uniq_path; utf8::encode $uniq;
1220 1312
1221 $self->{last_save} = $cf::RUNTIME;
1222
1223 return unless $self->dirty;
1224
1225 $self->{load_path} = $save; 1313 $self->{load_path} = $save;
1226 1314
1227 return if $self->{deny_save}; 1315 return if $self->{deny_save};
1316
1317 local $self->{last_access} = $self->last_access;#d#
1228 1318
1229 if ($uniq) { 1319 if ($uniq) {
1230 $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS); 1320 $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS);
1231 $self->save_objects ($uniq, cf::IO_UNIQUES); 1321 $self->save_objects ($uniq, cf::IO_UNIQUES);
1232 } else { 1322 } else {
1235} 1325}
1236 1326
1237sub swap_out { 1327sub swap_out {
1238 my ($self) = @_; 1328 my ($self) = @_;
1239 1329
1330 # save first because save cedes
1331 $self->save;
1332
1240 return if $self->players; 1333 return if $self->players;
1241 return if $self->in_memory != cf::MAP_IN_MEMORY; 1334 return if $self->in_memory != cf::MAP_IN_MEMORY;
1242 return if $self->{deny_save}; 1335 return if $self->{deny_save};
1243 1336
1244 $self->save;
1245 $self->clear; 1337 $self->clear;
1246 $self->in_memory (cf::MAP_SWAPPED); 1338 $self->in_memory (cf::MAP_SWAPPED);
1247} 1339}
1248 1340
1249sub reset_at { 1341sub reset_at {
1383 (ref $cf::CFG{"may_$access"} 1475 (ref $cf::CFG{"may_$access"}
1384 ? scalar grep $self->name eq $_, @{$cf::CFG{"may_$access"}} 1476 ? scalar grep $self->name eq $_, @{$cf::CFG{"may_$access"}}
1385 : $cf::CFG{"may_$access"}) 1477 : $cf::CFG{"may_$access"})
1386} 1478}
1387 1479
1480=item $player_object->enter_link
1481
1482Freezes the player and moves him/her to a special map (C<{link}>).
1483
1484The player should be reaosnably safe there for short amounts of time. You
1485I<MUST> call C<leave_link> as soon as possible, though.
1486
1487=item $player_object->leave_link ($map, $x, $y)
1488
1489Moves the player out of the specila link map onto the given map. If the
1490map is not valid (or omitted), the player will be moved back to the
1491location he/she was before the call to C<enter_link>, or, if that fails,
1492to the emergency map position.
1493
1494Might block.
1495
1496=cut
1497
1388sub cf::object::player::enter_link { 1498sub cf::object::player::enter_link {
1389 my ($self) = @_; 1499 my ($self) = @_;
1390 1500
1501 $self->deactivate_recursive;
1502
1391 return if $self->map == $LINK_MAP; 1503 return if $self->map == $LINK_MAP;
1392 1504
1393 $self->{_link_pos} = [$self->map->{path}, $self->x, $self->y] 1505 $self->{_link_pos} ||= [$self->map->{path}, $self->x, $self->y]
1394 if $self->map; 1506 if $self->map;
1395 1507
1396 $self->enter_map ($LINK_MAP, 20, 20); 1508 $self->enter_map ($LINK_MAP, 20, 20);
1397 $self->deactivate_recursive;
1398} 1509}
1399 1510
1400sub cf::object::player::leave_link { 1511sub cf::object::player::leave_link {
1401 my ($self, $map, $x, $y) = @_; 1512 my ($self, $map, $x, $y) = @_;
1402 1513
1403 my $link_pos = delete $self->{_link_pos}; 1514 my $link_pos = delete $self->{_link_pos};
1404 1515
1405 unless ($map) { 1516 unless ($map) {
1406 $self->message ("The exit is closed", cf::NDI_UNIQUE | cf::NDI_RED);
1407
1408 # restore original map position 1517 # restore original map position
1409 ($map, $x, $y) = @{ $link_pos || [] }; 1518 ($map, $x, $y) = @{ $link_pos || [] };
1410 $map = cf::map::find_map $map; 1519 $map = cf::map::find_map $map;
1411 1520
1412 unless ($map) { 1521 unless ($map) {
1427 1536
1428 $self->activate_recursive; 1537 $self->activate_recursive;
1429 $self->enter_map ($map, $x, $y); 1538 $self->enter_map ($map, $x, $y);
1430} 1539}
1431 1540
1541cf::player->attach (
1542 on_logout => sub {
1543 my ($pl) = @_;
1544
1545 # abort map switching before logout
1546 if ($pl->ob->{_link_pos}) {
1547 cf::sync_job {
1548 $pl->ob->leave_link
1549 };
1550 }
1551 },
1552 on_login => sub {
1553 my ($pl) = @_;
1554
1555 # try to abort aborted map switching on player login :)
1556 # should happen only on crashes
1557 if ($pl->ob->{_link_pos}) {
1558 $pl->ob->enter_link;
1559 cf::async {
1560 # we need this sleep as the login has a concurrent enter_exit running
1561 # and this sleep increases chances of the player not ending up in scorn
1562 Coro::Timer::sleep 1;
1563 $pl->ob->leave_link;
1564 };
1565 }
1566 },
1567);
1568
1432=item $player_object->goto_map ($map, $x, $y) 1569=item $player_object->goto_map ($path, $x, $y)
1433 1570
1434=cut 1571=cut
1435 1572
1436sub cf::object::player::goto_map { 1573sub cf::object::player::goto_map {
1437 my ($self, $path, $x, $y) = @_; 1574 my ($self, $path, $x, $y) = @_;
1438 1575
1439 $self->enter_link; 1576 $self->enter_link;
1440 1577
1441 (Coro::async { 1578 (cf::async {
1442 $path = new cf::path $path; 1579 $path = new cf::path $path;
1443 1580
1444 my $map = cf::map::find_map $path->as_string; 1581 my $map = cf::map::find_map $path->as_string;
1445 $map = $map->customise_for ($self) if $map; 1582 $map = $map->customise_for ($self) if $map;
1446 1583
1447 warn "entering ", $map->path, " at ($x, $y)\n" 1584# warn "entering ", $map->path, " at ($x, $y)\n"
1448 if $map; 1585# if $map;
1586
1587 $map or $self->message ("The exit is closed", cf::NDI_UNIQUE | cf::NDI_RED);
1449 1588
1450 $self->leave_link ($map, $x, $y); 1589 $self->leave_link ($map, $x, $y);
1451 })->prio (1); 1590 })->prio (1);
1452} 1591}
1453 1592
1506 1645
1507 return unless $self->type == cf::PLAYER; 1646 return unless $self->type == cf::PLAYER;
1508 1647
1509 $self->enter_link; 1648 $self->enter_link;
1510 1649
1511 (Coro::async { 1650 (cf::async {
1512 unless (eval { 1651 unless (eval {
1513
1514 prepare_random_map $exit 1652 prepare_random_map $exit
1515 if $exit->slaying eq "/!"; 1653 if $exit->slaying eq "/!";
1516 1654
1517 my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path; 1655 my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path;
1518 $self->goto_map ($path, $exit->stats->hp, $exit->stats->sp); 1656 $self->goto_map ($path, $exit->stats->hp, $exit->stats->sp);
1580 on_reply => sub { 1718 on_reply => sub {
1581 my ($ns, $msg) = @_; 1719 my ($ns, $msg) = @_;
1582 1720
1583 # this weird shuffling is so that direct followup queries 1721 # this weird shuffling is so that direct followup queries
1584 # get handled first 1722 # get handled first
1585 my $queue = delete $ns->{query_queue}; 1723 my $queue = delete $ns->{query_queue}
1724 or return; # be conservative, not sure how that can happen, but we saw a crash here
1586 1725
1587 (shift @$queue)->[1]->($msg); 1726 (shift @$queue)->[1]->($msg);
1588 1727
1589 push @{ $ns->{query_queue} }, @$queue; 1728 push @{ $ns->{query_queue} }, @$queue;
1590 1729
1607=cut 1746=cut
1608 1747
1609sub cf::client::coro { 1748sub cf::client::coro {
1610 my ($self, $cb) = @_; 1749 my ($self, $cb) = @_;
1611 1750
1612 my $coro; $coro = async { 1751 my $coro = &cf::async ($cb);
1613 eval {
1614 $cb->();
1615 };
1616 warn $@ if $@;
1617 };
1618 1752
1619 $coro->on_destroy (sub { 1753 $coro->on_destroy (sub {
1620 delete $self->{_coro}{$coro+0}; 1754 delete $self->{_coro}{$coro+0};
1621 }); 1755 });
1622 1756
1794 1928
1795{ 1929{
1796 my $path = cf::localdir . "/database.pst"; 1930 my $path = cf::localdir . "/database.pst";
1797 1931
1798 sub db_load() { 1932 sub db_load() {
1799 warn "loading database $path\n";#d# remove later
1800 $DB = stat $path ? Storable::retrieve $path : { }; 1933 $DB = stat $path ? Storable::retrieve $path : { };
1801 } 1934 }
1802 1935
1803 my $pid; 1936 my $pid;
1804 1937
1805 sub db_save() { 1938 sub db_save() {
1806 warn "saving database $path\n";#d# remove later
1807 waitpid $pid, 0 if $pid; 1939 waitpid $pid, 0 if $pid;
1808 if (0 == ($pid = fork)) { 1940 if (0 == ($pid = fork)) {
1809 $DB->{_meta}{version} = 1; 1941 $DB->{_meta}{version} = 1;
1810 Storable::nstore $DB, "$path~"; 1942 Storable::nstore $DB, "$path~";
1811 rename "$path~", $path; 1943 rename "$path~", $path;
1859 open my $fh, "<:utf8", cf::confdir . "/config" 1991 open my $fh, "<:utf8", cf::confdir . "/config"
1860 or return; 1992 or return;
1861 1993
1862 local $/; 1994 local $/;
1863 *CFG = YAML::Syck::Load <$fh>; 1995 *CFG = YAML::Syck::Load <$fh>;
1996
1997 $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_105_115", 5, 37];
1998
1999 if (exists $CFG{mlockall}) {
2000 eval {
2001 $CFG{mlockall} ? &mlockall : &munlockall
2002 and die "WARNING: m(un)lockall failed: $!\n";
2003 };
2004 warn $@ if $@;
2005 }
1864} 2006}
1865 2007
1866sub main { 2008sub main {
1867 # we must not ever block the main coroutine 2009 # we must not ever block the main coroutine
1868 local $Coro::idle = sub { 2010 local $Coro::idle = sub {
1869 Carp::cluck "FATAL: Coro::idle was called, major BUG\n";#d# 2011 Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d#
1870 (Coro::unblock_sub { 2012 (Coro::unblock_sub {
1871 Event::one_event; 2013 Event::one_event;
1872 })->(); 2014 })->();
1873 }; 2015 };
1874 2016
2071 or warn "ERROR: unable to write runtime file: $!"; 2213 or warn "ERROR: unable to write runtime file: $!";
2072 })->(); 2214 })->();
2073 }, 2215 },
2074); 2216);
2075 2217
2218END { cf::emergency_save }
2219
20761 22201
2077 2221

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines