SocketMonitor.hxx 3.96 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef MPD_SOCKET_MONITOR_HXX
#define MPD_SOCKET_MONITOR_HXX

23
#include "PollGroup.hxx"
24
#include "net/SocketDescriptor.hxx"
25

26 27
#include <boost/intrusive/list_hook.hpp>

28
#include <cassert>
29
#include <cstddef>
30 31
#include <type_traits>

32 33
class EventLoop;

34 35 36 37 38
/**
 * Monitor events on a socket.  Call Schedule() to announce events
 * you're interested in, or Cancel() to cancel your subscription.  The
 * #EventLoop will invoke virtual method OnSocketReady() as soon as
 * any of the subscribed events are ready.
39
 *
40 41 42
 * This class does not feel responsible for closing the socket.  Call
 * Close() to do it manually.
 *
43 44 45
 * This class is not thread-safe, all methods must be called from the
 * thread that runs the #EventLoop, except where explicitly documented
 * as thread-safe.
46
 */
47
class SocketMonitor {
48 49 50 51 52
	friend class EventLoop;

	using ReadyListHook = boost::intrusive::list_member_hook<boost::intrusive::link_mode<boost::intrusive::auto_unlink>>;
	ReadyListHook ready_siblings;

53
	SocketDescriptor fd = SocketDescriptor::Undefined();
54
	EventLoop &loop;
55 56

	/**
57 58
	 * A bit mask of events that is currently registered in the
	 * #EventLoop.
59
	 */
60
	unsigned scheduled_flags = 0;
61

62 63 64 65 66 67 68
	/**
	 * A bit mask of events which have been reported as "ready" by
	 * epoll_wait().  If non-zero, then the #EventLoop will call
	 * Dispatch() soon.
	 */
	unsigned ready_flags = 0;

69
public:
70 71 72 73
	static constexpr unsigned READ = PollGroup::READ;
	static constexpr unsigned WRITE = PollGroup::WRITE;
	static constexpr unsigned ERROR = PollGroup::ERROR;
	static constexpr unsigned HANGUP = PollGroup::HANGUP;
74

75 76 77 78 79 80
	/**
	 * These flags are always reported by epoll_wait() and don't
	 * need to be registered with epoll_ctl().
	 */
	static constexpr unsigned IMPLICIT_FLAGS = ERROR|HANGUP;

81 82
	typedef std::make_signed<size_t>::type ssize_t;

83
	explicit SocketMonitor(EventLoop &_loop) noexcept
84
		:loop(_loop) {}
85

86
	SocketMonitor(SocketDescriptor _fd, EventLoop &_loop) noexcept
87
		:fd(_fd), loop(_loop) {}
88

89
	~SocketMonitor() noexcept;
90

91
	auto &GetEventLoop() const noexcept {
92 93 94
		return loop;
	}

95
	bool IsDefined() const noexcept {
96
		return fd.IsDefined();
97 98
	}

99
	SocketDescriptor GetSocket() const noexcept {
100 101 102 103 104
		assert(IsDefined());

		return fd;
	}

105
	void Open(SocketDescriptor _fd) noexcept;
106

107 108
	/**
	 * "Steal" the socket descriptor.  This abandons the socket
109
	 * and returns it.
110
	 */
111
	SocketDescriptor Steal() noexcept;
112

113
	void Close() noexcept;
114

115
	unsigned GetScheduledFlags() const noexcept {
116 117
		assert(IsDefined());

118
		return scheduled_flags;
119 120
	}

121 122 123 124
	void SetReadyFlags(unsigned flags) noexcept {
		ready_flags = flags;
	}

125 126 127 128 129
	/**
	 * @return true on success, false on error (with errno set if
	 * USE_EPOLL is defined)
	 */
	bool Schedule(unsigned flags) noexcept;
130

131
	void Cancel() noexcept {
132
		Schedule(0);
133 134
	}

135
	bool ScheduleRead() noexcept {
136
		return Schedule(GetScheduledFlags() | READ);
137 138
	}

139 140
	bool ScheduleWrite() noexcept {
		return Schedule(GetScheduledFlags() | WRITE);
141 142
	}

143
	void CancelRead() noexcept {
144
		Schedule(GetScheduledFlags() & ~READ);
145 146
	}

147
	void CancelWrite() noexcept {
148
		Schedule(GetScheduledFlags() & ~WRITE);
149 150 151
	}

protected:
152 153 154
	/**
	 * @return false if the socket has been closed
	 */
155
	virtual bool OnSocketReady(unsigned flags) noexcept = 0;
156 157

public:
158
	void Dispatch() noexcept;
159 160 161
};

#endif