X-Git-Url: https://git.artsoft.org/?p=rocksndiamonds.git;a=blobdiff_plain;f=src%2Fnetserv.c;h=25055e019e06a73745d5c2353ea4482e62156ec7;hp=21a48572d08af642fc09fbb42cd94630971fa0b6;hb=7732b3bda99e3cafc7db8718f2cac1f507c978e8;hpb=c9433eab5c4317ed4f89164b386a7d33562e29be diff --git a/src/netserv.c b/src/netserv.c index 21a48572..25055e01 100644 --- a/src/netserv.c +++ b/src/netserv.c @@ -1,244 +1,597 @@ -/*********************************************************** -* Rocks'n'Diamonds -- McDuffin Strikes Back! * -*----------------------------------------------------------* -* (c) 1995-2002 Artsoft Entertainment * -* Holger Schemel * -* Detmolder Strasse 189 * -* 33604 Bielefeld * -* Germany * -* e-mail: info@artsoft.org * -*----------------------------------------------------------* -* network.c * -***********************************************************/ - -#include "libgame/platform.h" - -#if defined(PLATFORM_UNIX) +// ============================================================================ +// Rocks'n'Diamonds - McDuffin Strikes Back! +// ---------------------------------------------------------------------------- +// (c) 1995-2014 by Artsoft Entertainment +// Holger Schemel +// info@artsoft.org +// http://www.artsoft.org/ +// ---------------------------------------------------------------------------- +// netserv.c +// ============================================================================ #include #include #include -#include #include -#include -#include -#include -#include -#include #include "libgame/libgame.h" #include "netserv.h" +#include "main.h" + -static int clients = 0; -static int onceonly = 0; +static int num_clients = 0; +static boolean run_server_only_once = FALSE; struct NetworkServerPlayerInfo { - int fd; - char player_name[16]; - unsigned char number; - struct NetworkServerPlayerInfo *next; - char active; - char introduced; - unsigned char readbuffer[MAX_BUFFER_SIZE]; - unsigned char writbuffer[MAX_BUFFER_SIZE]; - int nread, nwrite; + TCPsocket fd; + char player_name[MAX_PLAYER_NAME_LEN + 1]; + int number; + boolean active; + boolean introduced; byte action; boolean action_received; + + struct NetworkServerPlayerInfo *next; }; static struct NetworkServerPlayerInfo *first_player = NULL; #define NEXT(player) ((player)->next ? (player)->next : first_player) -static struct sockaddr_in saddr; -static int lfd; -static unsigned char realbuffer[512], *buffer = realbuffer + 4; +// TODO: peer address +static TCPsocket lfd; // listening TCP socket +static UDPsocket udp; // listening UDP socket +static SDLNet_SocketSet fds; // socket set + +static struct NetworkBuffer *read_buffer = NULL; +static struct NetworkBuffer *write_buffer = NULL; + +static unsigned int ServerFrameCounter = 0; + + +int getNetwork8BitInteger(byte *ptr) +{ + return ptr[0]; +} + +int putNetwork8BitInteger(byte *ptr, int value) +{ + ptr[0] = value; + + return 1; +} + +int getNetwork16BitInteger(byte *ptr) +{ + return ((ptr[0] << 8) | + (ptr[1] << 0)); +} + +int putNetwork16BitInteger(byte *ptr, int value) +{ + ptr[0] = (value >> 8) & 0xff; + ptr[1] = (value >> 0) & 0xff; + + return 2; +} + +int getNetwork32BitInteger(byte *ptr) +{ + return ((ptr[0] << 24) | + (ptr[1] << 16) | + (ptr[2] << 8) | + (ptr[3] << 0)); +} + +int putNetwork32BitInteger(byte *ptr, int value) +{ + ptr[0] = (value >> 24) & 0xff; + ptr[1] = (value >> 16) & 0xff; + ptr[2] = (value >> 8) & 0xff; + ptr[3] = (value >> 0) & 0xff; + + return 4; +} -static int interrupt; -static int tcp = -1; +char *getNetworkString(byte *ptr) +{ + return (char *)ptr; +} -static unsigned long ServerFrameCounter = 0; +int putNetworkString(byte *ptr, char *s) +{ + strcpy((char *)ptr, s); -static fd_set fds; + return strlen(s) + 1; +} -static void addtobuffer(struct NetworkServerPlayerInfo *player, - unsigned char *b, int len) +struct NetworkBuffer *newNetworkBuffer(void) { - if (player->nwrite + len >= MAX_BUFFER_SIZE) - Error(ERR_EXIT_NETWORK_SERVER, - "internal error: network send buffer overflow"); + struct NetworkBuffer *new = checked_calloc(sizeof(struct NetworkBuffer)); + + new->max_size = MAX_BUFFER_SIZE; + new->size = 0; + new->pos = 0; - memcpy(player->writbuffer + player->nwrite, b, len); - player->nwrite += len; + new->buffer = checked_calloc(new->max_size); + + return new; } -static void flushuser(struct NetworkServerPlayerInfo *player) +static void resetNetworkBufferForReading(struct NetworkBuffer *nb) { - if (player->nwrite) + nb->pos = 0; +} + +static void resetNetworkBufferForWriting(struct NetworkBuffer *nb) +{ + nb->size = 0; + nb->pos = 0; +} + +void initNetworkBufferForReceiving(struct NetworkBuffer *nb) +{ + resetNetworkBufferForWriting(nb); +} + +void initNetworkBufferForReading(struct NetworkBuffer *nb) +{ + resetNetworkBufferForReading(nb); + + // skip message length header + getNetworkBuffer32BitInteger(nb); +} + +void initNetworkBufferForWriting(struct NetworkBuffer *nb, int message_type, + int player_nr) +{ + resetNetworkBufferForWriting(nb); + + // will be replaced with message length before sending + putNetworkBuffer32BitInteger(nb, 0); + + putNetworkBuffer8BitInteger(nb, message_type); + putNetworkBuffer8BitInteger(nb, player_nr); +} + +static void copyNetworkBufferForWriting(struct NetworkBuffer *nb_from, + struct NetworkBuffer *nb_to, + int player_nr) +{ + initNetworkBufferForReading(nb_from); + + int message_type = getNetworkBuffer8BitInteger(nb_from); + + // skip player number + getNetworkBuffer8BitInteger(nb_from); + + initNetworkBufferForWriting(nb_to, message_type, player_nr); + + while (nb_from->pos < nb_from->size) { - write(player->fd, player->writbuffer, player->nwrite); - player->nwrite = 0; + int b = getNetworkBuffer8BitInteger(nb_from); + + putNetworkBuffer8BitInteger(nb_to, b); } } -static void broadcast(struct NetworkServerPlayerInfo *except, - int len, int activeonly) +static void increaseNetworkBuffer(struct NetworkBuffer *nb, int additional_size) +{ + // add some more buffer size than is really required this time + nb->max_size += additional_size + MAX_BUFFER_SIZE; + nb->buffer = checked_realloc(nb->buffer, nb->max_size); +} + +int receiveNetworkBufferBytes(struct NetworkBuffer *nb, TCPsocket socket, + int num_bytes) +{ + if (num_bytes > MAX_PACKET_SIZE) + { + Error(ERR_NETWORK_SERVER, "protocol error: invalid packet size %d", + num_bytes); + + return -1; + } + + if (nb->pos + num_bytes > nb->max_size) + increaseNetworkBuffer(nb, num_bytes); + + int result = SDLNet_TCP_Recv(socket, &nb->buffer[nb->pos], num_bytes); + + if (result != num_bytes) + return result; + + nb->pos += num_bytes; + nb->size = nb->pos; + + return num_bytes; +} + +int receiveNetworkBufferPacket(struct NetworkBuffer *nb, TCPsocket socket) +{ + int num_bytes, num_bytes_head, num_bytes_body; + + num_bytes_head = 4; + num_bytes = receiveNetworkBufferBytes(nb, socket, num_bytes_head); + + if (num_bytes != num_bytes_head) + return num_bytes; + + num_bytes_body = getNetwork32BitInteger(nb->buffer); + num_bytes = receiveNetworkBufferBytes(nb, socket, num_bytes_body); + + return num_bytes; +} + +int getNetworkBuffer8BitInteger(struct NetworkBuffer *nb) +{ + int num_bytes = 1; + + if (nb->pos + num_bytes > nb->size) + return 0; + + int value = getNetwork8BitInteger(&nb->buffer[nb->pos]); + + nb->pos += num_bytes; + + return value; +} + +void putNetworkBuffer8BitInteger(struct NetworkBuffer *nb, int value) +{ + int num_bytes = 1; + + if (nb->pos + num_bytes > nb->max_size) + increaseNetworkBuffer(nb, num_bytes); + + nb->pos += putNetwork8BitInteger(&nb->buffer[nb->pos], value); + nb->size = nb->pos; +} + +int getNetworkBuffer16BitInteger(struct NetworkBuffer *nb) +{ + int num_bytes = 2; + + if (nb->pos + num_bytes > nb->size) + return 0; + + int value = getNetwork16BitInteger(&nb->buffer[nb->pos]); + + nb->pos += num_bytes; + + return value; +} + +void putNetworkBuffer16BitInteger(struct NetworkBuffer *nb, int value) +{ + int num_bytes = 2; + + if (nb->pos + num_bytes > nb->max_size) + increaseNetworkBuffer(nb, num_bytes); + + nb->pos += putNetwork16BitInteger(&nb->buffer[nb->pos], value); + nb->size = nb->pos; +} + +int getNetworkBuffer32BitInteger(struct NetworkBuffer *nb) +{ + int num_bytes = 4; + + if (nb->pos + num_bytes > nb->size) + return 0; + + int value = getNetwork32BitInteger(&nb->buffer[nb->pos]); + + nb->pos += num_bytes; + + return value; +} + +void putNetworkBuffer32BitInteger(struct NetworkBuffer *nb, int value) +{ + int num_bytes = 4; + + if (nb->pos + num_bytes > nb->max_size) + increaseNetworkBuffer(nb, num_bytes); + + nb->pos += putNetwork32BitInteger(&nb->buffer[nb->pos], value); + nb->size = nb->pos; +} + +char *getNetworkBufferString(struct NetworkBuffer *nb) +{ + char *s = getNetworkString(&nb->buffer[nb->pos]); + + nb->pos += strlen(s) + 1; + + return s; +} + +void putNetworkBufferString(struct NetworkBuffer *nb, char *s) +{ + int num_bytes = strlen(s) + 1; + + if (nb->pos + num_bytes > nb->max_size) + increaseNetworkBuffer(nb, num_bytes); + + nb->pos += putNetworkString(&nb->buffer[nb->pos], s); + nb->size = nb->pos; +} + +int getNetworkBufferFile(struct NetworkBuffer *nb, char *filename) +{ + FILE *file; + int num_bytes = getNetworkBuffer32BitInteger(nb); + int i; + + if (!(file = fopen(filename, MODE_WRITE))) + { + Error(ERR_WARN, "cannot write file '%s' from network buffer", filename); + + return 0; + } + + for (i = 0; i < num_bytes; i++) + { + int b = getNetworkBuffer8BitInteger(nb); + + putFile8Bit(file, b); + } + + fclose(file); + + return num_bytes; +} + +int putNetworkBufferFile(struct NetworkBuffer *nb, char *filename) +{ + File *file; + int filesize_pos = nb->pos; + int num_bytes = 0; + + // will be replaced with file size + putNetworkBuffer32BitInteger(nb, 0); + + if (!(file = openFile(filename, MODE_READ))) + { + Error(ERR_WARN, "cannot read file '%s' to network buffer", filename); + + return 0; + } + + while (1) + { + int b = getFile8Bit(file); + + if (checkEndOfFile(file)) + break; + + putNetworkBuffer8BitInteger(nb, b); + + num_bytes++; + } + + closeFile(file); + + // set file size + putNetwork32BitInteger(&nb->buffer[filesize_pos], num_bytes); + + return num_bytes; +} + +void dumpNetworkBuffer(struct NetworkBuffer *nb) +{ + int i; + + printf("::: network buffer maximum size: %d\n", nb->max_size); + printf("::: network buffer size: %d\n", nb->size); + printf("::: network buffer position : %d\n", nb->pos); + + for (i = 0; i < nb->size; i++) + { + if ((i % 16) == 0) + printf("\n::: "); + + printf("%02x ", nb->buffer[i]); + } + + printf("\n"); +} + +static void SendNetworkBufferToAllButOne(struct NetworkBuffer *nb, + struct NetworkServerPlayerInfo *except) { struct NetworkServerPlayerInfo *player; - realbuffer[0] = realbuffer[1] = realbuffer[2] = 0; - realbuffer[3] = (unsigned char)len; - for (player=first_player; player; player=player->next) - if (player != except && (player->active || !activeonly) && player->introduced) - addtobuffer(player, realbuffer, 4 + len); + // set message length header + putNetwork32BitInteger(nb->buffer, nb->size - 4); + + for (player = first_player; player != NULL; player = player->next) + { + if (player != except && player->introduced) + { + // directly send the buffer to the network client + SDLNet_TCP_Send(player->fd, nb->buffer, nb->size); + } + } +} + +static void SendNetworkBufferToAll(struct NetworkBuffer *nb) +{ + SendNetworkBufferToAllButOne(nb, NULL); } -static void sendtoone(struct NetworkServerPlayerInfo *to, int len) +static void SendNetworkBufferToClient(struct NetworkBuffer *nb, + struct NetworkServerPlayerInfo *player) { - realbuffer[0] = realbuffer[1] = realbuffer[2] = 0; - realbuffer[3] = (unsigned char)len; - addtobuffer(to, realbuffer, 4 + len); + // set message length header + putNetwork32BitInteger(nb->buffer, nb->size - 4); + + // directly send the buffer to the network client + SDLNet_TCP_Send(player->fd, nb->buffer, nb->size); } static void RemovePlayer(struct NetworkServerPlayerInfo *player) { - struct NetworkServerPlayerInfo *v; - if (options.verbose) Error(ERR_NETWORK_SERVER, "dropping client %d (%s)", player->number, player->player_name); + SDLNet_TCP_DelSocket(fds, player->fd); + SDLNet_TCP_Close(player->fd); + if (player == first_player) + { first_player = player->next; + } else { - for (v=first_player; v; v=v->next) + struct NetworkServerPlayerInfo *p; + + for (p = first_player; p != NULL; p = p->next) { - if (v->next && v->next == player) + if (p->next && p->next == player) { - v->next = player->next; + p->next = player->next; + break; } } } - close(player->fd); if (player->introduced) { - buffer[0] = player->number; - buffer[1] = OP_PLAYER_DISCONNECTED; - broadcast(player, 2, 0); + initNetworkBufferForWriting(write_buffer, OP_PLAYER_DISCONNECTED, + player->number); + + SendNetworkBufferToAllButOne(write_buffer, player); } free(player); - clients--; + num_clients--; - if (onceonly && clients == 0) +#if 0 // do not terminate network server if last player disconnected + if (run_server_only_once && num_clients == 0) { if (options.verbose) { Error(ERR_NETWORK_SERVER, "no clients left"); Error(ERR_NETWORK_SERVER, "aborting"); } + exit(0); } +#endif } -static void AddPlayer(int fd) +static void AddPlayer(TCPsocket fd) { - struct NetworkServerPlayerInfo *player, *v; - unsigned char nxn; + struct NetworkServerPlayerInfo *player, *p; + int number = 1; + boolean again = TRUE; - player = checked_malloc(sizeof (struct NetworkServerPlayerInfo)); + SDLNet_TCP_AddSocket(fds, fd); + + player = checked_calloc(sizeof(struct NetworkServerPlayerInfo)); player->fd = fd; player->player_name[0] = 0; - player->next = first_player; - player->active = 0; - player->nread = 0; - player->nwrite = 0; - player->introduced = 0; + player->active = FALSE; + player->introduced = FALSE; player->action = 0; player->action_received = FALSE; + player->next = first_player; first_player = player; - nxn = 1; - - again: - v = player->next; - while(v) + while (again) { - if (v->number == nxn) + again = FALSE; + p = player->next; + + while (p) { - nxn++; - goto again; + if (p->number == number) + { + number++; + + again = TRUE; + + break; + } + + p = p->next; } - v = v->next; } - player->number = nxn; - if (options.verbose) - Error(ERR_NETWORK_SERVER, "client %d connecting from %s", - nxn, inet_ntoa(saddr.sin_addr)); - clients++; + player->number = number; + num_clients++; + + initNetworkBufferForWriting(write_buffer, OP_YOUR_NUMBER, 0); + + putNetworkBuffer8BitInteger(write_buffer, player->number); - buffer[0] = 0; - buffer[1] = OP_YOUR_NUMBER; - buffer[2] = player->number; - sendtoone(player, 3); + SendNetworkBufferToClient(write_buffer, player); } -static void Handle_OP_PROTOCOL_VERSION(struct NetworkServerPlayerInfo *player, - unsigned int len) +static void Handle_OP_PROTOCOL_VERSION(struct NetworkServerPlayerInfo *player) { - if (len != 5 || - buffer[2] != PROTOCOL_VERSION_1 || - buffer[3] != PROTOCOL_VERSION_2) + int protocol_version_major = getNetworkBuffer8BitInteger(read_buffer); + int protocol_version_minor = getNetworkBuffer8BitInteger(read_buffer); + int protocol_version_patch = getNetworkBuffer8BitInteger(read_buffer); + + if (protocol_version_major != PROTOCOL_VERSION_MAJOR || + protocol_version_minor != PROTOCOL_VERSION_MINOR) { if (options.verbose) Error(ERR_NETWORK_SERVER, "client %d (%s) has wrong protocol version %d.%d.%d", - player->number, player->player_name, buffer[2], buffer[3], buffer[4]); + player->number, player->player_name, + protocol_version_major, + protocol_version_minor, + protocol_version_patch); + + initNetworkBufferForWriting(write_buffer, OP_BAD_PROTOCOL_VERSION, 0); - buffer[0] = 0; - buffer[1] = OP_BAD_PROTOCOL_VERSION; - buffer[2] = PROTOCOL_VERSION_1; - buffer[3] = PROTOCOL_VERSION_2; - buffer[4] = PROTOCOL_VERSION_3; - sendtoone(player, 5); - flushuser(player); + putNetworkBuffer8BitInteger(write_buffer, PROTOCOL_VERSION_MAJOR); + putNetworkBuffer8BitInteger(write_buffer, PROTOCOL_VERSION_MINOR); + putNetworkBuffer8BitInteger(write_buffer, PROTOCOL_VERSION_PATCH); + + SendNetworkBufferToClient(write_buffer, player); RemovePlayer(player); - interrupt = 1; } else { if (options.verbose) Error(ERR_NETWORK_SERVER, "client %d (%s) uses protocol version %d.%d.%d", - player->number, player->player_name, buffer[2], buffer[3], buffer[4]); + player->number, player->player_name, + protocol_version_major, + protocol_version_minor, + protocol_version_patch); } } static void Handle_OP_NUMBER_WANTED(struct NetworkServerPlayerInfo *player) { - struct NetworkServerPlayerInfo *v; + int nr_wanted = getNetworkBuffer8BitInteger(read_buffer); int client_nr = player->number; - int nr_wanted = buffer[2]; - int nr_is_free = 1; + boolean nr_is_free = TRUE; + struct NetworkServerPlayerInfo *p; if (options.verbose) Error(ERR_NETWORK_SERVER, "client %d (%s) wants to switch to # %d", player->number, player->player_name, nr_wanted); - for (v=first_player; v; v=v->next) + for (p = first_player; p != NULL; p = p->next) { - if (v->number == nr_wanted) + if (p->number == nr_wanted) { - nr_is_free = 0; + nr_is_free = FALSE; + break; } } @@ -249,109 +602,117 @@ static void Handle_OP_NUMBER_WANTED(struct NetworkServerPlayerInfo *player) Error(ERR_NETWORK_SERVER, "client %d (%s) switches to # %d", player->number, player->player_name, nr_wanted); else if (player->number == nr_wanted) - Error(ERR_NETWORK_SERVER, "client %d (%s) still has # %d", + Error(ERR_NETWORK_SERVER, "client %d (%s) already has # %d", player->number, player->player_name, nr_wanted); else Error(ERR_NETWORK_SERVER, - "client %d (%s) cannot switch (client %d still exists)", + "client %d (%s) cannot switch (client %d already exists)", player->number, player->player_name, nr_wanted); } if (nr_is_free) player->number = nr_wanted; - buffer[0] = client_nr; - buffer[1] = OP_NUMBER_WANTED; - buffer[2] = nr_wanted; - buffer[3] = player->number; + initNetworkBufferForWriting(write_buffer, OP_NUMBER_WANTED, client_nr); - /* - sendtoone(player, 4); - */ + putNetworkBuffer8BitInteger(write_buffer, nr_wanted); + putNetworkBuffer8BitInteger(write_buffer, player->number); - broadcast(NULL, 4, 0); + SendNetworkBufferToAll(write_buffer); } -static void Handle_OP_PLAYER_NAME(struct NetworkServerPlayerInfo *player, - unsigned int len) +static void Handle_OP_PLAYER_NAME(struct NetworkServerPlayerInfo *player) { - struct NetworkServerPlayerInfo *v; + char *player_name = getNetworkBufferString(read_buffer); int i; - if (len>16) - len=16; - memcpy(player->player_name, &buffer[2], len-2); - player->player_name[len-2] = 0; - for (i=0; iplayer_name, player_name, MAX_PLAYER_NAME_LEN); + player->player_name[MAX_PLAYER_NAME_LEN] = '\0'; + + for (i = 0; i < MAX_PLAYER_NAME_LEN; i++) { - if (player->player_name[i] < ' ' || + if (player->player_name[i] < ' ' || ((unsigned char)(player->player_name[i]) > 0x7e && (unsigned char)(player->player_name[i]) <= 0xa0)) { player->player_name[i] = 0; + break; } } if (!player->introduced) { - buffer[0] = player->number; - buffer[1] = OP_PLAYER_CONNECTED; - broadcast(player, 2, 0); + initNetworkBufferForWriting(write_buffer, OP_PLAYER_CONNECTED, + player->number); + + SendNetworkBufferToAllButOne(write_buffer, player); } if (options.verbose) Error(ERR_NETWORK_SERVER, "client %d calls itself \"%s\"", player->number, player->player_name); - buffer[1] = OP_PLAYER_NAME; - broadcast(player, len, 0); + + copyNetworkBufferForWriting(read_buffer, write_buffer, player->number); + + SendNetworkBufferToAllButOne(write_buffer, player); if (!player->introduced) { - for (v=first_player; v; v=v->next) + struct NetworkServerPlayerInfo *p; + + for (p = first_player; p != NULL; p = p->next) { - if (v != player && v->introduced) + if (p != player && p->introduced) { - buffer[0] = v->number; - buffer[1] = OP_PLAYER_CONNECTED; - sendtoone(player, 2); - buffer[1] = OP_PLAYER_NAME; - memcpy(&buffer[2], v->player_name, 14); - sendtoone(player, 2+strlen(v->player_name)); + initNetworkBufferForWriting(write_buffer, OP_PLAYER_CONNECTED, + p->number); + + SendNetworkBufferToClient(write_buffer, player); + + initNetworkBufferForWriting(write_buffer, OP_PLAYER_NAME, p->number); + + putNetworkBufferString(write_buffer, p->player_name); + + SendNetworkBufferToClient(write_buffer, player); } } } - player->introduced = 1; + player->introduced = TRUE; } static void Handle_OP_START_PLAYING(struct NetworkServerPlayerInfo *player) { - struct NetworkServerPlayerInfo *v, *w; + char *new_leveldir_identifier = getNetworkBufferString(read_buffer); + int level_nr = getNetworkBuffer16BitInteger(read_buffer); if (options.verbose) Error(ERR_NETWORK_SERVER, - "client %d (%s) starts game [level %d from levedir %d (%s)]", - player->number, player->player_name, - (buffer[2] << 8) + buffer[3], - (buffer[4] << 8) + buffer[5], - &buffer[6]); + "client %d (%s) starts game [level %d from level set '%s']", + player->number, player->player_name, level_nr, + new_leveldir_identifier); - for (w=first_player; w; w=w->next) - if (w->introduced) - w->active = 1; + struct NetworkServerPlayerInfo *p; - /* reset frame counter */ + // reset frame counter ServerFrameCounter = 0; - /* reset player actions */ - for (v=first_player; v; v=v->next) + Error(ERR_NETWORK_SERVER, "resetting ServerFrameCounter to 0"); + + // reset player actions + for (p = first_player; p != NULL; p = p->next) { - v->action = 0; - v->action_received = FALSE; + p->action = 0; + p->action_received = FALSE; + + if (p->introduced) + p->active = TRUE; } - broadcast(NULL, 10 + strlen((char *)&buffer[10])+1, 0); + copyNetworkBufferForWriting(read_buffer, write_buffer, player->number); + + SendNetworkBufferToAll(write_buffer); } static void Handle_OP_PAUSE_PLAYING(struct NetworkServerPlayerInfo *player) @@ -359,7 +720,10 @@ static void Handle_OP_PAUSE_PLAYING(struct NetworkServerPlayerInfo *player) if (options.verbose) Error(ERR_NETWORK_SERVER, "client %d (%s) pauses game", player->number, player->player_name); - broadcast(NULL, 2, 0); + + copyNetworkBufferForWriting(read_buffer, write_buffer, player->number); + + SendNetworkBufferToAll(write_buffer); } static void Handle_OP_CONTINUE_PLAYING(struct NetworkServerPlayerInfo *player) @@ -367,301 +731,307 @@ static void Handle_OP_CONTINUE_PLAYING(struct NetworkServerPlayerInfo *player) if (options.verbose) Error(ERR_NETWORK_SERVER, "client %d (%s) continues game", player->number, player->player_name); - broadcast(NULL, 2, 0); + + copyNetworkBufferForWriting(read_buffer, write_buffer, player->number); + + SendNetworkBufferToAll(write_buffer); } static void Handle_OP_STOP_PLAYING(struct NetworkServerPlayerInfo *player) { + int cause_for_stopping = getNetworkBuffer8BitInteger(read_buffer); + if (options.verbose) - Error(ERR_NETWORK_SERVER, "client %d (%s) stops game", - player->number, player->player_name); - broadcast(NULL, 2, 0); + Error(ERR_NETWORK_SERVER, "client %d (%s) stops game [%d]", + player->number, player->player_name, cause_for_stopping); + + copyNetworkBufferForWriting(read_buffer, write_buffer, player->number); + + SendNetworkBufferToAll(write_buffer); } -static void Handle_OP_MOVE_FIGURE(struct NetworkServerPlayerInfo *player) +static void Handle_OP_MOVE_PLAYER(struct NetworkServerPlayerInfo *player) { - struct NetworkServerPlayerInfo *v; + int player_action = getNetworkBuffer8BitInteger(read_buffer); int last_client_nr = 0; + struct NetworkServerPlayerInfo *p; int i; - /* store player action */ - for (v=first_player; v; v=v->next) + // store player action + for (p = first_player; p != NULL; p = p->next) { - if (v->number == player->number) + if (p->number == player->number) { - v->action = buffer[2]; - v->action_received = TRUE; + p->action = player_action; + p->action_received = TRUE; } } - /* check if server received action from each player */ - for (v=first_player; v; v=v->next) + // check if server received action from each player + for (p = first_player; p != NULL; p = p->next) { - if (!v->action_received) + if (!p->action_received) return; - if (v->number > last_client_nr) - last_client_nr = v->number; + if (p->number > last_client_nr) + last_client_nr = p->number; } - /* initialize all player actions to zero */ - for (i=0; inext) + // initialize all player actions to zero + for (i = 0; i < last_client_nr; i++) + player_action_all[i] = 0; + + // broadcast actions of all players to all players + for (p = first_player; p != NULL; p = p->next) { - buffer[6 + v->number-1] = v->action; - v->action = 0; - v->action_received = FALSE; + player_action_all[p->number - 1] = p->action; + + p->action = 0; + p->action_received = FALSE; } - buffer[2] = (unsigned char)((ServerFrameCounter >> 24) & 0xff); - buffer[3] = (unsigned char)((ServerFrameCounter >> 16) & 0xff); - buffer[4] = (unsigned char)((ServerFrameCounter >> 8) & 0xff); - buffer[5] = (unsigned char)((ServerFrameCounter >> 0) & 0xff); + initNetworkBufferForWriting(write_buffer, OP_MOVE_PLAYER, player->number); - broadcast(NULL, 6 + last_client_nr, 0); + putNetworkBuffer32BitInteger(write_buffer, ServerFrameCounter); + + for (i = 0; i < last_client_nr; i++) + putNetworkBuffer8BitInteger(write_buffer, player_action_all[i]); + + SendNetworkBufferToAll(write_buffer); ServerFrameCounter++; } +static void Handle_OP_BROADCAST_MESSAGE(struct NetworkServerPlayerInfo *player) +{ + char *message = getNetworkBufferString(read_buffer); + + if (options.verbose) + Error(ERR_NETWORK_SERVER, "client %d (%s) sends message: %s", + player->number, player->player_name, message); + + copyNetworkBufferForWriting(read_buffer, write_buffer, player->number); + + SendNetworkBufferToAllButOne(write_buffer, player); +} + +static void Handle_OP_LEVEL_FILE(struct NetworkServerPlayerInfo *player) +{ + copyNetworkBufferForWriting(read_buffer, write_buffer, player->number); + + SendNetworkBufferToAllButOne(write_buffer, player); +} + +static void ExitNetworkServer(int exit_value) +{ + Error(ERR_NETWORK_SERVER, "exiting network server"); + + exit(exit_value); +} + +// the following is not used for a standalone server; +// the pointer points to an integer containing the port-number +int NetworkServerThread(void *ptr) +{ + NetworkServer(*((int *) ptr), 0); + + // should never be reached + return 0; +} + void NetworkServer(int port, int serveronly) { - int i, sl, on; struct NetworkServerPlayerInfo *player; - int mfd; - int r; - unsigned int len; - struct protoent *tcpproto; - struct timeval tv; - int is_daemon = 0; - -#ifndef NeXT + IPaddress ip; + +#if defined(PLATFORM_UNIX) && !defined(PLATFORM_NEXT) struct sigaction sact; #endif + if (read_buffer == NULL) + read_buffer = newNetworkBuffer(); + + if (write_buffer == NULL) + write_buffer = newNetworkBuffer(); + if (port == 0) port = DEFAULT_SERVER_PORT; - if (!serveronly) - onceonly = 1; + // if only running the network server, exit on Ctrl-C + if (serveronly) + signal(SIGINT, ExitNetworkServer); - if ((tcpproto = getprotobyname("tcp")) != NULL) - tcp = tcpproto->p_proto; + if (!serveronly) + run_server_only_once = TRUE; -#ifdef NeXT +#if defined(PLATFORM_UNIX) +#if defined(PLATFORM_NEXT) signal(SIGPIPE, SIG_IGN); #else sact.sa_handler = SIG_IGN; sigemptyset(&sact.sa_mask); sact.sa_flags = 0; sigaction(SIGPIPE, &sact, NULL); +#endif #endif + if (SDLNet_ResolveHost(&ip, NULL, port) == -1) + Error(ERR_EXIT_NETWORK_SERVER, "SDLNet_ResolveHost() failed: %s", + SDLNet_GetError()); - if ((lfd = socket(PF_INET, SOCK_STREAM, 0)) < 0) - Error(ERR_EXIT_NETWORK_SERVER, "socket() failed"); - - saddr.sin_family = AF_INET; - saddr.sin_addr.s_addr = htonl(INADDR_ANY); - saddr.sin_port = htons(port); - - on = 1; + if ((fds = SDLNet_AllocSocketSet(MAX_PLAYERS + 1 + 1)) == NULL) + Error(ERR_EXIT_NETWORK_SERVER, "SDLNet_AllocSocketSet() failed: %s"), + SDLNet_GetError(); - setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(int)); - if (bind(lfd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) - Error(ERR_EXIT_NETWORK_SERVER, "bind() failed"); + if ((lfd = SDLNet_TCP_Open(&ip)) == NULL) + Error(ERR_EXIT_NETWORK_SERVER, "SDLNet_TCP_Open() failed: %s"), + SDLNet_GetError(); - listen(lfd, 5); + if (SDLNet_TCP_AddSocket(fds, lfd) == -1) + Error(ERR_EXIT_NETWORK_SERVER, "SDLNet_TCP_AddSocket() failed: %s"), + SDLNet_GetError(); - if (is_daemon) - { - /* become a daemon, breaking all ties with the controlling terminal */ - options.verbose = 0; - for (i=0; i<255; i++) - { - if (i != lfd) - close(i); - } + if ((udp = SDLNet_UDP_Open(port)) == NULL) + Error(ERR_EXIT_NETWORK_SERVER, "SDLNet_UDP_Open() failed: %s", + SDLNet_GetError()); - if (fork()) - exit(0); - setsid(); - if (fork()) - exit(0); - chdir("/"); - - /* open a fake stdin, stdout, stderr, just in case */ - open("/dev/null", O_RDONLY); - open("/dev/null", O_WRONLY); - open("/dev/null", O_WRONLY); - } + if (SDLNet_UDP_AddSocket(fds, udp) == -1) + Error(ERR_EXIT_NETWORK_SERVER, "SDLNet_TCP_AddSocket() failed: %s"), + SDLNet_GetError(); if (options.verbose) { Error(ERR_NETWORK_SERVER, "started up, listening on port %d", port); Error(ERR_NETWORK_SERVER, "using protocol version %d.%d.%d", - PROTOCOL_VERSION_1, PROTOCOL_VERSION_2, PROTOCOL_VERSION_3); + PROTOCOL_VERSION_MAJOR, + PROTOCOL_VERSION_MINOR, + PROTOCOL_VERSION_PATCH); } - while(1) + while (1) { - interrupt = 0; + // wait for 100 ms for activity on open network sockets + if (SDLNet_CheckSockets(fds, 100) < 1) + continue; + + // accept incoming TCP connections + if (SDLNet_SocketReady(lfd)) + { + Error(ERR_DEBUG, "got TCP packet"); - for (player=first_player; player; player=player->next) - flushuser(player); + TCPsocket newsock; - FD_ZERO(&fds); - mfd = lfd; - player = first_player; - while (player) + newsock = SDLNet_TCP_Accept(lfd); + + if (newsock) + AddPlayer(newsock); + } + + // accept incoming UDP packets + if (SDLNet_SocketReady(udp)) { - FD_SET(player->fd, &fds); - if (player->fd > mfd) - mfd = player->fd; - player = player->next; + Error(ERR_DEBUG, "got UDP packet"); + + static UDPpacket packet; + + int num_packets = SDLNet_UDP_Recv(udp, &packet); + + if (num_packets == 1) + { + // bounce packet + SDLNet_UDP_Send(udp, -1, &packet); + } } - FD_SET(lfd, &fds); - tv.tv_sec = 0; - tv.tv_usec = 500000; - if ((sl = select(mfd + 1, &fds, NULL, NULL, &tv)) < 0) + + for (player = first_player; player != NULL; player = player->next) { - if (errno != EINTR) - Error(ERR_EXIT_NETWORK_SERVER, "select() failed"); - else + if (!SDLNet_SocketReady(player->fd)) continue; - } - if (sl < 0) - continue; - - if (sl == 0) - continue; + initNetworkBufferForReceiving(read_buffer); - if (FD_ISSET(lfd, &fds)) - { - int newfd, slen; + int num_bytes = receiveNetworkBufferPacket(read_buffer, player->fd); - slen = sizeof(saddr); - newfd = accept(lfd, (struct sockaddr *)&saddr, &slen); - if (newfd < 0) + if (num_bytes <= 0) { - if (errno != EINTR) - Error(ERR_EXIT_NETWORK_SERVER, "accept() failed"); + if (options.verbose) + Error(ERR_NETWORK_SERVER, "EOF from client %d (%s)", + player->number, player->player_name); + + RemovePlayer(player); + + break; } - else + + initNetworkBufferForReading(read_buffer); + + int message_type = getNetworkBuffer8BitInteger(read_buffer); + + // skip player number + getNetworkBuffer8BitInteger(read_buffer); + + if (!player->introduced && + message_type != OP_PLAYER_NAME && + message_type != OP_PROTOCOL_VERSION) { - if (tcp != -1) - { - on = 1; - setsockopt(newfd, tcp, TCP_NODELAY, (char *)&on, sizeof(int)); - } - AddPlayer(newfd); - } - continue; - } + if (options.verbose) + Error(ERR_NETWORK_SERVER, "got opcode %d for client %d which is not introduced yet (expected OP_PLAYER_NAME or OP_PROTOCOL_VERSION)", message_type, player->number); - player = first_player; + RemovePlayer(player); - do - { - if (FD_ISSET(player->fd, &fds)) + break; + } + + switch (message_type) { - r = read(player->fd, player->readbuffer + player->nread, MAX_BUFFER_SIZE - player->nread); - if (r <= 0) - { - if (options.verbose) - Error(ERR_NETWORK_SERVER, "EOF from client %d (%s)", - player->number, player->player_name); - RemovePlayer(player); - interrupt = 1; + case OP_PROTOCOL_VERSION: + Handle_OP_PROTOCOL_VERSION(player); + break; + + case OP_NUMBER_WANTED: + Handle_OP_NUMBER_WANTED(player); break; - } - player->nread += r; - while (player->nread >= 4 && player->nread >= 4 + player->readbuffer[3]) - { - len = player->readbuffer[3]; - if (player->readbuffer[0] || player->readbuffer[1] || player->readbuffer[2]) - { - if (options.verbose) - Error(ERR_NETWORK_SERVER, "crap from client %d (%s)", - player->number, player->player_name); - RemovePlayer(player); - interrupt = 1; - break; - } - memcpy(buffer, &player->readbuffer[4], len); - player->nread -= 4 + len; - memmove(player->readbuffer, player->readbuffer + 4 + len, player->nread); - - buffer[0] = player->number; - if (!player->introduced && buffer[1] != OP_PLAYER_NAME) - { - if (options.verbose) - Error(ERR_NETWORK_SERVER, "!(client %d)->introduced && buffer[1]==%d (expected OP_PLAYER_NAME)", buffer[0], buffer[1]); - - RemovePlayer(player); - interrupt = 1; - break; - } - - switch(buffer[1]) - { - case OP_PLAYER_NAME: - Handle_OP_PLAYER_NAME(player, len); - break; - - case OP_PROTOCOL_VERSION: - Handle_OP_PROTOCOL_VERSION(player, len); - break; - - case OP_NUMBER_WANTED: - Handle_OP_NUMBER_WANTED(player); - break; - - case OP_START_PLAYING: - Handle_OP_START_PLAYING(player); - break; - - case OP_PAUSE_PLAYING: - Handle_OP_PAUSE_PLAYING(player); - break; - - case OP_CONTINUE_PLAYING: - Handle_OP_CONTINUE_PLAYING(player); - break; - - case OP_STOP_PLAYING: - Handle_OP_STOP_PLAYING(player); - break; - - case OP_MOVE_FIGURE: - Handle_OP_MOVE_FIGURE(player); - break; - - case OP_BROADCAST_MESSAGE: - buffer[len] = '\0'; - if (options.verbose) - Error(ERR_NETWORK_SERVER, "client %d (%s) sends message: %s", - player->number, player->player_name, &buffer[2]); - broadcast(player, len, 0); - break; - - default: - if (options.verbose) - Error(ERR_NETWORK_SERVER, - "unknown opcode %d from client %d (%s)", - buffer[0], player->number, player->player_name); - } - } - } - if (player && !interrupt) - player = player->next; + case OP_PLAYER_NAME: + Handle_OP_PLAYER_NAME(player); + break; + + case OP_START_PLAYING: + Handle_OP_START_PLAYING(player); + break; + + case OP_PAUSE_PLAYING: + Handle_OP_PAUSE_PLAYING(player); + break; + + case OP_CONTINUE_PLAYING: + Handle_OP_CONTINUE_PLAYING(player); + break; + + case OP_STOP_PLAYING: + Handle_OP_STOP_PLAYING(player); + break; + + case OP_MOVE_PLAYER: + Handle_OP_MOVE_PLAYER(player); + break; + + case OP_BROADCAST_MESSAGE: + Handle_OP_BROADCAST_MESSAGE(player); + break; + + case OP_LEVEL_FILE: + Handle_OP_LEVEL_FILE(player); + break; + + default: + if (options.verbose) + Error(ERR_NETWORK_SERVER, + "unknown opcode %d from client %d (%s)", + message_type, player->number, player->player_name); + } } - while (player && !interrupt); } } - -#endif /* PLATFORM_UNIX */