completely refactored client and server side network protocol code
authorHolger Schemel <info@artsoft.org>
Tue, 21 Aug 2018 09:32:51 +0000 (11:32 +0200)
committerHolger Schemel <info@artsoft.org>
Tue, 21 Aug 2018 09:34:57 +0000 (11:34 +0200)
src/netserv.c
src/netserv.h
src/network.c

index 4b846f9da413425db5e86999fd149111f34ea77c..551772a00c883b5053ddfe00b2925f6bf8ee2b5d 100644 (file)
 #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
 {
   TCPsocket 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;
+  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;
@@ -47,96 +45,354 @@ static TCPsocket lfd;              /* listening TCP socket */
 static UDPsocket udp;          /* listening UDP socket */
 static SDLNet_SocketSet fds;   /* socket set */
 
-static unsigned char realbuffer[512], *buffer = realbuffer + 4;
-
-static int interrupt;
+static struct NetworkBuffer *read_buffer = NULL;
+static struct NetworkBuffer *write_buffer = NULL;
 
 static unsigned int ServerFrameCounter = 0;
 
-static void addtobuffer(struct NetworkServerPlayerInfo *player,
-                       unsigned char *b, int len)
+
+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;
+}
+
+char *getNetworkString(byte *ptr)
+{
+  return (char *)ptr;
+}
+
+int putNetworkString(byte *ptr, char *s)
 {
-  if (player->nwrite + len >= MAX_BUFFER_SIZE)
-    Error(ERR_EXIT_NETWORK_SERVER,
-         "internal error: network send buffer overflow");
+  strcpy((char *)ptr, s);
 
-  memcpy(player->writbuffer + player->nwrite, b, len);
-  player->nwrite += len;
+  return strlen(s) + 1;
 }
 
-static void flushuser(struct NetworkServerPlayerInfo *player)
+struct NetworkBuffer *newNetworkBuffer()
 {
-  if (player->nwrite)
+  struct NetworkBuffer *new = checked_calloc(sizeof(struct NetworkBuffer));
+
+  new->max_size = MAX_BUFFER_SIZE;
+  new->size = 0;
+  new->pos = 0;
+
+  new->buffer = checked_calloc(new->max_size);
+
+  return new;
+}
+
+static void resetNetworkBufferForReading(struct NetworkBuffer *nb)
+{
+  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)
   {
-    SDLNet_TCP_Send(player->fd, player->writbuffer, player->nwrite);
+    int b = getNetworkBuffer8BitInteger(nb_from);
 
-    player->nwrite = 0;
+    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 (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;
+}
+
+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->introduced &&
-       (player->active || !activeonly))
-      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 sendtoone(struct NetworkServerPlayerInfo *to, int len)
+static void SendNetworkBufferToAll(struct NetworkBuffer *nb)
 {
-  realbuffer[0] = realbuffer[1] = realbuffer[2] = 0;
-  realbuffer[3] = (unsigned char)len;
-  addtobuffer(to, realbuffer, 4 + len);
+  SendNetworkBufferToAllButOne(nb, NULL);
+}
+
+static void SendNetworkBufferToClient(struct NetworkBuffer *nb,
+                                     struct NetworkServerPlayerInfo *player)
+{
+  /* 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;
       }
     }
   }
 
-  SDLNet_TCP_DelSocket(fds, player->fd);
-  SDLNet_TCP_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 0  /* do not terminate network server if last player disconnected */
-  if (onceonly && clients == 0)
+  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
@@ -144,103 +400,110 @@ static void RemovePlayer(struct NetworkServerPlayerInfo *player)
 
 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;
-
-  SDLNet_TCP_AddSocket(fds, fd);
+  player->next = first_player;
 
   first_player = player;
 
-  nxn = 1;
-
   while (again)
   {
     again = FALSE;
-    v = player->next;
+    p = player->next;
 
-    while (v)
+    while (p)
     {
-      if (v->number == nxn)
+      if (p->number == number)
       {
-       nxn++;
+       number++;
 
        again = TRUE;
+
        break;
       }
-      v = v->next;
+
+      p = p->next;
     }
   }
 
-  player->number = nxn;
-  clients++;
+  player->number = number;
+  num_clients++;
 
-  buffer[0] = 0;
-  buffer[1] = OP_YOUR_NUMBER;
-  buffer[2] = player->number;
-  sendtoone(player, 3);
+  initNetworkBufferForWriting(write_buffer, OP_YOUR_NUMBER, 0);
+
+  putNetworkBuffer8BitInteger(write_buffer, player->number);
+
+  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;
     }
   }
@@ -262,86 +525,87 @@ static void Handle_OP_NUMBER_WANTED(struct NetworkServerPlayerInfo *player)
   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; i < len - 2; i++)
