1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
|
/*
* slave-network-handler
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "netmaze.h"
#include "netcmds.h"
#include "network.h"
#define TRUE 1
#define FALSE 0
extern struct shared_struct *sm;
static void handle_packet(char *buf,int len);
static void send_owndata(void);
void send_owncomment(void);
static void send_ownready(void);
static void send_ident(void);
static void send_end(void);
static void send_addplayer(void);
static void send_join(void);
extern void move_all(PLAYER*,int*);
extern void run_game(MAZE*,PLAYER*);
extern void myrandominit(long);
extern void inactivate_player(int);
extern void activate_player(int);
extern int own_socket; /* streams socket descriptor */
struct robot_functions robot = { 0, } ;
/******************************************************
* send the ident/add/join stuff (also used by robot.c)
*/
void ident_player(void)
{
send_ident();
send_addplayer();
send_join();
send_owncomment();
}
/**************************************************
* handle the incoming data (also used by robot.c)
*/
void handle_socket(void)
{
static char buf[256];
int count,len;
static int frag=0,fraglen;
if(frag > 0)
{ /* we allow exact 1 fragmentation (our messages aren't long) */
if((count = recv(own_socket,buf+fraglen,frag,0)) != frag)
{
fprintf(stderr,"Major protocoll-error: %d!!\n",buf[0]);
exit(1);
}
fprintf(stderr,"Tried to handle fragmentation\n");
handle_packet(buf,fraglen+frag);
frag = 0;
return;
}
else if( (count = recv(own_socket,buf,1,0)) != 1) /* nicht optimal */
{
if(count == 0)
{
fprintf(stderr,"I've lost the Server-Connection\n");
exit(3);
}
fprintf(stderr,"IO: error reading ident\n"); return;
}
if((len = (int) nm_field[(unsigned char)*buf]) > 0)
{
if(len > 1)
{
count = recv(own_socket,buf+1,len-1,0);
if(count != len-1)
{
fprintf(stderr,"IO: wrong length: %d %d\n",len-1,count);
if(count >= 0)
{
frag = len - 2 - count;
fraglen = count + 2;
return;
}
}
}
handle_packet(buf,len);
}
else
{
if((count = recv(own_socket,buf+1,1,0)) != 1) /* nicht optimal */
{
fprintf(stderr,"IO: error reading blklen\n"); return;
}
len = (int) buf[1];
if(len > 2)
{
if((count = recv(own_socket,buf+2,len-2,0)) != (len-2) )
{
fprintf(stderr,"IO-1: wrong length - possibly fragmented %d %d %d %d\n",len-2,count,(int) buf[0],(int) buf[1]);
if(count >= 0)
{
frag = len - 2 - count;
fraglen = count + 2;
return;
}
}
}
handle_packet(buf,len);
}
}
/*************************************
* handle packets
*/
static void handle_packet(char *buf,int len)
{
char data[1];
int (*hfeld)[MAZEDIMENSION],(*vfeld)[MAZEDIMENSION];
int i,j;
long randbase;
switch(*buf)
{
case NM_STARTGAME:
sm->anzplayers = (int) buf[2];
sm->shownumber = sm->shownumbertmp = (int) buf[3];
sm->numteams = (int) buf[6];
for(i=0;i<sm->anzplayers;i++)
{
sm->playfeld[i].team = buf[16+i];
}
randbase = (long) (unsigned char) buf[5];
randbase += ((long) (unsigned char) buf[4]) << 8;
myrandominit(randbase);
sm->gamemode = (unsigned char) buf[7];
sm->gamemode += ((int)(unsigned char)buf[8])<<8;
sm->config.divider = (int) buf[9];
nm_field[NM_ALLDATA] = (char) sm->anzplayers + 1;
run_game(&(sm->std_maze),sm->playfeld);
if(robot.valid)
(robot.start)(sm->shownumber);
if(!sm->camera)
send_ownready();
sm->gameflag = TRUE;
break;
case NM_STOPGAME:
if(sm->gameflag)
{
sm->gameflag = FALSE;
sm->winnerdraw = FALSE;
sm->bgdraw = TRUE;
sm->screendraw = FALSE;
}
break;
case NM_MAZEH:
hfeld = sm->std_maze.hwalls;
sm->std_maze.xdim = (int) buf[2];
i = (int) buf[3];
for(j=0;j<sm->std_maze.xdim;j++)
{
hfeld[i][j] = (int) buf[j+4];
}
break;
case NM_MAZEV:
vfeld = sm->std_maze.vwalls;
sm->std_maze.ydim = (int) buf[2];
i = (int) buf[3];
for(j=0;j<sm->std_maze.ydim;j++)
{
vfeld[j][i] = (int) buf[j+4];
}
break;
case NM_ALLDATA:
for(i=0;i<sm->anzplayers;i++)
sm->sticks[i] = (int) buf[i+1];
move_all(sm->playfeld,sm->sticks);
if(robot.valid)
sm->ownstick = (robot.action)();
if(sm->gameflag)
{
if(!sm->camera)
send_owndata();
}
else
send_end();
break;
case NM_MESSAGE:
fprintf(stderr,"%s\n",buf + 6);
break;
case NM_ALLNAMES:
strncpy(sm->playfeld[(unsigned char) buf[2]].name,buf+3,buf[1]-3);
sm->playfeld[(unsigned char)buf[2]].name[(int)(unsigned char)buf[1]-3] = 0;
break;
case NM_PING:
data[0] = NM_PONG;
send(own_socket,&data,1,0);
break;
case NM_ALLCOMMENTS:
strncpy(sm->playfeld[(int) (unsigned char) buf[2]].comment,buf+3,buf[1]-3);
sm->playfeld[(int)(unsigned char)buf[2]].comment[(int)(unsigned char)buf[1]-3] = 0;
break;
case NM_ACTIVATE:
activate_player((int) buf[2]);
break;
case NM_INACTIVATE:
inactivate_player((int) buf[2]);
break;
}
}
static void send_owndata(void)
{
char data[2];
data[0] = NM_OWNDATA;
data[1] = (char) sm->ownstick;
send(own_socket,data,2,0);
}
static void send_ownready(void)
{
char data = NM_READY;
send(own_socket,&data,1,0);
}
void send_owncomment(void)
{
char buf[100];
buf[0] = NM_OWNCOMMENT;
buf[1] = strlen(sm->owncomment)+2;
strcpy(buf+2,sm->owncomment);
send(own_socket,buf,buf[1],0);
}
static void send_ident(void)
{
char buf[10];
if(!sm->camera)
{
buf[1] = (char) ((PLAYERMAGIC>>24) & 0xff);
buf[2] = (char) ((PLAYERMAGIC>>16) & 0xff);
buf[3] = (char) ((PLAYERMAGIC>>8) & 0xff);
buf[4] = (char) (PLAYERMAGIC & 0xff);
}
else
{
buf[1] = (char) ((CAMMAGIC>>24) & 0xff);
buf[2] = (char) ((CAMMAGIC>>16) & 0xff);
buf[3] = (char) ((CAMMAGIC>>8) & 0xff);
buf[4] = (char) (CAMMAGIC & 0xff);
}
buf[0] = NM_SETMODE;
send(own_socket,buf,5,0);
}
/*
static void send_docommand(char *c,int len)
{
char data[256];
data[0] = NM_DOCOMMAND;
data[1] = len+2;
memcpy(data+2,c,len);
send(own_socket,data,data[1],0);
}
*/
static void send_end(void)
{
char data[] = { NM_END };
send(own_socket,data,1,0);
}
static void send_addplayer(void)
{
char data[32];
data[0] = NM_ADDPLAYER;
data[1] = strlen(sm->ownname)+5;
data[2] = 0;
data[3] = 1;
strcpy(data+4,sm->ownname);
send(own_socket,data,data[1],0);
}
static void send_join(void)
{
char data[4];
data[0] = NM_JOIN;
data[1] = 0;
data[2] = 1;
data[3] = 1;
send(own_socket,data,4,0);
}
|