2013-01-10 19:05:47 +01:00
|
|
|
/*
|
|
|
|
* 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"
|
|
|
|
|
2013-08-07 22:16:59 +02:00
|
|
|
#ifdef USE_EPOLL
|
|
|
|
#include <sys/epoll.h>
|
|
|
|
#else
|
2013-01-10 19:05:47 +01:00
|
|
|
#include <glib.h>
|
2013-08-07 22:16:59 +02:00
|
|
|
#endif
|
2013-01-10 19:05:47 +01:00
|
|
|
|
2013-01-30 10:39:17 +01:00
|
|
|
#include <type_traits>
|
|
|
|
|
2013-01-10 19:05:47 +01:00
|
|
|
#include <assert.h>
|
2013-01-30 10:39:17 +01:00
|
|
|
#include <stddef.h>
|
2013-01-10 19:05:47 +01:00
|
|
|
|
|
|
|
#ifdef WIN32
|
2013-11-24 16:07:12 +01:00
|
|
|
/* ERROR is a WIN32 macro that poisons our namespace; this is a kludge
|
|
|
|
to allow us to use it anyway */
|
2013-01-10 19:05:47 +01:00
|
|
|
#ifdef ERROR
|
|
|
|
#undef ERROR
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
class EventLoop;
|
|
|
|
|
|
|
|
class SocketMonitor {
|
2013-08-07 22:16:59 +02:00
|
|
|
#ifdef USE_EPOLL
|
|
|
|
#else
|
2013-01-10 19:05:47 +01:00
|
|
|
struct Source {
|
|
|
|
GSource base;
|
|
|
|
|
|
|
|
SocketMonitor *monitor;
|
|
|
|
};
|
2013-08-07 22:16:59 +02:00
|
|
|
#endif
|
2013-01-10 19:05:47 +01:00
|
|
|
|
|
|
|
int fd;
|
|
|
|
EventLoop &loop;
|
2013-08-07 22:16:59 +02:00
|
|
|
|
|
|
|
#ifdef USE_EPOLL
|
|
|
|
/**
|
|
|
|
* A bit mask of events that is currently registered in the EventLoop.
|
|
|
|
*/
|
|
|
|
unsigned scheduled_flags;
|
|
|
|
#else
|
2013-01-10 19:05:47 +01:00
|
|
|
Source *source;
|
|
|
|
GPollFD poll;
|
2013-08-07 22:16:59 +02:00
|
|
|
#endif
|
2013-01-10 19:05:47 +01:00
|
|
|
|
|
|
|
public:
|
2013-08-07 22:16:59 +02:00
|
|
|
#ifdef USE_EPOLL
|
|
|
|
static constexpr unsigned READ = EPOLLIN;
|
|
|
|
static constexpr unsigned WRITE = EPOLLOUT;
|
|
|
|
static constexpr unsigned ERROR = EPOLLERR;
|
|
|
|
static constexpr unsigned HANGUP = EPOLLHUP;
|
|
|
|
#else
|
2013-01-10 19:05:47 +01:00
|
|
|
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;
|
2013-08-07 22:16:59 +02:00
|
|
|
#endif
|
2013-01-10 19:05:47 +01:00
|
|
|
|
2013-01-30 10:39:17 +01:00
|
|
|
typedef std::make_signed<size_t>::type ssize_t;
|
|
|
|
|
2013-08-07 22:16:59 +02:00
|
|
|
#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
|
2013-01-15 22:48:38 +01:00
|
|
|
SocketMonitor(EventLoop &_loop)
|
|
|
|
:fd(-1), loop(_loop), source(nullptr) {}
|
|
|
|
|
2013-01-10 19:05:47 +01:00
|
|
|
SocketMonitor(int _fd, EventLoop &_loop);
|
2013-08-07 22:16:59 +02:00
|
|
|
#endif
|
2013-01-10 19:05:47 +01:00
|
|
|
|
|
|
|
~SocketMonitor();
|
|
|
|
|
2013-08-08 21:49:17 +02:00
|
|
|
EventLoop &GetEventLoop() {
|
|
|
|
return loop;
|
|
|
|
}
|
|
|
|
|
2013-01-10 19:05:47 +01:00
|
|
|
bool IsDefined() const {
|
|
|
|
return fd >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Get() const {
|
|
|
|
assert(IsDefined());
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2013-01-15 22:48:38 +01:00
|
|
|
void Open(int _fd);
|
|
|
|
|
2013-01-27 22:37:01 +01:00
|
|
|
/**
|
|
|
|
* "Steal" the socket descriptor. This abandons the socket
|
|
|
|
* and puts the responsibility for closing it to the caller.
|
|
|
|
*/
|
|
|
|
int Steal();
|
|
|
|
|
2013-11-06 18:20:51 +01:00
|
|
|
/**
|
|
|
|
* Somebody has closed the socket. Unregister this object.
|
|
|
|
*/
|
|
|
|
void Abandon();
|
|
|
|
|
2013-01-10 19:05:47 +01:00
|
|
|
void Close();
|
|
|
|
|
2013-08-07 23:57:30 +02:00
|
|
|
unsigned GetScheduledFlags() const {
|
2013-08-07 23:56:49 +02:00
|
|
|
assert(IsDefined());
|
|
|
|
|
2013-08-07 22:16:59 +02:00
|
|
|
#ifdef USE_EPOLL
|
|
|
|
return scheduled_flags;
|
|
|
|
#else
|
2013-08-07 23:57:30 +02:00
|
|
|
return poll.events;
|
2013-08-07 22:16:59 +02:00
|
|
|
#endif
|
2013-01-10 19:05:47 +01:00
|
|
|
}
|
|
|
|
|
2013-08-07 23:57:30 +02:00
|
|
|
void Schedule(unsigned flags);
|
|
|
|
|
2013-01-10 19:05:47 +01:00
|
|
|
void Cancel() {
|
2013-08-07 23:57:30 +02:00
|
|
|
Schedule(0);
|
2013-01-10 19:05:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScheduleRead() {
|
2013-08-07 23:57:30 +02:00
|
|
|
Schedule(GetScheduledFlags() | READ | HANGUP | ERROR);
|
2013-01-10 19:05:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScheduleWrite() {
|
2013-08-07 23:57:30 +02:00
|
|
|
Schedule(GetScheduledFlags() | WRITE);
|
2013-01-10 19:05:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CancelRead() {
|
2013-08-07 23:57:30 +02:00
|
|
|
Schedule(GetScheduledFlags() & ~(READ|HANGUP|ERROR));
|
2013-01-10 19:05:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CancelWrite() {
|
2013-08-07 23:57:30 +02:00
|
|
|
Schedule(GetScheduledFlags() & ~WRITE);
|
2013-01-10 19:05:47 +01:00
|
|
|
}
|
|
|
|
|
2013-01-30 10:39:17 +01:00
|
|
|
ssize_t Read(void *data, size_t length);
|
|
|
|
ssize_t Write(const void *data, size_t length);
|
|
|
|
|
2013-01-10 19:05:47 +01:00
|
|
|
protected:
|
2013-01-30 10:53:32 +01:00
|
|
|
/**
|
|
|
|
* @return false if the socket has been closed
|
|
|
|
*/
|
|
|
|
virtual bool OnSocketReady(unsigned flags) = 0;
|
2013-01-10 19:05:47 +01:00
|
|
|
|
|
|
|
public:
|
2013-08-07 22:16:59 +02:00
|
|
|
#ifdef USE_EPOLL
|
|
|
|
void Dispatch(unsigned flags);
|
|
|
|
#else
|
2013-01-10 19:05:47 +01:00
|
|
|
/* 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 {
|
2013-08-07 23:56:49 +02:00
|
|
|
assert(IsDefined());
|
|
|
|
|
2013-01-10 19:05:47 +01:00
|
|
|
return (poll.revents & poll.events) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Dispatch() {
|
2013-08-07 23:56:49 +02:00
|
|
|
assert(IsDefined());
|
|
|
|
|
2013-01-10 19:05:47 +01:00
|
|
|
OnSocketReady(poll.revents & poll.events);
|
|
|
|
}
|
2013-08-07 22:16:59 +02:00
|
|
|
#endif
|
2013-01-10 19:05:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|