SocketMonitor.hxx 3.78 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * Copyright (C) 2003-2013 The Music Player Daemon Project
 * 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"

25 26 27
#ifdef USE_EPOLL
#include <sys/epoll.h>
#else
28
#include <glib.h>
29
#endif
30

31 32
#include <type_traits>

33
#include <assert.h>
34
#include <stddef.h>
35 36 37 38 39 40 41 42 43 44 45 46

#ifdef WIN32
/* ERRORis a WIN32 macro that poisons our namespace; this is a
   kludge to allow us to use it anyway */
#ifdef ERROR
#undef ERROR
#endif
#endif

class EventLoop;

class SocketMonitor {
47 48
#ifdef USE_EPOLL
#else
49 50 51 52 53
	struct Source {
		GSource base;

		SocketMonitor *monitor;
	};
54
#endif
55 56 57

	int fd;
	EventLoop &loop;
58 59 60 61 62 63 64

#ifdef USE_EPOLL
	/**
	 * A bit mask of events that is currently registered in the EventLoop.
	 */
	unsigned scheduled_flags;
#else
65 66
	Source *source;
	GPollFD poll;
67
#endif
68 69

public:
70 71 72 73 74 75
#ifdef USE_EPOLL
	static constexpr unsigned READ = EPOLLIN;
	static constexpr unsigned WRITE = EPOLLOUT;
	static constexpr unsigned ERROR = EPOLLERR;
	static constexpr unsigned HANGUP = EPOLLHUP;
#else
76 77 78 79
	static constexpr unsigned READ = G_IO_IN;
	static constexpr unsigned WRITE = G_IO_OUT;
	static constexpr unsigned ERROR = G_IO_ERR;
	static constexpr unsigned HANGUP = G_IO_HUP;
80
#endif
81

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

84 85 86 87 88 89 90
#ifdef USE_EPOLL
	SocketMonitor(EventLoop &_loop)
		:fd(-1), loop(_loop), scheduled_flags(0) {}

	SocketMonitor(int _fd, EventLoop &_loop)
		:fd(_fd), loop(_loop), scheduled_flags(0) {}
#else
91 92 93
	SocketMonitor(EventLoop &_loop)
		:fd(-1), loop(_loop), source(nullptr) {}

94
	SocketMonitor(int _fd, EventLoop &_loop);
95
#endif
96 97 98

	~SocketMonitor();

99 100 101 102
	EventLoop &GetEventLoop() {
		return loop;
	}

103 104 105 106 107 108 109 110 111 112
	bool IsDefined() const {
		return fd >= 0;
	}

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

		return fd;
	}

113 114
	void Open(int _fd);

115 116 117 118 119 120
	/**
	 * "Steal" the socket descriptor.  This abandons the socket
	 * and puts the responsibility for closing it to the caller.
	 */
	int Steal();

121 122
	void Close();

123
	unsigned GetScheduledFlags() const {
124 125
		assert(IsDefined());

126 127 128
#ifdef USE_EPOLL
		return scheduled_flags;
#else
129
		return poll.events;
130
#endif
131 132
	}

133 134
	void Schedule(unsigned flags);

135
	void Cancel() {
136
		Schedule(0);
137 138 139
	}

	void ScheduleRead() {
140
		Schedule(GetScheduledFlags() | READ | HANGUP | ERROR);
141 142 143
	}

	void ScheduleWrite() {
144
		Schedule(GetScheduledFlags() | WRITE);
145 146 147
	}

	void CancelRead() {
148
		Schedule(GetScheduledFlags() & ~(READ|HANGUP|ERROR));
149 150 151
	}

	void CancelWrite() {
152
		Schedule(GetScheduledFlags() & ~WRITE);
153 154
	}

155 156 157
	ssize_t Read(void *data, size_t length);
	ssize_t Write(const void *data, size_t length);

158
protected:
159 160 161 162
	/**
	 * @return false if the socket has been closed
	 */
	virtual bool OnSocketReady(unsigned flags) = 0;
163 164

public:
165 166 167
#ifdef USE_EPOLL
	void Dispatch(unsigned flags);
#else
168 169 170 171 172 173 174 175
	/* GSource callbacks */
	static gboolean Prepare(GSource *source, gint *timeout_r);
	static gboolean Check(GSource *source);
	static gboolean Dispatch(GSource *source, GSourceFunc callback,
				 gpointer user_data);

private:
	bool Check() const {
176 177
		assert(IsDefined());

178 179 180 181
		return (poll.revents & poll.events) != 0;
	}

	void Dispatch() {
182 183
		assert(IsDefined());

184 185
		OnSocketReady(poll.revents & poll.events);
	}
186
#endif
187 188 189
};

#endif