/*
* Empire - A multi-player, client/server Internet based war game.
- * Copyright (C) 1986-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
- * Ken Stevens, Steve McClure
+ * Copyright (C) 1986-2021, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ * Ken Stevens, Steve McClure, Markus Armbruster
*
- * This program is free software; you can redistribute it and/or modify
+ * Empire is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* ---
*
* Known contributors to this file:
* Dave Pare, 1994
* Steve McClure, 2000
- * Markus Armbruster, 2004-2009
+ * Markus Armbruster, 2004-2012
* Ron Koenderink, 2005-2009
*/
#include "com.h"
#include "empio.h"
#include "empthread.h"
-#include "file.h"
+#include "journal.h"
#include "match.h"
#include "misc.h"
#include "nat.h"
#include "player.h"
#include "proto.h"
#include "prototypes.h"
+#include "server.h"
static int client_cmd(void);
static int coun_cmd(void);
void
player_login(void *ud)
{
+ time_t deadline;
char buf[128];
char space[128];
- int ac;
- int cmd;
- int res;
+ int res, ac, cmd, prev_state;
player->proc = empth_self();
pr_id(player, C_INIT, "Empire server ready\n");
- while (player->state != PS_SHUTDOWN) {
- io_output(player->iop, 1);
+ for (;;) {
+ deadline = player_io_deadline(player, 0);
+ if (io_outputwaiting(player->iop)) {
+ if (io_output(player->iop, deadline) <= 0)
+ break;
+ continue;
+ }
if (io_gets(player->iop, buf, sizeof(buf)) < 0) {
- res = io_input(player->iop, 1);
- if (res <= 0) {
- if (res == 0 && !io_eof(player->iop))
- pr_id(player, C_DATA, "idle connection terminated\n");
+ res = io_input(player->iop, deadline);
+ if (res <= 0)
break;
- }
continue;
}
+ journal_input(buf);
ac = parse(buf, space, player->argp, NULL, NULL, NULL);
+ if (ac <= 0) {
+ pr_id(player, C_BADCMD, "Can't parse command\n");
+ continue;
+ }
cmd = comtch(player->argp[0], login_coms, 0);
if (cmd < 0) {
pr_id(player, C_BADCMD, "Command %s not found\n", player->argp[0]);
break;
}
}
+ prev_state = player->state;
player->state = PS_SHUTDOWN;
- if (!io_eof(player->iop)) {
- pr_id(player, C_EXIT, "so long...\n");
- while (io_output(player->iop, 1) > 0) ;
- }
+ if (prev_state == PS_PLAYING)
+ empth_rwlock_unlock(shutdown_lock);
+ pr_id(player, C_EXIT, "so long...\n");
player_delete(player);
empth_exit();
/*NOTREACHED*/
}
if (!may_play())
return RET_FAIL;
- other = getplayer((natid)player->cnum);
+ other = getplayer(player->cnum);
if (other) {
natp = getnatp(player->cnum);
if (natp->nat_stat != STAT_VIS) {
return RET_FAIL;
}
snprintf(buf, sizeof(buf), "Play#%d", player->cnum);
- empth_set_name(empth_self(), buf);
logerror("%s logged in as country #%d", praddr(player), player->cnum);
+ journal_login();
+ empth_set_name(empth_self(), buf);
pr_id(player, C_INIT, "%d\n", CLIENTPROTO);
+ empth_rwlock_rdlock(shutdown_lock);
+ player->state = PS_PLAYING;
player_main(player);
+ journal_logout();
logerror("%s logged out, country #%d", praddr(player), player->cnum);
- player->state = PS_SHUTDOWN;
+ if (!io_eof(player->iop) && !io_error(player->iop))
+ io_set_eof(player->iop);
return RET_OK;
}
return RET_FAIL;
}
logerror("%s killed country #%d", praddr(player), player->cnum);
- io_shutdown(other->iop, IO_READ | IO_WRITE);
- pr_id(player, C_EXIT, "closed socket of offending job\n");
+ pr_flash(other, "Disconnected by %s\n", praddr(player));
+ io_set_eof(other->iop);
+ other->aborted = 1;
+ other->may_sleep = PLAYER_SLEEP_NEVER;
+ empth_wakeup(other->proc);
+ pr_id(player, C_EXIT, "terminated %s's connection\n", praddr(other));
return RET_OK;
}
static int
quit_cmd(void)
{
- pr_id(player, C_EXIT, "so long\n");
- io_shutdown(player->iop, IO_READ);
+ io_set_eof(player->iop);
return RET_OK;
}