How to use libev for one event, waiting two sockets by two ev_io and ev_watch?

2.2k views Asked by At

Now I'm writing a proxy server for a specify true server.

The architecture of the system can be represent by the graph below:

(I actually use multi-threads to handle all clients.)

'client A' ←––––– –––––→ 'proxy server' ←––––– –––––→ 'true Server'    


I use "libev.h" to implement it, so one event need to listen two socket.

I find some example like:

struct MYIO w
{
    ev_io io;
    int serverfd;
    int clientfd;
}

int main()
{

    ...

    struct MYIO w;

    w.clientfd = new_tcp_client ("127.0.0.1", 12346);

    ev_io_init (&w.io, client2proxy_func, clientfd, EV_READ);
    ev_io_start (loop, &w_io);

    ev_timer_init (&timeout_watcher, timer_func, 5, 0.);
    ev_timer_start (loop, &timeout_watcher);

    ...

}

This only for one io event.

If I want to wait two io_ev then it didn't work... like this below:

(I also try some different way but all failed.)

...

w.clientfd = new_tcp_client ("127.0.0.1", 12346);
w.serverfd = new_tcp_server ("127.0.0.1", 12345);

ev_io_init (&w.io, client2proxy_func, clientfd, EV_READ);
ev_io_start (loop, &w_io);
ev_io_init (&w.io, proxy2server_func, serverfd, EV_READ);
ev_io_start (loop, &w_io);

ev_timer_init (&timeout_watcher, timer_func, 5, 0.);
ev_timer_start (loop, &timeout_watcher);

...

How to use libev in multi-task?

How to use libev for one event, waiting two sockets by two ev_io and ev_watch?

1

There are 1 answers

0
James Chong On BEST ANSWER

You'll need separate watchers for the sockets.

struct MYIO w
{
    ev_io io_server;
    ev_io io_client;
    int serverfd;
    int clientfd;
}

...

w.serverfd = new_tcp_server ("127.0.0.1", 12345);
w.clientfd = new_tcp_client ("127.0.0.1", 12346);

ev_io_init (&w.io_server, client2proxy_func, w.serverfd, EV_READ);
ev_io_start (loop, &w.io_server);
ev_io_init (&w.io_client, client2proxy_func, w.clientfd, EV_READ);
ev_io_start (loop, &w.io_client);

...

Make sure client2proxy_func knows how to handle the two separate watchers that will be calling it. Perhaps it would be better to have separate callback functions.