Exemplo n.º 1
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.º 2
0
void
test_set_stacksize(void)
{
    coroutine_attr_t attr;

    crt_attr_setstacksize(&attr, 1024);
    CU_ASSERT(attr.stacksize == 1024);
}