[Tux4kids-commits] r1120 - tuxmath/branches/lan/server

akash gangil gangil-guest at alioth.debian.org
Tue Jun 30 21:28:55 UTC 2009


Author: gangil-guest
Date: 2009-06-30 21:28:54 +0000 (Tue, 30 Jun 2009)
New Revision: 1120

Modified:
   tuxmath/branches/lan/server/server.c
   tuxmath/branches/lan/server/testclient.c
Log:
added ping setup , doesnt work , but no loss of functionality

Modified: tuxmath/branches/lan/server/server.c
===================================================================
--- tuxmath/branches/lan/server/server.c	2009-06-30 17:30:00 UTC (rev 1119)
+++ tuxmath/branches/lan/server/server.c	2009-06-30 21:28:54 UTC (rev 1120)
@@ -118,6 +118,7 @@
 void game_msg_quit(int i);
 void game_msg_exit(int i);
 void start_game(int i);
+void ping_client(int i);
 int SendQuestion(MC_FlashCard flash, TCPsocket client_sock);
 int SendMessage(int message, int z, TCPsocket client_sock);
 
@@ -417,9 +418,56 @@
       printf("Warning: SDLNet_CheckSockets() reported %d active sockets,\n"
              "but only %d messages received.\n", actives, msg_found);
       /* We can investigate further - maybe ping all the sockets, etc. */
+      for(i = 0; i < MAX_CLIENTS; i++)
+      {
+        ping_client(i);
+      }
+    
+   
+      /* Check the client socket set for activity: */
+      actives = SDLNet_CheckSockets(client_set, 0);
+      if(actives == 0)
+      {
+        printf("No clients , All clients have disconnected...=(\n");
+      }
+
+      else if(actives == -1)
+      {
+        printf("SDLNet_CheckSockets: %s\n", SDLNet_GetError());
+        //most of the time this is a system error, where perror might help you.
+        perror("SDLNet_CheckSockets");
+      }
+
+      else if(actives) 
+      {
+#ifdef LAN_DEBUG
+       printf("There are %d sockets with activity\n", actives);
+#endif
+
+       // check all sockets with SDLNet_SocketReady and handle the active ones.
+       // NOTE we have to check all the slots in the set because
+       // the set will become discontinuous if someone disconnects
+       for(i = 0; i < MAX_CLIENTS; i++)
+       {
+         if((client[i].sock != NULL)
+            && (SDLNet_SocketReady(client[i].sock))) 
+         {
+#ifdef LAN_DEBUG
+           printf("client socket %d is ready\n", i);
+#endif
+           if (SDLNet_TCP_Recv(client[i].sock, buffer, NET_BUF_LEN) > 0)
+           {
+#ifdef LAN_DEBUG
+             printf("buffer received from client %d is: %s\n", i, buffer);
+#endif
+             if(strncmp(buffer,"PING_BACK",9))
+             printf("%s is connected =) \n",client[i].name);       
+           }
+         }
+       }
+      }
     }
   }
-  return(0);
 }
 
 void handle_client_nongame_msg(int i,char *buffer)
@@ -440,7 +488,8 @@
 #endif
   sscanf (buffer,"%s %d\n",
                   command,
-                  &id);  
+                  &id);
+
   if(strncmp(command, "CORRECT_ANSWER", 14) == 0)
   {
     game_msg_correct_answer(i,id);
@@ -491,7 +540,24 @@
 
 }
 
+void ping_client(int i)
+{
+  char buf[NET_BUF_LEN];
+  char msg[NET_BUF_LEN];
+  int x;
 
+  sprintf(msg,"%s", "PING\n");
+
+  snprintf(buf, NET_BUF_LEN, "%s\t%s\n", "SEND_MESSAGE", msg);
+  x = SDLNet_TCP_Send(client[i].sock, buf, NET_BUF_LEN);
+
+//#ifdef LAN_DEBUG
+  printf("buf is: %s\n", buf);
+  printf("SendMessage() - buf sent:::: %d bytes\n", x);
+//#endif
+}
+
+
 void game_msg_exit(int i)
 {
   printf("LEFT the GAME : %s",client[i].name);
@@ -673,10 +739,10 @@
   snprintf(buf, NET_BUF_LEN, "%s\t%s\n", "SEND_MESSAGE", msg);
   x = SDLNet_TCP_Send(client_sock, buf, NET_BUF_LEN);
 
-//#ifdef LAN_DEBUG
+#ifdef LAN_DEBUG
   printf("buf is: %s\n", buf);
   printf("SendMessage() - buf sent:::: %d bytes\n", x);
-//#endif
+#endif
 
   return 1;
 }

Modified: tuxmath/branches/lan/server/testclient.c
===================================================================
--- tuxmath/branches/lan/server/testclient.c	2009-06-30 17:30:00 UTC (rev 1119)
+++ tuxmath/branches/lan/server/testclient.c	2009-06-30 21:28:54 UTC (rev 1120)
@@ -33,8 +33,8 @@
 int Make_Flashcard(char *buf, MC_FlashCard* fc);
 int LAN_AnsweredCorrectly(MC_FlashCard* fc);
 int playgame(void);
+void server_pinged(void);
 
-
 int main(int argc, char **argv)
 {
   IPaddress ip;           /* Server address */
@@ -172,7 +172,22 @@
   return 1;
 }
                 
+void server_pinged(void)
+{ 
+  int len;
+  char buffer[NET_BUF_LEN];
+
+  snprintf(buffer, NET_BUF_LEN, 
+                  "%s \n",
+                  "PING_BACK");
+  len = strlen(buffer) + 1;
+  if (SDLNet_TCP_Send(sd, (void *)buffer, NET_BUF_LEN) < NET_BUF_LEN)
+  {
+    fprintf(stderr, "SDLNet_TCP_Send: %s\n", SDLNet_GetError());
+    exit(EXIT_FAILURE);
+  }
  
+}
 
 
 int Make_Flashcard(char* buf, MC_FlashCard* fc)
@@ -324,6 +339,11 @@
             else
               printf("Unable to parse buffer into FlashCard\n");
           }
+          if(strncmp(command,"PING",4)==0)
+          {
+            server_pinged();
+          }
+
         }
       }
     } // End of loop for checking server activity




More information about the Tux4kids-commits mailing list