event/SocketMonitor: add method Open()

Allow creating a closed SocketMonitor instance.
This commit is contained in:
Max Kellermann 2013-01-15 22:48:38 +01:00
parent 0c6072c4e4
commit a0ebd444ad
2 changed files with 27 additions and 9 deletions

View File

@ -74,16 +74,11 @@ static GSourceFuncs socket_monitor_source_funcs = {
}; };
SocketMonitor::SocketMonitor(int _fd, EventLoop &_loop) SocketMonitor::SocketMonitor(int _fd, EventLoop &_loop)
:fd(_fd), loop(_loop), :fd(-1), loop(_loop),
source((Source *)g_source_new(&socket_monitor_source_funcs, source(nullptr) {
sizeof(*source))), assert(_fd >= 0);
poll{fd, 0, 0} {
assert(fd >= 0);
source->monitor = this; Open(_fd);
g_source_attach(&source->base, loop.GetContext());
g_source_add_poll(&source->base, &poll);
} }
SocketMonitor::~SocketMonitor() SocketMonitor::~SocketMonitor()
@ -92,6 +87,24 @@ SocketMonitor::~SocketMonitor()
Close(); Close();
} }
void
SocketMonitor::Open(int _fd)
{
assert(fd < 0);
assert(source == nullptr);
assert(_fd >= 0);
fd = _fd;
poll = {fd, 0, 0};
source = (Source *)g_source_new(&socket_monitor_source_funcs,
sizeof(*source));
source->monitor = this;
g_source_attach(&source->base, loop.GetContext());
g_source_add_poll(&source->base, &poll);
}
void void
SocketMonitor::Close() SocketMonitor::Close()
{ {

View File

@ -54,6 +54,9 @@ public:
static constexpr unsigned ERROR = G_IO_ERR; static constexpr unsigned ERROR = G_IO_ERR;
static constexpr unsigned HANGUP = G_IO_HUP; static constexpr unsigned HANGUP = G_IO_HUP;
SocketMonitor(EventLoop &_loop)
:fd(-1), loop(_loop), source(nullptr) {}
SocketMonitor(int _fd, EventLoop &_loop); SocketMonitor(int _fd, EventLoop &_loop);
~SocketMonitor(); ~SocketMonitor();
@ -68,6 +71,8 @@ public:
return fd; return fd;
} }
void Open(int _fd);
void Close(); void Close();
void Schedule(unsigned flags) { void Schedule(unsigned flags) {