2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * projects/authors will amend these files as needed.
28 * accept.c: Keep track of people logged in
30 * Known contributors to this file:
34 #include "prototypes.h"
37 #include "empthread.h"
48 #include <sys/socket.h>
52 #include <netinet/in.h>
53 #include <sys/ioctl.h>
64 struct emp_qelem Players;
69 emp_initque(&Players);
70 init_player_commands();
74 player_new(int s, struct sockaddr_in *sin)
77 struct hostent *hostp;
79 lp = (struct player *)malloc(sizeof(struct player));
82 memset(lp, 0, sizeof(struct player));
84 /* real player, not dummy created by update and market update */
86 IO_READ | IO_WRITE | IO_NBLOCK,
92 emp_insque(&lp->queue, &Players);
93 strcpy(lp->hostaddr, inet_ntoa(sin->sin_addr));
94 #ifdef RESOLVE_IPADDRESS
97 gethostbyaddr((char *)&sin->sin_addr, sizeof(sin->sin_addr),
99 strcpy(lp->hostname, hostp->h_name);
100 #endif /* RESOLVE_IPADDRESS */
109 player_delete(struct player *lp)
113 back = (struct player *)lp->queue.q_back;
115 emp_remque(&lp->queue);
117 /* it's a real player */
122 /* XXX may need to free bigmap here */
127 player_next(struct player *lp)
130 lp = (struct player *)Players.q_forw;
132 lp = (struct player *)lp->queue.q_forw;
133 if (&lp->queue == &Players)
139 player_prev(struct player *lp)
142 lp = (struct player *)Players.q_back;
144 lp = (struct player *)lp->queue.q_back;
145 if (&lp->queue == &Players)
151 getplayer(natid cnum)
153 register struct emp_qelem *qp;
155 for (qp = Players.q_forw; qp != &Players; qp = qp->q_forw)
156 if (((struct player *)qp)->cnum == cnum)
157 return (struct player *)qp;
163 player_find_other(struct player *us, register natid cnum)
165 register struct emp_qelem *qp;
167 for (qp = Players.q_forw; qp != &Players; qp = qp->q_forw)
168 if (((struct player *)qp)->cnum == cnum &&
169 ((struct player *)qp != us) &&
170 (((struct player *)qp)->state == PS_PLAYING))
171 return (struct player *)qp;
178 player_wakeup_all(natid cnum)
180 register struct player *lp;
182 if (NULL != (lp = getplayer(cnum)))
187 player_wakeup(struct player *pl)
190 empth_wakeup(pl->proc);
195 player_accept(void *argv)
197 extern s_char *loginport;
198 struct sockaddr_in sin;
212 sp = getservbyname("empire", "tcp");
214 port = htons(atoi(loginport));
217 sin.sin_addr.s_addr = INADDR_ANY;
219 sin.sin_family = AF_INET;
220 if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
221 logerror("inet socket create");
225 #if !(defined(__linux__) && defined(__alpha__))
226 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val))
228 logerror("inet socket setsockopt SO_REUSEADDR (%d)", errno);
233 ("Alpha/Linux? You don't support SO_REUSEADDR yet, do you?\n");
235 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
236 logerror("inet socket bind");
239 #ifdef LISTENMAXCONN /* because someone in linux world didn't want to use
240 * SOMAXCONN as defined in the header files... */
241 if (listen(s, LISTENMAXCONN) < 0) {
242 logerror("inet socket listen");
246 if (listen(s, SOMAXCONN) < 0) {
247 logerror("inet socket listen");
251 maxfd = getfdtablesize() - 1;
253 empth_select(s, EMPTH_FD_READ);
255 ns = accept(s, (struct sockaddr *)&sin, &len);
257 logerror("new socket accept");
260 (void)setsockopt(ns, SOL_SOCKET, SO_KEEPALIVE,
261 (char *)&set, sizeof(set));
263 logerror("new fd %d, max %d, no fd's left for new user",
268 np = player_new(ns, &sin);
270 logerror("can't create player for fd %d", ns);
274 /* XXX may not be big enough */
276 /* budget */ + max(WORLD_X * WORLD_Y / 2 * sizeof(int) * 7,
277 /* power */ MAXNOC * sizeof(struct powstr));
278 sprintf(buf, "Player (fd #%d)", ns);
279 empth_create(PP_PLAYER, player_login, stacksize,
280 0, buf, "Empire player", np);