MPD 0.20.6

- Main Page
- Namespaces
- Data Structures
- Files

- File List
- Globals

SocketMonitor.hxx
Go to the documentation of this file.
1 /*
2 * Copyright 2003-2017 The Music Player Daemon Project
3 * http://www.musicpd.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef MPD_SOCKET_MONITOR_HXX
21 #define MPD_SOCKET_MONITOR_HXX
22
23 #include "check.h"
24 #include "PollGroup.hxx"
25
26 #include <type_traits>
27
28 #include <assert.h>
29 #include <stddef.h>
30
31 #ifdef WIN32
32 /* ERROR is a WIN32 macro that poisons our namespace; this is a kludge
33 to allow us to use it anyway */
34 #ifdef ERROR
35 #undef ERROR
36 #endif
37 #endif
38
39 class EventLoop;
40
54 class SocketMonitor {
55 int fd;
56 EventLoop &loop;
57
61 unsigned scheduled_flags;
62
63 public:
64 static constexpr unsigned READ = PollGroup::READ;
65 static constexpr unsigned WRITE = PollGroup::WRITE;
66 static constexpr unsigned ERROR = PollGroup::ERROR;
67 static constexpr unsigned HANGUP = PollGroup::HANGUP;
68
69 typedef std::make_signed<size_t>::type ssize_t;
70
71 SocketMonitor(EventLoop &_loop)
72 :fd(-1), loop(_loop), scheduled_flags(0) {}
73
74 SocketMonitor(int _fd, EventLoop &_loop)
75 :fd(_fd), loop(_loop), scheduled_flags(0) {}
76
77 ~SocketMonitor();
78
79 EventLoop &GetEventLoop() {
80 return loop;
81 }
82
83 bool IsDefined() const {
84 return fd >= 0;
85 }
86
87 int Get() const {
88 assert(IsDefined());
89
90 return fd;
91 }
92
93 void Open(int _fd);
94
99 int Steal();
100
104 void Abandon();
105
106 void Close();
107
108 unsigned GetScheduledFlags() const {
109 assert(IsDefined());
110
111 return scheduled_flags;
112 }
113
114 void Schedule(unsigned flags);
115
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 ssize_t Read(void *data, size_t length);
137 ssize_t Write(const void *data, size_t length);
138
139 protected:
143 virtual bool OnSocketReady(unsigned flags) = 0;
144
145 public:
146 void Dispatch(unsigned flags);
147 };
148
149 #endif
SocketMonitor::Dispatch
void Dispatch(unsigned flags)
check.h
SocketMonitor::WRITE
static constexpr unsigned WRITE
Definition: SocketMonitor.hxx:65
SocketMonitor::SocketMonitor
SocketMonitor(int _fd, EventLoop &_loop)
Definition: SocketMonitor.hxx:74
SocketMonitor::GetScheduledFlags
unsigned GetScheduledFlags() const
Definition: SocketMonitor.hxx:108
SocketMonitor::Get
int Get() const
Definition: SocketMonitor.hxx:87
SocketMonitor::Read
ssize_t Read(void *data, size_t length)
SocketMonitor::OnSocketReady
virtual bool OnSocketReady(unsigned flags)=0
SocketMonitor::SocketMonitor
SocketMonitor(EventLoop &_loop)
Definition: SocketMonitor.hxx:71
EventLoop
An event loop that polls for events on file/socket descriptors.
Definition: Loop.hxx:51
SocketMonitor::ScheduleWrite
void ScheduleWrite()
Definition: SocketMonitor.hxx:124
SocketMonitor::ssize_t
std::make_signed< size_t >::type ssize_t
Definition: SocketMonitor.hxx:69
SocketMonitor::Schedule
void Schedule(unsigned flags)
SocketMonitor::Write
ssize_t Write(const void *data, size_t length)
SocketMonitor::Abandon
void Abandon()
Somebody has closed the socket.
SocketMonitor::IsDefined
bool IsDefined() const
Definition: SocketMonitor.hxx:83
SocketMonitor::Cancel
void Cancel()
Definition: SocketMonitor.hxx:116
SocketMonitor::Steal
int Steal()
"Steal" the socket descriptor.
SocketMonitor::ERROR
static constexpr unsigned ERROR
Definition: SocketMonitor.hxx:66
SocketMonitor::ScheduleRead
void ScheduleRead()
Definition: SocketMonitor.hxx:120
SocketMonitor
Monitor events on a socket.
Definition: SocketMonitor.hxx:54
SocketMonitor::HANGUP
static constexpr unsigned HANGUP
Definition: SocketMonitor.hxx:67
SocketMonitor::GetEventLoop
EventLoop & GetEventLoop()
Definition: SocketMonitor.hxx:79
SocketMonitor::READ
static constexpr unsigned READ
Definition: SocketMonitor.hxx:64
PollGroup.hxx
SocketMonitor::Open
void Open(int _fd)
SocketMonitor::CancelRead
void CancelRead()
Definition: SocketMonitor.hxx:128
SocketMonitor::~SocketMonitor
~SocketMonitor()
SocketMonitor::Close
void Close()
SocketMonitor::CancelWrite
void CancelWrite()
Definition: SocketMonitor.hxx:132