0

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?

4

1 回答 1

0

您需要单独的套接字观察程序。

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);

...

确保client2proxy_func知道如何处理将调用它的两个单独的观察者。也许最好有单独的回调函数。

于 2013-09-08T09:52:07.673 回答