00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef __CODEX_QUORUM_SOCKET_H__
00022 #define __CODEX_QUORUM_SOCKET_H__
00023
00024 #include <sys/types.h>
00025 #include <sys/socket.h>
00026 #include "QSExceptions.h"
00027 #include <vector>
00028 #include <queue>
00029
00030 namespace CODEX_Quorum
00031 {
00032 class RemoteServer;
00033 class Message;
00034
00038 class SocketBase
00039 {
00040 public :
00042 typedef std::queue< Message* > MsgQueueType;
00043
00047 enum StateType
00048 {
00049 kRead,
00050 kWrite,
00051 kError
00052 };
00053
00076 SocketBase( int domain=PF_INET ,
00077 int type=SOCK_STREAM ,
00078 int protocol=0 ,
00079 bool blocking=false );
00080 virtual ~SocketBase();
00081
00093 virtual void setup(int port, int backlog);
00094
00100 virtual void setup(struct sockaddr* my_addr,
00101 socklen_t addrlen,
00102 int backlog);
00103
00114 virtual void connect(const RemoteServer& server);
00115
00146 virtual SocketBase* accept();
00147
00163 virtual size_t readFrom( void* output, size_t maxSize=1024 ) const;
00164
00186 virtual size_t readAll( Message& msg, size_t length = 0 ) const;
00187
00193 virtual void writeTo( const Message& input ) const;
00194
00206 virtual int set_fd( fd_set* fd_bitmap, StateType s ) const;
00207
00222 virtual bool isset_fd( const fd_set* fd_bitmap, StateType s ) const;
00223
00231 virtual void flush() const;
00232
00235 protected :
00236
00250 SocketBase( const SocketBase& aOther );
00251
00261 virtual SocketBase* clone();
00262
00274
00275 virtual SocketBase* protected_accept();
00276
00285 virtual void finish_accept();
00286 virtual void protected_bind(int port);
00288 virtual void protected_bind(struct sockaddr* my_addr,
00289 socklen_t addrlen);
00291 virtual void protected_listen(int backlog);
00292
00293 void setSocket( int socketFD );
00294 void setBacklog( int backlog );
00295 void setPort( int port );
00296
00306 int domain() const { return m_domain; }
00307 int type() const { return m_type; }
00308 int protocol() const { return m_protocol; }
00309 bool blocking() const { return m_blocking; }
00310 int port() const { return m_port; }
00311 int backlog() const { return m_backlog; }
00312 int socket() const { return m_socket; }
00313
00327 virtual int internal_write( const unsigned char* output,
00328 size_t maxSize ) const;
00329
00331 mutable MsgQueueType m_msgQueue;
00332
00334 mutable unsigned int m_msgOffset;
00335
00336 private :
00337 int m_domain;
00338 int m_type;
00339 int m_protocol;
00340 bool m_blocking;
00341 int m_port;
00342 int m_backlog;
00343
00344 int m_socket;
00345 };
00346
00347
00348
00349
00350
00354 class QSESocketBase : public QSExceptionBase
00355 {
00356 public :
00362 QSESocketBase( const string& fname, int line, unsigned long error ) :
00363 QSExceptionBase( fname, line ),
00364 m_error( error )
00365 {}
00366
00368 virtual ~QSESocketBase() {}
00369
00371 unsigned long error() const { return m_error; }
00372
00373 protected :
00374 void derivedMsg() const;
00379 virtual void errMsg() const = 0;
00380
00382 unsigned long m_error;
00383 };
00384
00388 class QSESocketBaseBadSocket : public QSESocketBase
00389 {
00390 public :
00392 QSESocketBaseBadSocket( const string& fname,
00393 int line,
00394 int domain,
00395 int type,
00396 int protocol,
00397 unsigned long error ) :
00398 QSESocketBase( fname, line, error ),
00399 m_domain( domain ),
00400 m_type( type ),
00401 m_protocol( protocol )
00402 {}
00404 int domain() const { return m_domain; }
00406 int type() const { return m_type; }
00408 int protocol() const { return m_protocol; }
00409
00410 protected :
00411 void errMsg() const;
00412
00413 private :
00414 int m_domain;
00415 int m_type;
00416 int m_protocol;
00417 };
00418
00422 class QSESocketBaseCannotBind : public QSESocketBase
00423 {
00424 public :
00426 QSESocketBaseCannotBind( const string& fname,
00427 int line,
00428 int socket,
00429 unsigned long error ) :
00430 QSESocketBase( fname, line, error ),
00431 m_socket( socket )
00432 {}
00434 int socket() const { return m_socket; }
00435
00436 protected :
00437 void errMsg() const;
00438
00439 private :
00440 int m_socket;
00441 };
00442
00446 class QSESocketBaseListenFailed : public QSESocketBase
00447 {
00448 public :
00450 QSESocketBaseListenFailed( const string& fname,
00451 int line,
00452 int socket,
00453 int backlog,
00454 unsigned long error ) :
00455 QSESocketBase( fname, line, error ),
00456 m_socket( socket ),
00457 m_backlog( backlog )
00458 {}
00460 int socket() const { return m_socket; }
00462 int backlog() const { return m_backlog; }
00463
00464 protected :
00465 void errMsg() const;
00466
00467 private :
00468 int m_socket;
00469 int m_backlog;
00470 };
00471
00479 class QSESocketBaseAcceptFailed : public QSESocketBase
00480 {
00481 public :
00483 QSESocketBaseAcceptFailed( const string& fname,
00484 int line,
00485 int socket,
00486 unsigned long error ) :
00487 QSESocketBase( fname, line, error ),
00488 m_socket( socket )
00489 {}
00491 int socket() const { return m_socket; }
00492
00493 protected :
00494 void errMsg() const;
00495
00496 private :
00497 int m_socket;
00498 };
00499
00503 class QSESocketBaseCannotConnect : public QSESocketBase
00504 {
00505 public :
00507 QSESocketBaseCannotConnect( const string& fname,
00508 int line,
00509 int socket,
00510 const RemoteServer& server,
00511 unsigned long error ) :
00512 QSESocketBase( fname, line, error ),
00513 m_socket( socket ),
00514 m_server( server )
00515 {}
00517 int socket() const { return m_socket; }
00519 const RemoteServer& server() const { return m_server; }
00520
00521 protected :
00522 void errMsg() const;
00523
00524 private :
00525 int m_socket;
00526 const RemoteServer& m_server;
00527 };
00528
00532 class QSESocketBaseSocketClosed : public QSESocketBase
00533 {
00534 public :
00536 QSESocketBaseSocketClosed( const string& fname,
00537 int line,
00538 int socket,
00539 unsigned long error ) :
00540 QSESocketBase( fname, line, error ),
00541 m_socket( socket )
00542 {}
00543
00545 int socket() const { return m_socket; }
00546
00547 protected :
00548 void errMsg() const;
00549
00550 private :
00551 int m_socket;
00552 };
00553
00557 class QSESocketBaseReadFailed : public QSESocketBase
00558 {
00559 public :
00561 QSESocketBaseReadFailed( const string& fname,
00562 int line,
00563 int socket,
00564 unsigned long error ) :
00565 QSESocketBase( fname, line, error ),
00566 m_socket( socket )
00567 {}
00569 int socket() const { return m_socket; }
00570
00571 protected :
00572 void errMsg() const;
00573
00574 private :
00575 int m_socket;
00576 };
00577
00581 class QSESocketBaseWriteFailed : public QSESocketBase
00582 {
00583 public :
00585 QSESocketBaseWriteFailed( const string& fname,
00586 int line,
00587 int socket,
00588 unsigned long error ) :
00589 QSESocketBase( fname, line, error ),
00590 m_socket( socket )
00591 {}
00593 int socket() const { return m_socket; }
00594
00595 protected :
00596 void errMsg() const;
00597
00598 private :
00599 int m_socket;
00600 };
00601
00605 class QSESocketBaseMessageTooLong : public QSESocketBase
00606 {
00607 public :
00609 QSESocketBaseMessageTooLong( const string& fname,
00610 int line,
00611 int socket,
00612 unsigned long error ) :
00613 QSESocketBase( fname, line, error ),
00614 m_socket( socket )
00615 {}
00616
00618 int socket() const { return m_socket; }
00619
00620 protected :
00621 void errMsg() const;
00622
00623 private :
00624 int m_socket;
00625 };
00626
00630 class QSESocketBaseMessageTooShort : public QSESocketBase
00631 {
00632 public :
00634 QSESocketBaseMessageTooShort( const string& fname,
00635 int line,
00636 int socket,
00637 unsigned long error ) :
00638 QSESocketBase( fname, line, error ),
00639 m_socket( socket )
00640 {}
00641
00643 int socket() const { return m_socket; }
00644
00645 protected :
00646 void errMsg() const;
00647
00648 private :
00649 int m_socket;
00650 };
00651
00652 }
00653
00654 #endif