+  strncpy(player->player_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 leveldir %d (%s)]",
-         player->number, player->player_name,
-         (buffer[2] << 8) + buffer[3],
-         (buffer[4] << 8) + buffer[5],
-         &buffer[10]);
+         "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 */
   ServerFrameCounter = 0;
@@ -349,13 +613,18 @@ static void Handle_OP_START_PLAYING(struct NetworkServerPlayerInfo *player)
   Error(ERR_NETWORK_SERVER, "resetting ServerFrameCounter to 0");
 
   /* reset player actions */
-  for (v = first_player; v; v = v->next)
+  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)
@@ -363,7 +632,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)
@@ -371,67 +643,92 @@ 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 = buffer[2];
+  int cause_for_stopping = getNetworkBuffer8BitInteger(read_buffer);
 
   if (options.verbose)
     Error(ERR_NETWORK_SERVER, "client %d (%s) stops game [%d]",
          player->number, player->player_name, cause_for_stopping);
-  broadcast(NULL, 3, 0);
+
+  copyNetworkBufferForWriting(read_buffer, write_buffer, player->number);
+
+  SendNetworkBufferToAll(write_buffer);
 }
 
 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)
+  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)
+  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;
   }
 
+  int player_action_all[last_client_nr];
+
   /* initialize all player actions to zero */
   for (i = 0; i < last_client_nr; i++)
-    buffer[6 + i] = 0;
+    player_action_all[i] = 0;
 
   /* broadcast actions of all players to all players */
-  for (v = first_player; v; v = v->next)
+  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);
+
+  putNetworkBuffer32BitInteger(write_buffer, ServerFrameCounter);
+
+  for (i = 0; i < last_client_nr; i++)
+    putNetworkBuffer8BitInteger(write_buffer, player_action_all[i]);
 
-  broadcast(NULL, 6 + last_client_nr, 0);
+  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);
+}
+
 void ExitNetworkServer(int exit_value)
 {
   Error(ERR_NETWORK_SERVER, "exiting network server");
@@ -452,14 +749,18 @@ int NetworkServerThread(void *ptr)
 void NetworkServer(int port, int serveronly)
 {
   struct NetworkServerPlayerInfo *player;
-  int r; 
-  unsigned int len;
   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;
 
@@ -468,7 +769,7 @@ void NetworkServer(int port, int serveronly)
     signal(SIGINT, ExitNetworkServer);
 
   if (!serveronly)
-    onceonly = 1;
+    run_server_only_once = TRUE;
 
 #if defined(PLATFORM_UNIX)
 #if defined(PLATFORM_NEXT)
@@ -509,16 +810,13 @@ void NetworkServer(int port, int serveronly)
   {
     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)
   {
-    interrupt = 0;
-
-    for (player = first_player; player; player = player->next)
-      flushuser(player);
-
     // wait for 100 ms for activity on open network sockets
     if (SDLNet_CheckSockets(fds, 100) < 1)
       continue;
@@ -552,108 +850,89 @@ void NetworkServer(int port, int serveronly)
       }
     }
 
-    player = first_player;
-
-    while (player && !interrupt)
+    for (player = first_player; player != NULL; player = player->next)
     {
-      if (SDLNet_SocketReady(player->fd))
+      if (!SDLNet_SocketReady(player->fd))
+       continue;
+
+      initNetworkBufferForReceiving(read_buffer);
+
+      int num_bytes = receiveNetworkBufferPacket(read_buffer, player->fd);
+
+      if (num_bytes <= 0)
       {
-       /* read only 1 byte, because SDLNet blocks when we want more than is
-          in the buffer */
-       r = SDLNet_TCP_Recv(player->fd, player->readbuffer + player->nread, 1);
+       if (options.verbose)
+         Error(ERR_NETWORK_SERVER, "EOF from client %d (%s)",
+               player->number, player->player_name);
 
-       if (r <= 0)
-       {
-         if (options.verbose)
-           Error(ERR_NETWORK_SERVER, "EOF from client %d (%s)",
-                 player->number, player->player_name);
-         RemovePlayer(player);
-         interrupt = 1;
-         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 &&
-             buffer[1] != OP_PROTOCOL_VERSION)
-         {
-           if (options.verbose)
-             Error(ERR_NETWORK_SERVER, "!(client %d)->introduced && buffer[1]==%d (expected OP_PLAYER_NAME or OP_PROTOCOL_VERSION)", 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_PLAYER:
-             Handle_OP_MOVE_PLAYER(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);
-         }
-       }
+       RemovePlayer(player);
+
+       break;
       }
 
-      if (player && !interrupt)
-       player = player->next;
+      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 (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);
+
+       RemovePlayer(player);
+
+       break;
+      }
+
+      switch (message_type)
+      {
+       case OP_PROTOCOL_VERSION:
+         Handle_OP_PROTOCOL_VERSION(player);
+         break;
+
+       case OP_NUMBER_WANTED:
+         Handle_OP_NUMBER_WANTED(player);
+         break;
+
+       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;
+
+       default:
+         if (options.verbose)
+           Error(ERR_NETWORK_SERVER,
+                 "unknown opcode %d from client %d (%s)",
+                 message_type, player->number, player->player_name);
+      }
     }
   }
 }
