]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/selector.h
Security-54.1.3.tar.gz
[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 public:
45 class Client; friend class Client;
46
47 Selector();
48 virtual ~Selector();
49
50 //@@@ preliminary interface
51 void operator () (); // run just once (now)
52 void operator () (Time::Absolute stopTime);
53 void operator () (Time::Interval duration)
54 { (*this)(Time::now() + duration); }
55
56 typedef unsigned int Type;
57 static const Type none = 0x00;
58 static const Type input = 0x01;
59 static const Type output = 0x02;
60 static const Type critical = 0x04;
61 static const Type all = input | output | critical;
62
63 public:
64 class Client {
65 public:
66 typedef Selector::Type Type;
67 friend class Selector;
68
69 Client() : mSelector(NULL) { }
70 virtual void notify(int fd, Type type) = 0;
71 virtual ~Client() { }
72
73 bool isActive() const { return mSelector != NULL; }
74
75 static const Type input = Selector::input;
76 static const Type output = Selector::output;
77 static const Type critical = Selector::critical;
78
79 protected:
80 void events(Type type) { mSelector->set(mFd, type); mEvents = type; }
81 Type events() const { return mEvents; }
82
83 void enable(Type type) { events(events() | type); }
84 void disable(Type type) { events(events() & ~type); }
85
86 template <class Sel> Sel &selectorAs()
87 { assert(mSelector); return safer_cast<Sel &>(*mSelector); }
88
89 private:
90 int mFd;
91 Selector *mSelector;
92 Type mEvents;
93 };
94
95 void add(int fd, Client &client, Type type = all);
96 void remove(int fd);
97 bool isEmpty() const { return clientMap.empty(); }
98
99 private:
100 void set(int fd, Type type); // (re)set mask for one client
101
102 void singleStep(Time::Interval maxWait);
103
104 private:
105 unsigned int fdSetSize; // number of fd_masks allocated in FDSets
106 int fdMin, fdMax; // highest/lowest fds in use
107 FDSet inSet, outSet, errSet; // current in/out/error select masks
108
109 private:
110 typedef map<int, Client *> ClientMap;
111 ClientMap clientMap;
112 };
113
114
115 } // end namespace UnixPlusPlus
116 } // end namespace Security
117
118
119 #endif //_H_SELECTOR