Custom file descriptor handler

Marat Dakota dakota at brokenpipe.ru
Mon Feb 9 01:18:58 UTC 2015


Here's what I've come up with:

// In upper scope.
ngx_connection_t myConn;
ngx_event_t myConnRev;


static ngx_int_t
my_module_init(ngx_cycle_t *cycle) {
    int myFd;
    ... // Initialize myFd.

    memset(&myConn, 0, sizeof(myConn));
    memset(&myConnRev, 0, sizeof(myConnRev));

    myConn.number = ngx_atomic_fetch_add(ngx_connection_counter, 1);
    myConn.fd = myFd;
    myConn.pool = cycle->pool;
    myConn.log = cycle->log;
    myConn.read = &myConnRev;

    myConnRev.data = &myConn;
    myConnRev.ready = 1;
    myConnRev.active = 1;
    myConnRev.instance = 1;
    myConnRev.handler = myReadCallback;
    myConnRev.log = cycle->log;

    ngx_add_event(&myConnRev, NGX_READ_EVENT, 0);
}


It looks like working on OSX, I had no chance to test it on other
platforms, but there are a couple of questions.

Is it enough, should it be stable? And I also have no actual idea what
`ready`, `active` and `instance` flags actually mean. Do I need to
`ngx_atomic_fetch_add(ngx_connection_counter, 1);` my connection number? Is
it ok to leave write event field blank (I will only read from the
descriptor)? Do I need to fill some other fields?

The idea is that the file descriptor is initialized once the module is
initialized and lives for as long as worker process does.

Thanks!

--
Marat


On Sun, Feb 8, 2015 at 7:17 AM, Marat Dakota <dakota at brokenpipe.ru> wrote:

> Hi,
>
> I have a file descriptor (it is created by other software, not by nginx).
> How to add it to nginx event loop watcher properly? All I need is to have
> my callback called when my file descriptor has a new data to read (I'll
> read the data by myself).
>
> Browsing the code shows functions like ngx_add_conn and ngx_add_event, but
> it seems like a non-trivial thing to prepare the proper input arguments.
>
> Thanks!
>
> --
> Marat
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx-devel/attachments/20150209/b67c7341/attachment.html>


More information about the nginx-devel mailing list