Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

cabal_socket.h

00001 /*
00002    cabal_socket.h
00003    Basic socket object for cabal
00004 
00005    $Id: cabal_socket.h,v 1.5 2004/03/29 02:15:52 jonnymind Exp $
00006 ---------------------------------------------
00007    Begin      : 2003-12-13 05:00
00008    Author     : Giancarlo Niccolai
00009 
00010    Last modified because:
00011 
00012 */
00013 
00014 #ifndef CABAL_SOCKET_H
00015 #define CABAL_SOCKET_H
00016 
00017 #include <cabal_address.h>
00018 #include <cabal_recpt.h>
00019 #include <cabal_error.h>
00020 #include <cabal_os.h>
00021 #include <cabal_ref.h>
00022 
00023 #include <string>
00024 
00025 
00026 #define CABAL_DOMAIN    PF_INET
00027 
00028 namespace Cabal
00029 {
00030 
00031 class Channel;
00032 
00033 typedef enum
00034 {
00035    ST_TCP = 1,
00036    ST_UDP = 2,
00037 } SockType;
00038 
00039 typedef enum
00040 {
00041    OT_NONE = 0,
00042    OT_CONNECTING,
00043    OT_SENDINGALL,
00044    OT_RECEIVINGALL
00045 } OpType;
00046 
00047 
00048 
00074 class Socket: public Referenceable 
00075 {
00076 
00078    inline void initialize( const SockType type ) 
00079    {
00080       m_type = type;
00081       m_timeout = -1;
00082       m_bTout = false;
00083       m_osError = 0;
00084       m_channel = 0;
00085       m_refcount = 1;
00086       m_recpt = 0;
00087    }
00088 
00089 protected:
00090    SockType m_type;
00091    long m_timeout;
00092    bool m_bTout;
00093    int m_osError;
00094    int m_refcount;
00095    Channel *m_channel;
00096    Reception *m_recpt;
00097 
00098    Address m_remote;
00099    Address m_local;
00100 
00101    // internal socket
00102    RawSocket m_skCom;
00103 
00104    int Socket::selectFor( bool rw );
00105    
00114    virtual int rawRecv( void *data, const int len ) = 0;
00123    virtual int rawSend( const void *data, const int len ) = 0;
00124    
00134    Socket( const SockType type )
00135    {
00136       initialize( type );
00137    }
00138 
00139    Socket( const SockType type ,
00140             const char *ipaddr_local, const int port_local )
00141       : m_local( ipaddr_local, port_local )
00142    {
00143       initialize( type );
00144    }
00145    
00146    Socket( const SockType type, 
00147            const char *ipaddr_local, const int port_local,
00148            const char *ipaddr_remote, const int port_remote )
00149       : m_local( ipaddr_local, port_local ),
00150         m_remote( ipaddr_remote, port_remote )
00151    {
00152       initialize( type );
00153    }
00154       
00190    friend class ServerSocket;
00191 public:
00192    
00196    virtual ~Socket()
00197    {
00198       close();
00199    }
00200    
00208    Socket( const Socket & ) throw( InitError );
00209    
00218    bool bind( const Address &local );
00219    
00242    virtual int recv( void *buffer, const int len );
00243    
00269    virtual int send( const void *buffer, const int len );
00270    
00271    bool continueOp();
00272    
00279    virtual bool close();
00280    
00287    inline const Address &remote() const { return m_remote; }
00288    
00294    inline const Address &local() const { return m_local; }
00295 
00301    inline long timeout() const { return m_timeout; }
00302    
00309    inline void timeout( const long to ) { m_timeout = to; }
00310    
00316    inline bool hasTimedOut() const { return m_bTout; }
00317    
00323    inline int osError() const { return m_osError; }
00324    
00328    inline bool closed() const { return ( ! Net::validSocket( m_skCom ) ); }
00329 
00337    inline RawSocket rawSocket() const { return m_skCom; }
00338 
00367    void channel( Channel *chn, Reception *rcpt ); 
00368    
00369    
00370    /* Returns the reception object used by this socket. 
00371    */
00372    inline Reception *reception() const { return m_recpt; }
00373    
00378    inline Channel *channel() const { return m_channel; }
00379    
00388    inline bool managed() const { return m_channel != 0 ; }
00389    
00397    inline const char *remote_ip() { return m_remote.ip(); }
00399    inline int remote_port() const { return m_remote.port(); }
00400    
00404    inline const char *local_ip() { return m_remote.ip(); }
00406    inline int local_port() const { return m_remote.port(); }
00407    
00408    
00411    virtual inline void incref() {
00412       m_refcount ++; 
00413 
00414    }
00415    
00418    virtual inline void decref() 
00419    {
00420       m_refcount--;
00421       if ( m_refcount == 0) {
00422          delete this;
00423       }
00424    }
00425    
00426 };
00427 
00428 }
00429 
00430 #endif
00431 
00432 /* end of cabal_ssocket.h */

Generated on Sat Apr 10 17:41:48 2004 for Cabal by doxygen 1.3.5