index 463b202f0bae28324f7cf5ba0cd4070d901bf12d..6017557ab9b8f272347e55420dfc575df2ec2ee1 100644 (file)
@@ -16,9 +16,9 @@
 
 #define DEFAULT_SERVER_PORT    19504
 
-#define PROTOCOL_VERSION_1     2
-#define PROTOCOL_VERSION_2     0
-#define PROTOCOL_VERSION_3     0
+#define PROTOCOL_VERSION_MAJOR 2
+#define PROTOCOL_VERSION_MINOR 0
+#define PROTOCOL_VERSION_PATCH 0
 
 #define OP_PROTOCOL_VERSION    1
 #define OP_BAD_PROTOCOL_VERSION        2
 
 #define MAX_BUFFER_SIZE                4096
 
+
+struct NetworkBuffer
+{
+  unsigned int max_size;
+  unsigned int size;
+  unsigned int pos;
+
+  byte *buffer;
+};
+
+
+int getNetwork8BitInteger(byte *);
+int putNetwork8BitInteger(byte *, int);
+int getNetwork16BitInteger(byte *);
+int putNetwork16BitInteger(byte *, int);
+int getNetwork32BitInteger(byte *);
+int putNetwork32BitInteger(byte *, int);
+char *getNetworkString(byte *);
+int putNetworkString(byte *, char *);
+
+struct NetworkBuffer *newNetworkBuffer();
+void initNetworkBufferForReceiving(struct NetworkBuffer *);
+void initNetworkBufferForReading(struct NetworkBuffer *);
+void initNetworkBufferForWriting(struct NetworkBuffer *, int, int);
+
+int receiveNetworkBufferBytes(struct NetworkBuffer *, TCPsocket, int);
+int receiveNetworkBufferPacket(struct NetworkBuffer *, TCPsocket);
+
+int getNetworkBuffer8BitInteger(struct NetworkBuffer *);
+void putNetworkBuffer8BitInteger(struct NetworkBuffer *, int);
+int getNetworkBuffer16BitInteger(struct NetworkBuffer *);
+void putNetworkBuffer16BitInteger(struct NetworkBuffer *, int);
+int getNetworkBuffer32BitInteger(struct NetworkBuffer *);
+void putNetworkBuffer32BitInteger(struct NetworkBuffer *, int);
+char *getNetworkBufferString(struct NetworkBuffer *);
+void putNetworkBufferString(struct NetworkBuffer *, char *);
+
 int NetworkServerThread(void *);
 void NetworkServer(int, int);
 
index b0a6a0eee2fcc78096a6c25eae72a34cd4358b94..728517465eb8d5123dc5652f5ab9cbcf716a4a2b 100644 (file)
@@ -43,10 +43,9 @@ static TCPsocket sfd;                /* TCP server socket */
 static UDPsocket udp;          /* UDP server socket */
 static SDLNet_SocketSet rfds;  /* socket set */
 
-static byte realbuffer[512];
-static byte readbuffer[MAX_BUFFER_SIZE], writbuffer[MAX_BUFFER_SIZE];
-static byte *buffer = realbuffer + 4;
-static int nread = 0, nwrite = 0;
+static struct NetworkBuffer *read_buffer = NULL;
+static struct NetworkBuffer *write_buffer = NULL;
+
 static boolean stop_network_game = FALSE;
 static boolean stop_network_client = FALSE;
 static char stop_network_client_message[MAX_OUTPUT_LINESIZE + 1];
@@ -128,24 +127,16 @@ static void DrawNetworkText_Title(char *message)
   DrawNetworkTextExt(message, FC_GREEN, TRUE);
 }
 
-static void SendBufferToServer(int size)
+static void SendNetworkBufferToServer(struct NetworkBuffer *nb)
 {
   if (!network.enabled)
     return;
 
-  realbuffer[0] = realbuffer[1] = realbuffer[2] = 0;
-  realbuffer[3] = (byte)size;
-  buffer[0] = 0;
-
-  if (nwrite + 4 + size >= MAX_BUFFER_SIZE)
-    Error(ERR_EXIT, "internal error: network send buffer overflow");
-
-  memcpy(writbuffer + nwrite, realbuffer, 4 + size);
-  nwrite += 4 + size;
+  /* set message length header */
+  putNetwork32BitInteger(nb->buffer, nb->size - 4);
 
   /* directly send the buffer to the network server */
-  SDLNet_TCP_Send(sfd, writbuffer, nwrite);
-  nwrite = 0;
+  SDLNet_TCP_Send(sfd, nb->buffer, nb->size);
 }
 
 struct NetworkClientPlayerInfo *getNetworkPlayer(int player_nr)
