VgaGames4

VgaGames4 - network man-pages

[.. upper level ..]

# vg4->nw->xdata_retag()

Set a new tag of exchange-data.

SYNTAX
void vg4->nw->xdata_retag(void)
DESCRIPTION
Set a new tag of exchange-data. If more than one type of exchange-data is used, e.g. at first a race track, then the selected cars of each client, each type should be tagged with a new tag to prevent the next type receive delayed data from the previous type. Exchange-data is normally used before the game-loop to inform the other clients of certain user-selections, e.g. the user-avatar or a selected race track ...
EXAMPLE

    int master_clnr = 1;  /* master client-number */
    char *ex_data;
    size_t ex_size;
    int ex_clnr;

    /* +++ first inform all clients about the race track +++ */

    if (vg4->nw->local_clnr() == master_clnr) ) {  /* the master selects the race track */
      char racetrack[32];
      snprintf(racetrack, sizeof(racetrack), "Forest Track 5");

      /* now send racetrack-information to other clients including me */
      if (!vg4->nw->xdata_send(racetrack, strlen(racetrack) + 1)) { VG_dest(); exit(0); }
    }

    /* show info */
    { struct VG_Image *img;
      char btxt[64];
      snprintf(btxt, sizeof(btxt), "Receiving data ...");
      img = vg4->font->totext(btxt, NULL, NULL, NULL, NULL);
      vg4->window->clear();
      vg4->window->copy(img, NULL, NULL);
      vg4->window->flush();
      vg4->image->destroy(img);
    }

    /* all clients including master receive the racetrack-information */
    for (;;) {
      if (!vg4->nw->xdata_recv(&ex_data, &ex_size, &ex_clnr)) { VG_dest(); exit(0); }

      if (ex_size > 0) {  /* just output received data to stdout */
        printf("Client %d selected: %s\n", ex_clnr, ex_data);
        free(ex_data);
        break;
      }

      /* still no data received, wait and try again */
      if (!vg4->nw->is_connected(master_clnr)) { VG_dest(); exit(0); }  /* master disconnected */
      vg4->window->flush();
      vg4->misc->wait_time(100);
    }

    /* +++ second inform all clients about the difficulty level +++ */

    /* retag */
    vg4->nw->xdata_retag();

    if (vg4->nw->local_clnr() == master_clnr) ) {  /* the master selects the difficulty level */
      char level[32];
      snprintf(level, sizeof(level), "Advanced");

      /* now send level-information to other clients including me */
      if (!vg4->nw->xdata_send(level, strlen(level) + 1)) { VG_dest(); exit(0); }
    }

    /* show info */
    { struct VG_Image *img;
      char btxt[64];
      snprintf(btxt, sizeof(btxt), "Receiving data ...");
      img = vg4->font->totext(btxt, NULL, NULL, NULL, NULL);
      vg4->window->clear();
      vg4->window->copy(img, NULL, NULL);
      vg4->window->flush();
      vg4->image->destroy(img);
    }

    /* all clients including master receive the level-information */
    for (;;) {
      if (!vg4->nw->xdata_recv(&ex_data, &ex_size, &ex_clnr)) { VG_dest(); exit(0); }

      if (ex_size > 0) {  /* just output received data to stdout */
        printf("Client %d selected: %s\n", ex_clnr, ex_data);
        free(ex_data);
        break;
      }

      /* still no data received, wait and try again */
      if (!vg4->nw->is_connected(master_clnr)) { VG_dest(); exit(0); }  /* master disconnected */
      vg4->window->flush();
      vg4->misc->wait_time(100);
    }

SEE ALSO
vg4->nw->xdata_recv vg4->nw->xdata_send