CPINLINE int cli_real_send(cpClient *cli, zval *send_data,cpMasterInfo *info) {
    int ret = 0;
    if (cli->released == CP_FD_RELEASED) {
        cpTcpEvent event;
        event.type = CP_TCPEVENT_GET;
        event.ClientPid = cpPid;
        int ret = cpClient_send(cli->sock, (char *) &event, sizeof (event), 0);
        if (ret < 0) {
            zend_error(E_ERROR, "send failed in GET. Error:%d", errno);
        }
        int n = cpClient_recv(cli, info, sizeof (cpMasterInfo), 1);
        if (n > 0) {
            ret = CP_CLIENT_SERIALIZE_SEND_MEM(send_data, info->worker_id, info->max, info->semid);
            if (ret == SUCCESS) {
                cli->released = CP_FD_NRELEASED;
            }
        } else if (n == 0) {
            php_pdo_connect_pool_close(cli);
            zend_error(E_ERROR, "connect_pool: connect with conPool close");
        } else {
            zend_error(E_ERROR, "connect_pool: recv failed. Error: %s [%d]", strerror(errno), errno);
        }
    } else {
        ret = CP_CLIENT_SERIALIZE_SEND_MEM(send_data, info->worker_id, info->max, info->semid);
    }
    return ret;
}
Beispiel #2
0
static void* connect_pool_perisent(zval* zres, zval* data_source)
{
    //        cpLog_init("/tmp/pool_client.log");
    zend_rsrc_list_entry sock_le;
    int ret;
    cpClient* cli = (cpClient*) pecalloc(sizeof (cpClient), 1, 1);
    if (cpClient_create(cli) < 0)
    {
        php_error_docref(NULL TSRMLS_CC, E_ERROR, "pdo_connect_pool: create sock fail. Error: %s [%d]", strerror(errno), errno);
    }
    ret = cpClient_connect(cli, "127.0.0.1", 6253, (float) 100, 0); //所有的操作100s超时
    if (ret < 0)
    {
        pefree(cli, 1);
        return NULL;
    }
    sock_le.type = le_cli_connect_pool;
    sock_le.ptr = cli;
    ZEND_REGISTER_RESOURCE(zres, cli, le_cli_connect_pool);
    zend_hash_update(&EG(persistent_list), Z_STRVAL_P(data_source), Z_STRLEN_P(data_source), (void*) &sock_le, sizeof (zend_rsrc_list_entry), NULL);
    cli->lock = cpMutexLock;
    cli->unLock = cpMutexUnLock;

    cpTcpEvent event = {0};
    event.type = CP_TCPEVENT_GETFD;
    cpClient_send(cli->sock, (char *) &event, sizeof (event), 0);
    cpMasterInfo info;
    ret = cpClient_recv(cli->sock, &info, sizeof (cpMasterInfo), 1);
    if (ret < 0)
    {
        php_error_docref(NULL TSRMLS_CC, E_ERROR, "recv from pool server error  [%d],%s", errno, strerror(errno));
    }
    cli->server_fd = info.server_fd;
    cpClient_attach_mem();
    CONN(cli)->release = CP_FD_RELEASED;
    return cli;
}