Exemplo n.º 1
0
void
test_sock_timeout(void)
{
    pthread_t tid;

    /* test crt_tcp_read_to on a server which will do nothing except accept */
    pthread_create(&tid, NULL, dummy_none_op_server, NULL);
    sched_yield();
    usleep(1000);

    crt_create(NULL, NULL, sock_timeout, NULL);
    usleep(20 * 1000);
    CU_ASSERT(coroutine_env.info.ran == 7);

    pthread_join(tid, NULL);

    /* test crt_tcp_read_to on a server which will response a bit of data */
    pthread_create(&tid, NULL, dummy_slow_server, NULL);
    sched_yield();
    usleep(1000);

    crt_create(NULL, NULL, sock_timeout2, NULL);
    usleep(20 * 1000);
    CU_ASSERT(coroutine_env.info.ran == 8);

    pthread_join(tid, NULL);
}
Exemplo n.º 2
0
int main()
{
    int listenfd;

    crt_init();

    if ((listenfd = co_tcp4_open_bind(NULL, 80)) == -1) {
        perror("open bind error");
        exit(-1);
    }

    if (co_tcp_listen(listenfd, 1024) == -1) {
      perror("listen error");
      exit(-1);
    }

    for (;;) {
      int connfd;

      if ((connfd = co_tcp_accept(listenfd, NULL, NULL)) == -1) {
        perror("accept error");
        exit(-1);
      }

      if (crt_create(NULL, co_listen, (void*)(uint64_t)connfd) == NULL) {
        printf("crt_create error");
        exit(-1);
      }
    }

    return 0;
}
Exemplo n.º 3
0
static int
accept_loop(void)
{
    int retval;
    int listenfd;
    struct sockaddr_in server_addr;
    int flag;

    retval = RET_FAILED;
    listenfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (listenfd < 0)
    {
        log_error("socket() failed: %d - %s", errno, strerror(errno));
        retval = RET_FAILED;
        goto done;
    }
    flag = 1;
    setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag));
    bzero(&server_addr, sizeof(server_addr));
    server_addr.sin_addr.s_addr = g_master_settings.bind_ip;
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(g_master_settings.accept_port);
    if (bind(listenfd, (struct sockaddr *)(&server_addr), sizeof(server_addr)) != 0)
    {
        log_error("bind port %d failed: %d - %s", g_master_settings.accept_port, errno, strerror(errno));
        retval = RET_FAILED;
        goto done;
    }
    if (listen(listenfd, 128) < 0)
    {
        log_error("listen() failed: %d - %s", errno, strerror(errno));
        retval = RET_FAILED;
        goto done;
    }

    intptr_t sockfd;
    struct sockaddr_in remote_peer;
    socklen_t len;
    coroutine_attr_t crt_attr;

    crt_attr_setstacksize(&crt_attr, MASTER_COROUTINE_STACKSIZE);
    len = sizeof remote_peer;
    for ( ; ; )
    {
        sockfd = (intptr_t)accept(listenfd, (struct sockaddr *)&remote_peer, &len);
        if (sockfd < 0)
        {
            char ip[IP_STR_LEN];
            inet_ntop(AF_INET, &(remote_peer.sin_addr), ip, sizeof ip);
            log_warn("accept() failed: %d - %s, ret = %d, ip = %s", errno, strerror(errno), (int)sockfd, ip);
            continue;
        }

        crt_create(NULL, NULL, handle_request, (void*)sockfd);
    }

