]>
git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/selector.h
d39ada012c5c96e274ca08ef941c60a85f6b83a8
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.
44 class Client
; friend class Client
;
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
); }
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
;
64 typedef Selector::Type Type
;
65 friend class Selector
;
67 Client() : mSelector(NULL
) { }
68 virtual void notify(int fd
, Type type
) = 0;
71 bool isActive() const { return mSelector
!= NULL
; }
73 static const Type input
= Selector::input
;
74 static const Type output
= Selector::output
;
75 static const Type critical
= Selector::critical
;
78 void events(Type type
) { mSelector
->set(mFd
, type
); mEvents
= type
; }
79 Type
events() const { return mEvents
; }
81 void enable(Type type
) { events(events() | type
); }
82 void disable(Type type
) { events(events() & ~type
); }
84 template <class Sel
> Sel
&selectorAs()
85 { assert(mSelector
); return safer_cast
<Sel
&>(*mSelector
); }
93 void add(int fd
, Client
&client
, Type type
= all
);
95 bool isEmpty() const { return clientMap
.empty(); }
98 void set(int fd
, Type type
); // (re)set mask for one client
100 void singleStep(Time::Interval maxWait
);
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
108 typedef map
<int, Client
*> ClientMap
;
113 } // end namespace UnixPlusPlus
114 } // end namespace Security