connection.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef EQNET_CONNECTION_H
00019 #define EQNET_CONNECTION_H
00020
00021 #include <eq/net/connectionDescription.h>
00022 #include <eq/net/packets.h>
00023 #include <eq/net/types.h>
00024
00025 #include <eq/base/base.h>
00026 #include <eq/base/refPtr.h>
00027 #include <eq/base/referenced.h>
00028 #include <eq/base/scopedMutex.h>
00029 #include <eq/base/lock.h>
00030
00031 #include <sys/types.h>
00032 #include <string.h>
00033 #include <vector>
00034
00035 #ifdef WIN32_API
00036 # include <malloc.h>
00037 #endif
00038
00039 #ifdef WIN32
00040 # define EQ_DEFAULT_PORT (4242)
00041 #else
00042 # define EQ_DEFAULT_PORT (4242 + getuid())
00043 #endif
00044
00045 namespace eq
00046 {
00047 namespace net
00048 {
00049 class ConnectionListener;
00050 enum ConnectionType;
00051
00069 class Connection : public base::Referenced, public base::NonCopyable
00070 {
00071 public:
00072 enum State
00073 {
00074 STATE_CLOSED,
00075 STATE_CONNECTING,
00076 STATE_CONNECTED,
00077 STATE_LISTENING
00078 };
00079
00089 EQ_EXPORT static ConnectionPtr create( ConnectionDescriptionPtr
00090 description );
00091
00095 State getState() const { return _state; }
00096
00098 bool isClosed() const { return _state == STATE_CLOSED; }
00099
00101 bool isConnected() const { return _state == STATE_CONNECTED; }
00102
00104 bool isListening() const { return _state == STATE_LISTENING; }
00105
00111 EQ_EXPORT void setDescription( ConnectionDescriptionPtr description );
00112
00114 EQ_EXPORT ConnectionDescriptionPtr getDescription() const;
00116
00117
00129 virtual bool connect() { return false; }
00130
00140 virtual bool listen() { return false; }
00141
00145 virtual void close(){};
00147
00151 void addListener( ConnectionListener* listener );
00152
00154 void removeListener( ConnectionListener* listener );
00156
00168 virtual void acceptNB() { EQUNIMPLEMENTED; }
00169
00175 virtual ConnectionPtr acceptSync()
00176 { EQUNIMPLEMENTED; return 0; }
00178
00179
00193 EQ_EXPORT void recvNB( void* buffer, const uint64_t bytes );
00194
00207 EQ_EXPORT bool recvSync( void** buffer, uint64_t* bytes );
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
00237 virtual int64_t readSync( void* buffer, const uint64_t bytes ) = 0;
00239
00256 EQ_EXPORT 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 EQ_EXPORT bool send( Packet& packet, const void* data,
00311 const uint64_t size );
00312
00321 static bool send( const ConnectionVector& connections,
00322 const Packet& packet, const bool isLocked = false );
00334 static bool send( const ConnectionVector& connections, Packet& packet,
00335 const void* data, const uint64_t size,
00336 const bool isLocked = false );
00338
00343 #ifdef WIN32
00344 typedef HANDLE Notifier;
00345 #else
00346 typedef int Notifier;
00347 #endif
00348
00349 virtual Notifier getNotifier() const { return 0; }
00350
00351 protected:
00352 Connection();
00353 virtual ~Connection();
00354
00355 void _fireStateChanged();
00356
00366 virtual int64_t write( const void* buffer, const uint64_t bytes ) = 0;
00368
00369 State _state;
00370 ConnectionDescriptionPtr _description;
00371
00373 mutable base::Lock _sendLock;
00374
00375 private:
00376 void* _aioBuffer;
00377 uint64_t _aioBytes;
00378
00380 std::vector< ConnectionListener* > _listeners;
00381
00382 friend class PairConnection;
00383 };
00384
00385 std::ostream& operator << ( std::ostream&, const Connection* );
00386
00387 # include "connection.ipp"
00388
00389 }
00390 }
00391 #endif //EQNET_CONNECTION_H