done:

    return retval;
}
Exemplo n.º 4
0
int expert_crt_save(const char *filename)
{
    FILE *fd;
    crt_chip_header_t chip;

    DBG(("EXPERT: save crt '%s'\n", filename));

    if (expert_ram == NULL) {
        DBG(("EXPERT: ERROR expert_ram == NULL\n"));
        return -1;
    }

    fd = crt_create(filename, CARTRIDGE_EXPERT, 1, 1, STRING_EXPERT);

    if (fd == NULL) {
        return -1;
    }

    chip.type = 2;               /* Chip type. (= FlashROM?) */
    chip.bank = 0;               /* Bank nr. (= 0) */
    chip.start = 0x8000;         /* Address. (= 0x8000) */
    chip.size = EXPERT_RAM_SIZE; /* Length. (= 0x2000) */

    /* Write CHIP packet data. */
    if (crt_write_chip(expert_ram, &chip, fd)) {
        fclose(fd);
        return -1;
    }

    fclose(fd);
    DBG(("EXPERT: ERROR save crt ok\n"));

    return 0;
}
Exemplo n.º 5
0
void
test_bg_run(void)
{
    int pipe_fd[2];
    char buf[16];

    pthread_t *t1_ptr, *t2_ptr, t3;

    pipe(pipe_fd);
    crt_create(NULL, NULL, bg_run_main_func, (void*)&pipe_fd[1]);
    read(pipe_fd[0], &buf[0], sizeof 16);

    t1_ptr = (pthread_t *)&buf[0];
    t2_ptr = (pthread_t *)&buf[8];
    t3 = pthread_self();
    CU_ASSERT(memcmp(t1_ptr, t2_ptr, 8)  != 0);
    CU_ASSERT(memcmp(t1_ptr, &t3, 8) != 0);
    CU_ASSERT(memcmp(t2_ptr, &t3, 8) != 0);

    usleep(1000 * 10);

    close(pipe_fd[0]);
    close(pipe_fd[1]);
    CU_ASSERT(coroutine_env.info.cid == 13);
    CU_ASSERT(coroutine_env.info.ran == 13);
}
Exemplo n.º 6
0
void
test_call_in_coroutine(void)
{
    CU_ASSERT(crt_create(NULL, NULL, call_in_coroutine, NULL) == 0);
    usleep(1000*10);
    CU_ASSERT(coroutine_env.info.ran == 2);
}
Exemplo n.º 7
0
void
test_msleep(void)
{
    crt_create(NULL, NULL, msleep_func, NULL);
    usleep(200 * 1000); /* 200 ms */

    CU_ASSERT(coroutine_env.info.cid == 12);
    CU_ASSERT(coroutine_env.info.ran == 12);
}
Exemplo n.º 8
0
void
test_tcp_timeout_connect(void)
{
    pthread_t tid;
    pthread_create(&tid, NULL, dummy_write_server, NULL);
    sched_yield();
    usleep(1000);

    crt_create(NULL, NULL, tcp_timeout_connect, NULL);

    usleep(20 * 1000);
    CU_ASSERT(coroutine_env.info.ran == 11);
    pthread_join(tid, NULL);
}
Exemplo n.º 9
0
void
test_sock_read(void)
{
    pthread_t tid;
    pthread_create(&tid, NULL, dummy_write_server, NULL);
    sched_yield();
    usleep(1000);

    crt_create(NULL, NULL, sock_read, NULL);
    usleep(1000);
    CU_ASSERT(coroutine_env.info.ran == 6);

    pthread_join(tid, NULL);
}
Exemplo n.º 10
0
void
test_disk_write(void)
{
    int lowestfd = dup(0);
    close(lowestfd);

    CU_ASSERT(crt_create(NULL, NULL, disk_write, NULL) == 0);
    usleep(1000 * 10);
    CU_ASSERT(coroutine_env.info.ran == 4);

    int curr_lowestfd = dup(0);
    close(curr_lowestfd);
    CU_ASSERT(lowestfd == curr_lowestfd);

    assert(unlink("/tmp/test_acoro.dat") == 0);
}
Exemplo n.º 11
0
void
test_disk_read(void)
{
    int ret;
    int lowestfd = dup(0);
    close(lowestfd);
    CU_ASSERT(coroutine_env.info.ran == 2);
    ret = crt_create(NULL, NULL, disk_read, NULL);
    CU_ASSERT(ret == 0);

    usleep(1000*20);
    CU_ASSERT(coroutine_env.info.ran == 3);
    int curr_lowestfd = dup(0);
    close(curr_lowestfd);
    CU_ASSERT(lowestfd == curr_lowestfd);
}
Exemplo n.º 12
0
void
test_disk_io_failed(void)
{
    int lowestfd = dup(0);
    close(lowestfd);

    coroutine_t cid;
    CU_ASSERT(crt_create(&cid, NULL, disk_io_failed, NULL) == 0);
    usleep(1000 * 10);
    CU_ASSERT(coroutine_env.info.ran == 5);
    CU_ASSERT(cid == 5);
    assert(unlink("/tmp/test_acoro.dat") == 0);

    int curr_lowestfd = dup(0);
    close(curr_lowestfd);
    CU_ASSERT(lowestfd == curr_lowestfd);
}
Exemplo n.º 13
0
void
test_sock_write(void)
{
    pthread_t tid;
    pthread_create(&tid, NULL, dummy_read_server, NULL);
    sched_yield();
    usleep(1000);

    crt_create(NULL, NULL, sock_write, NULL);
    usleep(10000);
    CU_ASSERT(coroutine_env.info.ran == 9);
    if (coroutine_env.info.ran != 9)
    {
        printf("\n[coroutine_env.info.ran] = %" PRIu64 "\n", coroutine_env.info.ran);
    }

    pthread_join(tid, NULL);
}
Exemplo n.º 14
0
void
test_null_coroutine(void)
{
    coroutine_t cid;
    int ret;

    CU_ASSERT(coroutine_env.info.cid == 0);
    CU_ASSERT(coroutine_env.info.ran == 0);

    ret = crt_create(&cid, NULL, null_coroutine, (void*)(intptr_t)0xbeef);

    CU_ASSERT(coroutine_env.info.cid == 1);
    CU_ASSERT(cid == 1);

    CU_ASSERT(ret == 0);

    usleep(1000*20);
    CU_ASSERT(coroutine_env.info.ran == 1);
}