Overview
Comment: | Clean up serverbrowser.mm |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
4d3e20926014cf9b4cbc48b2636feb35 |
User & Date: | js on 2025-03-09 21:33:54 |
Other Links: | manifest | tags |
Context
2025-03-09
| ||
21:36 | Remove snprintf check-in: 8386716c19 user: js tags: trunk | |
21:33 | Clean up serverbrowser.mm check-in: 4d3e209260 user: js tags: trunk | |
18:57 | Convert dynent to a class check-in: d2b3ff790f user: js tags: trunk | |
Changes
Modified src/MD2.mm from [074abf7ae2] to [18fb7f56bd].
︙ | ︙ | |||
59 60 61 62 63 64 65 | @try { stream = (OFSeekableStream *)[[OFIRIHandler handlerForIRI:IRI] openItemAtIRI:IRI mode:@"r"]; } @catch (id e) { return false; } | | | 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | @try { stream = (OFSeekableStream *)[[OFIRIHandler handlerForIRI:IRI] openItemAtIRI:IRI mode:@"r"]; } @catch (id e) { return false; } if (![stream isKindOfClass:OFSeekableStream.class]) return false; md2_header header; [stream readIntoBuffer:&header exactLength:sizeof(md2_header)]; endianswap( &header, sizeof(int), sizeof(md2_header) / sizeof(int)); |
︙ | ︙ |
Modified src/MenuItem.m from [d1fadd8d50] to [b6800d39f8].
︙ | ︙ | |||
11 12 13 14 15 16 17 | return self; } - (OFComparisonResult)compare:(id)otherObject { MenuItem *otherItem; | | | 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | return self; } - (OFComparisonResult)compare:(id)otherObject { MenuItem *otherItem; if (![otherObject isKindOfClass:MenuItem.class]) @throw [OFInvalidArgumentException exception]; int x = (int)_text.longLongValue; int y = (int)otherItem.text.longLongValue; if (x > y) return OFOrderedAscending; |
︙ | ︙ |
Modified src/commands.mm from [e9d197474b] to [095c9bcfa3].
︙ | ︙ | |||
24 25 26 27 28 29 30 | persisted:true]; if (identifiers == nil) identifiers = [[OFMutableDictionary alloc] init]; identifiers[name] = alias; } else { | | | 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | persisted:true]; if (identifiers == nil) identifiers = [[OFMutableDictionary alloc] init]; identifiers[name] = alias; } else { if ([alias isKindOfClass:Alias.class]) alias.action = action; else conoutf( @"cannot redefine builtin %@ with an alias", name); } } COMMAND(alias, ARG_2STR) |
︙ | ︙ | |||
75 76 77 78 79 80 81 | } OFString * getalias(OFString *name) { Alias *alias = identifiers[name]; | | | 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | } OFString * getalias(OFString *name) { Alias *alias = identifiers[name]; if ([alias isKindOfClass:Alias.class]) return alias.action; return nil; } bool addcommand(OFString *name, void (*function)(), int argumentsTypes) |
︙ | ︙ | |||
166 167 168 169 170 171 172 | OFString * lookup(OFString *n) // find value of ident referenced with $ in exp { @autoreleasepool { __kindof Identifier *identifier = identifiers[[n substringFromIndex:1]]; | | | | 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | OFString * lookup(OFString *n) // find value of ident referenced with $ in exp { @autoreleasepool { __kindof Identifier *identifier = identifiers[[n substringFromIndex:1]]; if ([identifier isKindOfClass:Variable.class]) { return [OFString stringWithFormat:@"%d", *[identifier storage]]; } else if ([identifier isKindOfClass:Alias.class]) return [identifier action]; } conoutf(@"unknown alias lookup: %@", [n substringFromIndex:1]); return n; } |
︙ | ︙ | |||
234 235 236 237 238 239 240 | if (identifier == nil) { @try { val = (int)[c longLongValueWithBase:0]; } @catch (OFInvalidFormatException *e) { conoutf(@"unknown command: %@", c); } } else { | | < | < | | 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | if (identifier == nil) { @try { val = (int)[c longLongValueWithBase:0]; } @catch (OFInvalidFormatException *e) { conoutf(@"unknown command: %@", c); } } else { if ([identifier isKindOfClass:Command.class]) { // game defined commands use very // ad-hoc function signature, and just // call it OFArray<OFString *> *arguments = [[OFArray alloc] initWithObjects:w count:numargs]; val = [identifier callWithArguments:arguments isDown:isDown]; } else if ([identifier isKindOfClass:Variable.class]) { // game defined variables if (isDown) { if (w[1].length == 0) [identifier printValue]; else [identifier setValue: (int)[w[1] longLongValueWithBase: 0]]; } } else if ([identifier isKindOfClass:Alias.class]) { // alias, also used as functions and // (global) variables for (int i = 1; i < numargs; i++) { // set any arguments as // (global) arg values so // functions can access them OFString *t = [OFString |
︙ | ︙ | |||
388 389 390 391 392 393 394 | @"autoexec.cfg to override anything\n" @"\n"]; writeclientinfo(stream); [stream writeString:@"\n"]; [identifiers enumerateKeysAndObjectsUsingBlock:^( OFString *name, __kindof Identifier *identifier, bool *stop) { | | | | 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 | @"autoexec.cfg to override anything\n" @"\n"]; writeclientinfo(stream); [stream writeString:@"\n"]; [identifiers enumerateKeysAndObjectsUsingBlock:^( OFString *name, __kindof Identifier *identifier, bool *stop) { if (![identifier isKindOfClass:Variable.class] || ![identifier persisted]) return; [stream writeFormat:@"%@ %d\n", identifier.name, *[identifier storage]]; }]; [stream writeString:@"\n"]; writebinds(stream); [stream writeString:@"\n"]; [identifiers enumerateKeysAndObjectsUsingBlock:^( OFString *name, __kindof Identifier *identifier, bool *stop) { if (![identifier isKindOfClass:Alias.class] || [identifier.name hasPrefix:@"nextmap_"]) return; [stream writeFormat:@"alias \"%@\" [%@]\n", identifier.name, [identifier action]]; }]; |
︙ | ︙ |
Modified src/serverbrowser.mm from [7dc044a533] to [c76375cc88].
1 2 3 4 5 6 | // serverbrowser.cpp: eihrul's concurrent resolver, and server browser window // management #include "SDL_thread.h" #include "cube.h" | | > > > | | | < > | | | | > > > | | | < | | | | < | | > | < | | | > > | > | | | > | | | > | | > > | > | > > > > > > > > > > > > > > > | > > > > < > | | | < < | | | | | < | > > | | | > > > | | > | | | > | | < | | | < | | | | < > | | | | | | | > | | < | > | > | | | | > > > > > | | > | > > > > > > > > | > > > > > | < > > > | | > | < < < | < < < < | | | < | | | < | | | | | > | | | | | | > > > > > > < | | > > | > | < | | | > < | | > | > > | > < < < < < < < < | < | > | | > | > | < | | | | | < < < | > > | | | | | | > > > > | | > > > > > | > | > > | > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 | // serverbrowser.cpp: eihrul's concurrent resolver, and server browser window // management #include "SDL_thread.h" #include "cube.h" @interface ResolverThread: OFThread { volatile bool _stop; } @property (copy, nonatomic) OFString *query; @property (nonatomic) int starttime; @end @interface ResolverResult: OFObject @property (readonly, nonatomic) OFString *query; @property (readonly, nonatomic) ENetAddress address; - (instancetype)init OF_UNAVAILABLE; - (instancetype)initWithQuery:(OFString *)query address:(ENetAddress)address; @end static OFMutableArray<ResolverThread *> *resolverthreads; static OFMutableArray<OFString *> *resolverqueries; static OFMutableArray<ResolverResult *> *resolverresults; static SDL_sem *resolversem; static int resolverlimit = 1000; @implementation ResolverThread - (id)main { while (!_stop) { SDL_SemWait(resolversem); @synchronized(ResolverThread.class) { if (resolverqueries.count == 0) continue; _query = resolverqueries.lastObject; [resolverqueries removeObjectAtIndex:resolverqueries.count - 1]; _starttime = lastmillis; } ENetAddress address = { ENET_HOST_ANY, CUBE_SERVINFO_PORT }; enet_address_set_host(&address, _query.UTF8String); @synchronized(ResolverThread.class) { [resolverresults addObject:[[ResolverResult alloc] initWithQuery:_query address:address]]; _query = NULL; _starttime = 0; } } return nil; } - (void)stop { _stop = true; } @end @implementation ResolverResult - (instancetype)initWithQuery:(OFString *)query address:(ENetAddress)address { self = [super init]; _query = query; _address = address; return self; } @end void resolverinit(int threads, int limit) { resolverthreads = [[OFMutableArray alloc] init]; resolverqueries = [[OFMutableArray alloc] init]; resolverresults = [[OFMutableArray alloc] init]; resolverlimit = limit; resolversem = SDL_CreateSemaphore(0); while (threads > 0) { ResolverThread *rt = [[ResolverThread alloc] init]; rt.name = @"resolverthread"; [resolverthreads addObject:rt]; [rt start]; --threads; } } void resolverstop(size_t i, bool restart) { @synchronized(ResolverThread.class) { ResolverThread *rt = resolverthreads[i]; [rt stop]; if (restart) { rt = [[ResolverThread alloc] init]; rt.name = @"resolverthread"; resolverthreads[i] = rt; [rt start]; } else [resolverthreads removeObjectAtIndex:i]; } } void resolverclear() { @synchronized(ResolverThread.class) { [resolverqueries removeAllObjects]; [resolverresults removeAllObjects]; while (SDL_SemTryWait(resolversem) == 0) ; for (size_t i = 0; i < resolverthreads.count; i++) resolverstop(i, true); } } void resolverquery(OFString *name) { @synchronized(ResolverThread.class) { [resolverqueries addObject:name]; SDL_SemPost(resolversem); } } bool resolvercheck(OFString **name, ENetAddress *address) { @synchronized(ResolverThread.class) { if (resolverresults.count > 0) { ResolverResult *rr = resolverresults.lastObject; *name = rr.query; *address = rr.address; [resolverresults removeObjectAtIndex:resolverresults.count - 1]; return true; } for (size_t i = 0; i < resolverthreads.count; i++) { ResolverThread *rt = resolverthreads[i]; if (rt.query) { if (lastmillis - rt.starttime > resolverlimit) { resolverstop(i, true); *name = rt.query; return true; } } } } return false; } @interface ServerInfo: OFObject <OFComparing> @property (nonatomic) OFString *name; @property (nonatomic) OFString *full; @property (nonatomic) OFString *map; @property (nonatomic) OFString *sdesc; @property (nonatomic) int mode, numplayers, ping, protocol, minremain; @property (nonatomic) ENetAddress address; @end @implementation ServerInfo - (OFComparisonResult)compare:(id)otherObject { if (![otherObject isKindOfClass:ServerInfo.class]) @throw [OFInvalidArgumentException exception]; if (_ping > [otherObject ping]) return OFOrderedDescending; if (_ping < [otherObject ping]) return OFOrderedAscending; return [_name compare:[otherObject name]]; } @end static OFMutableArray<ServerInfo *> *servers; static ENetSocket pingsock = ENET_SOCKET_NULL; static int lastinfo = 0; OFString * getservername(int n) { return servers[n].name; } void addserver(OFString *servername) { @autoreleasepool { for (ServerInfo *si in servers) if ([si.name isEqual:servername]) return; ServerInfo *si = [[ServerInfo alloc] init]; si.name = servername; si.full = @""; si.mode = 0; si.numplayers = 0; si.ping = 9999; si.protocol = 0; si.minremain = 0; si.map = @""; si.sdesc = @""; ENetAddress address = { .host = ENET_HOST_ANY, .port = CUBE_SERVINFO_PORT }; si.address = address; if (servers == nil) servers = [[OFMutableArray alloc] init]; [servers addObject:si]; } } void pingservers() { ENetBuffer buf; uchar ping[MAXTRANS]; uchar *p; for (ServerInfo *si in servers) { if (si.address.host == ENET_HOST_ANY) continue; p = ping; putint(p, lastmillis); buf.data = ping; buf.dataLength = p - ping; ENetAddress address = si.address; enet_socket_send(pingsock, &address, &buf, 1); } lastinfo = lastmillis; } void checkresolver() { OFString *name = nil; ENetAddress addr = { ENET_HOST_ANY, CUBE_SERVINFO_PORT }; while (resolvercheck(&name, &addr)) { if (addr.host == ENET_HOST_ANY) continue; for (ServerInfo *si in servers) { if ([name isEqual:si.name]) { si.address = addr; addr.host = ENET_HOST_ANY; break; } } } } void checkpings() { enet_uint32 events = ENET_SOCKET_WAIT_RECEIVE; ENetBuffer buf; ENetAddress addr; uchar ping[MAXTRANS], *p; char text[MAXTRANS]; buf.data = ping; buf.dataLength = sizeof(ping); while (enet_socket_wait(pingsock, &events, 0) >= 0 && events) { if (enet_socket_receive(pingsock, &addr, &buf, 1) <= 0) return; for (ServerInfo *si in servers) { if (addr.host == si.address.host) { p = ping; si.ping = lastmillis - getint(p); si.protocol = getint(p); if (si.protocol != PROTOCOL_VERSION) si.ping = 9998; si.mode = getint(p); si.numplayers = getint(p); si.minremain = getint(p); sgetstr(); @autoreleasepool { si.map = @(text); } sgetstr(); @autoreleasepool { si.sdesc = @(text); } break; } } } } void refreshservers() { checkresolver(); checkpings(); if (lastmillis - lastinfo >= 5000) pingservers(); [servers sort]; int maxmenu = 16; size_t i = 0; for (ServerInfo *si in servers) { if (si.address.host != ENET_HOST_ANY && si.ping != 9999) { if (si.protocol != PROTOCOL_VERSION) si.full = [[OFString alloc] initWithFormat: @"%@ [different cube protocol]", si.name]; else si.full = [[OFString alloc] initWithFormat:@"%d\t%d\t%@, %@: %@ %@", si.ping, si.numplayers, si.map.length > 0 ? si.map : @"[unknown]", modestr(si.mode), si.name, si.sdesc]; } else si.full = [[OFString alloc] initWithFormat: (si.address.host != ENET_HOST_ANY ? @"%@ [waiting for server response]" : @"%@ [unknown host]\t"), si.name]; // cut off too long server descriptions @autoreleasepool { if (si.full.length > 50) si.full = [si.full substringToIndex:50]; } menumanual(1, i, si.full); if (!--maxmenu) return; i++; } } void servermenu() { if (pingsock == ENET_SOCKET_NULL) { pingsock = enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM, NULL); resolverinit(1, 1000); } resolverclear(); for (ServerInfo *si in servers) resolverquery(si.name); refreshservers(); menuset(1); } void updatefrommaster() { const int MAXUPD = 32000; uchar buf[MAXUPD]; uchar *reply = retrieveservers(buf, MAXUPD); if (!*reply || strstr((char *)reply, "<html>") || strstr((char *)reply, "<HTML>")) conoutf(@"master server not replying"); else { [servers removeAllObjects]; @autoreleasepool { execute(@((char *)reply)); } } servermenu(); } COMMAND(addserver, ARG_1STR) COMMAND(servermenu, ARG_NONE) COMMAND(updatefrommaster, ARG_NONE) void writeservercfg() { FILE *f = fopen("servers.cfg", "w"); if (!f) return; fprintf(f, "// servers connected to are added here automatically\n\n"); @autoreleasepool { for (ServerInfo *si in servers.reversedArray) fprintf(f, "addserver %s\n", si.name.UTF8String); } fclose(f); } |