Libevent: how to close all open sockets on shutdow

2019-07-19 04:26发布

I have created a simple HTTP proxy using libevent. It can be shutdown by sending it a SIGHUP signal which is caught by the signal handler. The shutdown function calls event_base_loopexit, frees structures and other heap allocations and exits.

The problem is if a SIGHUP is caught when a connection is open. I need to be able to close the socket, ideally invoking the same close function that is called when a close event is caught.

Is there a correct or standard way to do this?

At the moment, the only thing I can think of is to keep a linked list of connections and simply iterate through this on shutdown, closing each in turn.

标签: c libevent
1条回答
Lonely孤独者°
2楼-- · 2019-07-19 04:57

At the moment, the only thing I can think of is to keep a linked list of connections and simply >iterate through this on shutdown, closing each in turn.

That's what you have to do.

(Note that sockets are closed when the application exits. But if you need to do custom logic on all the connections on shutdown, you need to keep track of them and iterate through them.)

查看更多
登录 后发表回答