2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2005, 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:
40 #include "prototypes.h"
43 #include "empthread.h"
52 #include <arpa/inet.h>
53 #include <sys/socket.h>
57 #include <netinet/in.h>
58 #include <sys/ioctl.h>
67 static struct emp_qelem Players;
68 static int player_socket;
73 struct sockaddr_in sin;
79 emp_initque(&Players);
80 init_player_commands();
82 sp = getservbyname("empire", "tcp");
84 port = htons(atoi(loginport));
87 sin.sin_addr.s_addr = INADDR_ANY;
89 sin.sin_family = AF_INET;
90 if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
91 logerror("inet socket create");
96 * WIN32's SO_REUSEADDR operates differently than POSIX's SO_REUSEADDR.
97 * WIN32's SO_REUSEADDR allows the port number to be used immediately
98 * even if the port number is currently being used by another program.
99 * In WIN32, there is no waiting time when a port is closed before it
100 * can be reused so SO_REUSEADDR is not required for WIN32.
103 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0) {
104 logerror("inet socket setsockopt SO_REUSEADDR (%d)", errno);
108 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
109 logerror("inet socket bind");
112 if (listen(s, SOMAXCONN) < 0) {
113 logerror("inet socket listen");
120 player_new(int s, struct sockaddr_in *sin)
123 #ifdef RESOLVE_IPADDRESS
124 struct hostent *hostp;
127 lp = malloc(sizeof(struct player));
130 memset(lp, 0, sizeof(struct player));
132 /* real player, not dummy created by update and market update */
134 IO_READ | IO_WRITE | IO_NBLOCK,
140 emp_insque(&lp->queue, &Players);
141 strcpy(lp->hostaddr, inet_ntoa(sin->sin_addr));
142 #ifdef RESOLVE_IPADDRESS
144 (hostp = gethostbyaddr(&sin->sin_addr, sizeof(sin->sin_addr),
146 strcpy(lp->hostname, hostp->h_name);
147 #endif /* RESOLVE_IPADDRESS */
156 player_delete(struct player *lp)
160 back = (struct player *)lp->queue.q_back;
162 emp_remque(&lp->queue);
164 /* it's a real player */
169 /* XXX may need to free bigmap here */
174 player_next(struct player *lp)
177 lp = (struct player *)Players.q_forw;
179 lp = (struct player *)lp->queue.q_forw;
180 if (&lp->queue == &Players)
186 player_prev(struct player *lp)
189 lp = (struct player *)Players.q_back;
191 lp = (struct player *)lp->queue.q_back;
192 if (&lp->queue == &Players)
198 getplayer(natid cnum)
200 register struct emp_qelem *qp;
202 for (qp = Players.q_forw; qp != &Players; qp = qp->q_forw)
203 if (((struct player *)qp)->cnum == cnum)
204 return (struct player *)qp;
210 player_find_other(struct player *us, register natid cnum)
212 register struct emp_qelem *qp;
214 for (qp = Players.q_forw; qp != &Players; qp = qp->q_forw)
215 if (((struct player *)qp)->cnum == cnum &&
216 ((struct player *)qp != us) &&
217 (((struct player *)qp)->state == PS_PLAYING))
218 return (struct player *)qp;
225 player_wakeup_all(natid cnum)
227 register struct player *lp;
229 if (NULL != (lp = getplayer(cnum)))
234 player_wakeup(struct player *pl)
237 empth_wakeup(pl->proc);
242 player_accept(void *unused)
244 struct sockaddr_in sin;
245 int s = player_socket;
254 empth_select(s, EMPTH_FD_READ);
256 ns = accept(s, (struct sockaddr *)&sin, &len);
258 logerror("new socket accept");
261 (void)setsockopt(ns, SOL_SOCKET, SO_KEEPALIVE, &set, sizeof(set));
262 np = player_new(ns, &sin);
264 logerror("can't create player for fd %d", ns);
268 /* XXX may not be big enough */
270 /* budget */ + max(WORLD_X * WORLD_Y / 2 * sizeof(int) * 7,
271 /* power */ MAXNOC * sizeof(struct powstr));
272 sprintf(buf, "Player (fd #%d)", ns);
273 empth_create(PP_PLAYER, player_login, stacksize,
274 0, buf, "Empire player", np);