Welcome! Log In Create A New Profile

Advanced

How to detect rest of c->recv

November 09, 2020 10:10PM
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
_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Subject Author Posted

How to detect rest of c->recv

tomleb November 09, 2020 10:10PM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 64
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready