libdill icon indicating copy to clipboard operation
libdill copied to clipboard

dill_brecv must be in the same dill_coroutine whith dill_tcp_connect?

Open szyusong opened this issue 6 years ago • 0 comments

The test code is below:

#include <stdio.h>
#include <libdill.h>

static dill_coroutine void
test_1(void)
{
  int ret;

  while(dill_msleep(dill_now() + 1000) == 0)
  {
    printf("test_1: %d\n", ret);
  }
}

static int bndl;
static int socket;

static dill_coroutine void
test_process_tx(void)
{
  while(1)
  {
    if (dill_bsend(socket, "Hello\n", 6, -1) < 0)
    {
      break;
    }
    if (dill_msleep(dill_now() + 1000) < 0)
    {
      break;
    }
  }
}

static dill_coroutine void
test_process_rx(void)
{
  uint8_t c;

  while(dill_brecv(socket, &c, 1, -1) == 0)
  {
    printf("rx: 0x%02x\n", c);
  }
  printf("=================== test_process_rx done !!!\n");
}

static dill_coroutine void
test_tcp_ok(void)
{
  struct dill_ipaddr addr;

  if (dill_ipaddr_remote(&addr, "127.0.0.1", 2345, 0, dill_now() + 5000) < 0)
  {
    printf("server addr not reachable\n");
    return;
  }
  socket = dill_tcp_connect(&addr, dill_now() + 10 * 1000);
  if (socket < 0)
  {
    printf("server connect failed\n");
    return;
  }

  dill_bundle_go(bndl, test_process_tx());

  test_process_rx();

  dill_tcp_close(socket, -1);
}

static dill_coroutine void
test_tcp_failed(void)
{
  struct dill_ipaddr addr;

  if (dill_ipaddr_remote(&addr, "127.0.0.1", 2345, 0, dill_now() + 5000) < 0)
  {
    printf("server addr not reachable\n");
    return;
  }
  socket = dill_tcp_connect(&addr, dill_now() + 10 * 1000);
  if (socket < 0)
  {
    printf("server connect failed\n");
    return;
  }

  dill_bundle_go(bndl, test_process_rx());

  test_process_tx();

  dill_tcp_close(socket, -1);
}


int
main(void)
{
  bndl = dill_bundle();

  dill_bundle_go(bndl, test_1());
  //dill_bundle_go(bndl, test_tcp_ok());
  dill_bundle_go(bndl, test_tcp_failed());

  dill_msleep(dill_now() + 10 * 1000);

  dill_hclose(bndl);

  return 0;
}
  • test_tcp_ok is the ok case, the dill_brecv is in the same dill_coroutine whith dill_tcp_connect. When dill_hclose(bndl); invoked, then “test_process_rx done” printed.
~ # ./test_dill
test_1: 0
test_1: 0
test_1: 0
rx: 0x73
rx: 0x64
rx: 0x66
rx: 0x67
rx: 0x73
rx: 0x64
rx: 0x66
rx: 0x67
rx: 0x0a
test_1: 0
test_1: 0
test_1: 0
test_1: 0
test_1: 0
test_1: 0
=================== test_process_rx done !!!
  • test_tcp_failed is the failed case, the dill_brecv is run in another dill_coroutine. When dill_hclose(bndl); invoked, the “test_process_rx done” not printed, but assert failed in fd.c
~ # ./test_dill
test_1: 0
test_1: 0
test_1: 0
test_1: 0
test_1: 0
rx: 0x73
rx: 0x61
rx: 0x64
rx: 0x66
rx: 0x61
test_1: 0
test_1: 0
test_1: 0
test_1: 0
Assert failed: rc == 0 (fd.c:388)
Aborted

szyusong avatar Apr 18 '19 01:04 szyusong