]>
git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/selector.h
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
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
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.
20 // selector - I/O stream multiplexing
25 #include <Security/utilities.h>
26 #include <Security/fdsel.h>
28 #include <sys/types.h>
32 #include <Security/debugging.h>
36 namespace UnixPlusPlus
{
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.
45 class Client
; friend class Client
;
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
); }
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
;
66 typedef Selector::Type Type
;
67 friend class Selector
;
69 Client() : mSelector(NULL
) { }
70 virtual void notify(int fd
, Type type
) = 0;
73 bool isActive() const { return mSelector
!= NULL
; }
75 static const Type input
= Selector::input
;
76 static const Type output
= Selector::output
;
77 static const Type critical
= Selector::critical
;
80 void events(Type type
) { mSelector
->set(mFd
, type
); mEvents
= type
; }
81 Type
events() const { return mEvents
; }
83 void enable(Type type
) { events(events() | type
); }
84 void disable(Type type
) { events(events() & ~type
); }
86 template <class Sel
> Sel
&selectorAs()
87 { assert(mSelector
); return safer_cast
<Sel
&>(*mSelector
); }
95 void add(int fd
, Client
&client
, Type type
= all
);
97 bool isEmpty() const { return clientMap
.empty(); }
100 void set(int fd
, Type type
); // (re)set mask for one client
102 void singleStep(Time::Interval maxWait
);
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
110 typedef map
<int, Client
*> ClientMap
;
115 } // end namespace UnixPlusPlus
116 } // end namespace Security