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

cabal_ssocket.cpp

00001 /*
00002    CABAL PROTOCOL
00003    CAched BAlanced LAN
00004 
00005    Server (TCP) socket class
00006 
00007    $Id: cabal_ssocket.cpp,v 1.5 2004/04/08 15:31:51 jonnymind Exp $
00008 ---------------------------------------------
00009    Begin      : Thu, 11 Mar 2004 04:49:19 +0100
00010    Author     : Giancarlo Niccolai
00011 
00012    Last modified because:
00013       Parent became TCPSocket; was formerly Socket
00014 
00015 */
00016 
00017 #ifdef __MSC_VER
00018 #pragma warning ( disable : 4786 )
00019 #endif
00020 
00021 #include <cabal_ssocket.h>
00022 
00023 namespace Cabal {
00024 
00025 
00026 ServerSocket::ServerSocket( const Address &local, int timeout, int incomingMax )
00027          throw( InitError )
00028          : TCPSocket( timeout, &local, 0 )
00029 {
00030    if ( ! Net::listen( m_skCom, incomingMax ) ) {
00031       Net::closeSocket( m_skCom );
00032       Net::invalidate( m_skCom );
00033       m_osError = Net::errorCode();
00034       throw InitError( "Can't listen on socket", Net::errorCode() );
00035    }
00036 }
00037 
00038 ServerSocket::ServerSocket( const char *ip, const int port, int timeout, int incomingMax )
00039          throw( InitError )
00040          : TCPSocket( timeout,ip, port )
00041 {
00042    if ( ! Net::listen( m_skCom, incomingMax ) ) {
00043       Net::closeSocket( m_skCom );
00044       Net::invalidate( m_skCom );
00045       m_osError = Net::errorCode();
00046       throw InitError( "Can't listen on socket", Net::errorCode() );
00047    }
00048 }
00049 
00050 ServerSocket::ServerSocket( std::string const &ip, const int port, int timeout, int incomingMax )
00051          throw( InitError )
00052          : TCPSocket( timeout, ip.c_str(), port )
00053 {
00054    if ( ! Net::listen( m_skCom, incomingMax ) ) {
00055       Net::closeSocket( m_skCom );
00056       Net::invalidate( m_skCom );
00057       m_osError = Net::errorCode();
00058       throw InitError( "Can't listen on socket", Net::errorCode() );
00059    }
00060 }
00061 
00062 TCPSocket *ServerSocket::accept()
00063 {
00064    int err = 0;
00065    SockAddrInet addr;
00066    RawSocket incoming;
00067 
00068    while( ! selectFor( false ) ) //read
00069    {
00070       err = Net::errorCode();
00071       if ( Net::errorAgain( err ) ) 
00072          continue;
00073    
00074       if ( err != 0 ) 
00075          break;
00076          
00077       // timed out
00078       m_bTout = true;
00079       m_osError = 0;
00080       return 0;
00081    }
00082 
00083    Net::invalidate( incoming );
00084    while ( err == 0 && ! Net::validSocket( incoming ) )
00085    {         
00086       /* On error (e.g. async connection closed) , com will be -1 and
00087          errno == 22 (invalid argument ) */
00088       incoming = Net::accept( m_skCom, &addr );
00089       if ( ! Net::validSocket( incoming ) ) {
00090          err = Net::errorCode();
00091          if ( ! Net::errorAgain( err ) ) 
00092             err = 0;
00093       }
00094    }
00095 
00096    m_bTout = false;
00097 
00098    if ( err == 0 )
00099    {
00100       TCPSocket *con = new TCPSocket();
00101       con->m_skCom = incoming;
00102       con->m_local.copy( m_local );
00103       con->m_remote.set( addr );
00104       m_osError = 0;
00105 
00106       return con;
00107    }
00108    else {
00109       m_osError = err;
00110       return 0;
00111    }
00112 
00113 }
00114 
00115 } // End of namespace

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