Statistics
| Branch: | Tag: | Revision:

dvbd / select.cpp @ bfdb7446

History | View | Annotate | Download (2.01 KB)

1
/*
2
  Copyright 2003 John Knottenbelt
3
  
4
  This program is free software; you can redistribute it and/or modify
5
  it under the terms of the GNU General Public License as published by
6
  the Free Software Foundation; either version 2 of the License, or
7
  (at your option) any later version.
8
 
9
  This program is distributed in the hope that it will be useful,
10
  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
  GNU General Public License for more details.
13
 
14
  You should have received a copy of the GNU General Public License
15
  along with this program; if not, write to the Free Software
16
  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17
*/
18

    
19
#include "config.h"
20
#include "select.h"
21

    
22
#include <cstdlib>
23

    
24
Select::Select() {
25
  clearFDs();
26
};
27

    
28
void Select::clearFDs() {
29
  FD_ZERO(&readfds);
30
  FD_ZERO(&writefds);
31
  FD_ZERO(&exceptfds);
32
  maxFD = -1;
33
  tv_sec = -1;
34
  tv_usec = -1;
35
};
36

    
37
void Select::addReadFD(int fd) {
38
  FD_SET(fd, &readfds);
39
  setMaxFD(fd);
40
};
41

    
42
void Select::addWriteFD(int fd) {
43
  FD_SET(fd, &writefds);
44
  setMaxFD(fd);
45
};
46

    
47
void Select::addExceptFD(int fd) {
48
  FD_SET(fd, &exceptfds);
49
  setMaxFD(fd);
50
};
51

    
52
void Select::addAlarm(long sec, long usec)
53
{
54
  if (tv_sec == -1 && tv_usec == -1) {
55
    tv_sec = sec;
56
    tv_usec = usec;
57
  }
58
  else if (sec < tv_sec) {
59
    tv_sec = sec;
60
    tv_usec = usec;
61
  }
62
  else if (sec == tv_sec && usec < tv_usec) 
63
    tv_usec = usec;
64
}
65

    
66
int Select::wait() {
67
  
68
  if (tv_sec == -1 && tv_usec == -1)
69
    return select(maxFD + 1, &readfds, &writefds, &exceptfds, NULL);
70

    
71
  struct timeval tv;
72
  tv.tv_sec = tv_sec;
73
  tv.tv_usec = tv_usec;
74

    
75
  return select(maxFD + 1, &readfds, &writefds, &exceptfds, &tv);
76
};
77

    
78
bool Select::readyToRead(int fd) const {
79
  return FD_ISSET(fd, &readfds);
80
};
81

    
82
bool Select::readyToWrite(int fd) const {
83
  return FD_ISSET(fd, &writefds);
84
};
85

    
86
bool Select::readyToExcept(int fd) const {
87
  return FD_ISSET(fd, &exceptfds);
88
};
89

    
90
void Select::setMaxFD(int fd) {
91
  if (fd > maxFD)
92
    maxFD = fd;
93
}