#include <errno.h>
#include "network.h"
+#include "netserv.h"
#include "game.h"
#include "tape.h"
#include "files.h"
#include "tools.h"
#include "misc.h"
-int norestart = 0;
-int nospeedup = 0;
-
-#define DEFAULTPORT 19503
-
-#define PROT_VERS_1 1
-#define PROT_VERS_2 0
-#define PROT_VERS_3 1
-
-#define OP_NICK 1
-#define OP_PLAY 2
-#define OP_MOVE 3
-#define OP_NRWANTED 4
-#define OP_LOST 5
-#define OP_GONE 6
-#define OP_CLEAR 7
-#define OP_NEW 8
-#define OP_LINES 9
-#define OP_GROW 10
-#define OP_MODE 11
-#define OP_LEVEL 12
-#define OP_BOT 13
-#define OP_KILL 14
-#define OP_PAUSE 15
-#define OP_CONT 16
-#define OP_VERSION 17
-#define OP_BADVERS 18
-#define OP_MSG 19
-#define OP_YOUARE 20
-#define OP_LINESTO 21
-#define OP_WON 22
-#define OP_ZERO 23
-
#define MAXNICKLEN 14
struct user
/* server stuff */
-#define BUFLEN 4096
-
int sfd;
-unsigned char realbuf[512], readbuf[BUFLEN], writbuf[BUFLEN];
+unsigned char realbuf[512], readbuf[MAX_BUFFER_SIZE], writbuf[MAX_BUFFER_SIZE];
unsigned char *buf = realbuf + 4;
int nread = 0, nwrite = 0;
-/* like memcpy, but guaranteed to handle overlap when s <= t */
-void copydown(char *s, char *t, int n)
-{
- for (; n; n--)
- *(s++) = *(t++);
-}
-
void sysmsg(char *s)
{
if (verbose)
}
}
-void fatal(char *s)
-{
- fprintf(stderr, "%s.\n", s);
- exit(1);
-}
-
void *mmalloc(int n)
{
void *r;
realbuf[0] = realbuf[1] = realbuf[2] = 0;
realbuf[3] = (unsigned char)len;
buf[0] = 0;
- if (nwrite + 4 + len >= BUFLEN)
+ if (nwrite + 4 + len >= MAX_BUFFER_SIZE)
fatal("Internal error: send buffer overflow");
memcpy(writbuf + nwrite, realbuf, 4 + len);
nwrite += 4 + len;
return("no name");
}
-void startserver()
+void InitNetworkServer(int port)
{
- char *options[2];
- int n = 0;
-
- options[0] = options[1] = NULL;
- if (verbose)
- options[n++] = "-v";
- if (nospeedup)
- options[n++] = "-nospeedup";
-
switch (fork())
{
case 0:
- execlp(
-#ifdef XTRISPATH
- XTRISPATH "/rnd_server",
-#else
- "rnd_server",
-#endif
- "rnd_server", "-once", options[0], options[1], NULL);
-
- fprintf(stderr, "Can't start server '%s'.\n",
-#ifdef XTRISPATH
- XTRISPATH "/rnd_server"
-#else
- "rnd_server"
-#endif
- );
-
- _exit(1);
-
+ NetworkServer(port, !serveronly);
+
+ /* never reached */
+ exit(0);
+
case -1:
- fatal("fork() failed");
-
+ Error(ERR_RETURN,
+ "cannot create network server process - no network games");
+ standalone = TRUE;
+ return;
+
default:
+ /* we are parent process -- resume normal operation */
return;
}
}
if (!host)
{
printf("No rocksndiamonds server on localhost - starting up one ...\n");
- startserver();
+
+ InitNetworkServer(port);
+
for (i=0; i<6; i++)
{
u_sleep(500000);
{
static char msgbuf[300];
- buf[1] = OP_NICK;
+ buf[1] = OP_NICKNAME;
memcpy(&buf[2], nickname, strlen(nickname));
sendbuf(2 + strlen(nickname));
sprintf(msgbuf, "you set your nick to \"%s\"", nickname);
void SendToServer_ProtocolVersion()
{
- buf[1] = OP_VERSION;
+ buf[1] = OP_PROTOCOL_VERSION;
buf[2] = PROT_VERS_1;
buf[3] = PROT_VERS_2;
buf[4] = PROT_VERS_3;
void SendToServer_NrWanted(int nr_wanted)
{
- buf[1] = OP_NRWANTED;
+ buf[1] = OP_NUMBER_WANTED;
buf[2] = nr_wanted;
sendbuf(3);
{
unsigned long new_random_seed = InitRND(NEW_RANDOMIZE);
- buf[1] = OP_PLAY;
+ buf[1] = OP_START_PLAYING;
buf[2] = (byte)(level_nr >> 8);
buf[3] = (byte)(level_nr & 0xff);
buf[4] = (byte)(leveldir_nr >> 8);
void SendToServer_MovePlayer(byte player_action)
{
- buf[1] = OP_MOVE;
+ buf[1] = OP_MOVE_FIGURE;
buf[2] = player_action;
sendbuf(3);
switch(buf[1])
{
- case OP_YOUARE:
+ case OP_YOUR_NUMBER:
{
int new_client_nr = buf[2];
int new_index_nr = new_client_nr - 1;
- printf("OP_YOUARE: %d\n", buf[0]);
+ printf("OP_YOUR_NUMBER: %d\n", buf[0]);
me.nr = new_client_nr;
stored_player[new_index_nr] = *local_player;
break;
}
- case OP_NRWANTED:
+ case OP_NUMBER_WANTED:
{
int client_nr_wanted = buf[2];
int new_client_nr = buf[3];
int new_index_nr = new_client_nr - 1;
- printf("OP_NRWANTED: %d\n", buf[0]);
+ printf("OP_NUMBER_WANTED: %d\n", buf[0]);
if (new_client_nr != client_nr_wanted)
{
break;
}
- case OP_NEW:
- printf("OP_NEW: %d\n", buf[0]);
+ case OP_PLAYER_CONNECTED:
+ printf("OP_PLAYER_CONNECTED: %d\n", buf[0]);
sprintf(msgbuf, "new client %d connected", buf[0]);
sysmsg(msgbuf);
break;
- case OP_NICK:
- printf("OP_NICK: %d\n", buf[0]);
+ case OP_NICKNAME:
+ printf("OP_NICKNAME: %d\n", buf[0]);
u = finduser(buf[0]);
buf[len] = 0;
sprintf(msgbuf, "client %d calls itself \"%s\"", buf[0], &buf[2]);
break;
- case OP_BADVERS:
+ case OP_BAD_PROTOCOL_VERSION:
Error(ERR_RETURN, "protocol version mismatch");
Error(ERR_EXIT, "server expects %d.%d.x instead of %d.%d.%d",
buf[2], buf[3], PROT_VERS_1, PROT_VERS_2, PROT_VERS_3);
break;
- case OP_PLAY:
+ case OP_START_PLAYING:
{
int new_level_nr, new_leveldir_nr;
unsigned long new_random_seed;
unsigned char *new_leveldir_name;
+ /*
if (game_status == PLAYING)
break;
+ */
new_level_nr = (buf[2] << 8) + buf[3];
new_leveldir_nr = (buf[4] << 8) + buf[5];
(buf[6] << 24) | (buf[7] << 16) | (buf[8] << 8) | (buf[9]);
new_leveldir_name = &buf[10];
- printf("OP_PLAY: %d\n", buf[0]);
+ printf("OP_START_PLAYING: %d\n", buf[0]);
sprintf(msgbuf, "client %d starts game [level %d from levedir %d (%s)]\n",
buf[0],
new_level_nr,
*/
if (tape.recording)
- {
tape.random_seed = new_random_seed;
- InitRND(tape.random_seed);
- }
+
+ InitRND(new_random_seed);
+
/*
printf("tape.random_seed == %d\n", tape.random_seed);
break;
}
- case OP_MOVE:
+ case OP_MOVE_FIGURE:
{
int frame_nr;
int i;
{
int r;
- r = read(sfd, readbuf + nread, BUFLEN - nread);
+ r = read(sfd, readbuf + nread, MAX_BUFFER_SIZE - nread);
if (r < 0)
fatal("Error reading from server");