SocketMonitor.hxx 3.25 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2018 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 <type_traits>

28
#include <assert.h>
29
#include <stddef.h>
30 31 32

class EventLoop;

33 34 35 36 37
/**
 * 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.
38
 *
39 40 41
 * This class does not feel responsible for closing the socket.  Call
 * Close() to do it manually.
 *
42 43 44
 * 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.
45
 */
46
class SocketMonitor {
47
	SocketDescriptor fd = SocketDescriptor::Undefined();
48
	EventLoop &loop;
49 50 51 52

	/**
	 * A bit mask of events that is currently registered in the EventLoop.
	 */
53
	unsigned scheduled_flags = 0;
54 55

public:
56 57 58 59
	static constexpr unsigned READ = PollGroup::READ;
	static constexpr unsigned WRITE = PollGroup::WRITE;
	static constexpr unsigned ERROR = PollGroup::ERROR;
	static constexpr unsigned HANGUP = PollGroup::HANGUP;
60

61 62
	typedef std::make_signed<size_t>::type ssize_t;

63
	explicit SocketMonitor(EventLoop &_loop) noexcept
64
		:loop(_loop) {}
65

66
	SocketMonitor(SocketDescriptor _fd, EventLoop &_loop) noexcept
67
		:fd(_fd), loop(_loop) {}
68

69
	~SocketMonitor() noexcept;
70

71
	auto &GetEventLoop() const noexcept {
72 73 74
		return loop;
	}

75
	bool IsDefined() const noexcept {
76
		return fd.IsDefined();
77 78
	}

79
	SocketDescriptor GetSocket() const noexcept {
80 81 82 83 84
		assert(IsDefined());

		return fd;
	}

85
	void Open(SocketDescriptor _fd) noexcept;
86

87 88
	/**
	 * "Steal" the socket descriptor.  This abandons the socket
89
	 * and returns it.
90
	 */
91
	SocketDescriptor Steal() noexcept;
92

93
	void Close() noexcept;
94

95
	unsigned GetScheduledFlags() const noexcept {
96 97
		assert(IsDefined());

98
		return scheduled_flags;
99 100
	}

101
	void Schedule(unsigned flags) noexcept;
102

103
	void Cancel() noexcept {
104
		Schedule(0);
105 106
	}

107
	void ScheduleRead() noexcept {
108
		Schedule(GetScheduledFlags() | READ | HANGUP | ERROR);
109 110
	}

111
	void ScheduleWrite() noexcept {
112
		Schedule(GetScheduledFlags() | WRITE);
113 114
	}

115
	void CancelRead() noexcept {
116
		Schedule(GetScheduledFlags() & ~(READ|HANGUP|ERROR));
117 118
	}

119
	void CancelWrite() noexcept {
120
		Schedule(GetScheduledFlags() & ~WRITE);
121 122 123
	}

protected:
124 125 126
	/**
	 * @return false if the socket has been closed
	 */
127
	virtual bool OnSocketReady(unsigned flags) noexcept = 0;
128 129

public:
130
	void Dispatch(unsigned flags) noexcept;
131 132 133
};

#endif