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
88 if (NULL != (hostp = gethostbyaddr((char *) &sin->sin_addr, sizeof(sin->sin_addr), AF_INET)))
89 strcpy(lp->hostname, hostp->h_name);
90 #endif /* RESOLVE_IPADDRESS */
94 lp->iop = io_open(s, IO_READ|IO_WRITE|IO_NBLOCK,
101 player_delete(struct player *lp)
105 back = (struct player *) lp->queue.q_back;
107 emp_remque(&lp->queue);
109 /* it's a real player */
114 /* XXX may need to free bigmap here */
119 player_next(struct player *lp)
122 lp = (struct player *)Players.q_forw;
124 lp = (struct player *)lp->queue.q_forw;
125 if (&lp->queue == &Players)
131 player_prev(struct player *lp)
134 lp = (struct player *)Players.q_back;
136 lp = (struct player *)lp->queue.q_back;
137 if (&lp->queue == &Players)
143 getplayer(natid cnum)
145 register struct emp_qelem *qp;
147 for (qp = Players.q_forw; qp != &Players; qp = qp->q_forw)
148 if (((struct player *)qp)->cnum == cnum)
149 return (struct player *)qp;
155 player_find_other(struct player *us, register natid cnum)
157 register struct emp_qelem *qp;
159 for (qp = Players.q_forw; qp != &Players; qp = qp->q_forw)
160 if (((struct player *)qp)->cnum == cnum &&
161 ((struct player *)qp != us) &&
162 (((struct player *)qp)->state == PS_PLAYING))
163 return (struct player *)qp;
170 player_wakeup_all(natid cnum)
172 register struct player *lp;
174 if (NULL != (lp = getplayer(cnum)))
179 player_wakeup(struct player *pl)
182 empth_wakeup(pl->proc);
187 player_accept(void *argv)
189 extern s_char *loginport;
191 struct sockaddr_in sin;
205 sp = getservbyname("empire", "tcp");
207 port = htons(atoi(loginport));
210 sin.sin_addr.s_addr = INADDR_ANY;
212 sin.sin_family = AF_INET;
213 if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
214 logerror("inet socket create");
218 #if !(defined(__linux__) && defined(__alpha__))
219 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val)) < 0) {
220 logerror("inet socket setsockopt SO_REUSEADDR (%d)", errno);
224 logerror("Alpha/Linux? You don't support SO_REUSEADDR yet, do you?\n");
226 if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
227 logerror("inet socket bind");
230 #ifdef LISTENMAXCONN /* because someone in linux world didn't want to use
231 * SOMAXCONN as defined in the header files... */
232 if (listen(s, LISTENMAXCONN) < 0) {
233 logerror("inet socket listen");
237 if (listen(s, SOMAXCONN) < 0) {
238 logerror("inet socket listen");
242 maxfd = getfdtablesize() - 1;
244 empth_select(s, EMPTH_FD_READ);
246 ns = accept(s, (struct sockaddr *) &sin, &len);
248 logerror("new socket accept");
251 (void) setsockopt(ns, SOL_SOCKET, SO_KEEPALIVE,
252 (char *) &set, sizeof(set));
254 logerror("new fd %d, max %d, no fd's left for new user",
259 np = player_new(ns, &sin);
260 /* XXX may not be big enough */
262 /* budget */ + max(WORLD_X*WORLD_Y/2 * sizeof(int) * 7,
263 /* power */ MAXNOC * sizeof(struct powstr));
264 sprintf(buf, "Player (fd #%d)", ns);
265 empth_create(PP_PLAYER, player_login, stacksize,
266 0, buf, "Empire player", np);