summaryrefslogtreecommitdiffstats
path: root/src/Net/FdManager.cpp
blob: ffa4d8bbc11532204ff237fab766f271d82610af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * FdManager.cpp
 *
 * Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de>
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

#include "FdManager.h"
#include <signal.h>
#include <unistd.h>
#include <sys/fcntl.h>

#include <sigc++/adaptors/hide.h>


namespace Mad {
namespace Net {

FdManager FdManager::fdManager;


FdManager::FdManager() {
  pipe(interruptPipe);

  int flags = fcntl(interruptPipe[0], F_GETFL, 0);
  fcntl(interruptPipe[0], F_SETFL, flags | O_NONBLOCK);

  flags = fcntl(interruptPipe[1], F_GETFL, 0);
  fcntl(interruptPipe[1], F_SETFL, flags | O_NONBLOCK);

  registerFd(interruptPipe[0], sigc::hide(sigc::mem_fun(this, &FdManager::readInterrupt)), POLLIN);
}

FdManager::~FdManager() {
  unregisterFd(interruptPipe[0]);

  close(interruptPipe[0]);
  close(interruptPipe[1]);
}


bool FdManager::registerFd(int fd, const sigc::slot<void, short> &handler, short events) {
  struct pollfd pollfd = {fd, events, 0};

  pollfds.insert(std::make_pair(fd, pollfd));

  return handlers.insert(std::make_pair(fd, handler)).second;
}

bool FdManager::unregisterFd(int fd) {
  pollfds.erase(fd);
  return handlers.erase(fd);
}

bool FdManager::setFdEvents(int fd, short events) {
  std::map<int, struct pollfd>::iterator pollfd = pollfds.find(fd);

  if(pollfd == pollfds.end())
    return false;

  if(pollfd->second.events != events) {
    pollfd->second.events = events;
    interrupt();
  }

  return true;
}

short FdManager::getFdEvents(int fd) const {
  std::map<int, struct pollfd>::const_iterator pollfd = pollfds.find(fd);

  if(pollfd == pollfds.end())
    return -1;

  return pollfd->second.events;
}

void FdManager::readInterrupt() {
  char buf[20];

  while(read(interruptPipe[0], buf, sizeof(buf)) > 0) {}
}

void FdManager::interrupt() {
  char buf = 0;

  write(interruptPipe[1], &buf, sizeof(buf));
}

void FdManager::run() {
  readInterrupt();

  size_t count = pollfds.size();
  struct pollfd *fdarray = new struct pollfd[count];

  std::map<int, struct pollfd>::iterator pollfd = pollfds.begin();

  for(size_t n = 0; n < count; ++n) {
    fdarray[n] = pollfd->second;
    ++pollfd;
  }

  if(poll(fdarray, count, -1) > 0) {
    for(size_t n = 0; n < count; ++n) {
      if(fdarray[n].revents)
        handlers[fdarray[n].fd](fdarray[n].revents);
    }
  }

  delete [] fdarray;
}

}
}