rtmp_connection_t *get_connection(rtmp_listening_t  *ls,fd_t s)
{
    rtmp_connection_t  *c;
    rtmp_event_t       *rev, *wev;
    mem_pool_t         *pool;
    rtmp_cycle_t       *ngx_cycle;

    ngx_cycle = ls->cycle;

    c = ngx_cycle->free_connections;

    if (c == NULL) {
        ngx_drain_connections();
        c = ngx_cycle->free_connections;
    }

    if (c == NULL) {
        printf("there is not enough connections!\n");
        return NULL;
    }

    ngx_cycle->free_connections = c->next;
    rev = c->read;
    wev = c->write;
    pool = c->pool;

    memset(c, 0, sizeof(rtmp_connection_t));

    c->read = rev;
    c->write = wev;
    c->fd = s;
    c->pool = pool;

    memset(rev, 0 ,sizeof(rtmp_event_t));
    memset(wev, 0, sizeof(rtmp_event_t));

    rev->data = c;
    wev->data = c;
    wev->write = 1;

    rev->index = -1;
    wev->index = -1;

    return c;
}
//从连接池中获取一个ngx_connection_t结构体,同时获取相应的读/写事件
ngx_connection_t *
ngx_get_connection(ngx_socket_t s/* 这条连接的套接字句柄 */, ngx_log_t *log)
{
    ngx_uint_t         instance;
    ngx_event_t       *rev, *wev;
    ngx_connection_t  *c;

    /* disable warning: Win32 SOCKET is u_int while UNIX socket is int */

    if (ngx_cycle->files && (ngx_uint_t) s >= ngx_cycle->files_n) {
        ngx_log_error(NGX_LOG_ALERT, log, 0,
                      "the new socket has number %d, "
                      "but only %ui files are available",
                      s, ngx_cycle->files_n);
        return NULL;
    }

    c = ngx_cycle->free_connections;

    if (c == NULL) {
        ngx_drain_connections();
        c = ngx_cycle->free_connections;
    }

    if (c == NULL) {
        ngx_log_error(NGX_LOG_ALERT, log, 0,
                      "%ui worker_connections are not enough",
                      ngx_cycle->connection_n);

        return NULL;
    }

    ngx_cycle->free_connections = c->data;
    ngx_cycle->free_connection_n--;

    if (ngx_cycle->files) {
        ngx_cycle->files[s] = c;
    }

    rev = c->read;
    wev = c->write;

    ngx_memzero(c, sizeof(ngx_connection_t));

    c->read = rev;
    c->write = wev;
    c->fd = s;
    c->log = log;

    instance = rev->instance;

    ngx_memzero(rev, sizeof(ngx_event_t));
    ngx_memzero(wev, sizeof(ngx_event_t));

    rev->instance = !instance;
    wev->instance = !instance;

    rev->index = NGX_INVALID_INDEX;
    wev->index = NGX_INVALID_INDEX;

    rev->data = c;
    wev->data = c;

    wev->write = 1;

    return c;
}
Beispiel #3
0
/* 获取套接字对应的连接 */
ngx_connection_t *
ngx_get_connection(ngx_socket_t s, ngx_log_t *log)
{
    ngx_uint_t         instance;
    ngx_event_t       *rev, *wev;
    ngx_connection_t  *c;

    /* disable warning: Win32 SOCKET is u_int while UNIX socket is int */

    /* 如果超过了可以监听的最大文件数,则出错 */
    if (ngx_cycle->files && (ngx_uint_t) s >= ngx_cycle->files_n) {
        ngx_log_error(NGX_LOG_ALERT, log, 0,
                      "the new socket has number %d, "
                      "but only %ui files are available",
                      s, ngx_cycle->files_n);
        return NULL;
    }

    c = ngx_cycle->free_connections;

    /* 此时连接池已经被用完 */
    if (c == NULL) {
        /* 将一些keepalive的连接给释放掉 */
        ngx_drain_connections();
        c = ngx_cycle->free_connections;
    }

    if (c == NULL) {
        ngx_log_error(NGX_LOG_ALERT, log, 0,
                      "%ui worker_connections are not enough",
                      ngx_cycle->connection_n);

        return NULL;
    }

    ngx_cycle->free_connections = c->data;
    ngx_cycle->free_connection_n--;

    /* 设置套接字描述符对应的连接 */
    if (ngx_cycle->files) {
        ngx_cycle->files[s] = c;
    }

    rev = c->read;
    wev = c->write;

    /* 连接数据进行清除 */
    ngx_memzero(c, sizeof(ngx_connection_t));

    c->read = rev;
    c->write = wev;
    c->fd = s;
    c->log = log;

    instance = rev->instance;

    ngx_memzero(rev, sizeof(ngx_event_t));
    ngx_memzero(wev, sizeof(ngx_event_t));

    rev->instance = !instance;
    wev->instance = !instance;

    rev->index = NGX_INVALID_INDEX;
    wev->index = NGX_INVALID_INDEX;

    /* 读写时间携带的数据是连接 */
    rev->data = c;
    wev->data = c;

    wev->write = 1;

    return c;
}
/* 从连接池中获取连接,并进行一些初始化 */
ngx_connection_t *
ngx_get_connection(ngx_socket_t s, ngx_log_t *log) //已经带上连接好的套接字,直接可以放入连接结构体
{
    ngx_uint_t         instance;
    ngx_event_t       *rev, *wev;
    ngx_connection_t  *c;

    /* disable warning: Win32 SOCKET is u_int while UNIX socket is int */

    if (ngx_cycle->files && (ngx_uint_t) s >= ngx_cycle->files_n) {
        ngx_log_error(NGX_LOG_ALERT, log, 0,
                      "the new socket has number %d, "
                      "but only %ui files are available",
                      s, ngx_cycle->files_n);
        return NULL;
    }

    /* ngx_mutex_lock */

    //从连接池中获取一个空闲连接
    c = ngx_cycle->free_connections;

    if (c == NULL) {
        ngx_drain_connections();// 将一些keepalive连接给释放掉
        c = ngx_cycle->free_connections;
    }

    if (c == NULL) {
        ngx_log_error(NGX_LOG_ALERT, log, 0,
                      "%ui worker_connections are not enough",
                      ngx_cycle->connection_n);

        /* ngx_mutex_unlock */

        return NULL;
    }

    ngx_cycle->free_connections = c->data; //c->data中的为next,空闲链接指向下一个,c指向的已被使用
    ngx_cycle->free_connection_n--; //空闲连接数-1

    /* ngx_mutex_unlock */

    if (ngx_cycle->files) {
        ngx_cycle->files[s] = c;
    }

    //使用指针保留原读写事件event
    rev = c->read;
    wev = c->write;

    ngx_memzero(c, sizeof(ngx_connection_t)); //清空

    c->read = rev; //使用之前保留的read事件
    c->write = wev; //使用之前保留的write事件
    c->fd = s;
    c->log = log;

    instance = rev->instance; //保留原过期标志位

    //清空
    ngx_memzero(rev, sizeof(ngx_event_t));
    ngx_memzero(wev, sizeof(ngx_event_t));

    //instance标志位置反,用于标示一个事件是否过期
    rev->instance = !instance;
    wev->instance = !instance;

    rev->index = NGX_INVALID_INDEX;
    wev->index = NGX_INVALID_INDEX;

    //读写事件的data都指回连接本身
    rev->data = c;
    wev->data = c;

    wev->write = 1;

    return c;
}