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

#include "check.h"
24
#include "PollGroup.hxx"
25
#include "net/SocketDescriptor.hxx"
26

27 28
#include <type_traits>

29
#include <assert.h>
30
#include <stddef.h>
31 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
	SocketDescriptor fd = SocketDescriptor::Undefined();
49
	EventLoop &loop;
50 51 52 53

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

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

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

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

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

70
	~SocketMonitor() noexcept;
71

72
	EventLoop &GetEventLoop() noexcept {
73 74 75
		return loop;
	}

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

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

		return fd;
	}

86
	void Open(SocketDescriptor _fd) noexcept;
87

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

94
	void Close() noexcept;
95

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

99
		return scheduled_flags;
100 101
	}

102
	void Schedule(unsigned flags) noexcept;
103

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

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

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

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

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

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

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

#endif