(player_new): Proper error checking. Oops, now it may return NULL.

Proper error checking in callers required.
(update_wait, market_update): If player_new() returns null pointer,
skip update.
(player_accept): If player_new() returns null pointer, close
connection.
This commit is contained in:
Markus Armbruster 2004-01-23 13:44:19 +00:00
parent f841200923
commit 632fbf8d12
3 changed files with 25 additions and 6 deletions

View file

@ -77,10 +77,18 @@ player_new(int s, struct sockaddr_in *sin)
struct hostent *hostp; struct hostent *hostp;
lp = (struct player *)malloc(sizeof(struct player)); lp = (struct player *)malloc(sizeof(struct player));
if (!lp)
return NULL;
memset(lp, 0, sizeof(struct player)); memset(lp, 0, sizeof(struct player));
if (sin) { if (sin) {
/* update uses dummy player */ /* real player, not dummy created by update and market update */
/* so does the market updater */ lp->iop = io_open(s,
IO_READ | IO_WRITE | IO_NBLOCK,
IO_BUFSIZE, 0, 0);
if (!lp->iop) {
free(lp);
return NULL;
}
emp_insque(&lp->queue, &Players); emp_insque(&lp->queue, &Players);
strcpy(lp->hostaddr, inet_ntoa(sin->sin_addr)); strcpy(lp->hostaddr, inet_ntoa(sin->sin_addr));
#ifdef RESOLVE_IPADDRESS #ifdef RESOLVE_IPADDRESS
@ -93,8 +101,6 @@ player_new(int s, struct sockaddr_in *sin)
lp->cnum = 255; lp->cnum = 255;
lp->curid = -1; lp->curid = -1;
time(&lp->curup); time(&lp->curup);
lp->iop = io_open(s, IO_READ | IO_WRITE | IO_NBLOCK,
IO_BUFSIZE, 0, 0);
} }
return lp; return lp;
} }
@ -260,6 +266,11 @@ player_accept(void *argv)
continue; continue;
} }
np = player_new(ns, &sin); np = player_new(ns, &sin);
if (!np) {
logerror("can't create player for fd %d", ns);
close(ns);
continue;
}
/* XXX may not be big enough */ /* XXX may not be big enough */
stacksize = 100000 stacksize = 100000
/* budget */ + max(WORLD_X * WORLD_Y / 2 * sizeof(int) * 7, /* budget */ + max(WORLD_X * WORLD_Y / 2 * sizeof(int) * 7,

View file

@ -77,8 +77,12 @@ void *argv;
time(&now); time(&now);
/* logerror("Checking the world markets at %s", ctime(&now));*/ /* logerror("Checking the world markets at %s", ctime(&now));*/
dp = player_new(0, 0); dp = player_new(0, 0);
empth_create(PP_UPDATE, check_all_markets, (50 * 1024), 0, if (dp) {
"MarketCheck", "Checks the world markets", dp); empth_create(PP_UPDATE, check_all_markets, (50 * 1024), 0,
"MarketCheck", "Checks the world markets", dp);
} else {
logerror("can't create dummy player for market update");
}
now = now + 300; /* Every 5 minutes */ now = now + 300; /* Every 5 minutes */
empth_sleep(now); empth_sleep(now);
} }

View file

@ -142,6 +142,10 @@ void *argv;
* in the land so it can finish before it yields. * in the land so it can finish before it yields.
*/ */
dp = player_new(0, 0); dp = player_new(0, 0);
if (!dp) {
logerror("can't create dummy player for update");
continue;
}
stacksize = 100000 + stacksize = 100000 +
/* finish_sects */ WORLD_X * WORLD_Y * (2 * sizeof(double) + /* finish_sects */ WORLD_X * WORLD_Y * (2 * sizeof(double) +
sizeof(s_char *)); sizeof(s_char *));