How to detect rest of c->recv

Tom Lebreux me at tomlebreux.com
Tue Nov 10 03:08:41 UTC 2020


Hi,

I have been working my way around the code base and developing some 
modules purely for fun[0].

I am now building a core module that acts as an echo server[1]. This is 
to learn how to actually work with the event loop and clients, etc.

One question I have is: How do I know if there is more data to be 
recv()'d? Here is an example code taken from [1].

     size = 10;

     b = c->buffer;

     if (b == NULL) {
         b = ngx_create_temp_buf(c->pool, size);
         if (b == NULL) {
             ngx_echo_close_connection(c);
             return;
         }

         c->buffer = b;
     }

     n = c->recv(c, b->last, size);

     if (n == NGX_AGAIN) {

In this case, I'm using a small buffer for testing purpose. What is the 
idiomatic way of knowing if there is more data coming in?

Also, what is the proper way of "waiting" again on the next received 
data? I tried simply returning, but the read handler does not run again 
(until the client inputs more data.)

Here's the scenario I am describing:

1. client sends "Hello, world!", which is 13 bytes.
2. read handler reads 10 bytes, stores that into another buffer 
(ngx_str_t in this case)
3. here we want to run the read handler again, probably increase the 
buffer size
4. echo back data to client

Thanks!

[0]: https://git.sr.ht/~tomleb/nginx-stream-upstream-time-module
[1]: https://git.sr.ht/~tomleb/nginx-echo-module


More information about the nginx mailing list