MPD: event/PollGroupWinSelect.hxx Source File

MPD 0.20.15

PollGroupWinSelect.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_EVENT_POLLGROUP_WINSELECT_HXX
21 #define MPD_EVENT_POLLGROUP_WINSELECT_HXX
22
23 #include "check.h"
24
25 #include "PollResultGeneric.hxx"
26
27 #include <assert.h>
28 #include <string.h>
29
30 #include <unordered_map>
31
32 #include <windows.h>
33 #include <winsock2.h>
34
35 #ifdef ERROR
36 #undef ERROR
37 #endif
38
39 class SocketSet
40 {
41 fd_set set;
42 public:
43 SocketSet() { set.fd_count = 0; }
44 SocketSet(SocketSet &other) {
45 set.fd_count = other.set.fd_count;
46 memcpy(set.fd_array,
47 other.set.fd_array,
48 sizeof (SOCKET) * set.fd_count);
49 }
50
51 fd_set *GetPtr() { return &set; }
52 int Size() { return set.fd_count; }
53 bool IsEmpty() { return set.fd_count == 0; }
54 bool IsFull() { return set.fd_count == FD_SETSIZE; }
55
56 int operator[](int index) {
57 assert(index >= 0 && (u_int)index < set.fd_count);
58 return set.fd_array[index];
59 }
60
61 int Add(int fd) {
62 assert(!IsFull());
63 set.fd_array[set.fd_count] = fd;
64 return set.fd_count++;
65 }
66
67 void MoveToEnd(int index) {
68 assert(index >= 0 && (u_int)index < set.fd_count);
69 std::swap(set.fd_array[index], set.fd_array[set.fd_count - 1]);
70 }
71
72 void RemoveLast() {
73 assert(!IsEmpty());
74 --set.fd_count;
75 }
76 };
77
78 class PollGroupWinSelect
79 {
80 struct Item
81 {
82 int index[2];
83 void *obj;
84 unsigned events;
85 };
86
87 SocketSet event_set[2];
88 std::unordered_map<int, Item> items;
89
90 bool CanModify(Item &item, unsigned events, int event_id);
91 void Modify(Item &item, int fd, unsigned events, int event_id);
92
93 PollGroupWinSelect(PollGroupWinSelect &) = delete;
94 PollGroupWinSelect &operator=(PollGroupWinSelect &) = delete;
95 public:
96 static constexpr unsigned READ = 1;
97 static constexpr unsigned WRITE = 2;
98 static constexpr unsigned ERROR = 0;
99 static constexpr unsigned HANGUP = 0;
100
101 PollGroupWinSelect();
102 ~PollGroupWinSelect();
103
104 void ReadEvents(PollResultGeneric &result, int timeout_ms);
105 bool Add(int fd, unsigned events, void *obj);
106 bool Modify(int fd, unsigned events, void *obj);
107 bool Remove(int fd);
108 bool Abandon(int fd) { return Remove(fd); }
109 };
110
111 #endif
check.h
PollGroupWinSelect::HANGUP
static constexpr unsigned HANGUP
Definition: PollGroupWinSelect.hxx:99
SocketSet::operator[]
int operator[](int index)
Definition: PollGroupWinSelect.hxx:56
PollGroupWinSelect::ReadEvents
void ReadEvents(PollResultGeneric &result, int timeout_ms)
SocketSet::IsFull
bool IsFull()
Definition: PollGroupWinSelect.hxx:54
PollGroupWinSelect::WRITE
static constexpr unsigned WRITE
Definition: PollGroupWinSelect.hxx:97
PollGroupWinSelect
Definition: PollGroupWinSelect.hxx:78
PollGroupWinSelect::Remove
bool Remove(int fd)
SocketSet::GetPtr
fd_set * GetPtr()
Definition: PollGroupWinSelect.hxx:51
PollGroupWinSelect::Abandon
bool Abandon(int fd)
Definition: PollGroupWinSelect.hxx:108
PollResultGeneric.hxx
PollResultGeneric
Definition: PollResultGeneric.hxx:27
SocketSet::SocketSet
SocketSet(SocketSet &other)
Definition: PollGroupWinSelect.hxx:44
SocketSet::RemoveLast
void RemoveLast()
Definition: PollGroupWinSelect.hxx:72
SocketSet::Size
int Size()
Definition: PollGroupWinSelect.hxx:52
SocketSet::Add
int Add(int fd)
Definition: PollGroupWinSelect.hxx:61
PollGroupWinSelect::Add
bool Add(int fd, unsigned events, void *obj)
PollGroupWinSelect::PollGroupWinSelect
PollGroupWinSelect()
SocketSet::SocketSet
SocketSet()
Definition: PollGroupWinSelect.hxx:43
SocketSet::MoveToEnd
void MoveToEnd(int index)
Definition: PollGroupWinSelect.hxx:67
PollGroupWinSelect::~PollGroupWinSelect
~PollGroupWinSelect()
SocketSet
Definition: PollGroupWinSelect.hxx:39
SocketSet::IsEmpty
bool IsEmpty()
Definition: PollGroupWinSelect.hxx:53
PollGroupWinSelect::ERROR
static constexpr unsigned ERROR
Definition: PollGroupWinSelect.hxx:98
PollGroupWinSelect::READ
static constexpr unsigned READ
Definition: PollGroupWinSelect.hxx:96