@@ -199,6 +190,12 @@ boolean ConnectToServer(char *hostname, int port)
   int server_host = 0;
   int i;
 
+  if (read_buffer == NULL)
+    read_buffer = newNetworkBuffer();
+
+  if (write_buffer == NULL)
+    write_buffer = newNetworkBuffer();
+
   DrawNetworkText_Title("Initializing Network");
 
   if (port == 0)
@@ -350,107 +347,112 @@ boolean ConnectToServer(char *hostname, int port)
 
 void SendToServer_PlayerName(char *player_name)
 {
-  int len_player_name = strlen(player_name);
+  initNetworkBufferForWriting(write_buffer, OP_PLAYER_NAME, 0);
+
+  putNetworkBufferString(write_buffer, player_name);
+
+  SendNetworkBufferToServer(write_buffer);
 
-  buffer[1] = OP_PLAYER_NAME;
-  memcpy(&buffer[2], player_name, len_player_name);
-  SendBufferToServer(2 + len_player_name);
   Error(ERR_NETWORK_CLIENT, "you set your player name to \"%s\"", player_name);
 }
 
 void SendToServer_ProtocolVersion()
 {
-  buffer[1] = OP_PROTOCOL_VERSION;
-  buffer[2] = PROTOCOL_VERSION_1;
-  buffer[3] = PROTOCOL_VERSION_2;
-  buffer[4] = PROTOCOL_VERSION_3;
+  initNetworkBufferForWriting(write_buffer, OP_PROTOCOL_VERSION, 0);
 
-  SendBufferToServer(5);
+  putNetworkBuffer8BitInteger(write_buffer, PROTOCOL_VERSION_MAJOR);
+  putNetworkBuffer8BitInteger(write_buffer, PROTOCOL_VERSION_MINOR);
+  putNetworkBuffer8BitInteger(write_buffer, PROTOCOL_VERSION_PATCH);
+
+  SendNetworkBufferToServer(write_buffer);
 }
 
 void SendToServer_NrWanted(int nr_wanted)
 {
-  buffer[1] = OP_NUMBER_WANTED;
-  buffer[2] = nr_wanted;
+  initNetworkBufferForWriting(write_buffer, OP_NUMBER_WANTED, 0);
+
+  putNetworkBuffer8BitInteger(write_buffer, nr_wanted);
 
-  SendBufferToServer(3);
+  SendNetworkBufferToServer(write_buffer);
 }
 
 void SendToServer_StartPlaying()
 {
   unsigned int new_random_seed = InitRND(level.random_seed);
 
-  int dummy = 0;               /* !!! HAS NO MEANING ANYMORE !!! */
-                               /* the name of the level must be enough */
-
-  buffer[1] = OP_START_PLAYING;
-  buffer[2] = (byte)(level_nr >> 8);
-  buffer[3] = (byte)(level_nr & 0xff);
-  buffer[4] = (byte)(dummy >> 8);
-  buffer[5] = (byte)(dummy & 0xff);
-
-  buffer[6] = (unsigned char)((new_random_seed >> 24) & 0xff);
-  buffer[7] = (unsigned char)((new_random_seed >> 16) & 0xff);
-  buffer[8] = (unsigned char)((new_random_seed >>  8) & 0xff);
-  buffer[9] = (unsigned char)((new_random_seed >>  0) & 0xff);
+  initNetworkBufferForWriting(write_buffer, OP_START_PLAYING, 0);
 
-  strcpy((char *)&buffer[10], leveldir_current->identifier);
+  putNetworkBufferString(      write_buffer, leveldir_current->identifier);
+  putNetworkBuffer16BitInteger(write_buffer, level_nr);
+  putNetworkBuffer32BitInteger(write_buffer, new_random_seed);
 
-  SendBufferToServer(10 + strlen(leveldir_current->identifier) + 1);
+  SendNetworkBufferToServer(write_buffer);
 }
 
 void SendToServer_PausePlaying()
 {
-  buffer[1] = OP_PAUSE_PLAYING;
+  initNetworkBufferForWriting(write_buffer, OP_PAUSE_PLAYING, 0);
 
-  SendBufferToServer(2);
+  SendNetworkBufferToServer(write_buffer);
 }
 
 void SendToServer_ContinuePlaying()
 {
-  buffer[1] = OP_CONTINUE_PLAYING;
+  initNetworkBufferForWriting(write_buffer, OP_CONTINUE_PLAYING, 0);
 
-  SendBufferToServer(2);
+  SendNetworkBufferToServer(write_buffer);
 }
 
 void SendToServer_StopPlaying(int cause_for_stopping)
 {
-  buffer[1] = OP_STOP_PLAYING;
-  buffer[2] = cause_for_stopping;
+  initNetworkBufferForWriting(write_buffer, OP_STOP_PLAYING, 0);
 
-  SendBufferToServer(3);
+  putNetworkBuffer8BitInteger(write_buffer, cause_for_stopping);
+
+  SendNetworkBufferToServer(write_buffer);
 }
 
 void SendToServer_MovePlayer(byte player_action)
 {
-  buffer[1] = OP_MOVE_PLAYER;
-  buffer[2] = player_action;
+  initNetworkBufferForWriting(write_buffer, OP_MOVE_PLAYER, 0);
+
+  putNetworkBuffer8BitInteger(write_buffer, player_action);
 
-  SendBufferToServer(3);
+  SendNetworkBufferToServer(write_buffer);
 }
 
 static void Handle_OP_BAD_PROTOCOL_VERSION()
 {
+  int protocol_version_major = getNetworkBuffer8BitInteger(read_buffer);
+  int protocol_version_minor = getNetworkBuffer8BitInteger(read_buffer);
+
   Error(ERR_WARN, "protocol version mismatch");
   Error(ERR_WARN, "server expects %d.%d.x instead of %d.%d.%d",
-       buffer[2], buffer[3],
-       PROTOCOL_VERSION_1, PROTOCOL_VERSION_2, PROTOCOL_VERSION_3);
+       protocol_version_major,
+       protocol_version_minor,
+       PROTOCOL_VERSION_MAJOR,
+       PROTOCOL_VERSION_MINOR,
+       PROTOCOL_VERSION_PATCH);
 
   sprintf(stop_network_client_message, "Network protocol version mismatch! Server expects version %d.%d.x instead of %d.%d.%d!",
-         buffer[2], buffer[3],
-         PROTOCOL_VERSION_1, PROTOCOL_VERSION_2, PROTOCOL_VERSION_3);
+         protocol_version_major,
+         protocol_version_minor,
+         PROTOCOL_VERSION_MAJOR,
+         PROTOCOL_VERSION_MINOR,
+         PROTOCOL_VERSION_PATCH);
 
   stop_network_client = TRUE;
 }
 
 static void Handle_OP_YOUR_NUMBER()
 {
-  int new_client_nr = buffer[2];
+  int old_client_nr = getNetworkBuffer8BitInteger(read_buffer);
+  int new_client_nr = getNetworkBuffer8BitInteger(read_buffer);
   int new_index_nr = new_client_nr - 1;
   struct PlayerInfo *old_local_player = local_player;
   struct PlayerInfo *new_local_player = &stored_player[new_index_nr];
 
-  printf("OP_YOUR_NUMBER: %d\n", buffer[0]);
+  printf("OP_YOUR_NUMBER: %d\n", old_client_nr);
   first_player.nr = new_client_nr;
 
   if (old_local_player != new_local_player)
@@ -476,16 +478,16 @@ static void Handle_OP_YOUR_NUMBER()
 
 static void Handle_OP_NUMBER_WANTED()
 {
-  int client_nr_wanted = buffer[2];
-  int old_client_nr = buffer[0];
-  int new_client_nr = buffer[3];
+  int old_client_nr    = getNetworkBuffer8BitInteger(read_buffer);
+  int client_nr_wanted = getNetworkBuffer8BitInteger(read_buffer);
+  int new_client_nr    = getNetworkBuffer8BitInteger(read_buffer);
   int old_index_nr = old_client_nr - 1;
   int new_index_nr = new_client_nr - 1;
   int index_nr_wanted = client_nr_wanted - 1;
   struct PlayerInfo *old_player = &stored_player[old_index_nr];
   struct PlayerInfo *new_player = &stored_player[new_index_nr];
 
-  printf("OP_NUMBER_WANTED: %d\n", buffer[0]);
+  printf("OP_NUMBER_WANTED: %d\n", old_client_nr);
 
   if (new_client_nr == client_nr_wanted)       /* switching succeeded */
   {
@@ -533,23 +535,25 @@ static void Handle_OP_NUMBER_WANTED()
     DrawNetworkPlayers();
 }
 
-static void Handle_OP_PLAYER_NAME(unsigned int len)
+static void Handle_OP_PLAYER_NAME()
 {
-  struct NetworkClientPlayerInfo *player;
-  int player_nr = (int)buffer[0];
+  int player_nr = getNetworkBuffer8BitInteger(read_buffer);
+  char *player_name = getNetworkBufferString(read_buffer);
+  struct NetworkClientPlayerInfo *player = getNetworkPlayer(player_nr);
 
   printf("OP_PLAYER_NAME: %d\n", player_nr);
-  player = getNetworkPlayer(player_nr);
-  buffer[len] = 0;
+
+  strncpy(player->name, player_name, MAX_PLAYER_NAME_LEN);
+  player->name[MAX_PLAYER_NAME_LEN] = '\0';
+
   Error(ERR_NETWORK_CLIENT, "client %d calls itself \"%s\"",
-       buffer[0], &buffer[2]);
-  strncpy(player->name, (char *)&buffer[2], MAX_PLAYER_NAME_LEN);
+       player_nr, player->name);
 }
 
 static void Handle_OP_PLAYER_CONNECTED()
 {
   struct NetworkClientPlayerInfo *player, *last_player = NULL;
-  int new_client_nr = (int)buffer[0];
+  int new_client_nr = getNetworkBuffer8BitInteger(read_buffer);
   int new_index_nr = new_client_nr - 1;
 
   printf("OP_PLAYER_CONNECTED: %d\n", new_client_nr);
@@ -575,13 +579,13 @@ static void Handle_OP_PLAYER_CONNECTED()
 static void Handle_OP_PLAYER_DISCONNECTED()
 {
   struct NetworkClientPlayerInfo *player, *player_disconnected;
-  int player_nr = (int)buffer[0];
+  int player_nr = getNetworkBuffer8BitInteger(read_buffer);
   int index_nr = player_nr - 1;
 
   printf("OP_PLAYER_DISCONNECTED: %d\n", player_nr);
   player_disconnected = getNetworkPlayer(player_nr);
   Error(ERR_NETWORK_CLIENT, "client %d (%s) disconnected",
-       player_nr, getNetworkPlayerName(buffer[0]));
+       player_nr, getNetworkPlayerName(player_nr));
 
   for (player = &first_player; player; player = player->next)
     if (player->next == player_disconnected)
@@ -612,18 +616,14 @@ static void Handle_OP_PLAYER_DISCONNECTED()
 
 static void Handle_OP_START_PLAYING()
 {
-  LevelDirTree *new_leveldir;
-  int new_level_nr;
-  unsigned int new_random_seed;
-  char *new_leveldir_identifier;
+  int player_nr = getNetworkBuffer8BitInteger(read_buffer);
+  char *new_leveldir_identifier = getNetworkBufferString(read_buffer);
+  int new_level_nr = getNetworkBuffer16BitInteger(read_buffer);
+  unsigned int new_random_seed = getNetworkBuffer32BitInteger(read_buffer);
 
-  new_level_nr = (buffer[2] << 8) + buffer[3];
-  new_random_seed =
-    (buffer[6] << 24) | (buffer[7] << 16) | (buffer[8] << 8) | (buffer[9]);
-  new_leveldir_identifier = (char *)&buffer[10];
+  LevelDirTree *new_leveldir =
+    getTreeInfoFromIdentifier(leveldir_first, new_leveldir_identifier);
 
-  new_leveldir = getTreeInfoFromIdentifier(leveldir_first,
-                                          new_leveldir_identifier);
   if (new_leveldir == NULL)
   {
     Error(ERR_WARN, "no such level identifier: '%s'", new_leveldir_identifier);
@@ -633,10 +633,10 @@ static void Handle_OP_START_PLAYING()
     return;
   }
 
-  printf("OP_START_PLAYING: %d\n", buffer[0]);
+  printf("OP_START_PLAYING: %d\n", player_nr);
   Error(ERR_NETWORK_CLIENT,
        "client %d starts game [level %d from level identifier '%s']\n",
-       buffer[0], new_level_nr, new_leveldir->identifier);
+       player_nr, new_level_nr, new_leveldir->identifier);
 
   leveldir_current = new_leveldir;
   level_nr = new_level_nr;
@@ -650,48 +650,56 @@ static void Handle_OP_START_PLAYING()
 
 static void Handle_OP_PAUSE_PLAYING()
 {
-  printf("OP_PAUSE_PLAYING: %d\n", buffer[0]);
-  Error(ERR_NETWORK_CLIENT, "client %d pauses game", buffer[0]);
+  int player_nr = getNetworkBuffer8BitInteger(read_buffer);
+
+  printf("OP_PAUSE_PLAYING: %d\n", player_nr);
+  Error(ERR_NETWORK_CLIENT, "client %d pauses game", player_nr);
 
   if (game_status == GAME_MODE_PLAYING)
   {
     tape.pausing = TRUE;
-    DrawVideoDisplay(VIDEO_STATE_PAUSE_ON,0);
+    DrawVideoDisplay(VIDEO_STATE_PAUSE_ON, 0);
   }
 }
 
 static void Handle_OP_CONTINUE_PLAYING()
 {
-  printf("OP_CONTINUE_PLAYING: %d\n", buffer[0]);
-  Error(ERR_NETWORK_CLIENT, "client %d continues game", buffer[0]);
+  int player_nr = getNetworkBuffer8BitInteger(read_buffer);
+
+  printf("OP_CONTINUE_PLAYING: %d\n", player_nr);
+  Error(ERR_NETWORK_CLIENT, "client %d continues game", player_nr);
 
   if (game_status == GAME_MODE_PLAYING)
   {
     tape.pausing = FALSE;
-    DrawVideoDisplay(VIDEO_STATE_PAUSE_OFF,0);
+    DrawVideoDisplay(VIDEO_STATE_PAUSE_OFF, 0);
   }
 }
 
 static void Handle_OP_STOP_PLAYING()
 {
-  printf("OP_STOP_PLAYING: %d [%d]\n", buffer[0], buffer[2]);
-  Error(ERR_NETWORK_CLIENT, "client %d stops game [%d]", buffer[0], buffer[2]);
+  int client_nr = getNetworkBuffer8BitInteger(read_buffer);
+  int cause_for_stopping = getNetworkBuffer8BitInteger(read_buffer);
+
+  printf("OP_STOP_PLAYING: %d [%d]\n", client_nr, cause_for_stopping);
+  Error(ERR_NETWORK_CLIENT, "client %d stops game [%d]",
+       client_nr, cause_for_stopping);
 
   if (game_status == GAME_MODE_PLAYING)
   {
-    int client_nr = buffer[0];
     int index_nr = client_nr - 1;
     struct PlayerInfo *client_player = &stored_player[index_nr];
     boolean stopped_by_remote_player = (!client_player->connected_locally);
     char message[100];
 
-    sprintf(message, (buffer[2] == NETWORK_STOP_BY_PLAYER ?
+    sprintf(message, (cause_for_stopping == NETWORK_STOP_BY_PLAYER ?
                      "Network game stopped by player %d!" :
-                     buffer[2] == NETWORK_STOP_BY_ERROR ?
+                     cause_for_stopping == NETWORK_STOP_BY_ERROR ?
                      "Network game stopped due to internal error!" :
                      "Network game stopped!"), client_nr);
 
-    if (buffer[2] != NETWORK_STOP_BY_PLAYER || stopped_by_remote_player)
+    if (cause_for_stopping != NETWORK_STOP_BY_PLAYER ||
+       stopped_by_remote_player)
       Request(message, REQ_CONFIRM | REQ_STAY_CLOSED);
 
     SetGameStatus(GAME_MODE_MAIN);
@@ -700,20 +708,19 @@ static void Handle_OP_STOP_PLAYING()
   }
 }
 
-static void Handle_OP_MOVE_PLAYER(unsigned int len)
+static void Handle_OP_MOVE_PLAYER()
 {
-  int server_frame_counter;
+  int player_nr = getNetworkBuffer8BitInteger(read_buffer);
+  int server_frame_counter = getNetworkBuffer32BitInteger(read_buffer);
   int i;
 
   if (!network_playing)
     return;
 
-  server_frame_counter =
-    (buffer[2] << 24) | (buffer[3] << 16) | (buffer[4] << 8) | (buffer[5]);
-
   if (server_frame_counter != FrameCounter)
   {
-    Error(ERR_INFO, "client and servers frame counters out of sync");
+    Error(ERR_INFO, "frame counters of client %d and server out of sync",
+         player_nr);
     Error(ERR_INFO, "frame counter of client is %d", FrameCounter);
     Error(ERR_INFO, "frame counter of server is %d", server_frame_counter);
     Error(ERR_INFO, "this should not happen -- please debug");
@@ -723,81 +730,84 @@ static void Handle_OP_MOVE_PLAYER(unsigned int len)
     return;
   }
 
-  /* copy valid player actions */
+  /* copy valid player actions (will be set to 0 for not connected players) */
   for (i = 0; i < MAX_PLAYERS; i++)
     stored_player[i].effective_action =
-      (i < len - 6 ? buffer[6 + i] : 0);
+      getNetworkBuffer8BitInteger(read_buffer);
 
   network_player_action_received = TRUE;
 }
 
-static void HandleNetworkingMessages()
+static void Handle_OP_BROADCAST_MESSAGE()
 {
-  unsigned int message_length;
+  int player_nr = getNetworkBuffer8BitInteger(read_buffer);
+
+  printf("OP_BROADCAST_MESSAGE: %d\n", player_nr);
+  Error(ERR_NETWORK_CLIENT, "client %d sends message", player_nr);
+}
 
+static void HandleNetworkingMessage()
+{
   stop_network_game = FALSE;
 
-  while (nread >= 4 && nread >= 4 + readbuffer[3])
+  initNetworkBufferForReading(read_buffer);
+
+  int message_type = getNetworkBuffer8BitInteger(read_buffer);
+
+  switch (message_type)
   {
-    message_length = readbuffer[3];
-    if (readbuffer[0] || readbuffer[1] || readbuffer[2])
-      Error(ERR_EXIT, "wrong network server line length");
+    case OP_BAD_PROTOCOL_VERSION:
+      Handle_OP_BAD_PROTOCOL_VERSION();
+      break;
 
-    memcpy(buffer, &readbuffer[4], message_length);
-    nread -= 4 + message_length;
-    memmove(readbuffer, readbuffer + 4 + message_length, nread);
+    case OP_YOUR_NUMBER:
+      Handle_OP_YOUR_NUMBER();
+      break;
 
-    switch (buffer[1])
-    {
-      case OP_BAD_PROTOCOL_VERSION:
-       Handle_OP_BAD_PROTOCOL_VERSION();
-       break;
+    case OP_NUMBER_WANTED:
+      Handle_OP_NUMBER_WANTED();
+      break;
 
-      case OP_YOUR_NUMBER:
-       Handle_OP_YOUR_NUMBER();
-       break;
+    case OP_PLAYER_NAME:
+      Handle_OP_PLAYER_NAME();
+      break;
 
-      case OP_NUMBER_WANTED:
-       Handle_OP_NUMBER_WANTED();
-       break;
+    case OP_PLAYER_CONNECTED:
+      Handle_OP_PLAYER_CONNECTED();
+      break;
 
-      case OP_PLAYER_NAME:
-       Handle_OP_PLAYER_NAME(message_length);
-       break;
+    case OP_PLAYER_DISCONNECTED:
+      Handle_OP_PLAYER_DISCONNECTED();
+      break;
 
-      case OP_PLAYER_CONNECTED:
-       Handle_OP_PLAYER_CONNECTED();
-       break;
-      
-      case OP_PLAYER_DISCONNECTED:
-       Handle_OP_PLAYER_DISCONNECTED();
-       break;
+    case OP_START_PLAYING:
+      Handle_OP_START_PLAYING();
+      break;
 
-      case OP_START_PLAYING:
-       Handle_OP_START_PLAYING();
-       break;
+    case OP_PAUSE_PLAYING:
+      Handle_OP_PAUSE_PLAYING();
+      break;
 
-      case OP_PAUSE_PLAYING:
-       Handle_OP_PAUSE_PLAYING();
-       break;
+    case OP_CONTINUE_PLAYING:
+      Handle_OP_CONTINUE_PLAYING();
+      break;
 
-      case OP_CONTINUE_PLAYING:
-       Handle_OP_CONTINUE_PLAYING();
-       break;
+    case OP_STOP_PLAYING:
+      Handle_OP_STOP_PLAYING();
+      break;
 
-      case OP_STOP_PLAYING:
-       Handle_OP_STOP_PLAYING();
-       break;
+    case OP_MOVE_PLAYER:
+      Handle_OP_MOVE_PLAYER();
+      break;
 
-      case OP_MOVE_PLAYER:
-       Handle_OP_MOVE_PLAYER(message_length);
-       break;
+    case OP_BROADCAST_MESSAGE:
+      Handle_OP_BROADCAST_MESSAGE();
+      break;
 
-      case OP_BROADCAST_MESSAGE:
-       printf("OP_BROADCAST_MESSAGE: %d\n", buffer[0]);
-       Error(ERR_NETWORK_CLIENT, "client %d sends message", buffer[0]);
-       break;
-    }
+    default:
+      if (options.verbose)
+       Error(ERR_NETWORK_CLIENT,
+             "unknown opcode %d from server", message_type);
   }
 
   fflush(stdout);
@@ -813,7 +823,7 @@ static char *HandleNetworkingPackets()
   {
     /* ---------- check network server for activity ---------- */
 
-    int num_active_sockets = SDLNet_CheckSockets(rfds, 1);
+    int num_active_sockets = SDLNet_CheckSockets(rfds, 0);
 
     if (num_active_sockets < 0)
       return "Error checking network sockets!";
@@ -823,7 +833,9 @@ static char *HandleNetworkingPackets()
 
     /* ---------- read packets from network server ---------- */
 
-    int num_bytes = SDLNet_TCP_Recv(sfd, readbuffer + nread, 1);
+    initNetworkBufferForReceiving(read_buffer);
+
+    int num_bytes = receiveNetworkBufferPacket(read_buffer, sfd);
 
     if (num_bytes < 0)
       return "Error reading from network server!";
@@ -831,9 +843,7 @@ static char *HandleNetworkingPackets()
     if (num_bytes == 0)
       return "Connection to network server lost!";
 
-    nread += num_bytes;
-
-    HandleNetworkingMessages();
+    HandleNetworkingMessage();
 
     if (stop_network_client)
       return stop_network_client_message;