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

akash gangil gangil-guest at alioth.debian.org
Fri Jun 26 21:28:58 UTC 2009


Author: gangil-guest
Date: 2009-06-26 21:28:58 +0000 (Fri, 26 Jun 2009)
New Revision: 1094

Modified:
   tuxmath/branches/lan/server/server.c
   tuxmath/branches/lan/server/testclient.c
   tuxmath/branches/lan/server/transtruct.h
Log:
No time limit on connection

Modified: tuxmath/branches/lan/server/server.c
===================================================================
--- tuxmath/branches/lan/server/server.c	2009-06-25 22:19:42 UTC (rev 1093)
+++ tuxmath/branches/lan/server/server.c	2009-06-26 21:28:58 UTC (rev 1094)
@@ -28,7 +28,7 @@
 #define NUM_CLIENTS 16
 
 TCPsocket sd; /* Socket descriptor */
-SDLNet_SocketSet client_set;
+SDLNet_SocketSet client_set=NULL;
 
 
 static client_type client[NUM_CLIENTS];
@@ -40,6 +40,7 @@
   IPaddress ip, *remoteIP;
   int quit, quit2;
   char buffer[NET_BUF_LEN];
+  char buf[NET_BUF_LEN];
   int command_type = -1,numready,j;
   static int sockets_used=0;
   static int game_started=0;
@@ -47,7 +48,7 @@
   static int num_clients=0;
   MC_FlashCard flash;
   static int initialize = 0;
-  int id;
+  int id,x;
 
   for(h=0;h<NUM_CLIENTS;h++)
   {
@@ -93,8 +94,66 @@
  
   /* Wait for a client connections*/
   quit = 0;                                                    /*****can say this loop to be the connection manager, which after accepting starts the game*****/
-  while (quit!=5)
+  while (!quit)
   {
+
+      numready=SDLNet_CheckSockets(client_set,0);
+      if(numready==-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(numready) 
+      {
+        printf("There are %d sockets with activity!\n",numready);
+        // check all sockets with SDLNet_SocketReady and handle the active ones.
+        for(j=0;j<sockets_used;j++)
+        {
+#ifdef LAN_DEBUG
+  printf("inside for %d\n",quit);
+#endif
+
+         if(SDLNet_SocketReady(client[j].csd)) 
+         {
+#ifdef LAN_DEBUG
+  printf("inside ready\n",quit);
+#endif
+
+          if (SDLNet_TCP_Recv(client[j].csd, buffer, NET_BUF_LEN) > 0)
+          {
+#ifdef LAN_DEBUG
+  printf("inside recv %d         %s\n",quit,buffer);
+#endif
+#ifdef LAN_DEBUG
+  printf("inside recv %d  \n",strncmp(buffer,"start",5));
+#endif
+
+
+           if(strncmp(buffer,"start",5)==0)
+           {
+           quit=1;
+#ifdef LAN_DEBUG
+  printf("quit is %d\n",quit);
+#endif
+
+
+           snprintf(buf, NET_BUF_LEN, 
+                "%s\n",
+                "Success");
+           x = SDLNet_TCP_Send(client[j].csd, buf, sizeof(buf));
+#ifdef LAN_DEBUG
+  printf("buf sent:::: %d bytes\n", x);
+  printf("buf is: %s\n", buf);
+#endif
+           client[j].flag=1;
+           }
+          }
+         }
+        }
+      }
+
     /* This check the sd if there is a pending connection.
      * If there is one, accept that, and open a new socket for communicating */
     client[i].csd = SDLNet_TCP_Accept(sd);
@@ -117,7 +176,10 @@
       }
       else
         fprintf(stderr, "SDLNet_TCP_GetPeerAddress: %s\n", SDLNet_GetError());
-      
+
+#ifdef LAN_DEBUG
+printf("before add socket\n");
+#endif      
       sockets_used = SDLNet_TCP_AddSocket(client_set,client[i].csd);
       if(sockets_used == -1) 
       {
@@ -125,32 +187,68 @@
         // perhaps you need to restart the set and make it bigger...
       }
 
+
 #ifdef LAN_DEBUG
-      printf("%d\n",sockets_used);
+      printf("sockets used::::%d\n",sockets_used);
 #endif
-      client[i].flag=1;
+      
       i++;
     
     }//end of *if(client[i].csd = SDLNet_TCP_Accept(sd))*  
-    quit++;
-    SDL_Delay(2000);
+    
   }
 
