MPD  0.20.18
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 
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 
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 
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
static constexpr unsigned HANGUP
int operator[](int index)
void ReadEvents(PollResultGeneric &result, int timeout_ms)
static constexpr unsigned WRITE
bool Remove(int fd)
fd_set * GetPtr()
SocketSet(SocketSet &other)
int Add(int fd)
bool Add(int fd, unsigned events, void *obj)
void MoveToEnd(int index)
static constexpr unsigned ERROR
static constexpr unsigned READ