Equalizer  1.2.1
connection.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_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 <co/base/refPtr.h>
00027 #include <co/base/referenced.h>   // base class
00028 #include <co/base/lock.h>
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 #else
00038 #  define EQ_DEFAULT_PORT (4242 + getuid())
00039 #endif
00040 
00041 namespace co
00042 {
00043     class ConnectionDescription;
00044     class ConnectionListener;
00045 
00063     class Connection : public base::Referenced, public base::NonCopyable
00064     {
00065     public:
00066         enum State 
00067         {
00068             STATE_CLOSED,     
00069             STATE_CONNECTING, 
00070             STATE_CONNECTED,  
00071             STATE_LISTENING,  
00072             STATE_CLOSING     
00073         };
00074 
00084         CO_API static ConnectionPtr create( ConnectionDescriptionPtr 
00085                                                    description );
00086 
00090         State getState() const { return _state; }
00091 
00093         bool isClosed() const { return _state == STATE_CLOSED; }
00094 
00096         bool isConnected() const { return _state == STATE_CONNECTED; }
00097 
00099         bool isListening() const { return _state == STATE_LISTENING; }
00100 
00106         CO_API void setDescription( ConnectionDescriptionPtr description );
00107 
00109         CO_API ConnectionDescriptionPtr getDescription() const;
00110 
00112         bool operator == ( const Connection& rhs ) const;
00114 
00115 
00127         virtual bool connect() { return false; }
00128         
00138         virtual bool listen() { return false; }
00139 
00143         virtual void close() {}
00145 
00149         void addListener( ConnectionListener* listener );
00150 
00152         void removeListener( ConnectionListener* listener );
00154 
00166         virtual void acceptNB() { EQUNIMPLEMENTED; }
00167 
00173         virtual ConnectionPtr acceptSync() { EQUNIMPLEMENTED; return 0; }
00175 
00176 
00190         CO_API void recvNB( void* buffer, const uint64_t bytes );
00191 
00206         CO_API bool recvSync( void** buffer, uint64_t* bytes,
00207                                  const bool block = true );
00208 
00209         void getRecvData( void** buffer, uint64_t* bytes )
00210             { *buffer = _aioBuffer; *bytes = _aioBytes; }
00211 
00225         virtual void readNB( void* buffer, const uint64_t bytes ) = 0;
00226 
00239         virtual int64_t readSync( void* buffer, const uint64_t bytes,
00240                                   const bool block ) = 0;
00242 
00259         CO_API bool send( const void* buffer, const uint64_t bytes, 
00260                           const bool isLocked = false );
00261 
00263         void lockSend() const   { _sendLock.set(); }
00265         void unlockSend() const { _sendLock.unset(); }
00266             
00273         bool send( const Packet& packet )
00274             { return send( &packet, packet.size ); }
00275 
00288         bool send( Packet& packet, const std::string& string )
00289             { return send( packet, string.c_str(), string.size()+1 ); }
00290 
00301         template< typename T >
00302         bool send( Packet& packet, const std::vector<T>& data );
00303 
00313         CO_API bool send( Packet& packet, const void* data,
00314                           const uint64_t size );
00315 
00324         static CO_API bool send( const Connections& connections,
00325                                  const Packet& packet,
00326                                  const bool isLocked = false );
00338         static CO_API bool send( const Connections& connections,
00339                                  Packet& packet, const void* data,
00340                                  const uint64_t size,
00341                                  const bool isLocked = false );
00358         static bool CO_API send( const Connections& connections, 
00359                                  Packet& packet, const void* const* items, 
00360                                  const uint64_t* itemSizes,
00361                                  const size_t nItems );
00362 
00370         virtual int64_t write( const void* buffer, const uint64_t bytes ) = 0;
00371 
00373         virtual void finish() { EQUNIMPLEMENTED; }
00375 
00380 #ifdef _WIN32
00381         typedef HANDLE Notifier;
00382 #else
00383         typedef int Notifier;
00384 #endif
00385 
00386         virtual Notifier getNotifier() const = 0;
00387 
00388     protected:
00389         Connection();
00390         virtual ~Connection();
00391 
00392         void _fireStateChanged();
00393 
00394         State                    _state; 
00395         ConnectionDescriptionPtr _description; 
00396 
00398         mutable base::Lock _sendLock;
00399 
00400         enum ReadStatus
00401         {
00402             READ_TIMEOUT = -2,
00403             READ_ERROR   = -1
00404             // >= 0: nBytes read
00405         };
00406 
00407     private:
00408         void*         _aioBuffer;
00409         uint64_t      _aioBytes;
00410 
00412         std::vector< ConnectionListener* > _listeners;
00413     };
00414 
00415     CO_API std::ostream& operator << ( std::ostream&, const Connection& );
00416 
00417 #   include "connection.ipp" // template implementation
00418 
00419 }
00420 #endif //CO_CONNECTION_H
Generated on Fri Jun 8 2012 15:44:30 for Equalizer 1.2.1 by  doxygen 1.8.0