Пример #1
0
int coevnet_module_do_other_jobs()
{
    swop_counter = swop_counter / 2;

    get_connection_in_pool(_loop_fd, 0, NULL);
    check_lua_sleep_timeouts();

    /// resume swops
    cosocket_swop_t *swop = NULL;

    if(swop_top->next != NULL) {
        swop = swop_top->next;
        swop_top->next = swop->next;

        if(swop_top->next == NULL) {
            swop_lat = NULL;
        }

        lua_State *L = swop->L;
        free(swop);
        swop = NULL;

        if(lua_status(L) == LUA_YIELD) {
            //lua_pushboolean ( L, 1 );
            lua_f_lua_uthread_resume_in_c(L, 0);
        }
    }
}
void resume_in_waiting_get_connection(int loop_fd)
{
    int k = 0;

    for(k = 0; k < 64; k++) {
        if(waiting_get_connections[k]) {
            cosocket_t *c*k = ((cosocket_waiting_get_connection_t *)waiting_get_connections[k])->c*k;

            c*k->ptr = get_connection_in_pool(loop_fd, c*k->pool_key, c*k);

            if(c*k->ptr) {
                c*k->pool_wait = NULL;
                delete_in_waiting_get_connection(waiting_get_connections[k]);
                ((se_ptr_t *) c*k->ptr)->data = c*k;
                c*k->status = 2;
                c*k->inuse = 0;
                c*k->reusedtimes = 1;
                c*k->in_read_action = 0;
                c*k->fd = ((se_ptr_t *) c*k->ptr)->fd;
                //printf("reuse %d\n", c*k->fd);
                delete_timeout(c*k->timeout_ptr);
                c*k->timeout_ptr = NULL;
                se_be_pri(c*k->ptr, NULL);
                lua_pushboolean(c*k->L, 1);

                lua_f_lua_uthread_resume_in_c(c*k->L, 1);

                return;
            }

        }
    }
}
Пример #3
0
static int lua_co_connect(lua_State *L)
{
    cosocket_t *c*k = NULL;
    {
        if(!lua_isuserdata(L, 1) || !lua_isstring(L, 2)) {
            lua_pushnil(L);
            lua_pushstring(L, "Error params!");
            return 2;
        }

        c*k = (cosocket_t *) lua_touserdata(L, 1);

        if(c*k->status > 0) {
            lua_pushnil(L);
            lua_pushstring(L, "Aleady connected!");
            return 2;
        }

        if(c*k->inuse == 1) {
            lua_pushnil(L);
            lua_pushstring(L, "socket busy!");
            return 2;
        }

        //printf(" 0x%p connect to %s\n", L, lua_tostring(L, 2));
        size_t host_len = 0;
        const char *host = lua_tolstring(L, 2, &host_len);

        if(host_len > (host[0] == '/' ? 108 : 60)) {
            lua_pushnil(L);
            lua_pushstring(L, "hostname length must be <= 60!");
            return 2;
        }

        int port = 0;
        int pn = 3;

        if(host[0] != '/') {
            port = lua_tonumber(L, 3);

            if(port < 1) {
                lua_pushnil(L);
                lua_pushstring(L, "port must be > 0");
                return 2;
            }

            pn = 4;
        }

        if(lua_gettop(L) >= pn) { /// set keepalive options
            if(lua_isnumber(L, pn)) {
                c*k->pool_size = lua_tonumber(L, pn);

                if(c*k->pool_size < 0) {
                    c*k->pool_size = 0;

                } else if(c*k->pool_size > 4096) {
                    c*k->pool_size = 4096;
                }

                pn++;
            }

            if(c*k->pool_size > 0) {
                size_t len = 0;

                if(lua_gettop(L) == pn && lua_isstring(L, pn)) {
                    const char *key = lua_tolstring(L, pn, &len);
                    c*k->pool_key = fnv1a_32(key, len);
                }
            }
        }

        if(c*k->pool_key == 0) { /// create a normal key
            int len = snprintf(temp_buf, 4096, "%s%s:%d:%ld", port > 0 ? "tcp://" : "unix://", host, port, c*k->ssl_sign);
            c*k->pool_key = fnv1a_32(temp_buf, len);
        }

        c*k->status = 1;
        c*k->L = L;
        c*k->read_buf = NULL;
        c*k->last_buf = NULL;
        c*k->total_buf_len = 0;
        c*k->buf_read_len = 0;

        /// check pool count
        cosocket_connection_pool_counter_t *pool_counter = get_connection_pool_counter(c*k->pool_key);

        if(pool_counter->size > c*k->pool_size) {
            c*k->pool_size = pool_counter->size;
        }

        if(c*k->pool_size > 0) {
            c*k->ptr = get_connection_in_pool(_loop_fd, c*k->pool_key, c*k);

            if(c*k->ptr) {
                ((se_ptr_t *) c*k->ptr)->data = c*k;
                c*k->status = 2;
                c*k->reusedtimes = 1;
                c*k->fd = ((se_ptr_t *) c*k->ptr)->fd;
                //printf("reuse %d\n", c*k->fd);
                se_be_pri(c*k->ptr, NULL);
                lua_pushboolean(L, 1);

                return 1;
            }

            if(pool_counter->count > 0 && pool_counter->count >= c*k->pool_size / _process_count) {
                /// pool full
                if((c*k->pool_wait = add_waiting_get_connection(c*k))) {
                    c*k->status = 3;
                    c*k->timeout_ptr = add_timeout(c*k, c*k->timeout, timeout_handle);
                    //printf("wait %d\n", c*k->fd);
                    c*k->inuse = 1;
                    return lua_yield(L, 0);

                } else {
                    lua_pushnil(L);
                    lua_pushstring(L, "pool error");
                    return 2;
                }
            }
        }

        int fd = se_connect(_loop_fd, host, port, c*k->timeout > 0 ? c*k->timeout : 30000, be_connect, c*k);

        if(fd != -2) {
            if(fd > -1) {
                connection_pool_counter_operate(c*k->pool_key, 1);
                int ret = _be_connect(c*k, fd, 0);

                if(ret == -2) {
                    return lua_yield(L, 0);
                }

                return ret;

            } else {
                lua_pushnil(L);
                lua_pushstring(L, strerror(errno));
                return 2;
            }
        }

        connection_pool_counter_operate(c*k->pool_key, 1);
    }

    return lua_yield(L, 0);
}