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 <boost/noncopyable.hpp>
24 : #include <co/api.h>
25 : #include <co/types.h>
26 : #include <lunchbox/thread.h> // for LB_TS_VAR
27 :
28 : namespace co
29 : {
30 : namespace detail
31 : {
32 : class ConnectionSet;
33 : }
34 :
35 : /** Handles events on a set of connections. */
36 : class ConnectionSet : public boost::noncopyable
37 : {
38 : public:
39 : enum Event //!< Event types for select()
40 : {
41 : EVENT_NONE = 0, //!< No event has occurred
42 : EVENT_CONNECT, //!< A new connection
43 : EVENT_DISCONNECT, //!< A disconnect
44 : EVENT_DATA, //!< Data can be read
45 : EVENT_TIMEOUT, //!< The selection request timed out
46 : EVENT_INTERRUPT, //!< ConnectionSet::interrupt was called
47 : EVENT_ERROR, //!< A connection signaled an error
48 : EVENT_SELECT_ERROR, //!< An error occurred during select()
49 : EVENT_INVALID_HANDLE, //!< A connection is not select'able
50 : EVENT_ALL
51 : };
52 :
53 : /** Create a new connection set. @version 1.0 */
54 : CO_API ConnectionSet();
55 :
56 : /** Destruct this connection set. @version 1.0 */
57 : CO_API ~ConnectionSet();
58 :
59 : /** @name Managing connections */
60 : //@{
61 : /** Add the connection to this set. Thread-safe. @version 1.0 */
62 : CO_API void addConnection(ConnectionPtr connection);
63 :
64 : /** Remove the connection from this set. Thread-safe. @version 1.0 */
65 : CO_API bool removeConnection(ConnectionPtr connection);
66 :
67 : /** @return the number of managed connections. Thread-safe. @version 1.0 */
68 : CO_API size_t getSize() const;
69 :
70 : /**
71 : * @return true if this set has no connections. Thread-safe.
72 : * @version 1.0
73 : */
74 : CO_API bool isEmpty() const;
75 :
76 : /** @internal not thread-safe. */
77 : CO_API const Connections& getConnections() const;
78 : //@}
79 :
80 : /** @name Performing a selection */
81 : //@{
82 : /**
83 : * Select a Connection which is ready for I/O.
84 : *
85 : * Depending on the event, the error number and connection are set.
86 : *
87 : * @param timeout the timeout to wait for an event in milliseconds, or
88 : * LB_TIMEOUT_INDEFINITE if the call should block forever.
89 : * @return The type of the event occured during selection.
90 : * @sa getConnection(), getError()
91 : * @version 1.0
92 : */
93 : CO_API Event select(const uint32_t timeout = LB_TIMEOUT_INDEFINITE);
94 :
95 : /** Interrupt the current or next select call. @version 1.0 */
96 : CO_API void interrupt();
97 :
98 : /**
99 : * @return the error code when the last select() returned EVENT_ERROR.
100 : * @version 1.0
101 : */
102 : CO_API int getError() const;
103 :
104 : /**
105 : * @return the connection of the last select event, may be 0.
106 : * @version 1.0
107 : */
108 : CO_API ConnectionPtr getConnection();
109 :
110 : /** @internal Trigger rebuilding of internal caches. */
111 : void setDirty();
112 : //@}
113 :
114 : private:
115 : detail::ConnectionSet* const _impl;
116 :
117 : void _clear();
118 : bool _setupFDSet();
119 : bool _buildFDSet();
120 :
121 : Event _getSelectResult(const uint32_t index);
122 : Event _parseSelect(const uint32_t index);
123 106 : LB_TS_VAR(_selectThread);
124 : };
125 :
126 : /** @internal */
127 : CO_API std::ostream& operator<<(std::ostream&, const ConnectionSet&);
128 :
129 : /** @internal */
130 : CO_API std::ostream& operator<<(std::ostream&, const ConnectionSet::Event);
131 : }
132 :
133 : #endif // CO_CONNECTION_SET_H
|