Collage  0.6.1
connection.h
00001 
00002 /* Copyright (c) 2005-2012, 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_H
00019 #define CO_CONNECTION_H
00020 
00021 #include <co/connectionType.h>        // enum
00022 #include <co/packets.h>               // used in inline method
00023 #include <co/types.h>                 // Connections type
00024 #include <co/api.h>
00025 
00026 #include <lunchbox/lock.h>
00027 #include <lunchbox/refPtr.h>
00028 #include <lunchbox/referenced.h>   // base class
00029 
00030 #include <sys/types.h>
00031 #include <string.h>
00032 #include <vector>
00033 
00034 #ifdef _WIN32
00035 #  define EQ_DEFAULT_PORT (4242)
00036 #  include <malloc.h>
00037 #  include <lunchbox/os.h>
00038 #else
00039 #  define EQ_DEFAULT_PORT (4242 + getuid())
00040 #endif
00041 
00042 namespace co
00043 {
00061     class Connection : public lunchbox::Referenced, public lunchbox::NonCopyable
00062     {
00063     public:
00064         enum State 
00065         {
00066             STATE_CLOSED,     
00067             STATE_CONNECTING, 
00068             STATE_CONNECTED,  
00069             STATE_LISTENING,  
00070             STATE_CLOSING     
00071         };
00072 
00082         CO_API static ConnectionPtr create(ConnectionDescriptionPtr description);
00083 
00087         State getState() const { return _state; }
00088 
00090         bool isClosed() const { return _state == STATE_CLOSED; }
00091 
00093         bool isConnected() const { return _state == STATE_CONNECTED; }
00094 
00096         bool isListening() const { return _state == STATE_LISTENING; }
00097 
00103         CO_API void setDescription( ConnectionDescriptionPtr description );
00104 
00106         CO_API ConnectionDescriptionPtr getDescription() const;
00107 
00109         bool operator == ( const Connection& rhs ) const;
00111 
00112 
00124         virtual bool connect() { return false; }
00125         
00135         virtual bool listen() { return false; }
00136 
00140         virtual void close() {}
00142 
00146         void addListener( ConnectionListener* listener );
00147 
00149         void removeListener( ConnectionListener* listener );
00151 
00163         virtual void acceptNB() { LBUNIMPLEMENTED; }
00164 
00170         virtual ConnectionPtr acceptSync() { LBUNIMPLEMENTED; return 0; }
00172 
00173 
00187         CO_API void recvNB( void* buffer, const uint64_t bytes );
00188 
00203         CO_API bool recvSync( void** buffer, uint64_t* bytes,
00204                                  const bool block = true );
00205 
00206         void getRecvData( void** buffer, uint64_t* bytes )
00207             { *buffer = _aioBuffer; *bytes = _aioBytes; }
00208 
00222         virtual void readNB( void* buffer, const uint64_t bytes ) = 0;
00223 
00236         virtual int64_t readSync( void* buffer, const uint64_t bytes,
00237                                   const bool block ) = 0;
00239 
00256         CO_API bool send( const void* buffer, const uint64_t bytes, 
00257                           const bool isLocked = false );
00258 
00260         void lockSend() const   { _sendLock.set(); }
00262         void unlockSend() const { _sendLock.unset(); }
00263             
00270         bool send( const Packet& packet )
00271             { return send( &packet, packet.size ); }
00272 
00285         bool send( Packet& packet, const std::string& string )
00286             { return send( packet, string.c_str(), string.size()+1 ); }
00287 
00298         template< typename T >
00299         bool send( Packet& packet, const std::vector<T>& data );
00300 
00310         CO_API bool send( Packet& packet, const void* data,
00311                           const uint64_t size );
00312 
00321         static CO_API bool send( const Connections& connections,
00322                                  const Packet& packet,
00323                                  const bool isLocked = false );
00335         static CO_API bool send( const Connections& connections,
00336                                  Packet& packet, const void* data,
00337                                  const uint64_t size,
00338                                  const bool isLocked = false );
00355         static bool CO_API send( const Connections& connections, 
00356                                  Packet& packet, const void* const* items, 
00357                                  const uint64_t* itemSizes,
00358                                  const size_t nItems );
00359 
00367         virtual int64_t write( const void* buffer, const uint64_t bytes ) = 0;
00368 
00370         virtual void finish() { LBUNIMPLEMENTED; }
00372 
00377 #ifdef _WIN32
00378         typedef HANDLE Notifier;
00379 #else
00380         typedef int Notifier;
00381 #endif
00382 
00383         virtual Notifier getNotifier() const = 0;
00384 
00385     protected:
00386         Connection();
00387         virtual ~Connection();
00388 
00389         void _fireStateChanged();
00390 
00391         State                    _state; 
00392         ConnectionDescriptionPtr _description; 
00393 
00395         mutable lunchbox::Lock _sendLock;
00396 
00397         enum ReadStatus
00398         {
00399             READ_TIMEOUT = -2,
00400             READ_ERROR   = -1
00401             // >= 0: nBytes read
00402         };
00403 
00404     private:
00405         void*         _aioBuffer;
00406         uint64_t      _aioBytes;
00407 
00409         std::vector< ConnectionListener* > _listeners;
00410     };
00411 
00412     CO_API std::ostream& operator << ( std::ostream&, const Connection& );
00413 
00414 #   include "connection.ipp" // template implementation
00415 
00416 }
00417 #endif //CO_CONNECTION_H
Generated on Mon Nov 26 2012 14:41:44 for Collage 0.6.1 by  doxygen 1.7.6.1