Line data Source code
1 :
2 : /* Copyright (c) 2005-2014, Stefan Eilemann <eile@equalizergraphics.com>
3 : *
4 : * This file is part of Collage <https://github.com/Eyescale/Collage>
5 : *
6 : * This library is free software; you can redistribute it and/or modify it under
7 : * the terms of the GNU Lesser General Public License version 2.1 as published
8 : * by the Free Software Foundation.
9 : *
10 : * This library is distributed in the hope that it will be useful, but WITHOUT
11 : * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 : * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 : * details.
14 : *
15 : * You should have received a copy of the GNU Lesser General Public License
16 : * along with this library; if not, write to the Free Software Foundation, Inc.,
17 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 : */
19 :
20 : #ifndef CO_CONNECTION_SET_H
21 : #define CO_CONNECTION_SET_H
22 :
23 : #include <co/api.h>
24 : #include <co/types.h>
25 : #include <lunchbox/thread.h> // for LB_TS_VAR
26 : #include <boost/noncopyable.hpp>
27 :
28 : namespace co
29 : {
30 : namespace detail { class ConnectionSet; }
31 :
32 : /** Handles events on a set of connections. */
33 : class ConnectionSet : public boost::noncopyable
34 : {
35 : public:
36 : enum Event //!< Event types for select()
37 : {
38 : EVENT_NONE = 0, //!< No event has occurred
39 : EVENT_CONNECT, //!< A new connection
40 : EVENT_DISCONNECT, //!< A disconnect
41 : EVENT_DATA, //!< Data can be read
42 : EVENT_TIMEOUT, //!< The selection request timed out
43 : EVENT_INTERRUPT, //!< ConnectionSet::interrupt was called
44 : EVENT_ERROR, //!< A connection signaled an error
45 : EVENT_SELECT_ERROR, //!< An error occurred during select()
46 : EVENT_INVALID_HANDLE, //!< A connection is not select'able
47 : EVENT_ALL
48 : };
49 :
50 : /** Create a new connection set. @version 1.0 */
51 : CO_API ConnectionSet();
52 :
53 : /** Destruct this connection set. @version 1.0 */
54 : CO_API ~ConnectionSet();
55 :
56 : /** @name Managing connections */
57 : //@{
58 : /** Add the connection to this set. Thread-safe. @version 1.0 */
59 : CO_API void addConnection( ConnectionPtr connection );
60 :
61 : /** Remove the connection from this set. Thread-safe. @version 1.0 */
62 : CO_API bool removeConnection( ConnectionPtr connection );
63 :
64 : /** @return the number of managed connections. Thread-safe. @version 1.0 */
65 : CO_API size_t getSize() const;
66 :
67 : /**
68 : * @return true if this set has no connections. Thread-safe.
69 : * @version 1.0
70 : */
71 : CO_API bool isEmpty() const;
72 :
73 : /** @internal not thread-safe. */
74 : CO_API const Connections& getConnections() const;
75 : //@}
76 :
77 : /** @name Performing a selection */
78 : //@{
79 : /**
80 : * Select a Connection which is ready for I/O.
81 : *
82 : * Depending on the event, the error number and connection are set.
83 : *
84 : * @param timeout the timeout to wait for an event in milliseconds, or
85 : * LB_TIMEOUT_INDEFINITE if the call should block forever.
86 : * @return The type of the event occured during selection.
87 : * @sa getConnection(), getError()
88 : * @version 1.0
89 : */
90 : CO_API Event select( const uint32_t timeout = LB_TIMEOUT_INDEFINITE );
91 :
92 : /** Interrupt the current or next select call. @version 1.0 */
93 : CO_API void interrupt();
94 :
95 : /**
96 : * @return the error code when the last select() returned EVENT_ERROR.
97 : * @version 1.0
98 : */
99 : CO_API int getError() const;
100 :
101 : /**
102 : * @return the connection of the last select event, may be 0.
103 : * @version 1.0
104 : */
105 : CO_API ConnectionPtr getConnection();
106 :
107 : /** @internal Trigger rebuilding of internal caches. */
108 : void setDirty();
109 : //@}
110 :
111 : private:
112 : detail::ConnectionSet* const _impl;
113 :
114 : void _clear();
115 : bool _setupFDSet();
116 : bool _buildFDSet();
117 :
118 : Event _getSelectResult( const uint32_t index );
119 : Event _parseSelect( const uint32_t index );
120 110 : LB_TS_VAR( _selectThread );
121 : };
122 :
123 : /** @internal */
124 : CO_API std::ostream& operator << ( std::ostream&, const ConnectionSet& );
125 :
126 : /** @internal */
127 : CO_API std::ostream& operator << (std::ostream&,const ConnectionSet::Event);
128 : }
129 :
130 : #endif // CO_CONNECTION_SET_H
|