Exemple #1
0
static int
readAcqStat(int sd, AcqStatBlock *statblock )
{
	int		ival, nchr;
	AcqStatBlock	tmpblock;

	ival = wait_for_select( sd, 10 /* seconds */ );
	if (ival < 1)
        {
	   if (ival < 0)
              acq_errno = SELECT_ERROR;
	   else if (ival == 0)
              acq_errno = CONNECT_ERROR;
	  return( RET_ERROR );
	}
	nchr = read( sd, &tmpblock, sizeof(AcqStatBlock) );
        if (nchr == sizeof(AcqStatBlock) )
        {
	  memcpy( (char *) statblock, (char *) &tmpblock, nchr );
	  return( RET_OK );
        }
        else
        {
           acq_errno = READ_ERROR;
	   return( RET_ERROR );
        }
}
Exemple #2
0
int		start_main_cicle(t_server *server)
{
  while (1)
    {
      if (wait_for_select(server) == -1)
	return (-1);
      if (check_fd(server) == -1)
	return (-1);
      if (server->minerals_consumed >= server->generation_interval)
	{
	  if (generate_minerals(server) == -1)
	    return (-1);
	}
      if (check_requests(server) == -1)
	return (-1);
      if (check_life_players(server) == -1)
	return (-1);
      if (check_end_game(server, MAX_LVL) == -1)
	{
	  printf("Fin de partie\n");
	  return (-1);
	}
    }
  return (0);
}
Exemple #3
0
int prepare_reply_socket(int connsid )
{
	int	ival;

/*  Render the connected socket blocking  */

	ival = render_blocking( connsid );
	if (ival != 0) {
		if (ival == -2)
		  acq_errno = FCNTL_READ_ERROR;
		else
		  acq_errno = FCNTL_WRITE_ERROR;
		return( -1 );
	}

/*  Wait for Acqproc to reply or timeout  */

	ival = wait_for_select( connsid, 20 );
	if (ival < 1) {
		acq_errno = SELECT_ERROR;
		close( connsid );
		return( -1 );
	}

	return( 0 );
}
void display_message (int message, int lives, int level, int score)
{
  char text [80];
  int height = MAX_SCR_MESSAGE_Y;
  int y1 = (MAX_SCR_ROWS - height) / 2 - MSG_BORDER_Y;

  if ((message == MSG_FLOOR) || (message == MSG_COLLISION) || (message == MSG_FIRE) || (message == MSG_WATER))
    {
      if (lives == 0)
	draw_message (MAX_SCR_GAMEOVER_X, gameover);
      else
	draw_message (MAX_SCR_CRASH_X, crash);
      sprintf (text, "Caused by %s", crash_messages [message]);
      ansi_SetAttr (ATTR_reverse);
      centre_text (text, y1 + MAX_SCR_MESSAGE_Y + 2 * MSG_BORDER_Y, MAX_SCR_COLS);
      centre_text ("Press <5> <Enter> or <Space>", y1 + MAX_SCR_MESSAGE_Y + 2 * MSG_BORDER_Y + 1, MAX_SCR_COLS);
      centre_text ("to continue....", y1 + MAX_SCR_MESSAGE_Y + 2 * MSG_BORDER_Y + 2, MAX_SCR_COLS); 
      ansi_Position (1, MAX_SCR_ROWS);
      ansi_printf (" Game Info  -  Score: [%6d]  Lives Remaining: [%2d]  Next Level: [%2d]    ", score, lives, level);
      ansi_SetAttr (ATTR_normal);
    }
  else if (message == MSG_WINLEVEL)
    draw_message (MAX_SCR_WINLEVEL_X, winlevel);
  wait_for_select ();
}
Exemple #5
0
int talk2Acq(char *hostname, char *username, int cmd,
             char *msg_for_acq, char *msg_for_vnmr, int mfv_len )
{
#ifdef NESSIE
	int	 mlen;
	char	 msg[256];
	int	 ival;
	CommPort acq_addr  = &comm_addr[ACQ_COMM_ID];
        CommPort tmp_from =  &comm_addr[LOCAL_COMM_ID];

        (void) msg_for_vnmr;
        (void) mfv_len;
        tmp_from->path[0] = '\0';
        tmp_from->pid = getpid();
        strcpy(tmp_from->host,hostname);
        set_comm_port(tmp_from);	/* this call creates a socket */

	mlen = strlen( cmd_for_acq[cmd] ) +
	       strlen( tmp_from->path ) +
	       strlen( msg_for_acq ) + 2;
	if (mlen > 255) {
		acq_errno = MSG_LEN_ERROR;
		fprintf( stderr, "talk2Acq error - message len: %d greater than 255\n",mlen );
		return( -1 );
	}

        sprintf(msg,"%s%c%s%c%s%c%s%c%d%c%s",
                cmd_for_acq[cmd], DELIMITER_1,
                tmp_from->path,DELIMITER_2,
                username,DELIMITER_3,hostname,DELIMITER_3,tmp_from->pid,DELIMITER_2,
                msg_for_acq);

	if (SendAsyncInova( acq_addr, tmp_from, msg ) != RET_OK) {
		close( tmp_from->msgesocket );
		return( -1 );
	}

/*  Use `select' to wait for Acqproc to respond to us.	*/

	ival = wait_for_select( tmp_from->msgesocket, 20 );
	if (ival < 1) {
		if (ival < 0)
		  acq_errno = SELECT_ERROR;
		else if (ival == 0)
		  acq_errno = CONNECT_ERROR;
		close( tmp_from->msgesocket );
		return( -1 );
	}
	ival = accept( tmp_from->msgesocket, 0, 0 );
	close( tmp_from->msgesocket );		/* We are finished with */
						/* the original socket. */
	if (ival < 0) {
		acq_errno = ACCEPT_ERROR;
		return( -1 );
	}
	else
	  return( ival );	/* file descriptor for the connected socket.	*/
#endif
}
void game (void)
{
  int lives = MAX_LIVES;
  int score = 0;
  long int level_score;
  int level = START_LEVEL;
  int result;

  do {
    result = play_level (level, &level_score, lives, score);
    /* The level has been played, now we must check to see what happened, display an appropriate
       message to the terminal if necessary, add up points, etc */
    
    switch (result)
      {	
      case MSG_WINLEVEL:
	score+= level_score;
	level++;
	display_message (result, lives, level, score);
	break;
      case MSG_WINGAME:
	score+= level_score;
	level++; /* This is for the log_score function */
	game_win ();
	
	lives = 0; /* This lets us get out of this function */
	
	/* Display ending message, then go back to menu */
	ansi_PutString (1, 25, "Done.");
	wait_for_select ();
	break;
      case MSG_QUIT: return;
      default: /* We know the users aircraft has crashed in some way, so deduct one life and display a message */
	lives--;
	display_message (result, lives, level, score);
	break;
      }
  } while (lives > 0);

#ifndef __ANDROID__
  log_score (score, level); /* This records the score of anyone who finishes or runs out of lives */
#endif // __ANDROID__
}
Exemple #7
0
static int poke_acqproc(char *hostname, char *username,
                        int cmd, char *message )
{
    char	 acqproc_msg[ 256 ];
    int		 ival;
    int		 meslength;
    int		 on = 1;
#ifdef WINBRIDGE
    CommPort acq_addr  = &comm_addr[INFO_COMM_ID];
#else
    CommPort acq_addr  = &comm_addr[ACQ_COMM_ID];
#endif
    CommPort vnmr_addr = &comm_addr[VNMR_COMM_ID];
    CommPort tmp_from  = &comm_addr[LOCAL_COMM_ID];

        TPRINT0("Poke started \n");
    tmp_from->path[0] = '\0';
    tmp_from->pid = vnmr_addr->pid;
    strcpy(tmp_from->host,hostname);

    tmp_from->messname.sin_family = AF_INET;
    tmp_from->messname.sin_addr.s_addr = INADDR_ANY;
    tmp_from->messname.sin_port = 0;

    tmp_from->msgesocket = socket( AF_INET, SOCK_STREAM, 0 );
    if (tmp_from->msgesocket < 0) {
        acq_errno = tmp_from->msgesocket;
        return( -1 );					/*  Not ABORT !! */
    }
/* Specify socket options */
        TPRINT0("Poke socket \n");

#ifndef LINUX
    setsockopt(tmp_from->msgesocket,SOL_SOCKET,SO_USELOOPBACK,(char *)&on,sizeof(on));
#endif
    setsockopt(tmp_from->msgesocket,SOL_SOCKET,(~SO_LINGER),(char *)&on,sizeof(on));

    if (bind(tmp_from->msgesocket,(caddr_t) &(tmp_from->messname),
             sizeof(tmp_from->messname)) != 0) {
        acq_errno = BIND_ERROR;
        close( tmp_from->msgesocket );
        return( -1 );					/*  Not ABORT !! */
    }
        TPRINT0("Poke bind \n");

    meslength = sizeof(tmp_from->messname);
    getsockname(tmp_from->msgesocket,&tmp_from->messname,&meslength);
    tmp_from->port = tmp_from->messname.sin_port;
 
    listen(tmp_from->msgesocket,5);        /* set up listening queue ?? */


    sprintf( &acqproc_msg[ 0 ], "%s,%.200s", username,message);
    
#ifdef WINBRIDGE
#ifdef DEBUG    
    fprintf(stdout, "DEBUG jgw: socket.c poke_acqproc username = %s\n", username);
    fprintf(stdout, "DEBUG jgw: socket.c poke_acqproc message = %s\n", message);
    fprintf(stdout, "DEBUG jgw: socket.c poke_acqproc acq_addr->port = %d\n", acq_addr->port);
    fprintf(stdout, "DEBUG jgw: socket.c poke_acqproc cmd = %d\n", cmd);
#endif
    close( tmp_from->msgesocket );
    return( -1 );
#endif
    if (SendAsync( acq_addr, tmp_from, cmd, &acqproc_msg[ 0 ] ) == RET_ERROR )
    {
        close( tmp_from->msgesocket );
	return( -1 );
    }

/*  Use `select' to wait for Acqproc to respond to us.	*/

    ival = wait_for_select( tmp_from->msgesocket, 20 );
    if (ival < 1) {
	if (ival < 0)
		  acq_errno = SELECT_ERROR;
	else if (ival == 0)
		  acq_errno = CONNECT_ERROR;
        close( tmp_from->msgesocket );
	return( -1 );
    }
    ival = accept( tmp_from->msgesocket, 0, 0 );
    close( tmp_from->msgesocket );		/* We are finished with */
					/* the original socket. */
    if (ival < 0) {
		acq_errno = ACCEPT_ERROR;
		return( -1 );
    }
    else
	 return( ival );	/* file descriptor for the connected socket.	*/
}
/*----------------------------------------------------------------------*/
int get_http(int sock, char *path, FILE *outfile){
	int rc;
	int code;
	float version;
	/* NOTE: if you change any array sizes, change them in the scanfs, etc. */
	char buf[1024];
	char tmpbuf[32];
	char location[128];
	char c;

	/* write the request to the socket */
	/* if no path specified, make it / */
	sprintf(buf, "GET %s HTTP/1.0\r\n"
					"User-Agent: %s\r\n"
					"Accept: text/*\r\n\r\n", path[0] ? path : "/", appname, useraddr);
	rc = write(sock, buf, strlen(buf));

#ifdef DEBUG
	printf("waiting for reply...\n");
#endif
	

	/* set up the select stuff */
	/* clear the read set */
	FD_ZERO(&readset);
	FD_SET(sock,&readset);
	/* set the timeout */
	timeout.tv_sec = inter_time;
	timeout.tv_usec = 0;

#ifndef RAWGET
	/* first, get the header */
	getline(buf, sizeof(buf), sock);

	/* get the protocol and the code */
	sscanf(buf, "%4s/%4f %3d", tmpbuf, &version, &code);

#ifdef DEBUG
	printf("Got header... HTTP: %s, version: %f, code: %d\n",
		tmpbuf, version, code);
#endif

	/* check the header */
	if(strcmp(tmpbuf, "HTTP") ||
		version <= 0.0 ||
		code < 200 ||
		code > 503){
		/* error with the header */
		ERROR0("Error with the header.  Aborting.\n");
	}

	/* check the code */
	switch(code){
		case 400:       ERROR0("Bad request");
		case 401:       ERROR0("Unauthorized");
		case 403:       ERROR0("Forbidden");
		case 404:       ERROR0("Not Found");
		case 500:       ERROR0("Internal Server Error");
		case 501:       ERROR0("Not Implemented");
		case 502:       ERROR0("Bad Gateway");
		case 503:       ERROR0("Service Unavailable");
	}


	/* skip lines until we get an empty one */
	location[0]='\0';
	while( getline(buf, sizeof(buf), sock) > 0 ){
#ifdef DEBUG
		printf("Header line: %s", buf);
#endif
		sscanf(buf, "%31s", tmpbuf);
		if(strlen(tmpbuf)==0) break;
		if(strcmp(tmpbuf, "Location:")==0){
			sscanf(buf, "%31s %127s", tmpbuf, location);
		}
		tmpbuf[0]='\0';
	}

	/* check for redirect */
	if(code==301 || code==302){
		printf("Redirect: %s\n", location);
		/* close the current socket and fd, and call for a new location */
		shutdown(sock, 2);
		/* for recursion, do:
		return get_url(location, outfile);
		*/
		/* I will just exit, since I need to return the address */
		exit(0);
	}
#endif
/* END OF RAWGET IFDEF */

	/* get the body */
	wait_for_select(sock);
	while( rc = read(sock, buf, sizeof(buf)) ){
		fwrite(buf, rc, 1, outfile);
		wait_for_select(sock);
	}

	/* close the socket */
	shutdown(sock, 2);

	return 1;
}