SocketMonitor.hxx 3.34 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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

26 27
#include <type_traits>

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

#ifdef WIN32
32 33
/* ERROR is a WIN32 macro that poisons our namespace; this is a kludge
   to allow us to use it anyway */
34 35 36 37 38 39 40
#ifdef ERROR
#undef ERROR
#endif
#endif

class EventLoop;

41 42 43 44 45
/**
 * 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.
46
 *
47 48 49
 * This class does not feel responsible for closing the socket.  Call
 * Close() to do it manually.
 *
50 51 52
 * 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.
53
 */
54 55 56
class SocketMonitor {
	int fd;
	EventLoop &loop;
57 58 59 60 61

	/**
	 * A bit mask of events that is currently registered in the EventLoop.
	 */
	unsigned scheduled_flags;
62 63

public:
64 65 66 67
	static constexpr unsigned READ = PollGroup::READ;
	static constexpr unsigned WRITE = PollGroup::WRITE;
	static constexpr unsigned ERROR = PollGroup::ERROR;
	static constexpr unsigned HANGUP = PollGroup::HANGUP;
68

69 70
	typedef std::make_signed<size_t>::type ssize_t;

71 72 73 74 75
	SocketMonitor(EventLoop &_loop)
		:fd(-1), loop(_loop), scheduled_flags(0) {}

	SocketMonitor(int _fd, EventLoop &_loop)
		:fd(_fd), loop(_loop), scheduled_flags(0) {}
76 77 78

	~SocketMonitor();

79 80 81 82
	EventLoop &GetEventLoop() {
		return loop;
	}

83 84 85 86 87 88 89 90 91 92
	bool IsDefined() const {
		return fd >= 0;
	}

	int Get() const {
		assert(IsDefined());

		return fd;
	}

93 94
	void Open(int _fd);

95 96
	/**
	 * "Steal" the socket descriptor.  This abandons the socket
97
	 * and returns it.
98 99 100
	 */
	int Steal();

101 102 103 104 105
	/**
	 * Somebody has closed the socket.  Unregister this object.
	 */
	void Abandon();

106 107
	void Close();

108
	unsigned GetScheduledFlags() const {
109 110
		assert(IsDefined());

111
		return scheduled_flags;
112 113
	}

114 115
	void Schedule(unsigned flags);

116
	void Cancel() {
117
		Schedule(0);
118 119 120
	}

	void ScheduleRead() {
121
		Schedule(GetScheduledFlags() | READ | HANGUP | ERROR);
122 123 124
	}

	void ScheduleWrite() {
125
		Schedule(GetScheduledFlags() | WRITE);
126 127 128
	}

	void CancelRead() {
129
		Schedule(GetScheduledFlags() & ~(READ|HANGUP|ERROR));
130 131 132
	}

	void CancelWrite() {
133
		Schedule(GetScheduledFlags() & ~WRITE);
134 135
	}

136 137 138
	ssize_t Read(void *data, size_t length);
	ssize_t Write(const void *data, size_t length);

139
protected:
140 141 142 143
	/**
	 * @return false if the socket has been closed
	 */
	virtual bool OnSocketReady(unsigned flags) = 0;
144 145

public:
146
	void Dispatch(unsigned flags);
147 148 149
};

#endif