Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   Related Pages  

Socket.h

00001 /*
00002  * Copyright 2003 Michael A. Marsh, Cornell University. All rights reserved.
00003  * This software is released under the modified BSD license.
00004  * See the file LICENSE in the top-level directory for details.
00005  */
00006 //
00007 // $Id: Socket.h,v 1.3 2004/05/19 15:56:56 mmarsh Exp $
00008 //
00009 // $Log: Socket.h,v $
00010 // Revision 1.3  2004/05/19 15:56:56  mmarsh
00011 // *** empty log message ***
00012 //
00013 // Revision 1.2  2003/11/04 22:31:50  mmarsh
00014 // *** empty log message ***
00015 //
00016 //
00017 
00018 #ifndef __CODEX_QUORUM_SOCKET_H__
00019 #define __CODEX_QUORUM_SOCKET_H__
00020 
00021 #include <sys/types.h>
00022 #include <sys/socket.h>
00023 #include "QSExceptions.h"
00024 #include <vector>
00025 #include <queue>
00026 
00027 namespace CODEX_Quorum
00028 {
00029    class RemoteServer;
00030    class Message;
00031 
00035    class SocketBase
00036    {
00037       public :
00039          typedef std::queue< Message* > MsgQueueType;
00040 
00044          enum StateType
00045          {
00046             kRead,
00047             kWrite,
00048             kError
00049          };
00050 
00073          SocketBase( int   domain=PF_INET   ,
00074                      int   type=SOCK_STREAM ,
00075                      int   protocol=0       ,
00076                      bool  blocking=false   );
00077          virtual ~SocketBase();
00078 
00090          virtual void setup(int port, int backlog);
00091 
00097          virtual void setup(struct sockaddr* my_addr,
00098                             socklen_t addrlen,
00099                             int backlog);
00100 
00111          virtual void connect(const RemoteServer& server);
00112 
00143           virtual SocketBase* accept();
00144 
00160          virtual size_t readFrom( void* output, size_t maxSize=1024 ) const;
00161 
00183          virtual size_t readAll( Message& msg, size_t length = 0 ) const;
00184 
00190          virtual void writeTo( const Message& input ) const;
00191 
00203          virtual int set_fd( fd_set* fd_bitmap, StateType s ) const;
00204 
00219          virtual bool isset_fd( const fd_set* fd_bitmap, StateType s ) const;
00220 
00228          virtual void flush() const;
00229 
00232       protected :
00233 
00247          SocketBase( const SocketBase& aOther );
00248 
00258          virtual SocketBase* clone();
00259 
00271 
00272          virtual SocketBase* protected_accept();
00273 
00282          virtual void finish_accept();
00283          virtual void protected_bind(int port);
00285          virtual void protected_bind(struct sockaddr* my_addr,
00286                                      socklen_t addrlen);
00288          virtual void protected_listen(int backlog);
00289 
00290          void setSocket( int socketFD );
00291          void setBacklog( int backlog );
00292          void setPort( int port );
00293 
00303          int  domain()   const { return m_domain; }
00304          int  type()     const { return m_type; }
00305          int  protocol() const { return m_protocol; }
00306          bool blocking() const { return m_blocking; }
00307          int  port()     const { return m_port; }
00308          int  backlog()  const { return m_backlog; }
00309          int  socket()   const { return m_socket; }
00310 
00324          virtual int internal_write( const unsigned char* output,
00325                                      size_t maxSize ) const;
00326 
00328          mutable MsgQueueType  m_msgQueue;
00329 
00331          mutable unsigned int  m_msgOffset;
00332 
00333       private :
00334          int   m_domain;
00335          int   m_type;
00336          int   m_protocol;
00337          bool  m_blocking;
00338          int   m_port;
00339          int   m_backlog;
00340 
00341          int   m_socket;
00342    };
00343 
00344 
00345 
00346    //------ Exceptions ------//
00347 
00351    class QSESocketBase : public QSExceptionBase
00352    {
00353       public :
00359          QSESocketBase( const string& fname, int line, unsigned long error ) :
00360             QSExceptionBase( fname, line ),
00361             m_error( error )
00362          {}
00363 
00365          virtual ~QSESocketBase() {}
00366 
00368          unsigned long error() const { return m_error; }
00369 
00370       protected :
00371          void derivedMsg() const;
00376          virtual void errMsg() const = 0;
00377 
00379          unsigned long m_error;
00380    };
00381 
00385    class QSESocketBaseBadSocket : public QSESocketBase
00386    {
00387       public :
00389          QSESocketBaseBadSocket( const string& fname,
00390                                  int line,
00391                                  int domain,
00392                                  int type,
00393                                  int protocol,
00394                                  unsigned long error ) :
00395             QSESocketBase( fname, line, error ),
00396             m_domain( domain ),
00397             m_type( type ),
00398             m_protocol( protocol )
00399          {}
00401          int domain()   const { return m_domain; }
00403          int type()     const { return m_type; }
00405          int protocol() const { return m_protocol; }
00406 
00407       protected :
00408          void errMsg() const;
00409 
00410       private :
00411          int  m_domain;
00412          int  m_type;
00413          int  m_protocol;
00414    };
00415 
00419    class QSESocketBaseCannotBind : public QSESocketBase
00420    {
00421       public :
00423          QSESocketBaseCannotBind( const string& fname,
00424                                   int line,
00425                                   int socket,
00426                                   unsigned long error ) :
00427             QSESocketBase( fname, line, error ),
00428             m_socket( socket )
00429          {}
00431          int socket() const { return m_socket; }
00432 
00433       protected :
00434          void errMsg() const;
00435 
00436       private :
00437          int  m_socket;
00438    };
00439 
00443    class QSESocketBaseListenFailed : public QSESocketBase
00444    {
00445       public :
00447          QSESocketBaseListenFailed( const string& fname,
00448                                     int line,
00449                                     int socket,
00450                                     int backlog,
00451                                     unsigned long error ) :
00452             QSESocketBase( fname, line, error ),
00453             m_socket( socket ),
00454             m_backlog( backlog )
00455          {}
00457          int socket()  const { return m_socket; }
00459          int backlog() const { return m_backlog; }
00460 
00461       protected :
00462          void errMsg() const;
00463 
00464       private :
00465          int  m_socket;
00466          int  m_backlog;
00467    };
00468 
00476    class QSESocketBaseAcceptFailed : public QSESocketBase
00477    {
00478       public :
00480          QSESocketBaseAcceptFailed( const string& fname,
00481                                     int line,
00482                                     int socket,
00483                                     unsigned long error ) :
00484             QSESocketBase( fname, line, error ),
00485             m_socket( socket )
00486          {}
00488          int socket() const { return m_socket; }
00489 
00490       protected :
00491          void errMsg() const;
00492 
00493       private :
00494          int  m_socket;
00495    };
00496 
00500    class QSESocketBaseCannotConnect : public QSESocketBase
00501    {
00502       public :
00504          QSESocketBaseCannotConnect( const string& fname,
00505                                      int line,
00506                                      int socket,
00507                                      const RemoteServer& server,
00508                                      unsigned long error ) :
00509             QSESocketBase( fname, line, error ),
00510             m_socket( socket ),
00511             m_server( server )
00512          {}
00514          int                 socket() const { return m_socket; }
00516          const RemoteServer& server() const { return m_server; }
00517 
00518       protected :
00519          void errMsg() const;
00520 
00521       private :
00522          int            m_socket;
00523          const RemoteServer&  m_server;
00524    };
00525 
00529    class QSESocketBaseSocketClosed : public QSESocketBase
00530    {
00531       public :
00533          QSESocketBaseSocketClosed( const string& fname,
00534                                     int line,
00535                                     int socket,
00536                                     unsigned long error ) :
00537             QSESocketBase( fname, line, error ),
00538             m_socket( socket )
00539          {}
00540 
00542          int socket() const { return m_socket; }
00543 
00544       protected :
00545          void errMsg() const;
00546 
00547       private :
00548          int  m_socket;
00549    };
00550 
00554    class QSESocketBaseReadFailed : public QSESocketBase
00555    {
00556       public :
00558          QSESocketBaseReadFailed( const string& fname,
00559                                   int line,
00560                                   int socket,
00561                                   unsigned long error ) :
00562             QSESocketBase( fname, line, error ),
00563             m_socket( socket )
00564          {}
00566          int socket() const { return m_socket; }
00567 
00568       protected :
00569          void errMsg() const;
00570 
00571       private :
00572          int  m_socket;
00573    };
00574 
00578    class QSESocketBaseWriteFailed : public QSESocketBase
00579    {
00580       public :
00582          QSESocketBaseWriteFailed( const string& fname,
00583                                    int line,
00584                                    int socket,
00585                                    unsigned long error ) :
00586             QSESocketBase( fname, line, error ),
00587             m_socket( socket )
00588          {}
00590          int socket() const { return m_socket; }
00591 
00592       protected :
00593          void errMsg() const;
00594 
00595       private :
00596          int  m_socket;
00597    };
00598 
00602    class QSESocketBaseMessageTooLong : public QSESocketBase
00603    {
00604       public :
00606          QSESocketBaseMessageTooLong( const string& fname,
00607                                       int line,
00608                                       int socket,
00609                                       unsigned long error ) :
00610             QSESocketBase( fname, line, error ),
00611             m_socket( socket )
00612          {}
00613 
00615          int socket() const { return m_socket; }
00616 
00617       protected :
00618          void errMsg() const;
00619 
00620       private :
00621          int  m_socket;
00622    };
00623 
00627    class QSESocketBaseMessageTooShort : public QSESocketBase
00628    {
00629       public :
00631          QSESocketBaseMessageTooShort( const string& fname,
00632                                        int line,
00633                                        int socket,
00634                                        unsigned long error ) :
00635             QSESocketBase( fname, line, error ),
00636             m_socket( socket )
00637          {}
00638 
00640          int socket() const { return m_socket; }
00641 
00642       protected :
00643          void errMsg() const;
00644 
00645       private :
00646          int  m_socket;
00647    };
00648 
00649 }
00650 
00651 #endif /* __CODEX_QUORUM_SOCKET_H__ */

Generated on Wed Jun 2 16:32:56 2004 for COrnell Data EXchange (CODEX) by doxygen1.2.18