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

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


Author: dbruce-guest
Date: 2009-07-14 03:32:02 +0000 (Tue, 14 Jul 2009)
New Revision: 1198

Modified:
   tuxmath/branches/lan/server/server.c
   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:
further implementation of network.c and .h

Modified: tuxmath/branches/lan/server/server.c
===================================================================
--- tuxmath/branches/lan/server/server.c	2009-07-14 00:32:45 UTC (rev 1197)
+++ tuxmath/branches/lan/server/server.c	2009-07-14 03:32:02 UTC (rev 1198)
@@ -43,6 +43,7 @@
 void handle_client_nongame_msg(int i,char *buffer);
 int find_vacant_client(void);
 void remove_client(int i);
+int msg_set_name(int i, char* buf);
 void game_msg_correct_answer(int i, int id);
 void game_msg_quit(int i);
 void game_msg_exit(int i);
@@ -469,6 +470,10 @@
   {
     start_game(i);
   }
+  else if(strncmp(buffer, "SET_NAME", strlen("SET_NAME")) == 0)
+  {
+    msg_set_name(i, buffer);
+  }
 }
 
 
@@ -515,8 +520,29 @@
 }
 
 
+/* Functions for all the messages we can receive from the client: */
 
 
+int msg_set_name(int i, char* buf)
+{
+  char* p;
+  if(buf == NULL)
+    return 0;
+  p = strchr(buf, '\t');
+  if(p)
+  { 
+    p++;
+    strncpy(client[i].name, p, NAME_SIZE);
+    return 1;
+  }
+  else
+    return 0;
+}
+
+
+
+
+
 void game_msg_correct_answer(int i, int id)
 {
   int n;

Modified: tuxmath/branches/lan/server/testclient.c
===================================================================
--- tuxmath/branches/lan/server/testclient.c	2009-07-14 00:32:45 UTC (rev 1197)
+++ tuxmath/branches/lan/server/testclient.c	2009-07-14 03:32:02 UTC (rev 1198)
@@ -53,16 +53,35 @@
   char buf[NET_BUF_LEN];     // for network messages from server
   char buffer[NET_BUF_LEN];  // for command-line input
 
-
   /* Connect to server, create socket set, get player nickname, etc: */
-  if(!setup_client(argc, argv))
+  if(!LAN_Setup(argv[1], DEFAULT_PORT))
   {
     printf("setup_client() failed - exiting.\n");
     exit(EXIT_FAILURE);
   }
 
+  /* Now we are connected - get nickname from player: */
+  {
+    char name[NAME_SIZE];
+    char* p;
 
+    printf("Please enter your name:\n>\n");
+    fgets(buffer, NAME_SIZE, stdin);
+    p = strchr(buf, '\t');
+    if(p)
+      *p = '\0';
+    strncpy(name, buffer, NAME_SIZE);
+    /* If no nickname received, use default: */
+    if(strlen(name) == 1)
+      strcpy(name, "Anonymous Coward");
+  
+//  printf("name is %s, length %d\n", name, strlen(name));
+//  printf("buffer is %s, length %d\n", buffer, strlen(buffer));
 
+    snprintf(buffer, NET_BUF_LEN, "%s", name);
+    LAN_SetName(name);
+  }
+
   printf("Welcome to the Tux Math Test Client!\n");
 
   /* Send messages */
@@ -163,19 +182,8 @@
     return 0;
   }
   /* Now we are connected. Take in nickname and send to server. */
-  printf("Please enter your name:\n>\n");
-  check1 = fgets(buffer, NAME_SIZE, stdin);
-  strncpy(name, check1, NAME_SIZE);
-  /* If no nickname received, use default: */
-  if(strlen(name) == 1)
-    strcpy(name, "Anonymous Coward");
-  
-  printf("name is %s, length %d\n", name, strlen(name));
-  printf("buffer is %s, length %d\n", buffer, strlen(buffer));
 
-  snprintf(buffer, NET_BUF_LEN, "%s", name);
 
-
   if (SDLNet_TCP_Send(sd, (void*)buffer, NET_BUF_LEN) < NET_BUF_LEN)
   {
     fprintf(stderr, "SDLNet_TCP_Send: %s\n", SDLNet_GetError());

Modified: tuxmath/branches/lan/src/game.c
===================================================================
--- tuxmath/branches/lan/src/game.c	2009-07-14 00:32:45 UTC (rev 1197)
+++ tuxmath/branches/lan/src/game.c	2009-07-14 03:32:02 UTC (rev 1198)
@@ -248,7 +248,9 @@
     return 0;
   }        
 
+  LAN_SetName("Player_1");
 
+
   Uint32 last_time, now_time;
 
 #ifdef TUXMATH_DEBUG

Modified: tuxmath/branches/lan/src/network.c
===================================================================
--- tuxmath/branches/lan/src/network.c	2009-07-14 00:32:45 UTC (rev 1197)
+++ tuxmath/branches/lan/src/network.c	2009-07-14 03:32:02 UTC (rev 1198)
@@ -81,18 +81,7 @@
     // perhaps you need to restart the set and make it bigger...
   }
 
-  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);
-  }
- 
   return 1;
-
 }
 
 
@@ -112,6 +101,24 @@
   SDLNet_Quit();
 }
 
+
+
+int LAN_SetName(char* name)
+{
+  char buf[NET_BUF_LEN];
+
+  if(!name)
+    return 0;
+
+  snprintf(buf, NET_BUF_LEN, 
+                  "%s\t%s",
+                  "SET_NAME",
+                  name);
+
+  return say_to_server(buf);
+}
+
+
 /* This function prints the 'msg' part of the buffer (i.e. everything */
 /* after the first '\t') to stdout.                                   */
 int player_msg_recvd(char* buf)
@@ -132,20 +139,23 @@
 
 
 
-int say_to_server(char statement[20])
+int say_to_server(char* statement)
 {
   char buffer[NET_BUF_LEN];
 
-   snprintf(buffer, NET_BUF_LEN, 
+  if(!statement)
+    return 0;
+
+  snprintf(buffer, NET_BUF_LEN, 
                   "%s\n",
                   statement);
-   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);
-   }
-  
-   return 1;
+  if (SDLNet_TCP_Send(sd, (void *)buffer, NET_BUF_LEN) < NET_BUF_LEN)
+  {
+    fprintf(stderr, "SDLNet_TCP_Send: %s\n", SDLNet_GetError());
+    return 0;
+  }
+
+  return 1;
 }
 
 

Modified: tuxmath/branches/lan/src/network.h
===================================================================
--- tuxmath/branches/lan/src/network.h	2009-07-14 00:32:45 UTC (rev 1197)
+++ tuxmath/branches/lan/src/network.h	2009-07-14 03:32:02 UTC (rev 1198)
@@ -23,6 +23,8 @@
 int LAN_Setup(char *host, int port);
 void LAN_Cleanup(void);
 
+int LAN_SetName(char* name);
+
 /* Network replacement functions for mathcards "API": */
 /* These functions are how the client tells things to the server: */
 int LAN_StartGame(void);




More information about the Tux4kids-commits mailing list