Ignore:
Timestamp:
28/03/2008 22:18:44 (4 years ago)
Author:
chris
Message:

Improve logging with macros that consistently output strerror(errno) and
errno, replacing almost all use of strerror() in the main code.

Log a more detailed error message before throwing an exception for some
more system call failures.

Make FileStream? store its filename on all platforms, not just Windows.

Wrap some long lines at less than 80 characters to improve readability.

Fix some minor violations of coding standard (white space) and a typo
in a comment.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • box/trunk/lib/server/SocketListen.h

    r456 r2115  
    109109#endif 
    110110                        { 
    111                                 THROW_EXCEPTION(ServerException, SocketCloseError) 
     111                                BOX_LOG_SYS_ERROR("Failed to close network " 
     112                                        "socket"); 
     113                                THROW_EXCEPTION(ServerException, 
     114                                        SocketCloseError) 
    112115                        } 
    113116                } 
     
    115118        } 
    116119 
    117         // -------------------------------------------------------------------------- 
     120        // ------------------------------------------------------------------ 
    118121        // 
    119122        // Function 
     
    122125        //              Created: 2003/07/31 
    123126        // 
    124         // -------------------------------------------------------------------------- 
     127        // ------------------------------------------------------------------ 
    125128        void Listen(int Type, const char *Name, int Port = 0) 
    126129        { 
    127                 if(mSocketHandle != -1) {THROW_EXCEPTION(ServerException, SocketAlreadyOpen)} 
     130                if(mSocketHandle != -1) 
     131                { 
     132                        THROW_EXCEPTION(ServerException, SocketAlreadyOpen); 
     133                } 
    128134                 
    129135                // Setup parameters based on type, looking up names if required 
     
    131137                SocketAllAddr addr; 
    132138                int addrLen = 0; 
    133                 Socket::NameLookupToSockAddr(addr, sockDomain, Type, Name, Port, addrLen); 
     139                Socket::NameLookupToSockAddr(addr, sockDomain, Type, Name, 
     140                        Port, addrLen); 
    134141         
    135142                // Create the socket 
    136                 mSocketHandle = ::socket(sockDomain, SOCK_STREAM, 0 /* let OS choose protocol */); 
     143                mSocketHandle = ::socket(sockDomain, SOCK_STREAM, 
     144                        0 /* let OS choose protocol */); 
    137145                if(mSocketHandle == -1) 
    138146                { 
     147                        BOX_LOG_SYS_ERROR("Failed to create a network socket"); 
    139148                        THROW_EXCEPTION(ServerException, SocketOpenError) 
    140149                } 
     
    142151                // Set an option to allow reuse (useful for -HUP situations!) 
    143152#ifdef WIN32 
    144                 if(::setsockopt(mSocketHandle, SOL_SOCKET, SO_REUSEADDR, "", 0) == -1) 
     153                if(::setsockopt(mSocketHandle, SOL_SOCKET, SO_REUSEADDR, "", 
     154                        0) == -1) 
    145155#else 
    146156                int option = true; 
    147                 if(::setsockopt(mSocketHandle, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option)) == -1) 
    148 #endif 
    149                 { 
     157                if(::setsockopt(mSocketHandle, SOL_SOCKET, SO_REUSEADDR, 
     158                        &option, sizeof(option)) == -1) 
     159#endif 
     160                { 
     161                        BOX_LOG_SYS_ERROR("Failed to set socket options"); 
    150162                        THROW_EXCEPTION(ServerException, SocketOpenError) 
    151163                } 
     
    162174        } 
    163175         
    164         // -------------------------------------------------------------------------- 
     176        // ------------------------------------------------------------------ 
    165177        // 
    166178        // Function 
    167179        //              Name:    SocketListen::Accept(int) 
    168         //              Purpose: Accepts a connection, returning a pointer to a class of 
    169         //                               the specified type. May return a null pointer if a signal happens, 
    170         //                               or there's a timeout. Timeout specified in milliseconds, defaults to infinite time. 
     180        //              Purpose: Accepts a connection, returning a pointer to 
     181        //                       a class of the specified type. May return a 
     182        //                       null pointer if a signal happens, or there's 
     183        //                       a timeout. Timeout specified in 
     184        //                       milliseconds, defaults to infinite time. 
    171185        //              Created: 2003/07/31 
    172186        // 
    173         // -------------------------------------------------------------------------- 
    174         std::auto_ptr<SocketType> Accept(int Timeout = INFTIM, std::string *pLogMsg = 0) 
    175         { 
    176                 if(mSocketHandle == -1) {THROW_EXCEPTION(ServerException, BadSocketHandle)} 
     187        // ------------------------------------------------------------------ 
     188        std::auto_ptr<SocketType> Accept(int Timeout = INFTIM, 
     189                std::string *pLogMsg = 0) 
     190        { 
     191                if(mSocketHandle == -1) 
     192                { 
     193                        THROW_EXCEPTION(ServerException, BadSocketHandle); 
     194                } 
    177195                 
    178196                // Do the accept, using the supplied locking type 
     
    186204                        if(!socklock.HaveLock()) 
    187205                        { 
    188                                 // Didn't get the lock for some reason. Wait a while, then 
    189                                 // return nothing. 
     206                                // Didn't get the lock for some reason. 
     207                                // Wait a while, then return nothing. 
     208                                BOX_ERROR("Failed to get a lock on incoming " 
     209                                        "connection"); 
    190210                                ::sleep(1); 
    191211                                return std::auto_ptr<SocketType>(); 
     
    203223                                if(errno == EINTR) 
    204224                                { 
     225                                        BOX_ERROR("Failed to accept " 
     226                                                "connection: interrupted by " 
     227                                                "signal"); 
    205228                                        // return nothing 
    206229                                        return std::auto_ptr<SocketType>(); 
     
    208231                                else 
    209232                                { 
    210                                         THROW_EXCEPTION(ServerException, SocketPollError) 
     233                                        BOX_LOG_SYS_ERROR("Failed to poll " 
     234                                                "connection"); 
     235                                        THROW_EXCEPTION(ServerException, 
     236                                                SocketPollError) 
    211237                                } 
    212238                                break; 
     
    221247                        sock = ::accept(mSocketHandle, &addr, &addrlen); 
    222248                } 
    223                 // Got socket (or error), unlock (implcit in destruction) 
     249 
     250                // Got socket (or error), unlock (implicit in destruction) 
    224251                if(sock == -1) 
    225252                { 
     253                        BOX_LOG_SYS_ERROR("Failed to accept connection"); 
    226254                        THROW_EXCEPTION(ServerException, SocketAcceptError) 
    227255                } 
     
    230258                if(pLogMsg) 
    231259                { 
    232                         *pLogMsg = Socket::IncomingConnectionLogMessage(&addr, addrlen); 
     260                        *pLogMsg = Socket::IncomingConnectionLogMessage(&addr, 
     261                                addrlen); 
    233262                } 
    234263                else 
     
    244273        // on multiple sockets. 
    245274#ifdef HAVE_KQUEUE 
    246         // -------------------------------------------------------------------------- 
     275        // ------------------------------------------------------------------ 
    247276        // 
    248277        // Function 
     
    251280        //              Created: 9/3/04 
    252281        // 
    253         // -------------------------------------------------------------------------- 
     282        // ------------------------------------------------------------------ 
    254283        void FillInKEvent(struct kevent &rEvent, int Flags = 0) const 
    255284        { 
    256                 EV_SET(&rEvent, mSocketHandle, EVFILT_READ, 0, 0, 0, (void*)this); 
     285                EV_SET(&rEvent, mSocketHandle, EVFILT_READ, 0, 0, 0, 
     286                        (void*)this); 
    257287        } 
    258288#else 
    259         // -------------------------------------------------------------------------- 
     289        // ------------------------------------------------------------------ 
    260290        // 
    261291        // Function 
    262292        //              Name:    SocketListen::FillInPoll 
    263         //              Purpose: Fills in the data necessary for a poll operation 
     293        //              Purpose: Fills in the data necessary for a poll 
     294        //                       operation 
    264295        //              Created: 9/3/04 
    265296        // 
    266         // -------------------------------------------------------------------------- 
     297        // ------------------------------------------------------------------ 
    267298        void FillInPoll(int &fd, short &events, int Flags = 0) const 
    268299        { 
Note: See TracChangeset for help on using the changeset viewer.