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.131 by root, Thu Jan 4 00:53:54 2007 UTC vs.
Revision 1.134 by root, Thu Jan 4 17:28:49 2007 UTC

49our $UPTIME; $UPTIME ||= time; 49our $UPTIME; $UPTIME ||= time;
50our $RUNTIME; 50our $RUNTIME;
51 51
52our %MAP; # all maps 52our %MAP; # all maps
53our $LINK_MAP; # the special {link} map 53our $LINK_MAP; # the special {link} map
54our $FREEZE;
55our $RANDOM_MAPS = cf::localdir . "/random"; 54our $RANDOM_MAPS = cf::localdir . "/random";
56our %EXT_CORO; 55our %EXT_CORO;
57 56
58binmode STDOUT; 57binmode STDOUT;
59binmode STDERR; 58binmode STDERR;
213Wait until the given lock is available and then acquires it and returns 212Wait 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, 213a guard object. If the guard object gets destroyed (goes out of scope,
215for example when the coroutine gets canceled), the lock is automatically 214for example when the coroutine gets canceled), the lock is automatically
216returned. 215returned.
217 216
218Lock names should begin with a unique identifier (for example, find_map 217Lock names should begin with a unique identifier (for example, cf::map::find
219uses map_find and load_map uses map_load). 218uses map_find and cf::map::load uses map_load).
220 219
221=cut 220=cut
222 221
223our %LOCK; 222our %LOCK;
224 223
258 257
259 Coro::async { 258 Coro::async {
260 eval { $cb->() }; 259 eval { $cb->() };
261 warn $@ if $@; 260 warn $@ if $@;
262 } 261 }
262}
263
264sub freeze_mainloop {
265 return unless $TICK_WATCHER->is_active;
266
267 my $guard = guard { $TICK_WATCHER->start };
268 $TICK_WATCHER->stop;
269 $guard
263} 270}
264 271
265=item cf::sync_job { BLOCK } 272=item cf::sync_job { BLOCK }
266 273
267The design of crossfire+ requires that the main coro ($Coro::main) is 274The design of crossfire+ requires that the main coro ($Coro::main) is
282 # this is the main coro, too bad, we have to block 289 # this is the main coro, too bad, we have to block
283 # till the operation succeeds, freezing the server :/ 290 # till the operation succeeds, freezing the server :/
284 291
285 # TODO: use suspend/resume instead 292 # TODO: use suspend/resume instead
286 # (but this is cancel-safe) 293 # (but this is cancel-safe)
287 local $FREEZE = 1; 294 my $freeze_guard = freeze_mainloop;
288 295
289 my $busy = 1; 296 my $busy = 1;
290 my @res; 297 my @res;
291 298
292 (Coro::async { 299 (Coro::async {
1136package cf::map; 1143package cf::map;
1137 1144
1138use Fcntl; 1145use Fcntl;
1139use Coro::AIO; 1146use Coro::AIO;
1140 1147
1141our $MAX_RESET = 7200; 1148our $MAX_RESET = 3600;
1142our $DEFAULT_RESET = 3600; 1149our $DEFAULT_RESET = 3000;
1143 1150
1144sub generate_random_map { 1151sub generate_random_map {
1145 my ($path, $rmp) = @_; 1152 my ($path, $rmp) = @_;
1146 1153
1147 # mit "rum" bekleckern, nicht 1154 # mit "rum" bekleckern, nicht
1185 $map->{load_path} = $path; 1192 $map->{load_path} = $path;
1186 1193
1187 $map 1194 $map
1188} 1195}
1189 1196
1190sub find_map; 1197sub find;
1191sub find_map { 1198sub find {
1192 my ($path, $origin) = @_; 1199 my ($path, $origin) = @_;
1193 1200
1194 #warn "find_map<$path,$origin>\n";#d# 1201 #warn "find<$path,$origin>\n";#d#
1195 1202
1196 $path = new cf::path $path, $origin && $origin->path; 1203 $path = new cf::path $path, $origin && $origin->path;
1197 my $key = $path->as_string; 1204 my $key = $path->as_string;
1198 1205
1199 cf::lock_wait "map_find:$key"; 1206 cf::lock_wait "map_find:$key";
1202 my $guard = cf::lock_acquire "map_find:$key"; 1209 my $guard = cf::lock_acquire "map_find:$key";
1203 1210
1204 # do it the slow way 1211 # do it the slow way
1205 my $map = try_load_header $path->save_path; 1212 my $map = try_load_header $path->save_path;
1206 1213
1214 Coro::cede;
1215
1207 if ($map) { 1216 if ($map) {
1217 $map->last_access ((delete $map->{last_access})
1218 || $cf::RUNTIME); #d#
1208 # safety 1219 # safety
1209 $map->{instantiate_time} = $cf::RUNTIME 1220 $map->{instantiate_time} = $cf::RUNTIME
1210 if $map->{instantiate_time} > $cf::RUNTIME; 1221 if $map->{instantiate_time} > $cf::RUNTIME;
1211 } else { 1222 } else {
1212 if (my $rmp = $path->random_map_params) { 1223 if (my $rmp = $path->random_map_params) {
1217 1228
1218 $map or return; 1229 $map or return;
1219 1230
1220 $map->{load_original} = 1; 1231 $map->{load_original} = 1;
1221 $map->{instantiate_time} = $cf::RUNTIME; 1232 $map->{instantiate_time} = $cf::RUNTIME;
1233 $map->last_access ($cf::RUNTIME);
1222 $map->instantiate; 1234 $map->instantiate;
1223 1235
1224 # per-player maps become, after loading, normal maps 1236 # per-player maps become, after loading, normal maps
1225 $map->per_player (0) if $path->{user_rel}; 1237 $map->per_player (0) if $path->{user_rel};
1226 } 1238 }
1227 1239
1228 $map->path ($key); 1240 $map->path ($key);
1229 $map->{path} = $path; 1241 $map->{path} = $path;
1230 $map->{last_save} = $cf::RUNTIME; 1242 $map->{last_save} = $cf::RUNTIME;
1231 $map->last_access ($cf::RUNTIME); 1243
1244 Coro::cede;
1232 1245
1233 if ($map->should_reset) { 1246 if ($map->should_reset) {
1234 $map->reset; 1247 $map->reset;
1235 undef $guard; 1248 undef $guard;
1236 $map = find_map $path 1249 $map = find $path
1237 or return; 1250 or return;
1238 } 1251 }
1239 1252
1240 $cf::MAP{$key} = $map 1253 $cf::MAP{$key} = $map
1241 } 1254 }
1263 if (aio_open $uniq, O_RDONLY, 0) { 1276 if (aio_open $uniq, O_RDONLY, 0) {
1264 $self->clear_unique_items; 1277 $self->clear_unique_items;
1265 $self->load_objects ($uniq, 0); 1278 $self->load_objects ($uniq, 0);
1266 } 1279 }
1267 } 1280 }
1281
1282 Coro::cede;
1268 1283
1269 # now do the right thing for maps 1284 # now do the right thing for maps
1270 $self->link_multipart_objects; 1285 $self->link_multipart_objects;
1271 1286
1272 if ($self->{path}->is_style_map) { 1287 if ($self->{path}->is_style_map) {
1280 $self->difficulty ($self->estimate_difficulty) 1295 $self->difficulty ($self->estimate_difficulty)
1281 unless $self->difficulty; 1296 unless $self->difficulty;
1282 $self->activate; 1297 $self->activate;
1283 } 1298 }
1284 1299
1300 Coro::cede;
1301
1285 $self->in_memory (cf::MAP_IN_MEMORY); 1302 $self->in_memory (cf::MAP_IN_MEMORY);
1286} 1303}
1287 1304
1288sub load_map_sync { 1305sub find_sync {
1289 my ($path, $origin) = @_; 1306 my ($path, $origin) = @_;
1290 1307
1291 #warn "load_map_sync<$path, $origin>\n";#d# 1308 cf::sync_job { cf::map::find $path, $origin }
1309}
1292 1310
1293 cf::sync_job { 1311sub do_load_sync {
1294 my $map = cf::map::find_map $path, $origin 1312 my ($map) = @_;
1295 or return; 1313
1296 $map->load; 1314 cf::sync_job { $map->load };
1297 $map
1298 }
1299} 1315}
1300 1316
1301sub save { 1317sub save {
1302 my ($self) = @_; 1318 my ($self) = @_;
1303 1319
1309 my $uniq = $self->{path}->uniq_path; utf8::encode $uniq; 1325 my $uniq = $self->{path}->uniq_path; utf8::encode $uniq;
1310 1326
1311 $self->{load_path} = $save; 1327 $self->{load_path} = $save;
1312 1328
1313 return if $self->{deny_save}; 1329 return if $self->{deny_save};
1330
1331 local $self->{last_access} = $self->last_access;#d#
1314 1332
1315 if ($uniq) { 1333 if ($uniq) {
1316 $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS); 1334 $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS);
1317 $self->save_objects ($uniq, cf::IO_UNIQUES); 1335 $self->save_objects ($uniq, cf::IO_UNIQUES);
1318 } else { 1336 } else {
1403 1421
1404sub customise_for { 1422sub customise_for {
1405 my ($map, $ob) = @_; 1423 my ($map, $ob) = @_;
1406 1424
1407 if ($map->per_player) { 1425 if ($map->per_player) {
1408 return cf::map::find_map "~" . $ob->name . "/" . $map->{path}{path}; 1426 return cf::map::find "~" . $ob->name . "/" . $map->{path}{path};
1409 } 1427 }
1410 1428
1411 $map 1429 $map
1412} 1430}
1413 1431
1414sub emergency_save { 1432sub emergency_save {
1415 local $cf::FREEZE = 1; 1433 my $freeze_guard = cf::freeze_mainloop;
1416 1434
1417 warn "enter emergency map save\n"; 1435 warn "enter emergency map save\n";
1418 1436
1419 cf::sync_job { 1437 cf::sync_job {
1420 warn "begin emergency map save\n"; 1438 warn "begin emergency map save\n";
1510 my $link_pos = delete $self->{_link_pos}; 1528 my $link_pos = delete $self->{_link_pos};
1511 1529
1512 unless ($map) { 1530 unless ($map) {
1513 # restore original map position 1531 # restore original map position
1514 ($map, $x, $y) = @{ $link_pos || [] }; 1532 ($map, $x, $y) = @{ $link_pos || [] };
1515 $map = cf::map::find_map $map; 1533 $map = cf::map::find $map;
1516 1534
1517 unless ($map) { 1535 unless ($map) {
1518 ($map, $x, $y) = @$EMERGENCY_POSITION; 1536 ($map, $x, $y) = @$EMERGENCY_POSITION;
1519 $map = cf::map::find_map $map 1537 $map = cf::map::find $map
1520 or die "FATAL: cannot load emergency map\n"; 1538 or die "FATAL: cannot load emergency map\n";
1521 } 1539 }
1522 } 1540 }
1523 1541
1524 ($x, $y) = (-1, -1) 1542 ($x, $y) = (-1, -1)
1572 $self->enter_link; 1590 $self->enter_link;
1573 1591
1574 (cf::async { 1592 (cf::async {
1575 $path = new cf::path $path; 1593 $path = new cf::path $path;
1576 1594
1577 my $map = cf::map::find_map $path->as_string; 1595 my $map = cf::map::find $path->as_string;
1578 $map = $map->customise_for ($self) if $map; 1596 $map = $map->customise_for ($self) if $map;
1579 1597
1580# warn "entering ", $map->path, " at ($x, $y)\n" 1598# warn "entering ", $map->path, " at ($x, $y)\n"
1581# if $map; 1599# if $map;
1582 1600
1642 return unless $self->type == cf::PLAYER; 1660 return unless $self->type == cf::PLAYER;
1643 1661
1644 $self->enter_link; 1662 $self->enter_link;
1645 1663
1646 (cf::async { 1664 (cf::async {
1665 $self->deactivate_recursive; # just to be sure
1647 unless (eval { 1666 unless (eval {
1648 prepare_random_map $exit 1667 prepare_random_map $exit
1649 if $exit->slaying eq "/!"; 1668 if $exit->slaying eq "/!";
1650 1669
1651 my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path; 1670 my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path;
2026 return; 2045 return;
2027 } 2046 }
2028 2047
2029 warn "reloading..."; 2048 warn "reloading...";
2030 2049
2031 local $FREEZE = 1; 2050 my $guard = freeze_mainloop;
2032 cf::emergency_save; 2051 cf::emergency_save;
2033 2052
2034 eval { 2053 eval {
2035 # if anything goes wrong in here, we should simply crash as we already saved 2054 # if anything goes wrong in here, we should simply crash as we already saved
2036 2055
2172 reentrant => 0, 2191 reentrant => 0,
2173 prio => 0, 2192 prio => 0,
2174 at => $NEXT_TICK || $TICK, 2193 at => $NEXT_TICK || $TICK,
2175 data => WF_AUTOCANCEL, 2194 data => WF_AUTOCANCEL,
2176 cb => sub { 2195 cb => sub {
2177 unless ($FREEZE) {
2178 cf::server_tick; # one server iteration 2196 cf::server_tick; # one server iteration
2179 $RUNTIME += $TICK; 2197 $RUNTIME += $TICK;
2180 }
2181
2182 $NEXT_TICK += $TICK; 2198 $NEXT_TICK += $TICK;
2183 2199
2184 # if we are delayed by four ticks or more, skip them all 2200 # if we are delayed by four ticks or more, skip them all
2185 $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; 2201 $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4;
2186 2202

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines