VgaGames 3 - Network man-pages

[.. upper level ..]

VG3_nw_recv_data()

[added in version 1.2]

Receive data-packets if available.
SYNTAX
int VG3_nw_recv_data(struct vg3_nwclient *nwclnt)

FUNCTION PARAMETERS
nwclnt Network-struct

RETURN VALUE
Returns integer: - 0: OK - 1: Got timeout-request from the network-server, so exit the game

DESCRIPTION
Receive data-packets if available. This function receives data-packets sent with VG3_nw_send_data(), if there are any available (will not block). As VG3_nw_update() also receives data-packets, VG3_nw_recv_data() should only be used outside the game-loop, when sending key-strokes is not yet needed. To retrieve a data-packet, use VG3_nw_fetch_data(). Be aware, there is no data buffer, if a data-packet is received from a client, then a next data-packet from the same client will overwrite the former one, whether it was retrieved or not. Calling VG3_nw_recv_data() also pings the network-server to keep it running.

EXAMPLE

Sending time in seconds to other clients when return-key is being hit:
  struct vg3_window *wstruct;
  struct vg3_nwclient *nwptr;
  int clnr, masterbyte, i1;
  char nwserver[64], buf[128];
  int wsize, hsize;

  [... open window, network ...]

  /* get size of window */
  VG3_window_getsize(wstruct, &wsize, &hsize);

  /* connect to nwserver */
  clnr = VG3_nw_connect(nwptr, nwserver, NULL, NULL, 0, &masterbyte);

  /* loop for exchanging data-packets without sending key-strokes */
  for (;;) {
    /* get key-strokes (locally) */
    if (VG3_inputevent_update(wstruct)) { break; }

    /* quit on Q-key */
    if (VG3_key_ispressed(wstruct, VGAG3_KEY_Q, VGAG3_IS_PRESSED)) { break; }

    /* check for and receive data-packet(s) */
    if (VG3_nw_recv_data(nwptr)) { break; }

    /* on return-key send time in seconds to all clients */
    if (VG3_key_ispressed(wstruct, VGAG3_KEY_ENTER, VGAG3_IS_PRESSED)) {
      snprintf(buf, sizeof(buf), "%ld", (long)time(NULL));
      VG3_nw_send_data(nwptr, buf);
    }

    VG3_draw_clear(wstruct, NULL, VGAG3_COLOR_BLACK);

    /* while fetching new received data-packets, show the contents */
    do {
      i1 = VG3_nw_fetch_data(nwptr, buf, sizeof(buf));
      if (i1 > 0) {  /* show time in left upper corner */
        struct vg3_rect rect;
        struct vg3_text stxt;
        VGAG3_TEXT_ATTRIBUTES_SET(&stxt, NULL, '\n', 0, buf);
        rect.x = rect.y = 0; rect.w = wsize; rect.h = hsize;
        VG3_draw_text(wstruct, NULL, &rect, 0, &stxt, VGAG3_COLOR_YELLOW, VGAG3_COLOR_BLACK, 0);
      }
    } while (i1 > 0);

    VG3_window_update(wstruct, 0, 0);

    VG3_wait_time(50);
  }

  /* end */
  VG3_nw_close(nwptr);
  VG3_window_free(wstruct);

SEE ALSO
VG3_nw_send_data() VG3_nw_fetch_data()