VgaGames4

VgaGames4 - network man-pages

[.. upper level ..]

# vg4->nw->xdata_recv()

Receive exchange-data from network-server.

SYNTAX
VG_BOOL vg4->nw->xdata_recv(char **ex_data, size_t *ex_size, int *ex_clnr)
FUNCTION PARAMETERS

ex_data | For returning received exchange-data, will be allocated if ex_size returns greater 0
ex_size | For returning number of bytes in ex_data
ex_clnr | For returning client-number who sent this data

RETURN VALUE
Returns boolean: - VG_TRUE: OK - VG_FALSE: Got exit-request (or network-error occurred)
DESCRIPTION
Receive exchange-data from network-server. Receive also an update which clients are connected. If (still) no data is available, the function returns immediately with value 0 in ex_size. While receiving data, the function blocks until all data is received. 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;

    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);
    }

SEE ALSO
vg4->nw->xdata_send vg4->nw->xdata_retag