00001
00002
00003
00004
00005 #ifndef EQNET_CONNECTION_H
00006 #define EQNET_CONNECTION_H
00007
00008 #include <eq/net/connectionDescription.h>
00009 #include <eq/net/packets.h>
00010 #include <eq/net/types.h>
00011
00012 #include <eq/base/base.h>
00013 #include <eq/base/refPtr.h>
00014 #include <eq/base/referenced.h>
00015 #include <eq/base/scopedMutex.h>
00016 #include <eq/base/spinLock.h>
00017
00018 #include <sys/types.h>
00019 #include <string.h>
00020 #include <vector>
00021
00022 #ifdef WIN32_API
00023 # include <malloc.h>
00024 #endif
00025
00026 #ifdef WIN32
00027 # define EQ_DEFAULT_PORT (4242)
00028 #else
00029 # define EQ_DEFAULT_PORT (4242 + getuid())
00030 #endif
00031
00032 namespace eq
00033 {
00034 namespace net
00035 {
00036 class ConnectionListener;
00037 enum ConnectionType;
00038
00042 class EQ_EXPORT Connection : public base::Referenced
00043 {
00044 public:
00045 enum State
00046 {
00047 STATE_CLOSED,
00048 STATE_CONNECTING,
00049 STATE_CONNECTED,
00050 STATE_LISTENING
00051 };
00052
00063 static ConnectionPtr create( ConnectionDescriptionPtr description );
00064
00066
00072 State getState() const { return _state; }
00073
00075 bool isClosed() const { return _state == STATE_CLOSED; }
00076
00078 bool isConnected() const { return _state == STATE_CONNECTED; }
00079
00081 bool isListening() const { return _state == STATE_LISTENING; }
00082
00088 void setDescription( ConnectionDescriptionPtr description );
00089
00091 ConnectionDescriptionPtr getDescription() const;
00092
00093
00102 virtual bool connect() { return false; }
00103
00111 virtual bool listen() { return false; }
00112
00119 virtual ConnectionPtr accept() { return 0; }
00120
00129 virtual ConnectionPtr accept( const int timeout );
00130
00134 virtual void close(){};
00136
00138
00140 void addListener( ConnectionListener* listener );
00141
00143 void removeListener( ConnectionListener* listener );
00144
00145
00146
00148
00156 bool recv( void* buffer, const uint64_t bytes );
00157
00159 void lockSend() const { _sendLock.set(); }
00161 void unlockSend() const { _sendLock.unset(); }
00162
00171 bool send( const void* buffer, const uint64_t bytes,
00172 const bool isLocked = false ) const;
00173
00180 bool send( const Packet& packet ) const
00181 { return send( &packet, packet.size); }
00182
00190 bool send( Packet& packet, const std::string& string ) const
00191 { return send( packet, string.c_str(), string.size()+1 ); }
00192
00203 template< typename T >
00204 bool send( Packet& packet, const std::vector<T>& data ) const;
00205
00215 bool send( Packet& packet, const void* data, const uint64_t size )
00216 const;
00217
00226 static bool send( const ConnectionVector& connections,
00227 const Packet& packet, const bool isLocked = false );
00239 static bool send( const ConnectionVector& connections, Packet& packet,
00240 const void* data, const uint64_t size,
00241 const bool isLocked = false );
00242
00243
00245 #ifdef WIN32
00246 typedef HANDLE ReadNotifier;
00247 enum SelectResult
00248 {
00249 SELECT_TIMEOUT = WAIT_TIMEOUT,
00250 SELECT_ERROR = WAIT_FAILED,
00251 };
00252 #else
00253 typedef int ReadNotifier;
00254 enum SelectResult
00255 {
00256 SELECT_TIMEOUT = 0,
00257 SELECT_ERROR = -1,
00258 };
00259 #endif
00260 virtual ReadNotifier getReadNotifier() const { return 0; }
00261
00262 protected:
00263 State _state;
00264 ConnectionDescriptionPtr _description;
00265
00266 mutable base::SpinLock _sendLock;
00267
00269 std::vector< ConnectionListener* > _listeners;
00270
00271 friend class PairConnection;
00272
00273
00274 Connection();
00275 Connection(const Connection& conn);
00276 virtual ~Connection();
00277
00278 void _fireStateChanged();
00279
00292 virtual int64_t read( void* buffer, const uint64_t bytes ) = 0;
00293
00301 virtual int64_t write( const void* buffer, const uint64_t bytes )
00302 const = 0;
00304 };
00305
00306 std::ostream& operator << ( std::ostream&, const Connection* );
00307
00308 # include "connection.ipp"
00309
00310 }
00311 }
00312 #endif //EQNET_CONNECTION_H