[Tux4kids-commits] r1269 - in tuxmath/branches/lan: server src

David Bruce dbruce-guest at alioth.debian.org
Fri Jul 24 22:55:21 UTC 2009


Author: dbruce-guest
Date: 2009-07-24 22:55:21 +0000 (Fri, 24 Jul 2009)
New Revision: 1269

Modified:
   tuxmath/branches/lan/server/testclient.c
   tuxmath/branches/lan/server/transtruct.h
   tuxmath/branches/lan/src/network.c
   tuxmath/branches/lan/src/network.h
   tuxmath/branches/lan/src/transtruct.h
Log:
server autodetection - now creates list of all detected servers
(up to 50)

Modified: tuxmath/branches/lan/server/testclient.c
===================================================================
--- tuxmath/branches/lan/server/testclient.c	2009-07-24 20:30:28 UTC (rev 1268)
+++ tuxmath/branches/lan/server/testclient.c	2009-07-24 22:55:21 UTC (rev 1269)
@@ -24,13 +24,14 @@
 #include <fcntl.h> 
 
 //#include "SDL_net.h"
-#include "transtruct.h"
+#include "../src/transtruct.h"
 #include "mathcards.h"
 #include "testclient.h"
 #include "../src/throttle.h"
 #include "../src/network.h"
 
 /* Local (to testclient.c) "globals": */
+
 int quit = 0;
 int game_status = GAME_NOT_STARTED;
 MC_FlashCard flash;    //current question
@@ -73,6 +74,8 @@
   }
 
 
+  LAN_DetectServers();
+
   /* Connect to server, create socket set, get player nickname, etc: */
   if(!LAN_Setup(argv[1], DEFAULT_PORT))
   {

Modified: tuxmath/branches/lan/server/transtruct.h
===================================================================
--- tuxmath/branches/lan/server/transtruct.h	2009-07-24 20:30:28 UTC (rev 1268)
+++ tuxmath/branches/lan/server/transtruct.h	2009-07-24 22:55:21 UTC (rev 1269)
@@ -18,6 +18,7 @@
 #define NET_BUF_LEN 512
 #define DEFAULT_PORT 4779
 #define NAME_SIZE 50
+#define MAX_SERVERS 50
 
 #define MC_USE_NEWARC
 #define MC_FORMULA_LEN 40

Modified: tuxmath/branches/lan/src/network.c
===================================================================
--- tuxmath/branches/lan/src/network.c	2009-07-24 20:30:28 UTC (rev 1268)
+++ tuxmath/branches/lan/src/network.c	2009-07-24 22:55:21 UTC (rev 1269)
@@ -27,22 +27,32 @@
 TCPsocket sd;           /* Server socket descriptor */
 SDLNet_SocketSet set;
 IPaddress serv_ip;
+char servers[MAX_SERVERS][NAME_SIZE];
 
 /* Local function prototypes: */
 int say_to_server(char *statement);
 int evaluate(char *statement);
+int add_to_list(char* name);
 
 
-
 int LAN_DetectServers(void)
 {
   UDPsocket udpsock = NULL;  
   UDPpacket* out;
   UDPpacket* in;
   IPaddress bcast_ip;
+  IPaddress* ip_ptr;
   int sent = 0;
   int done = 0;
   int seconds = 0;
+  int num_servers = 0;
+  int i = 0;
+  char* serv_name;
+
+  //zero out old server list
+  for(i = 0; i < MAX_SERVERS; i++)
+    servers[i][0] = '\0';
+
   /* Docs say we are supposed to call SDL_Init() before SDLNet_Init(): */
   if(SDL_Init(0)==-1)
   {
@@ -99,16 +109,21 @@
     printf("UDP packets sent to %d addresses\n", sent);
     SDL_Delay(250);  //give server chance to answer
 
-    if(SDLNet_UDP_Recv(udpsock, in))
+    while(SDLNet_UDP_Recv(udpsock, in))
     {
       if(strncmp((char*)in->data, "TUXMATH_SERVER", strlen("TUXMATH_SERVER")) == 0)
       {
-        printf("Reply received from server\n");
         done = 1;
+        ip_ptr = &(in->address);
+        serv_name = SDLNet_ResolveIP(ip_ptr);
+        printf("Reply received from server: %s\n", serv_name);
+        num_servers = add_to_list(serv_name);
       }
     }
-
+    //Make sure we always scan at least five but not more than ten seconds:
     seconds++;
+    if(seconds < 5)
+      done = 0;
     if(seconds > 10)
       done = 1;
     Throttle(1000); //repeat once per second
@@ -116,6 +131,8 @@
 
   SDLNet_FreePacket(out); 
   SDLNet_FreePacket(in); 
+
+  return num_servers;
 }
 
 
@@ -135,9 +152,7 @@
     return 0;
   } 
 
-  LAN_DetectServers();
-
-  /* Resolve the host we are connecting to */
+   /* Resolve the host we are connecting to */
   if (SDLNet_ResolveHost(&ip, host, port) < 0)
   {
     fprintf(stderr, "SDLNet_ResolveHost: %s\n", SDLNet_GetError());
@@ -409,8 +424,34 @@
   return 1;
 }
 
+//add name to list, checking for duplicates:
+int add_to_list(char* name)
+{
+  int i = 0;
+  int already_in = 0;
 
+  if(!name)
+    return 0;
+ 
+  //first see if it is already in list:
+  while((i < MAX_SERVERS)
+      && (servers[i][0] != '\0'))
+  {
+    if(strncmp(servers[i], name, NAME_SIZE) == 0)
+      already_in = 1;
+    i++;
+  }
 
+  //Copy it in unless it's already there, or we are out of room:
+  if(!already_in && i < MAX_SERVERS)
+  {
+    strncpy(servers[i], name, NAME_SIZE);
+    i++;
+  }
 
+  return i;  //i should be the number of items in the list
+}
 
 
+
+

Modified: tuxmath/branches/lan/src/network.h
===================================================================
--- tuxmath/branches/lan/src/network.h	2009-07-24 20:30:28 UTC (rev 1268)
+++ tuxmath/branches/lan/src/network.h	2009-07-24 22:55:21 UTC (rev 1269)
@@ -20,7 +20,8 @@
 
 
 /* Networking setup and cleanup: */
-int LAN_Setup(char *host, int port);
+int LAN_DetectServers(void);
+int LAN_Setup(char* host, int port);
 void LAN_Cleanup(void);
 
 int LAN_SetName(char* name);

Modified: tuxmath/branches/lan/src/transtruct.h
===================================================================
--- tuxmath/branches/lan/src/transtruct.h	2009-07-24 20:30:28 UTC (rev 1268)
+++ tuxmath/branches/lan/src/transtruct.h	2009-07-24 22:55:21 UTC (rev 1269)
@@ -18,6 +18,7 @@
 #define NET_BUF_LEN 512
 #define DEFAULT_PORT 4779
 #define NAME_SIZE 50
+#define MAX_SERVERS 50
 
 #define MC_USE_NEWARC
 #define MC_FORMULA_LEN 40




More information about the Tux4kids-commits mailing list