connectionSet.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef EQNET_CONNECTION_SET_H
00019 #define EQNET_CONNECTION_SET_H
00020
00021 #include <eq/net/connectionListener.h>
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
00100 #ifdef WIN32
00101 base::Buffer< HANDLE > _fdSet;
00102 #else
00103 base::Buffer< pollfd > _fdSetCopy;
00104 base::Buffer< pollfd > _fdSet;
00105 #endif
00106 base::Buffer< Connection* > _fdSetConnections;
00107
00109 ConnectionPtr _selfConnection;
00111 uint8_t _selfCommand;
00112
00113
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