[Tux4kids-commits] r1270 - in tuxmath/branches/lan: server src
David Bruce
dbruce-guest at alioth.debian.org
Sat Jul 25 04:11:45 UTC 2009
Author: dbruce-guest
Date: 2009-07-25 04:11:44 +0000 (Sat, 25 Jul 2009)
New Revision: 1270
Modified:
tuxmath/branches/lan/server/server.c
tuxmath/branches/lan/server/testclient.c
tuxmath/branches/lan/src/network.c
tuxmath/branches/lan/src/network.h
Log:
server autodetection now working and connecting transparently if a single server is found.
Modified: tuxmath/branches/lan/server/server.c
===================================================================
--- tuxmath/branches/lan/server/server.c 2009-07-24 22:55:21 UTC (rev 1269)
+++ tuxmath/branches/lan/server/server.c 2009-07-25 04:11:44 UTC (rev 1270)
@@ -255,16 +255,6 @@
int recvd = 0;
UDPpacket* in = SDLNet_AllocPacket(NET_BUF_LEN);
recvd = SDLNet_UDP_Recv(udpsock, in);
- if(recvd)
- {
- printf("UDP Packet incoming\n");
- printf("\tChan: %d\n", in->channel);
- printf("\tData: %s\n", (char *)in->data);
- printf("\tLen: %d\n", in->len);
- printf("\tMaxlen: %d\n", in->maxlen);
- printf("\tStatus: %d\n", in->status);
- printf("\tAddress: %x %x\n", in->address.host, in->address.port);
- }
// See if packet contains identifying string:
if(strncmp((char*)in->data, "TUXMATH_CLIENT", strlen("TUXMATH_CLIENT")) == 0)
@@ -273,25 +263,14 @@
IPaddress bcast_ip;
int sent = 0;
- printf("packet received from client, sending reply\n");
-
out = SDLNet_AllocPacket(NET_BUF_LEN);
out->address.host = in->address.host;
out->address.port = in->address.port;
sprintf(out->data, "TUXMATH_SERVER");
out->len = strlen("TUXMATH_SERVER") + 1;
- printf("UDP Packet to be sent:\n");
- printf("\tChan: %d\n", out->channel);
- printf("\tData: %s\n", (char *)out->data);
- printf("\tLen: %d\n", out->len);
- printf("\tMaxlen: %d\n", out->maxlen);
- printf("\tStatus: %d\n", out->status);
- printf("\tAddress: %x %x\n", out->address.host, out->address.port);
-
// Send server reply:
sent = SDLNet_UDP_Send(udpsock, -1, out);
- printf("UDP packets sent to %d addresses\n", sent);
SDLNet_FreePacket(out);
}
}
Modified: tuxmath/branches/lan/server/testclient.c
===================================================================
--- tuxmath/branches/lan/server/testclient.c 2009-07-24 22:55:21 UTC (rev 1269)
+++ tuxmath/branches/lan/server/testclient.c 2009-07-25 04:11:44 UTC (rev 1270)
@@ -65,23 +65,40 @@
{
char buf[NET_BUF_LEN]; // for network messages from server
char buffer[NET_BUF_LEN]; // for command-line input
+ int servers_found = 0;
+ Uint32 server_ip = 0;
+ Uint16 server_port = DEFAULT_PORT;
- /* Start out with our "comets" empty: */
+ //Scan local network to find running server:
+ servers_found = LAN_DetectServers();
+
+ if(servers_found < 1)
{
- int i;
- for(i = 0; i < TEST_COMETS; i ++)
- erase_flashcard(&comets[i]);
+ printf("No server could be found - exiting.\n");
+ exit(EXIT_FAILURE);
}
+ else if(servers_found == 1) //One server - connect without player intervention
+ {
+ if(!LAN_AutoSetup())
+ {
+ printf("setup_client() failed - exiting.\n");
+ exit(EXIT_FAILURE);
+ }
+ }
+ else // More than one server - will have to get player selection
+ {
+ //Display list so player can choose
+ // TO BE IMPLEMENTED
- LAN_DetectServers();
-
- /* Connect to server, create socket set, get player nickname, etc: */
- if(!LAN_Setup(argv[1], DEFAULT_PORT))
- {
- printf("setup_client() failed - exiting.\n");
- exit(EXIT_FAILURE);
+ /* Connect to server, create socket set, get player nickname, etc: */
+ if(!LAN_Setup(server_ip, server_port))
+ {
+ printf("setup_client() failed - exiting.\n");
+ exit(EXIT_FAILURE);
+ }
}
+
/* Now we are connected - get nickname from player: */
{
@@ -339,6 +356,13 @@
LAN_StartGame();
game_status = GAME_IN_PROGRESS;
+ /* Start out with our "comets" empty: */
+ {
+ int i;
+ for(i = 0; i < TEST_COMETS; i ++)
+ erase_flashcard(&comets[i]);
+ }
+
//Begin game loop:
while (game_status == GAME_IN_PROGRESS)
{
Modified: tuxmath/branches/lan/src/network.c
===================================================================
--- tuxmath/branches/lan/src/network.c 2009-07-24 22:55:21 UTC (rev 1269)
+++ tuxmath/branches/lan/src/network.c 2009-07-25 04:11:44 UTC (rev 1270)
@@ -17,7 +17,6 @@
#include <unistd.h>
#include <fcntl.h>
-#include "SDL_net.h"
#include "transtruct.h"
#include "network.h"
#include "throttle.h"
@@ -27,31 +26,29 @@
TCPsocket sd; /* Server socket descriptor */
SDLNet_SocketSet set;
IPaddress serv_ip;
-char servers[MAX_SERVERS][NAME_SIZE];
+ServerEntry servers[MAX_SERVERS];
/* Local function prototypes: */
int say_to_server(char *statement);
int evaluate(char *statement);
-int add_to_list(char* name);
+int add_to_server_list(UDPpacket* pkt);
+void print_server_list(void);
-
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';
+ servers[i].ip.host = 0;
/* Docs say we are supposed to call SDL_Init() before SDLNet_Init(): */
if(SDL_Init(0)==-1)
@@ -86,14 +83,6 @@
out->address.port = bcast_ip.port;
out->len = strlen("TUXMATH_CLIENT") + 1;
- printf("UDP Packet to be sent:\n");
- printf("\tChan: %d\n", out->channel);
- printf("\tData: %s\n", (char *)out->data);
- printf("\tLen: %d\n", out->len);
- printf("\tMaxlen: %d\n", out->maxlen);
- printf("\tStatus: %d\n", out->status);
- printf("\tAddress: %x %x\n", out->address.host, out->address.port);
-
//Here we will need to send every few sec onds until we hear back from server
//and get its ip address: IPaddress bcast_ip;
@@ -106,7 +95,6 @@
SDLNet_ResolveHost(&bcast_ip, "localhost", DEFAULT_PORT);
out->address.host = bcast_ip.host;
}
- printf("UDP packets sent to %d addresses\n", sent);
SDL_Delay(250); //give server chance to answer
while(SDLNet_UDP_Recv(udpsock, in))
@@ -114,10 +102,8 @@
if(strncmp((char*)in->data, "TUXMATH_SERVER", strlen("TUXMATH_SERVER")) == 0)
{
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);
+ //add to list, checking for duplicates
+ num_servers = add_to_server_list(in);
}
}
//Make sure we always scan at least five but not more than ten seconds:
@@ -131,11 +117,40 @@
SDLNet_FreePacket(out);
SDLNet_FreePacket(in);
-
+ print_server_list();
return num_servers;
}
+//For the simple case where a single server is found and we
+//want to connect transparently to the user:
+int LAN_AutoSetup(void)
+{
+ /* Open a connection based on autodetection routine: */
+ if (!(sd = SDLNet_TCP_Open(&servers[0].ip)))
+ {
+ fprintf(stderr, "SDLNet_TCP_Open: %s\n", SDLNet_GetError());
+ return 0;
+ }
+ /* We create a socket set so we can check for activity: */
+ set = SDLNet_AllocSocketSet(1);
+ if(!set)
+ {
+ printf("SDLNet_AllocSocketSet: %s\n", SDLNet_GetError());
+ return 0;
+ }
+
+ 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...
+ }
+
+ return 1;
+}
+
+
+
int LAN_Setup(char *host, int port)
{
IPaddress ip; /* Server address */
@@ -425,19 +440,19 @@
}
//add name to list, checking for duplicates:
-int add_to_list(char* name)
+int add_to_server_list(UDPpacket* pkt)
{
int i = 0;
int already_in = 0;
- if(!name)
+ if(!pkt)
return 0;
//first see if it is already in list:
while((i < MAX_SERVERS)
- && (servers[i][0] != '\0'))
+ && (servers[i].ip.host != 0))
{
- if(strncmp(servers[i], name, NAME_SIZE) == 0)
+ if(pkt->address.host == servers[i].ip.host)
already_in = 1;
i++;
}
@@ -445,13 +460,25 @@
//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);
+ servers[i].ip.host = pkt->address.host;
+ servers[i].ip.port = pkt->address.port;
+ strncpy(servers[i].name, "TuxMath LAN Server" , NAME_SIZE);
i++;
}
return i; //i should be the number of items in the list
}
+void print_server_list(void)
+{
+ int i = 0;
+ printf("Detected servers:\n");
+ while(i < MAX_SERVERS && servers[i].ip.host != 0)
+ {
+ printf("Host %d: %s\n", i, servers[i].name);
+ i++;
+ }
+}
Modified: tuxmath/branches/lan/src/network.h
===================================================================
--- tuxmath/branches/lan/src/network.h 2009-07-24 22:55:21 UTC (rev 1269)
+++ tuxmath/branches/lan/src/network.h 2009-07-25 04:11:44 UTC (rev 1270)
@@ -17,10 +17,18 @@
#define NETWORK_H
#include "transtruct.h"
+#include "SDL_net.h"
+typedef struct {
+ IPaddress ip; /* 32-bit IPv4 host address */
+ char name[NAME_SIZE];
+}ServerEntry;
+
+
/* Networking setup and cleanup: */
int LAN_DetectServers(void);
+int LAN_AutoSetup(void);
int LAN_Setup(char* host, int port);
void LAN_Cleanup(void);
More information about the Tux4kids-commits
mailing list