+num_clients=sockets_used;
+
+ for(j=0;j<num_clients;j++)
+ {
+  if(client[j].flag!=1)
+  {
+   if (SDLNet_TCP_Recv(client[j].csd, buffer, NET_BUF_LEN) > 0)
+   {
+    if(strncmp(buffer,"start",5)==0)
+    {
+     client[j].flag=1;
+     snprintf(buf, NET_BUF_LEN, 
+                "%s\n",
+                "Success");
+     x = SDLNet_TCP_Send(client[j].csd, buf, sizeof(buf));
+#ifdef LAN_DEBUG
+  printf("buf sent:::: %d bytes\n", x);
+  printf("buf is: %s\n", buf);
+#endif
+    }
+   }
+  }
+ }
+
+#ifdef LAN_DEBUG
+printf("Out of the while loop.......\n",sockets_used);
+#endif
+
+
+
 /* If no players join the game */
-if(sockets_used==0)
+if(num_clients==0)
 {
  printf("There were no players........=(\n");
+ SDLNet_FreeSocketSet(client_set);              //releasing the memory of the client socket set
+ client_set=NULL; //this helps us remember that this set is not allocated
+ SDLNet_TCP_Close(sd);
+ SDLNet_Quit();
  exit(1);
 }
 
+
+
 game_started=1;                 //indicating the game has started
 
 #ifdef LAN_DEBUG
 printf("We have %d players.......\n",sockets_used);
 #endif
-num_clients=sockets_used;
 
 
+
  if (!MC_StartGame())                   //setting up the list itself
  {
   fprintf(stderr, "\nMC_StartGame() failed!");

Modified: tuxmath/branches/lan/server/testclient.c
===================================================================
--- tuxmath/branches/lan/server/testclient.c	2009-06-25 22:19:42 UTC (rev 1093)
+++ tuxmath/branches/lan/server/testclient.c	2009-06-26 21:28:58 UTC (rev 1094)
@@ -25,7 +25,7 @@
 #include "mathcards.h"
 #include "testclient.h"
 
-//#define LAN_DEBUG
+#define LAN_DEBUG
 
 TCPsocket sd;           /* Server socket descriptor */
 SDLNet_SocketSet set;
@@ -210,18 +210,55 @@
   int numready;
   int command_type;
   int ans = 0;
-  int x, i = 0;
+  int x=0, i = 0;
   int end = 0;
   int have_question = 0;
   int len = 0;
   char buf[NET_BUF_LEN];
+  char buffer[NET_BUF_LEN];
 
+#ifdef LAN_DEBUG
+  printf("Entering playgame()\n");
+#endif
 
 
+  snprintf(buffer, NET_BUF_LEN, 
+                  "%s\n",
+                  "start");
+  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);
+  }
 #ifdef LAN_DEBUG
-  printf("Entering playgame()\n");
+  printf("Sent the game notification %s\n",buffer);
 #endif
 
+
+
+  if( SDLNet_TCP_Recv(sd, buf, sizeof(buf)))
+  {
+   if(strncmp(buf,"Sorry",5) == 0)
+   {
+     printf("Sorry , the game has already been started.......=(\n");
+     SDLNet_TCP_Close(sd);
+     SDLNet_FreeSocketSet(set);
+     set=NULL; //this helps us remember that this set is not allocated
+     SDLNet_Quit();
+     exit(5);
+   }
+   else if(strncmp(buf,"Success",7)==0)
+   {
+    printf("You Are In game.....Waiting for other players to be ready...\n");
+   }
+  }
+#ifdef LAN_DEBUG
+  printf("Received %s\n",buf);
+#endif
+
+
+
   //Begin game loop:
   while (!end)
   {

Modified: tuxmath/branches/lan/server/transtruct.h
===================================================================
--- tuxmath/branches/lan/server/transtruct.h	2009-06-25 22:19:42 UTC (rev 1093)
+++ tuxmath/branches/lan/server/transtruct.h	2009-06-26 21:28:58 UTC (rev 1094)
@@ -16,7 +16,7 @@
 
 //#define LAN_DEBUG
 #define NET_BUF_LEN 512
-#define DEFAULT_PORT 4789
+#define DEFAULT_PORT 4779
 
 #define MC_USE_NEWARC
 #define MC_FORMULA_LEN 40




More information about the Tux4kids-commits mailing list