]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/selector.h
d39ada012c5c96e274ca08ef941c60a85f6b83a8
[apple/security.git] / cdsa / cdsa_utilities / selector.h
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19 //
20 // selector - I/O stream multiplexing
21 //
22 #ifndef _H_SELECTOR
23 #define _H_SELECTOR
24
25 #include <Security/utilities.h>
26 #include <Security/fdsel.h>
27 #include "timeflow.h"
28 #include <sys/types.h>
29 #include <cstdio>
30 #include <cstdarg>
31 #include <map>
32 #include <Security/debugging.h>
33
34
35 namespace Security {
36 namespace UnixPlusPlus {
37
38
39 //
40 // A Selector is an I/O dispatch facility that can supervise any number of "file descriptors",
41 // each of which can perform I/O. Obviously this is geared towards the UNIX facility.
42 //
43 class Selector {
44 class Client; friend class Client;
45 public:
46 Selector();
47 virtual ~Selector();
48
49 //@@@ preliminary interface
50 void operator () (); // run just once (now)
51 void operator () (Time::Absolute stopTime);
52 void operator () (Time::Interval duration)
53 { (*this)(Time::now() + duration); }
54
55 typedef unsigned int Type;
56 static const Type none = 0x00;
57 static const Type input = 0x01;
58 static const Type output = 0x02;
59 static const Type critical = 0x04;
60 static const Type all = input | output | critical;
61
62 public:
63 class Client {
64 typedef Selector::Type Type;
65 friend class Selector;
66 public:
67 Client() : mSelector(NULL) { }
68 virtual void notify(int fd, Type type) = 0;
69 virtual ~Client() { }
70
71 bool isActive() const { return mSelector != NULL; }
72
73 static const Type input = Selector::input;
74 static const Type output = Selector::output;
75 static const Type critical = Selector::critical;
76
77 protected:
78 void events(Type type) { mSelector->set(mFd, type); mEvents = type; }
79 Type events() const { return mEvents; }
80
81 void enable(Type type) { events(events() | type); }
82 void disable(Type type) { events(events() & ~type); }
83
84 template <class Sel> Sel &selectorAs()
85 { assert(mSelector); return safer_cast<Sel &>(*mSelector); }
86
87 private:
88 int mFd;
89 Selector *mSelector;
90 Type mEvents;
91 };
92
93 void add(int fd, Client &client, Type type = all);
94 void remove(int fd);
95 bool isEmpty() const { return clientMap.empty(); }
96
97 private:
98 void set(int fd, Type type); // (re)set mask for one client
99
100 void singleStep(Time::Interval maxWait);
101
102 private:
103 unsigned int fdSetSize; // number of fd_masks allocated in FDSets
104 int fdMin, fdMax; // highest/lowest fds in use
105 FDSet inSet, outSet, errSet; // current in/out/error select masks
106
107 private:
108 typedef map<int, Client *> ClientMap;
109 ClientMap clientMap;
110 };
111
112
113 } // end namespace UnixPlusPlus
114 } // end namespace Security
115
116
117 #endif //_H_SELECTOR