VgaGames4

VgaGames4 - network man-pages

[.. upper level ..]

# vg4->nw->xdata_allclients_recv()

Receive exchange-data from all clients.

SYNTAX
VG_BOOL vg4->nw->xdata_allclients_recv(struct VG_NwXdataList **xdatalistp, VG_BOOL exclusive, const char *mydata, size_t mysize, VG_BOOL *redo)
FUNCTION PARAMETERS

xdatalistp | For returning received exchange-data list, value must be set to NULL at the first function-call, the list must be freed with vg4->nw->xdata_allclients_free()
exclusive | Whether exclusive: each client must have different data
mydata | If exclusive: data the local client has sent to the network-server
mysize | If exclusive: number of bytes in mydata
redo | If exclusive: for returning whether local client must reselect its data

RETURN VALUE
Returns boolean: - VG_TRUE: OK - VG_FALSE: Got exit-request (or network-error occurred)
DESCRIPTION
Receive exchange-data from all clients. This function simplifies receiving exchange-data, when all clients sent data which all clients shall receive, e.g. each client selects a car for a race. When the selection needs not to be exclusive, this function waits until all data from all clients is received and returns it in xdatalistp. When the selection shall be exclusive, this function waits until all data from all clients is received, except when another client selected the same data the local client requested, which causes the function to return with redo = VG_TRUE, this means that the local client shall reselect its data and call this function again. The value of xdatalistp must be NULL at the first call. xdatalistp must be freed with vg4->nw->xdata_allclients_free().
EXAMPLE

    /* each clients selects exclusively an avatar and receives the avatars of all clients */

    char avatar[32];
    struct VG_NwXdataList *xdatalist, *xdptr;
    VG_BOOL redo;

    /* first set to NULL */
    xdatalist = NULL;

    reselect:
     [... select avatar ...]

    /* send selected avatar to network-server */
    if (!vg4->nw->xdata_send(avatar, strlen(avatar) + 1)) { return VG_FALSE; }

    /* receive avatars of each client from network-server */
    if (!vg4->nw->xdata_allclients_recv(&xdatalist, VG_TRUE, avatar, strlen(avatar) + 1, &redo)) { return VG_FALSE; }
    if (redo) {  /* select another avatar */
      show_info("Selected avatar is no more available");
      sleep(3);
      vg4->window->clear();
      goto reselect;
    }

    /* show avatars */
    for (xdptr = xdatalist; xdptr != NULL; xdptr = xdptr->next) {
      printf("Client %d selected %s\n", xdptr->clnr, xdptr->data);
    }

    /* free exchange-data list */
    vg4->nw->xdata_allclients_free(xdatalist);