]>
git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_utilities/lib/selector.h
2 * Copyright (c) 2000-2004,2011,2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 // selector - I/O stream multiplexing
31 #include <security_utilities/utilities.h>
32 #include <security_utilities/fdsel.h>
34 #include <sys/types.h>
38 #include <security_utilities/debugging.h>
42 namespace UnixPlusPlus
{
46 // A Selector is an I/O dispatch facility that can supervise any number of "file descriptors",
47 // each of which can perform I/O. Obviously this is geared towards the UNIX facility.
51 class Client
; friend class Client
;
56 //@@@ preliminary interface
57 void operator () (); // run just once (now)
58 void operator () (Time::Absolute stopTime
);
59 void operator () (Time::Interval duration
)
60 { (*this)(Time::now() + duration
); }
62 typedef unsigned int Type
;
63 static const Type none
= 0x00;
64 static const Type input
= 0x01;
65 static const Type output
= 0x02;
66 static const Type critical
= 0x04;
67 static const Type all
= input
| output
| critical
;
72 typedef Selector::Type Type
;
73 friend class Selector
;
75 Client() : mSelector(NULL
) { }
76 virtual void notify(int fd
, Type type
) = 0;
79 bool isActive() const { return mSelector
!= NULL
; }
81 static const Type input
= Selector::input
;
82 static const Type output
= Selector::output
;
83 static const Type critical
= Selector::critical
;
86 void events(Type type
) { mSelector
->set(mFd
, type
); mEvents
= type
; }
87 Type
events() const { return mEvents
; }
89 void enable(Type type
) { events(events() | type
); }
90 void disable(Type type
) { events(events() & ~type
); }
92 template <class Sel
> Sel
&selectorAs()
93 { assert(mSelector
); return safer_cast
<Sel
&>(*mSelector
); }
101 void add(int fd
, Client
&client
, Type type
= all
);
103 bool isEmpty() const { return clientMap
.empty(); }
106 void set(int fd
, Type type
); // (re)set mask for one client
108 void singleStep(Time::Interval maxWait
);
111 unsigned int fdSetSize
; // number of fd_masks allocated in FDSets
112 int fdMin
, fdMax
; // highest/lowest fds in use
113 FDSet inSet
, outSet
, errSet
; // current in/out/error select masks
116 typedef map
<int, Client
*> ClientMap
;
121 } // end namespace UnixPlusPlus
122 } // end namespace Security