[Tux4kids-commits] r1268 - in tuxmath/branches/lan: server src
David Bruce
dbruce-guest at alioth.debian.org
Fri Jul 24 20:30:29 UTC 2009
Author: dbruce-guest
Date: 2009-07-24 20:30:28 +0000 (Fri, 24 Jul 2009)
New Revision: 1268
Modified:
tuxmath/branches/lan/server/server.c
tuxmath/branches/lan/src/Makefile.am
tuxmath/branches/lan/src/network.c
tuxmath/branches/lan/src/throttle.c
Log:
further implementation of server autodetection system
Modified: tuxmath/branches/lan/server/server.c
===================================================================
--- tuxmath/branches/lan/server/server.c 2009-07-24 16:14:52 UTC (rev 1267)
+++ tuxmath/branches/lan/server/server.c 2009-07-24 20:30:28 UTC (rev 1268)
@@ -253,18 +253,47 @@
{
char buf[NET_BUF_LEN];
int recvd = 0;
- UDPpacket* pkt = SDLNet_AllocPacket(NET_BUF_LEN);
- recvd = SDLNet_UDP_Recv(udpsock, pkt);
+ UDPpacket* in = SDLNet_AllocPacket(NET_BUF_LEN);
+ recvd = SDLNet_UDP_Recv(udpsock, in);
if(recvd)
{
printf("UDP Packet incoming\n");
- printf("\tChan: %d\n", pkt->channel);
- printf("\tData: %s\n", (char *)pkt->data);
- printf("\tLen: %d\n", pkt->len);
- printf("\tMaxlen: %d\n", pkt->maxlen);
- printf("\tStatus: %d\n", pkt->status);
- printf("\tAddress: %x %x\n", pkt->address.host, pkt->address.port);
+ 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)
+ {
+ UDPpacket* out;
+ 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/src/Makefile.am
===================================================================
--- tuxmath/branches/lan/src/Makefile.am 2009-07-24 16:14:52 UTC (rev 1267)
+++ tuxmath/branches/lan/src/Makefile.am 2009-07-24 20:30:28 UTC (rev 1268)
@@ -50,7 +50,8 @@
SDL_rotozoom.c \
lessons.c \
scandir.c \
- pixels.c
+ pixels.c \
+ throttle.c
# HACK "TuxMath" is the Windows program, whereas "tuxmath" is the Unix program
@@ -88,7 +89,8 @@
scandir.h \
pixels.h \
compiler.h \
- transtruct.h
+ transtruct.h \
+ throttle.h
Modified: tuxmath/branches/lan/src/network.c
===================================================================
--- tuxmath/branches/lan/src/network.c 2009-07-24 16:14:52 UTC (rev 1267)
+++ tuxmath/branches/lan/src/network.c 2009-07-24 20:30:28 UTC (rev 1268)
@@ -20,7 +20,7 @@
#include "SDL_net.h"
#include "transtruct.h"
#include "network.h"
-
+#include "throttle.h"
//#include "testclient.h"
@@ -38,9 +38,11 @@
{
UDPsocket udpsock = NULL;
UDPpacket* out;
+ UDPpacket* in;
IPaddress bcast_ip;
int sent = 0;
-
+ int done = 0;
+ int seconds = 0;
/* Docs say we are supposed to call SDL_Init() before SDLNet_Init(): */
if(SDL_Init(0)==-1)
{
@@ -65,11 +67,14 @@
return 0;
}
- out = SDLNet_AllocPacket(NET_BUF_LEN);
+ out = SDLNet_AllocPacket(NET_BUF_LEN);
+ in = SDLNet_AllocPacket(NET_BUF_LEN);
+
SDLNet_ResolveHost(&bcast_ip, "255.255.255.255", DEFAULT_PORT);
out->address.host = bcast_ip.host;
+ sprintf(out->data, "TUXMATH_CLIENT");
out->address.port = bcast_ip.port;
- sprintf(out->data, "TUXMATH_CLIENT");
+ out->len = strlen("TUXMATH_CLIENT") + 1;
printf("UDP Packet to be sent:\n");
printf("\tChan: %d\n", out->channel);
@@ -79,11 +84,38 @@
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 seconds until we hear back from server
- //and get its ip address:
- sent = SDLNet_UDP_Send(udpsock, -1, out);
- printf("UDP packets sent to %d addresses\n", sent);
+ //Here we will need to send every few sec onds until we hear back from server
+ //and get its ip address: IPaddress bcast_ip;
+
+ while(!done)
+ {
+ sent = SDLNet_UDP_Send(udpsock, -1, out);
+ if(!sent)
+ {
+ printf("broadcast failed - network inaccessible.\nTrying localhost (for testing)\n");
+ 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
+
+ if(SDLNet_UDP_Recv(udpsock, in))
+ {
+ if(strncmp((char*)in->data, "TUXMATH_SERVER", strlen("TUXMATH_SERVER")) == 0)
+ {
+ printf("Reply received from server\n");
+ done = 1;
+ }
+ }
+
+ seconds++;
+ if(seconds > 10)
+ done = 1;
+ Throttle(1000); //repeat once per second
+ }
+
SDLNet_FreePacket(out);
+ SDLNet_FreePacket(in);
}
Modified: tuxmath/branches/lan/src/throttle.c
===================================================================
--- tuxmath/branches/lan/src/throttle.c 2009-07-24 16:14:52 UTC (rev 1267)
+++ tuxmath/branches/lan/src/throttle.c 2009-07-24 20:30:28 UTC (rev 1268)
@@ -19,11 +19,11 @@
static Uint32 now_t, last_t; //These will be zero first time through
int wait_t;
- //Target loop time must be between 0 and 100 msec:
+ //Target loop time must be between 0 and 1000 msec:
if(loop_msec < 0)
loop_msec = 0;
- if(loop_msec > 100)
- loop_msec = 100;
+ if(loop_msec > 1000)
+ loop_msec = 1000;
//See if we need to wait:
now_t = SDL_GetTicks();
More information about the Tux4kids-commits
mailing list