Equalizer 1.0

connectionSet.h

00001 
00002 /* Copyright (c) 2005-2011, Stefan Eilemann <eile@equalizergraphics.com> 
00003  *
00004  * This library is free software; you can redistribute it and/or modify it under
00005  * the terms of the GNU Lesser General Public License version 2.1 as published
00006  * by the Free Software Foundation.
00007  *  
00008  * This library is distributed in the hope that it will be useful, but WITHOUT
00009  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00010  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00011  * details.
00012  * 
00013  * You should have received a copy of the GNU Lesser General Public License
00014  * along with this library; if not, write to the Free Software Foundation, Inc.,
00015  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00016  */
00017 
00018 #ifndef CO_CONNECTION_SET_H
00019 #define CO_CONNECTION_SET_H
00020 
00021 #include <co/connectionListener.h> // base class
00022 
00023 #include <co/base/buffer.h> // member
00024 #include <co/base/refPtr.h> // member
00025 #include <co/base/thread.h> // for EQ_TS_VAR
00026 
00027 #ifndef _WIN32
00028 #  include <poll.h>
00029 #endif
00030 
00031 namespace co
00032 {
00033     class EventConnection;
00034     class ConnectionSetThread;
00035 
00041     class ConnectionSet : public ConnectionListener
00042     {
00043     public:
00044         enum Event
00045         {
00046             EVENT_NONE = 0,        
00047             EVENT_CONNECT,         
00048             EVENT_DISCONNECT,      
00049             EVENT_DATA,            
00050             EVENT_TIMEOUT,         
00051             EVENT_INTERRUPT,       
00052             EVENT_ERROR,           
00053             EVENT_SELECT_ERROR,    
00054             EVENT_INVALID_HANDLE,  
00055             EVENT_ALL
00056         };
00057 
00058         CO_API ConnectionSet();
00059         CO_API ~ConnectionSet();
00060 
00061         CO_API void addConnection( ConnectionPtr connection );
00062         CO_API bool removeConnection( ConnectionPtr connection );
00063         CO_API void clear();
00064         size_t getSize()  const { return _connections.size(); }
00065         bool   isEmpty() const { return _connections.empty(); }
00066 
00067         const Connections& getConnections() const{ return _allConnections; }
00068 
00079         CO_API Event select( const int timeout = -1 );
00080 
00084         CO_API void interrupt();
00085 
00087         void setDirty();
00088 
00089         int           getError()     { return _error; }
00090         ConnectionPtr getConnection(){ return _connection; }
00091 
00092     private:
00093  
00094 #ifdef _WIN32
00095         typedef ConnectionSetThread Thread;
00096         typedef std::vector< ConnectionSetThread* > Threads;
00098         Threads _threads;
00099 
00101         Thread* _thread;
00102 
00103         union Result
00104         {
00105             Connection* connection;
00106             Thread* thread;
00107         };
00108 
00109 #else
00110         union Result
00111         {
00112             Connection* connection;
00113         };
00114 #endif
00115 
00117         base::Lock _mutex;
00118 
00120         Connections _allConnections;
00121 
00123         Connections _connections;
00124 
00125         // Note: std::vector had to much overhead here
00126 #ifdef _WIN32
00127         base::Buffer< HANDLE > _fdSet;
00128 #else
00129         base::Buffer< pollfd > _fdSetCopy; // 'const' set
00130         base::Buffer< pollfd > _fdSet;     // copy of _fdSetCopy used to poll
00131 #endif
00132         base::Buffer< Result > _fdSetResult;
00133 
00135         base::RefPtr< EventConnection > _selfConnection;
00136 
00137         // result values
00138         ConnectionPtr _connection;
00139         int           _error;
00140 
00142         bool _dirty;
00143 
00144         bool _setupFDSet();
00145         bool _buildFDSet();
00146         virtual void notifyStateChanged( Connection* ) { _dirty = true; }
00147 
00148         Event _getSelectResult( const uint32_t index );
00149         EQ_TS_VAR( _selectThread );
00150     };
00151 
00152     CO_API std::ostream& operator << ( std::ostream& os, 
00153                                           const ConnectionSet* set );
00154     CO_API std::ostream& operator << ( std::ostream& os, 
00155                                           const ConnectionSet::Event event );
00156 }
00157 
00158 #endif // CO_CONNECTION_SET_H
Generated on Sun May 8 2011 19:11:05 for Equalizer 1.0 by  doxygen 1.7.3