~ubuntu-branches/debian/lenny/ggz-server/lenny

« back to all changes in this revision

Viewing changes to game_servers/ggzcards/main.c

  • Committer: Bazaar Package Importer
  • Author(s): Neil Williams
  • Date: 2008-08-30 22:28:31 UTC
  • mfrom: (3.1.5 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080830222831-lt2l462usl5opxfd
Tags: 0.0.14.1-1.2
* Non-maintainer upload.
* Improve the database.m4 fix to migrate to libdb4.6
  instead of 4.4 {request from Steve Langasek.}

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
 * Project: GGZCards Server
5
5
 * Date: 06/29/2000
6
6
 * Desc: Main loop
7
 
 * $Id: main.c 6077 2004-07-11 04:28:48Z jdorje $
 
7
 * $Id: main.c 8427 2006-07-31 22:50:50Z jdorje $
8
8
 *
9
9
 * This file was originally taken from La Pocha by Rich Gade.  It just
10
10
 * contains the startup, command-line option handling, and main loop
121
121
        int i;
122
122
        game_data_t *game_data = NULL;
123
123
        GGZdMod *ggz;
124
 
        
 
124
 
 
125
        if (!ggzdmod_is_ggz_mode()) {
 
126
                printf("This program should only be run from within GGZ.");
 
127
                printf("\n");
 
128
                exit(1);
 
129
        }
 
130
 
125
131
        initialize_debugging();
126
132
 
127
133
        /* Initialize GGZ structures. */
217
223
           the bot channels. */
218
224
        do {
219
225
                /* this is a whole lot of unnecessary code... */
220
 
                fd_set fdset;
 
226
                fd_set read_fd_set, write_fd_set;
221
227
                int max_fd = ggzdmod_get_fd(ggz), status;
222
228
 
223
 
                FD_ZERO(&fdset);
224
 
                FD_SET(max_fd, &fdset);
 
229
                FD_ZERO(&read_fd_set);
 
230
                FD_SET(max_fd, &read_fd_set);
 
231
 
 
232
                FD_ZERO(&write_fd_set);
225
233
 
226
234
                /* Assemble a list of file descriptors to monitor.  This list
227
235
                   includes the main GGZ connection, a connection for each
228
236
                   player (including bots), plus a stderr connection for bots.
229
237
                 */
230
238
                allplayers_iterate(p) {
231
 
                        int fd = get_player_socket(p);
 
239
                        GGZDataIO *dio = get_player_dio(p);
 
240
                        int fd = dio ? ggz_dio_get_socket(dio) : -1;
 
241
 
232
242
                        if (fd >= 0) {
233
243
                                max_fd = MAX(max_fd, fd);
234
 
                                assert(!FD_ISSET(fd, &fdset));
235
 
                                FD_SET(fd, &fdset);
 
244
 
 
245
#if 0 /* With nonatomic seat ops this is possible now. */
 
246
                                assert(!FD_ISSET(fd, &read_fd_set));
 
247
                                assert(!FD_ISSET(fd, &write_fd_set));
 
248
#endif
 
249
 
 
250
                                FD_SET(fd, &read_fd_set);
 
251
                                if (ggz_dio_is_write_pending(dio)) {
 
252
                                        FD_SET(fd, &write_fd_set);
 
253
                                }
236
254
                        }
237
255
 
238
256
#ifdef DEBUG
240
258
                                fd = game.players[p].err_fd;
241
259
                                if (fd >= 0) {
242
260
                                        max_fd = MAX(max_fd, fd);
243
 
                                        assert(!FD_ISSET(fd, &fdset));
244
 
                                        FD_SET(fd, &fdset);
 
261
                                        assert(!FD_ISSET(fd, &read_fd_set));
 
262
                                        FD_SET(fd, &read_fd_set);
245
263
                                }
246
264
                        }
247
265
#endif /* DEBUG */
248
266
                } allplayers_iterate_end;
249
267
 
250
 
                status = select(max_fd + 1, &fdset, NULL, NULL, NULL);
 
268
                status = select(max_fd + 1, &read_fd_set, &write_fd_set,
 
269
                                NULL, NULL);
251
270
 
252
271
                if (status <= 0) {
253
272
                        if (errno != EINTR)
255
274
                        continue;
256
275
                }
257
276
 
258
 
                if (FD_ISSET(ggzdmod_get_fd(ggz), &fdset))
 
277
                if (FD_ISSET(ggzdmod_get_fd(ggz), &read_fd_set))
259
278
                        ggzdmod_dispatch(ggz);
260
279
 
261
280
                /* Check each FD for activity */
262
281
                allplayers_iterate(p) {
263
 
                        int fd = get_player_socket(p);
 
282
                        GGZDataIO *dio = get_player_dio(p);
 
283
                        int fd = dio ? ggz_dio_get_socket(dio) : -1;
264
284
 
265
285
                        /* This is the player's communication socket.  Note
266
286
                           that AI players will have such a socket too, since
267
287
                           they are run as client-like programs. */
268
 
                        if (fd >= 0 && FD_ISSET(fd, &fdset)) {
 
288
                        if (fd >= 0 && FD_ISSET(fd, &read_fd_set)) {
269
289
                                /* Note - this handles spectator data too,
270
290
                                   but the spectator is given a player
271
291
                                   number. */
272
292
                                handle_player_data_event(p);
 
293
                                FD_CLR(fd, &read_fd_set);
 
294
                        }
 
295
 
 
296
                        if (fd >= 0 && FD_ISSET(fd, &write_fd_set)) {
 
297
                                ggz_dio_write_data(dio);
 
298
                                FD_CLR(fd, &write_fd_set);
273
299
                        }
274
300
 
275
301
#ifdef DEBUG
277
303
                           us and translated as debugging output. */
278
304
                        if (get_player_status(p) == GGZ_SEAT_BOT) {
279
305
                                fd = game.players[p].err_fd;
280
 
                                if (fd >= 0 && FD_ISSET(fd, &fdset))
 
306
                                if (fd >= 0 && FD_ISSET(fd, &read_fd_set))
281
307
                                        handle_ai_stderr(p);
282
308
                        }
283
309
#endif /* DEBUG */
288
314
static void handle_debug_message(int priority, const char *msg)
289
315
{
290
316
        if (!game.ggz
 
317
            || 1
291
318
            || ggzdmod_log(game.ggz, "%s", msg) < 0) {
292
319
                fflush(stdout);
293
320
                fputs(msg, stderr);