kaltura / nginx-srt-module

Nginx SRT/TCP gateway
GNU Affero General Public License v3.0
82 stars 15 forks source link

Hello, I'd like to ask you a question ? #2

Closed shenlei190810 closed 2 years ago

shenlei190810 commented 2 years ago

here is your function: ` static ngx_int_t ngx_srt_conn_send(ngx_srt_conn_t sc) { int n; int serr, serrno; size_t sent; size_t size; ngx_buf_t b; SRTSOCKET ss; ngx_chain_t out, cl;

out = ngx_srt_get_chain(&sc->srt_out.out, &sc->srt_out.out_lock);
if (out) {
    if (sc->srt_out.busy == NULL) {
        sc->srt_out.busy = out;

    } else {
        for (cl = sc->srt_out.busy; cl->next; cl = cl->next) { /* void */ }

        cl->next = out;
    }
}

/* send as much as possible */

ss = ngx_srt_session_sock(sc);
sent = 0;

for ( ;; ) {

    cl = sc->srt_out.busy;
    if (!cl) {
        break;
    }

    b = cl->buf;

    while (b->pos < b->last) {

        size = b->last - b->pos;
        if (size > sc->payload_size) {
            size = sc->payload_size;
        }

        n = srt_send(ss, (char *) b->pos, size);
        if (n < 0) {
            serr = srt_getlasterror(&serrno);
            if (serr == SRT_EASYNCSND) {
                goto done;
            }

            ngx_log_error(NGX_LOG_ERR, sc->srt_pool->log, serrno,
                "ngx_srt_conn_send: srt_send() failed %d", serr);
            return NGX_ERROR;
        }

        ngx_log_debug3(NGX_LOG_DEBUG_HTTP, sc->srt_pool->log, 0,
            "ngx_srt_conn_send: srt send %d, fd: %D, size: %uz",
            n, ss, size);

        b->pos += n;
        sent += n;
    }

    sc->srt_out.busy = cl->next;

    ngx_srt_chain_insert_head(&sc->srt_out.free, cl,
        &sc->srt_out.free_lock);
}

done: if (sent) { sc->connection->sent += sent;

    ngx_srt_conn_post_ngx(sc, NGX_SRT_POST_WRITE);
}

return NGX_OK;

}`

after srt_send data , you call this function below, can you explain why it is needed ? thank you very much.

image

kaltura-hooks commented 2 years ago

Hi @shenlei190810,

Thank for you reporting an issue and helping improve Kaltura!

To get the fastest response time, and help the maintainers review and test your reported issues or suggestions, please ensure that your issue includes the following (please comment with more info if you have not included all this info in your original issue):

For general troubleshooting see: https://github.com/kaltura/platform-install-packages/blob/Jupiter-10.13.0/doc/kaltura-packages-faq.md#troubleshooting-help

If you only have a general question rather than a bug report, please close this issue and post at: http://forum.kaltura.org

Thank you in advance,

erankor commented 2 years ago

can you explain what you meant by "this function"? are you asking about ngx_srt_chain_insert_head or ngx_srt_conn_post_ngx?

shenlei190810 commented 2 years ago

ngx_srt_conn_post_ngx,this function。

erankor commented 2 years ago

As I wrote on the prev issue, these functions are used for posting messages between the SRT thread and the nginx main thread. In this case, after the send is done on the SRT thread, it sends a write event to the main thread. Once the main nginx thread receives this notification, it calls the write handler of the connection - https://github.com/kaltura/nginx-srt-module/blob/master/src/ngx_srt_connection.c#L135 In the case of proxy (TCP->SRT or SRT->TCP), eventually, this would lead to the execution of ngx_srt_proxy_process_ngx_to_srt, which could trigger another recv on the TCP connection, and send any received data to the SRT thread.

shenlei190810 commented 2 years ago

ngx_srt_proxy_process_ngx_to_srt

yes. but in ngx_srt_proxy_process_ngx_to_srt function, there is a for loop, all tcp data can be recevied. if after this loop, more tcp data comes, it also will trige ngx_srt_proxy_process_ngx_to_srt function. all the above, I don't know reason you call ngx_srt_conn_post_ngx to notify nginx thread all data have sended by srt thread;because without it, all tcp data also can be transfered to srt thread. am i right? thank you very much

erankor commented 2 years ago

The reason this code is needed is because the buffer may get full, and then the module will not issue any more recv's. In this case, only when the send completes and the buffer is freed, the module can call recv again. This is why this code is required...

shenlei190810 commented 2 years ago

The reason this code is needed is because the buffer may get full, and then the module will not issue any more recv's. In this case, only when the send completes and the buffer is freed, the module can call recv again. This is why this code is required...

yes, you are right, but when I read nginx stream proxy module, after sent out some data, there is no this code. Do you know why? Is that a mistake?

erankor commented 2 years ago

It works in the same way in the stream proxy module... there it's a bit simpler because the two connections are of the same type, the code is symmetrical, and a single function can handle both cases. If you check the code, you'll see that ngx_stream_proxy_process_connection is called in 4 cases - (upstream, downstream) x (read, write). The reason it's called on write events is to handle the case of buffer full, that I referred to above. Here, we have the same 4 cases, but 2 are handled by ngx_to_srt and 2 are handled by srt_to_ngx.

shenlei190810 commented 2 years ago

It works in the same way in the stream proxy module... there it's a bit simpler because the two connections are of the same type, the code is symmetrical, and a single function can handle both cases. If you check the code, you'll see that ngx_stream_proxy_process_connection is called in 4 cases - (upstream, downstream) x (read, write). The reason it's called on write events is to handle the case of buffer full, that I referred to above. Here, we have the same 4 cases, but 2 are handled by ngx_to_srt and 2 are handled by srt_to_ngx.

thank you very much for your answer, that helps me a lot.