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"
38 #include "empthread.h"
49 #include <sys/socket.h>
53 #include <netinet/in.h>
54 #include <sys/ioctl.h>
65 struct emp_qelem Players;
70 emp_initque(&Players);
71 init_player_commands();
75 player_new(int s, struct sockaddr_in *sin)
78 struct hostent *hostp;
80 lp = (struct player *)calloc(1, sizeof(struct player));
81 bzero((s_char *)lp, sizeof(struct player));
83 /* update uses dummy player */
84 /* so does the market updater */
85 emp_insque(&lp->queue, &Players);
86 strcpy(lp->hostaddr, inet_ntoa(sin->sin_addr));
87 #ifdef RESOLVE_IPADDRESS
90 gethostbyaddr((char *)&sin->sin_addr, sizeof(sin->sin_addr),
92 strcpy(lp->hostname, hostp->h_name);
93 #endif /* RESOLVE_IPADDRESS */
97 lp->iop = io_open(s, IO_READ | IO_WRITE | IO_NBLOCK,
104 player_delete(struct player *lp)
108 back = (struct player *)lp->queue.q_back;
110 emp_remque(&lp->queue);
112 /* it's a real player */
117 /* XXX may need to free bigmap here */
122 player_next(struct player *lp)
125 lp = (struct player *)Players.q_forw;
127 lp = (struct player *)lp->queue.q_forw;
128 if (&lp->queue == &Players)
134 player_prev(struct player *lp)
137 lp = (struct player *)Players.q_back;
139 lp = (struct player *)lp->queue.q_back;
140 if (&lp->queue == &Players)
146 getplayer(natid cnum)
148 register struct emp_qelem *qp;
150 for (qp = Players.q_forw; qp != &Players; qp = qp->q_forw)
151 if (((struct player *)qp)->cnum == cnum)
152 return (struct player *)qp;
158 player_find_other(struct player *us, register natid cnum)
160 register struct emp_qelem *qp;
162 for (qp = Players.q_forw; qp != &Players; qp = qp->q_forw)
163 if (((struct player *)qp)->cnum == cnum &&
164 ((struct player *)qp != us) &&
165 (((struct player *)qp)->state == PS_PLAYING))
166 return (struct player *)qp;
173 player_wakeup_all(natid cnum)
175 register struct player *lp;
177 if (NULL != (lp = getplayer(cnum)))
182 player_wakeup(struct player *pl)
185 empth_wakeup(pl->proc);
190 player_accept(void *argv)
192 extern s_char *loginport;
194 struct sockaddr_in sin;
208 sp = getservbyname("empire", "tcp");
210 port = htons(atoi(loginport));
213 sin.sin_addr.s_addr = INADDR_ANY;
215 sin.sin_family = AF_INET;
216 if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
217 logerror("inet socket create");
221 #if !(defined(__linux__) && defined(__alpha__))
222 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val))
224 logerror("inet socket setsockopt SO_REUSEADDR (%d)", errno);
229 ("Alpha/Linux? You don't support SO_REUSEADDR yet, do you?\n");
231 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
232 logerror("inet socket bind");
235 #ifdef LISTENMAXCONN /* because someone in linux world didn't want to use
236 * SOMAXCONN as defined in the header files... */
237 if (listen(s, LISTENMAXCONN) < 0) {
238 logerror("inet socket listen");
242 if (listen(s, SOMAXCONN) < 0) {
243 logerror("inet socket listen");
247 maxfd = getfdtablesize() - 1;
249 empth_select(s, EMPTH_FD_READ);
251 ns = accept(s, (struct sockaddr *)&sin, &len);
253 logerror("new socket accept");
256 (void)setsockopt(ns, SOL_SOCKET, SO_KEEPALIVE,
257 (char *)&set, sizeof(set));
259 logerror("new fd %d, max %d, no fd's left for new user",
264 np = player_new(ns, &sin);
265 /* XXX may not be big enough */
267 /* budget */ + max(WORLD_X * WORLD_Y / 2 * sizeof(int) * 7,
268 /* power */ MAXNOC * sizeof(struct powstr));
269 sprintf(buf, "Player (fd #%d)", ns);
270 empth_create(PP_PLAYER, player_login, stacksize,
271 0, buf, "Empire player", np);