connectionSet.h

00001 
00002 /* Copyright (c) 2005-2009, 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 EQNET_CONNECTION_SET_H
00019 #define EQNET_CONNECTION_SET_H
00020 
00021 #include <eq/net/connectionListener.h> // base class
00022 
00023 #include <eq/base/base.h>
00024 #include <eq/base/buffer.h>
00025 #include <eq/base/hash.h>
00026 #include <eq/base/refPtr.h>
00027 
00028 #ifndef WIN32
00029 #  include <poll.h>
00030 #endif
00031 
00032 namespace eq
00033 {
00034 namespace net
00035 {
00036     class Message;
00037     class Network;
00038 
00044     class ConnectionSet : public ConnectionListener
00045     {
00046     public:
00047         enum Event
00048         {
00049             EVENT_NONE = 0,        
00050             EVENT_CONNECT,         
00051             EVENT_DISCONNECT,      
00052             EVENT_DATA,            
00053             EVENT_TIMEOUT,         
00054             EVENT_INTERRUPT,       
00055             EVENT_ERROR,           
00056             EVENT_SELECT_ERROR,    
00057             EVENT_INVALID_HANDLE,  
00058             EVENT_ALL
00059         };
00060 
00061         EQ_EXPORT ConnectionSet();
00062         EQ_EXPORT ~ConnectionSet();
00063 
00064         EQ_EXPORT void addConnection( ConnectionPtr connection );
00065         EQ_EXPORT bool removeConnection( ConnectionPtr connection );
00066         EQ_EXPORT void clear();
00067         size_t size()  const { return _connections.size(); }
00068         bool   empty() const { return _connections.empty(); }
00069 
00070         const ConnectionVector& getConnections() const { return _connections; }
00071 
00082         EQ_EXPORT Event select( const int timeout = -1 );
00083 
00087         EQ_EXPORT void interrupt();
00088 
00089         int           getError()     { return _error; }
00090         ConnectionPtr getConnection(){ return _connection; }
00091 
00092     private:
00094         base::Lock _mutex;
00095 
00097         ConnectionVector _connections;
00098 
00099         // Note: std::vector had to much overhead here
00100 #ifdef WIN32
00101         base::Buffer< HANDLE > _fdSet;
00102 #else
00103         base::Buffer< pollfd > _fdSetCopy; // 'const' set
00104         base::Buffer< pollfd > _fdSet;     // copy of _fdSetCopy used to poll
00105 #endif
00106         base::Buffer< Connection* > _fdSetConnections;
00107 
00109         ConnectionPtr _selfConnection;
00111         uint8_t _selfCommand;
00112 
00113         // result values
00114         ConnectionPtr _connection;
00115         int           _error;
00116 
00118         bool _dirty;
00119 
00120         void _dirtyFDSet();
00121         bool _setupFDSet();
00122         bool _buildFDSet();
00123         virtual void notifyStateChanged( Connection* ) { _dirty = true; }
00124 
00125         Event _handleSelfCommand();
00126         Event _getSelectResult( const uint32_t index );
00127     };
00128 
00129     EQ_EXPORT std::ostream& operator << ( std::ostream& os, 
00130                                           const ConnectionSet* set );
00131     EQ_EXPORT std::ostream& operator << ( std::ostream& os, 
00132                                           const ConnectionSet::Event event );
00133 }
00134 }
00135 
00136 #endif // EQNET_CONNECTION_SET_H
Generated on Mon Aug 10 18:58:32 2009 for Equalizer 0.9 by  doxygen 1.5.8