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

David Bruce dbruce-guest at alioth.debian.org
Tue Jul 14 00:32:46 UTC 2009


Author: dbruce-guest
Date: 2009-07-14 00:32:45 +0000 (Tue, 14 Jul 2009)
New Revision: 1197

Modified:
   tuxmath/branches/lan/server/testclient.c
   tuxmath/branches/lan/src/game.c
   tuxmath/branches/lan/src/network.c
   tuxmath/branches/lan/src/network.h
Log:
more work to tidy up network.c/h



Modified: tuxmath/branches/lan/server/testclient.c
===================================================================
--- tuxmath/branches/lan/server/testclient.c	2009-07-13 21:31:07 UTC (rev 1196)
+++ tuxmath/branches/lan/server/testclient.c	2009-07-14 00:32:45 UTC (rev 1197)
@@ -106,7 +106,7 @@
     Throttle(10);
   }
  
-  cleanup_client();
+  LAN_Cleanup();
  
   return EXIT_SUCCESS;
 }

Modified: tuxmath/branches/lan/src/game.c
===================================================================
--- tuxmath/branches/lan/src/game.c	2009-07-13 21:31:07 UTC (rev 1196)
+++ tuxmath/branches/lan/src/game.c	2009-07-14 00:32:45 UTC (rev 1197)
@@ -238,13 +238,14 @@
 
 int game(void)
 {
- 
+  /*FIXME this will eventually be somewhere in the program-wide Setup() */
+  /* or perhaps in titlescreen.c                                        */
   /*connecting to the server*/
-  if(!setup_net("localhost",DEFAULT_PORT))
+  if(!LAN_Setup("localhost", DEFAULT_PORT))
   {
     printf("Unable to connect to the server\n");
     game_cleanup();
-    exit(1);
+    return 0;
   }        
 
 
@@ -767,7 +768,7 @@
 void game_cleanup(void)
 {
   
-  cleanup_client();
+  LAN_Cleanup();
   /* Free background: */
   if (bkgd != NULL)
   {

Modified: tuxmath/branches/lan/src/network.c
===================================================================
--- tuxmath/branches/lan/src/network.c	2009-07-13 21:31:07 UTC (rev 1196)
+++ tuxmath/branches/lan/src/network.c	2009-07-14 00:32:45 UTC (rev 1197)
@@ -38,10 +38,9 @@
 int evaluate(char *statement);
 
 
-int setup_net(char *host, int port)
+int LAN_Setup(char *host, int port)
 {
   IPaddress ip;           /* Server address */
-  int sockets_used;
 //  int len;
 //  char buf[NET_BUF_LEN];     // for network messages from server
   char buffer[NET_BUF_LEN];  // for command-line input
@@ -51,50 +50,68 @@
   if (SDLNet_Init() < 0)
   {
     fprintf(stderr, "SDLNet_Init: %s\n", SDLNet_GetError());
-    exit(EXIT_FAILURE);
-  }
+    return 0;
+  } 
  
   /* Resolve the host we are connecting to */
   if (SDLNet_ResolveHost(&ip, host, port) < 0)
   {
     fprintf(stderr, "SDLNet_ResolveHost: %s\n", SDLNet_GetError());
-    exit(EXIT_FAILURE);
+    return 0;
   }
  
   /* Open a connection with the IP provided (listen on the host's port) */
   if (!(sd = SDLNet_TCP_Open(&ip)))
   {
     fprintf(stderr, "SDLNet_TCP_Open: %s\n", SDLNet_GetError());
-    exit(EXIT_FAILURE);
+    return 0;
   }
 
   /* We create a socket set so we can check for activity: */
   set = SDLNet_AllocSocketSet(1);
-  if(!set) {
+  if(!set)
+  {
     printf("SDLNet_AllocSocketSet: %s\n", SDLNet_GetError());
-    exit(EXIT_FAILURE);
+    return 0;
   }
 
-  sockets_used = SDLNet_TCP_AddSocket(set, sd);
-  if(sockets_used == -1) {
+  if(SDLNet_TCP_AddSocket(set, sd) == -1)
+  {
     printf("SDLNet_AddSocket: %s\n", SDLNet_GetError());
     // perhaps you need to restart the set and make it bigger...
   }
 
- snprintf(buffer, NET_BUF_LEN, 
+  snprintf(buffer, NET_BUF_LEN, 
                        "%s\n",
                        name);
  
   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);
+    fprintf(stderr, "SDLNet_TCP_Send: %s\n", SDLNet_GetError());
+    exit(EXIT_FAILURE);
   }
  
   return 1;
 
 }
 
+
+void LAN_Cleanup(void)
+{
+  if(sd)
+  {
+    SDLNet_TCP_Close(sd);
+    sd = NULL;
+  }
+
+  if(set)
+  {
+    SDLNet_FreeSocketSet(set);
+    set = NULL;
+  }
+  SDLNet_Quit();
+}
+
 /* This function prints the 'msg' part of the buffer (i.e. everything */
 /* after the first '\t') to stdout.                                   */
 int player_msg_recvd(char* buf)
@@ -114,6 +131,7 @@
 }
 
 
+
 int say_to_server(char statement[20])
 {
   char buffer[NET_BUF_LEN];
@@ -306,21 +324,7 @@
 }
     
 
-void cleanup_client(void)
-{
-  if(sd)
-  {
-    SDLNet_TCP_Close(sd);
-    sd = NULL;
-  }
 
-  if(set)
-  {
-    SDLNet_FreeSocketSet(set);
-    set = NULL;
-  }
-  SDLNet_Quit();
-}
 
 
 /*This mainly is a network version of all the MathCards Functions

Modified: tuxmath/branches/lan/src/network.h
===================================================================
--- tuxmath/branches/lan/src/network.h	2009-07-13 21:31:07 UTC (rev 1196)
+++ tuxmath/branches/lan/src/network.h	2009-07-14 00:32:45 UTC (rev 1197)
@@ -20,8 +20,8 @@
 
 
 /* Networking setup and cleanup: */
-int setup_net(char *host, int port);
-void cleanup_client(void);
+int LAN_Setup(char *host, int port);
+void LAN_Cleanup(void);
 
 /* Network replacement functions for mathcards "API": */
 /* These functions are how the client tells things to the server: */
@@ -32,6 +32,7 @@
 int LAN_NextMsg(char* buf);
 
 /* Functions to handle various messages from the server: */
+/* NOTE not sure if these belong here or in game.c/testclient.c */
 int player_msg_recvd(char* buf);
 
 




More information about the Tux4kids-commits mailing list