aboutsummaryrefslogtreecommitdiffstats
path: root/lib/iowatch.h
blob: 12abb1f1c0485faa33feff9b6e5676f047523a2f (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
#ifndef _iowatch_h
#define _iowatch_h

/**
 * A simple wrapper for select()
 *
 * IOWatch is a simple wrapper for the select
 * system call. In particular, it takes care
 * of passing the maximum file descriptor
 * argument (arg 1) of select() correctly.
 * IOWatch handles select on read descriptors only.
 */
class IOWatch {
public:
	/**
	 * Creates a new instance.
	 */
	IOWatch();

	/**
	 * Destroys an instance.
	 */
	~IOWatch();
 
	/**
	 * Adds a file descriptor to
	 * the set of descriptors.
	 *
	 * @param fd The file descriptor to add.
	 */
	void addIO(const int fd);

	/**
	 * Removes a file descriptor from the
	 * set of descriptors.
	 *
	 * @param fd The file descriptor to remove.
	 */
	void remIO(const int fd);

	/**
	 * Performs a select() call.
	 *
	 * @param secs Number of seconds to wait.
	 * @param usecs Number of microseconds to wait.
	 *
	 * @return true, if any of the descriptors is
	 * 	readable.
	 */
	bool watch(const long secs, const long usecs);

private:
	int num;
	int *io;
};

#endif