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.132 by root, Thu Jan 4 01:35:56 2007 UTC vs.
Revision 1.137 by root, Fri Jan 5 10:23:05 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;
180sub to_json($) { 179sub to_json($) {
181 $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs 180 $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs
182 JSON::Syck::Dump $_[0] 181 JSON::Syck::Dump $_[0]
183} 182}
184 183
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 184=item cf::lock_wait $string
208 185
209Wait until the given lock is available. See cf::lock_acquire. 186Wait until the given lock is available. See cf::lock_acquire.
210 187
211=item my $lock = cf::lock_acquire $string 188=item my $lock = cf::lock_acquire $string
212 189
213Wait until the given lock is available and then acquires it and returns 190Wait 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, 191a Coro::guard object. If the guard object gets destroyed (goes out of scope,
215for example when the coroutine gets canceled), the lock is automatically 192for example when the coroutine gets canceled), the lock is automatically
216returned. 193returned.
217 194
218Lock names should begin with a unique identifier (for example, find_map 195Lock names should begin with a unique identifier (for example, cf::map::find
219uses map_find and load_map uses map_load). 196uses map_find and cf::map::load uses map_load).
220 197
221=cut 198=cut
222 199
223our %LOCK; 200our %LOCK;
224 201
238 # wait, to be sure we are not locked 215 # wait, to be sure we are not locked
239 lock_wait $key; 216 lock_wait $key;
240 217
241 $LOCK{$key} = []; 218 $LOCK{$key} = [];
242 219
243 cf::guard { 220 Coro::guard {
244 # wake up all waiters, to be on the safe side 221 # wake up all waiters, to be on the safe side
245 $_->ready for @{ delete $LOCK{$key} }; 222 $_->ready for @{ delete $LOCK{$key} };
246 } 223 }
247} 224}
248 225
258 235
259 Coro::async { 236 Coro::async {
260 eval { $cb->() }; 237 eval { $cb->() };
261 warn $@ if $@; 238 warn $@ if $@;
262 } 239 }
240}
241
242sub freeze_mainloop {
243 return unless $TICK_WATCHER->is_active;
244
245 my $guard = Coro::guard { $TICK_WATCHER->start };
246 $TICK_WATCHER->stop;
247 $guard
263} 248}
264 249
265=item cf::sync_job { BLOCK } 250=item cf::sync_job { BLOCK }
266 251
267The design of crossfire+ requires that the main coro ($Coro::main) is 252The design of crossfire+ requires that the main coro ($Coro::main) is
282 # this is the main coro, too bad, we have to block 267 # this is the main coro, too bad, we have to block
283 # till the operation succeeds, freezing the server :/ 268 # till the operation succeeds, freezing the server :/
284 269
285 # TODO: use suspend/resume instead 270 # TODO: use suspend/resume instead
286 # (but this is cancel-safe) 271 # (but this is cancel-safe)
287 local $FREEZE = 1; 272 my $freeze_guard = freeze_mainloop;
288 273
289 my $busy = 1; 274 my $busy = 1;
290 my @res; 275 my @res;
291 276
292 (Coro::async { 277 (Coro::async {
1136package cf::map; 1121package cf::map;
1137 1122
1138use Fcntl; 1123use Fcntl;
1139use Coro::AIO; 1124use Coro::AIO;
1140 1125
1141our $MAX_RESET = 7200; 1126our $MAX_RESET = 3600;
1142our $DEFAULT_RESET = 3600; 1127our $DEFAULT_RESET = 3000;
1143 1128
1144sub generate_random_map { 1129sub generate_random_map {
1145 my ($path, $rmp) = @_; 1130 my ($path, $rmp) = @_;
1146 1131
1147 # mit "rum" bekleckern, nicht 1132 # mit "rum" bekleckern, nicht
1177 or return; 1162 or return;
1178 1163
1179 my $map = cf::map::new 1164 my $map = cf::map::new
1180 or return; 1165 or return;
1181 1166
1167 # for better error messages only, will be overwritten
1168 $map->path ($path);
1169
1182 $map->load_header ($path) 1170 $map->load_header ($path)
1183 or return; 1171 or return;
1184 1172
1185 $map->{load_path} = $path; 1173 $map->{load_path} = $path;
1186 1174
1187 $map 1175 $map
1188} 1176}
1189 1177
1190sub find_map; 1178sub find;
1191sub find_map { 1179sub find {
1192 my ($path, $origin) = @_; 1180 my ($path, $origin) = @_;
1193 1181
1194 #warn "find_map<$path,$origin>\n";#d# 1182 #warn "find<$path,$origin>\n";#d#
1195 1183
1196 $path = new cf::path $path, $origin && $origin->path; 1184 $path = new cf::path $path, $origin && $origin->path;
1197 my $key = $path->as_string; 1185 my $key = $path->as_string;
1198 1186
1199 cf::lock_wait "map_find:$key"; 1187 cf::lock_wait "map_find:$key";
1201 $cf::MAP{$key} || do { 1189 $cf::MAP{$key} || do {
1202 my $guard = cf::lock_acquire "map_find:$key"; 1190 my $guard = cf::lock_acquire "map_find:$key";
1203 1191
1204 # do it the slow way 1192 # do it the slow way
1205 my $map = try_load_header $path->save_path; 1193 my $map = try_load_header $path->save_path;
1194
1195 Coro::cede;
1206 1196
1207 if ($map) { 1197 if ($map) {
1208 $map->last_access ((delete $map->{last_access}) 1198 $map->last_access ((delete $map->{last_access})
1209 || $cf::RUNTIME); #d# 1199 || $cf::RUNTIME); #d#
1210 # safety 1200 # safety
1230 1220
1231 $map->path ($key); 1221 $map->path ($key);
1232 $map->{path} = $path; 1222 $map->{path} = $path;
1233 $map->{last_save} = $cf::RUNTIME; 1223 $map->{last_save} = $cf::RUNTIME;
1234 1224
1225 Coro::cede;
1226
1235 if ($map->should_reset) { 1227 if ($map->should_reset) {
1236 $map->reset; 1228 $map->reset;
1237 undef $guard; 1229 undef $guard;
1238 $map = find_map $path 1230 $map = find $path
1239 or return; 1231 or return;
1240 } 1232 }
1241 1233
1242 $cf::MAP{$key} = $map 1234 $cf::MAP{$key} = $map
1243 } 1235 }
1265 if (aio_open $uniq, O_RDONLY, 0) { 1257 if (aio_open $uniq, O_RDONLY, 0) {
1266 $self->clear_unique_items; 1258 $self->clear_unique_items;
1267 $self->load_objects ($uniq, 0); 1259 $self->load_objects ($uniq, 0);
1268 } 1260 }
1269 } 1261 }
1262
1263 Coro::cede;
1270 1264
1271 # now do the right thing for maps 1265 # now do the right thing for maps
1272 $self->link_multipart_objects; 1266 $self->link_multipart_objects;
1273 1267
1274 if ($self->{path}->is_style_map) { 1268 if ($self->{path}->is_style_map) {
1282 $self->difficulty ($self->estimate_difficulty) 1276 $self->difficulty ($self->estimate_difficulty)
1283 unless $self->difficulty; 1277 unless $self->difficulty;
1284 $self->activate; 1278 $self->activate;
1285 } 1279 }
1286 1280
1281 Coro::cede;
1282
1287 $self->in_memory (cf::MAP_IN_MEMORY); 1283 $self->in_memory (cf::MAP_IN_MEMORY);
1288} 1284}
1289 1285
1290sub load_map_sync { 1286sub find_sync {
1291 my ($path, $origin) = @_; 1287 my ($path, $origin) = @_;
1292 1288
1293 #warn "load_map_sync<$path, $origin>\n";#d# 1289 cf::sync_job { cf::map::find $path, $origin }
1290}
1294 1291
1295 cf::sync_job { 1292sub do_load_sync {
1296 my $map = cf::map::find_map $path, $origin 1293 my ($map) = @_;
1297 or return; 1294
1298 $map->load; 1295 cf::sync_job { $map->load };
1299 $map
1300 }
1301} 1296}
1302 1297
1303sub save { 1298sub save {
1304 my ($self) = @_; 1299 my ($self) = @_;
1300
1301 my $lock = cf::lock_acquire "map_data:" . $self->path;
1305 1302
1306 $self->{last_save} = $cf::RUNTIME; 1303 $self->{last_save} = $cf::RUNTIME;
1307 1304
1308 return unless $self->dirty; 1305 return unless $self->dirty;
1309 1306
1328 my ($self) = @_; 1325 my ($self) = @_;
1329 1326
1330 # save first because save cedes 1327 # save first because save cedes
1331 $self->save; 1328 $self->save;
1332 1329
1330 my $lock = cf::lock_acquire "map_data:" . $self->path;
1331
1333 return if $self->players; 1332 return if $self->players;
1334 return if $self->in_memory != cf::MAP_IN_MEMORY; 1333 return if $self->in_memory != cf::MAP_IN_MEMORY;
1335 return if $self->{deny_save}; 1334 return if $self->{deny_save};
1336 1335
1337 $self->clear; 1336 $self->clear;
1379} 1378}
1380 1379
1381sub reset { 1380sub reset {
1382 my ($self) = @_; 1381 my ($self) = @_;
1383 1382
1383 my $lock = cf::lock_acquire "map_data:" . $self->path;
1384
1384 return if $self->players; 1385 return if $self->players;
1385 return if $self->{path}{user_rel};#d# 1386 return if $self->{path}{user_rel};#d#
1386 1387
1387 warn "resetting map ", $self->path;#d# 1388 warn "resetting map ", $self->path;#d#
1388 1389
1407 1408
1408sub customise_for { 1409sub customise_for {
1409 my ($map, $ob) = @_; 1410 my ($map, $ob) = @_;
1410 1411
1411 if ($map->per_player) { 1412 if ($map->per_player) {
1412 return cf::map::find_map "~" . $ob->name . "/" . $map->{path}{path}; 1413 return cf::map::find "~" . $ob->name . "/" . $map->{path}{path};
1413 } 1414 }
1414 1415
1415 $map 1416 $map
1416} 1417}
1417 1418
1418sub emergency_save { 1419sub emergency_save {
1419 local $cf::FREEZE = 1; 1420 my $freeze_guard = cf::freeze_mainloop;
1420 1421
1421 warn "enter emergency map save\n"; 1422 warn "enter emergency map save\n";
1422 1423
1423 cf::sync_job { 1424 cf::sync_job {
1424 warn "begin emergency map save\n"; 1425 warn "begin emergency map save\n";
1514 my $link_pos = delete $self->{_link_pos}; 1515 my $link_pos = delete $self->{_link_pos};
1515 1516
1516 unless ($map) { 1517 unless ($map) {
1517 # restore original map position 1518 # restore original map position
1518 ($map, $x, $y) = @{ $link_pos || [] }; 1519 ($map, $x, $y) = @{ $link_pos || [] };
1519 $map = cf::map::find_map $map; 1520 $map = cf::map::find $map;
1520 1521
1521 unless ($map) { 1522 unless ($map) {
1522 ($map, $x, $y) = @$EMERGENCY_POSITION; 1523 ($map, $x, $y) = @$EMERGENCY_POSITION;
1523 $map = cf::map::find_map $map 1524 $map = cf::map::find $map
1524 or die "FATAL: cannot load emergency map\n"; 1525 or die "FATAL: cannot load emergency map\n";
1525 } 1526 }
1526 } 1527 }
1527 1528
1528 ($x, $y) = (-1, -1) 1529 ($x, $y) = (-1, -1)
1564 }; 1565 };
1565 } 1566 }
1566 }, 1567 },
1567); 1568);
1568 1569
1569=item $player_object->goto_map ($path, $x, $y) 1570=item $player_object->goto ($path, $x, $y)
1570 1571
1571=cut 1572=cut
1572 1573
1573sub cf::object::player::goto_map { 1574sub cf::object::player::goto {
1574 my ($self, $path, $x, $y) = @_; 1575 my ($self, $path, $x, $y) = @_;
1575 1576
1576 $self->enter_link; 1577 $self->enter_link;
1577 1578
1578 (cf::async { 1579 (cf::async {
1579 $path = new cf::path $path; 1580 $path = new cf::path $path;
1580 1581
1581 my $map = cf::map::find_map $path->as_string; 1582 my $map = cf::map::find $path->as_string;
1582 $map = $map->customise_for ($self) if $map; 1583 $map = $map->customise_for ($self) if $map;
1583 1584
1584# warn "entering ", $map->path, " at ($x, $y)\n" 1585# warn "entering ", $map->path, " at ($x, $y)\n"
1585# if $map; 1586# if $map;
1586 1587
1646 return unless $self->type == cf::PLAYER; 1647 return unless $self->type == cf::PLAYER;
1647 1648
1648 $self->enter_link; 1649 $self->enter_link;
1649 1650
1650 (cf::async { 1651 (cf::async {
1652 $self->deactivate_recursive; # just to be sure
1651 unless (eval { 1653 unless (eval {
1652 prepare_random_map $exit 1654 prepare_random_map $exit
1653 if $exit->slaying eq "/!"; 1655 if $exit->slaying eq "/!";
1654 1656
1655 my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path; 1657 my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path;
1656 $self->goto_map ($path, $exit->stats->hp, $exit->stats->sp); 1658 $self->goto ($path, $exit->stats->hp, $exit->stats->sp);
1657 1659
1658 1; 1660 1;
1659 }) { 1661 }) {
1660 $self->message ("Something went wrong deep within the crossfire server. " 1662 $self->message ("Something went wrong deep within the crossfire server. "
1661 . "I'll try to bring you back to the map you were before. " 1663 . "I'll try to bring you back to the map you were before. "
2030 return; 2032 return;
2031 } 2033 }
2032 2034
2033 warn "reloading..."; 2035 warn "reloading...";
2034 2036
2035 local $FREEZE = 1; 2037 my $guard = freeze_mainloop;
2036 cf::emergency_save; 2038 cf::emergency_save;
2037 2039
2038 eval { 2040 eval {
2039 # if anything goes wrong in here, we should simply crash as we already saved 2041 # if anything goes wrong in here, we should simply crash as we already saved
2040 2042
2176 reentrant => 0, 2178 reentrant => 0,
2177 prio => 0, 2179 prio => 0,
2178 at => $NEXT_TICK || $TICK, 2180 at => $NEXT_TICK || $TICK,
2179 data => WF_AUTOCANCEL, 2181 data => WF_AUTOCANCEL,
2180 cb => sub { 2182 cb => sub {
2181 unless ($FREEZE) {
2182 cf::server_tick; # one server iteration 2183 cf::server_tick; # one server iteration
2183 $RUNTIME += $TICK; 2184 $RUNTIME += $TICK;
2184 }
2185
2186 $NEXT_TICK += $TICK; 2185 $NEXT_TICK += $TICK;
2187 2186
2188 # if we are delayed by four ticks or more, skip them all 2187 # if we are delayed by four ticks or more, skip them all
2189 $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; 2188 $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4;
2190 2189

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines