MPD  0.20.18
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 
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 
72  :fd(-1), loop(_loop), scheduled_flags(0) {}
73 
74  SocketMonitor(int _fd, EventLoop &_loop)
75  :fd(_fd), loop(_loop), scheduled_flags(0) {}
76 
78 
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() {
122  }
123 
124  void ScheduleWrite() {
126  }
127 
128  void CancelRead() {
130  }
131 
132  void CancelWrite() {
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
void Dispatch(unsigned flags)
static constexpr unsigned WRITE
SocketMonitor(int _fd, EventLoop &_loop)
ssize_t Read(void *data, size_t length)
virtual bool OnSocketReady(unsigned flags)=0
SocketMonitor(EventLoop &_loop)
An event loop that polls for events on file/socket descriptors.
Definition: Loop.hxx:52
std::make_signed< size_t >::type ssize_t
void Schedule(unsigned flags)
int Get() const
ssize_t Write(const void *data, size_t length)
void Abandon()
Somebody has closed the socket.
int Steal()
"Steal" the socket descriptor.
bool IsDefined() const
static constexpr unsigned ERROR
Monitor events on a socket.
static constexpr unsigned HANGUP
EventLoop & GetEventLoop()
static constexpr unsigned READ
void Open(int _fd)
unsigned GetScheduledFlags() const