Changeset 491

Show
Ignore:
Timestamp:
04/08/06 19:29:40 (2 years ago)
Author:
common
Message:

library
- SQLManager & SQLHandler offer unescape for binary data
- SocketManager? remove WIN32 compatibility
- ModuleManager? remove WIN32 compatibility

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • library/trunk/library-core/include/SQLHandler.hpp

    r474 r491  
    4646 
    4747                virtual bool runQuery(SQLQuery *query)=0; 
     48                 
     49                virtual string escapeBinary(string *str)=0; 
     50                virtual string unescapeBinary(string *str)=0; 
     51 
    4852 
    4953                virtual string getSQLHandlerName() 
  • library/trunk/library-core/include/SQLManager.hpp

    r474 r491  
    3636#include "Manager.hpp" 
    3737 
     38#define SQL_ESCAPE_BINARY(x)    g_Library->getSQLMgr()->escapeBinary((x)) 
     39#define SQL_UNESCAPE_BINARY(x)  g_Library->getSQLMgr()->unescapeBinary((x)) 
     40 
    3841using namespace std; 
    3942 
     
    5154 
    5255                virtual SQLQuery *addQuery(string *query, SQLCallback *callback, void *obj); 
     56                virtual string escapeBinary(string *str); 
     57                virtual string unescapeBinary(string *str); 
    5358 
    5459                virtual bool setSQLHandler(SQLHandler *handler); 
  • library/trunk/library-core/src/ModuleManager.cpp

    r474 r491  
    202202        bool retval=true; 
    203203 
    204 #ifdef WIN32 
    205         HMODULE handle; 
    206 #else 
    207204    void *handle; 
    208 #endif 
    209  
    210     typedef int32_t (*module_init_proc)(int32_t, Module**, Library *); 
     205 
     206        typedef int32_t (*module_init_proc)(int32_t, Module**, Library *); 
    211207    module_init_proc module_init; 
    212208 
    213 #ifdef WIN32 
    214         handle = LoadLibrary(modulepath->c_str()); 
    215     if ( handle == NULL ) 
    216     { 
    217  
    218                 LPVOID lpMsgBuf; 
    219                 FormatMessage(  
    220                         FORMAT_MESSAGE_ALLOCATE_BUFFER |  
    221                         FORMAT_MESSAGE_FROM_SYSTEM |  
    222                         FORMAT_MESSAGE_IGNORE_INSERTS, 
    223                         NULL, 
    224                         GetLastError(), 
    225                         0, // Default language 
    226                         (LPTSTR) &lpMsgBuf, 
    227                         0, 
    228                         NULL  
    229                 ); 
    230         printf("LoadLibary %s\n",(char *)lpMsgBuf); 
    231         logCrit("%s\n","handle == NULL "); 
    232         return false; 
    233     } 
    234  
    235 #else     
    236         handle = dlopen (modulepath->c_str(), RTLD_NOW); 
     209        handle = dlopen (modulepath->c_str(), RTLD_NOW|RTLD_LOCAL); 
    237210 
    238211    if ( handle == NULL ) 
     
    242215        return false; 
    243216    } 
    244 #endif 
    245  
    246 #ifdef WIN32 
    247         (FARPROC&) module_init = GetProcAddress(handle, "module_init"); 
    248 #else 
     217 
     218 
    249219    module_init = (module_init_proc)dlsym(handle, "module_init"); 
    250 #endif 
    251     if ( module_init == NULL ) 
     220 
     221       if ( module_init == NULL ) 
    252222    { 
    253223        logCrit("%s\n","module_init == NULL" ); 
    254224 
    255 #ifdef WIN32 
    256                 FreeLibrary((HMODULE) handle); 
    257 #else 
    258225        dlclose (handle); 
    259 #endif 
    260  
    261226        return false; 
    262227    } 
     
    265230    if ( module_init (MODULE_IFACE_VERSION, &newmodule, m_Library) != 1 ) 
    266231    { 
    267  
    268232        logCrit("%s\n","module_init() != 1" ); 
    269 #ifdef WIN32 
    270  
    271 #else 
    272233        dlclose (handle); 
    273 #endif 
    274234        return false; 
    275235    } 
  • library/trunk/library-core/src/SQLManager.cpp

    r474 r491  
    5555{ 
    5656        logPF(); 
     57        logSpam("Query %s\nCallback %x\n",query->c_str(),callback); 
    5758        SQLQuery *sqlquery = new SQLQuery(query,callback, obj); 
    5859        m_SQLHandler->runQuery(sqlquery); 
     
    9596        } 
    9697} 
     98 
     99string SQLManager::escapeBinary(string *str) 
     100{ 
     101        return m_SQLHandler->escapeBinary(str); 
     102} 
     103 
     104string SQLManager::unescapeBinary(string *str) 
     105{ 
     106        return m_SQLHandler->unescapeBinary(str); 
     107} 
     108 
  • library/trunk/library-core/src/SocketManager.cpp

    r474 r491  
    2828/* $Id$ */ 
    2929 
    30 #ifdef WIN32 
    31 #include <winsock2.h> 
    32 #include <ws2tcpip.h> 
    33 #include <iostream.h> 
    34 #else 
    3530 
    3631#include <poll.h> 
     
    4237#include <arpa/inet.h> 
    4338#include <sys/ioctl.h> 
    44 #endif 
    4539 
    4640 
     
    112106bool  SocketManager::Init() 
    113107{ 
    114     try { 
    115         m_UseRawSockets = m_Library->getConfig()->getValInt("library.socketmanager.use_rawsockets"); 
    116         if (m_UseRawSockets) 
    117         { 
    118             logInfo("%s","Using Rawsockets\n"); 
    119         } 
    120     } catch ( ... ) { 
    121         logCrit("%s","Could not find library.socketmanager.use_rawsockets in config file, assuming no\n"); 
    122     } 
    123  
    124  
    125108        try { 
    126109                string bindAddressString = m_Library->getConfig()->getValString("library.socketmanager.bind_address"); 
    127110                 
    128                #ifdef __linux__ 
     111#ifdef __linux__ 
    129112                if(bindAddressString.substr(0, 3) == string("if:")) 
    130113                { 
     
    149132                         
    150133                } else 
    151                #endif 
     134#endif 
    152135                { 
    153136                        m_BindAddress = inet_addr(bindAddressString.c_str()); 
     
    162145        } 
    163146 
    164  
    165 #ifdef WIN32 
    166         WORD wVersionRequested; 
    167         WSADATA wsaData; 
    168         int32_t err; 
    169   
    170         wVersionRequested = MAKEWORD( 2, 2 ); 
    171   
    172         err = WSAStartup( wVersionRequested, &wsaData ); 
    173         if ( err != 0 ) { 
    174                 /* Tell the user that we could not find a usable */ 
    175                 /* WinSock DLL.                                  */ 
    176                 logCrit("%s\n","Could not find good Windows Socket dll"); 
    177                 return false; 
    178         }else 
    179         { 
    180                 logDebug("%s\n","WSAStartup worked"); 
    181         } 
    182 #endif 
    183147 
    184148        return true; 
     
    214178 *         else false 
    215179 */ 
    216 #ifdef WIN32 
    217         #error "i changed some stuff and found no reason to port it" 
    218         #error "to be precise, the setStatus thing for connected sockets" 
    219          
     180 
    220181bool SocketManager::doLoop(uint32_t polltimeout) 
    221 {// FIXME .. 
     182
     183 
     184 
     185 
     186 
    222187        list <Socket *>::iterator itSocket; 
    223188 
     
    250215        } 
    251216 
    252         fd_set rfds; 
    253         fd_set wfds; 
    254  
    255         FD_ZERO(&rfds); 
    256         FD_ZERO(&wfds); 
    257  
    258  
     217        pollfd *polls = (pollfd *) malloc( (m_Sockets.size())* sizeof(pollfd)); 
     218        memset(polls,0,(m_Sockets.size())* sizeof(pollfd)); 
    259219        int32_t i=0; 
    260220        for (itSocket = m_Sockets.begin();itSocket != m_Sockets.end(); itSocket++) 
     
    277237                        { 
    278238                        case 0: // der socket is soweit okay 
    279 //                              logSpam("Socket %s is OK\n",(*itSocket)->getDescription().c_str()); 
    280                                 (*itSocket)->setPolled(); 
     239                        case EISCONN: 
     240                                if ((*itSocket)->getStatus() == SS_CONNECTING) 
     241                                { 
     242                                        (*itSocket)->setStatus(SS_CONNECTED); 
     243                                } 
     244                                (*itSocket)->setPolled();       // der socket ist am start 
    281245                                break; 
    282246 
    283                         case WSAEINPROGRESS: // der socket versuchts 
     247                        case EINPROGRESS: // der socket versuchts 
    284248                                (*itSocket)->unsetPolled(); 
    285249                                break; 
    286250 
    287                         case WSAEISCONN: 
    288                                 (*itSocket)->setPolled();       // der socket ist am start 
    289                                 break; 
    290  
    291251 
    292252                        default: 
     
    297257 
    298258        i=0; 
    299         int32_t maxsock=-1; 
    300259        for (itSocket = m_Sockets.begin();itSocket != m_Sockets.end(); itSocket++) 
    301         {        
     260        { 
     261                polls[i].events = 0; 
    302262                if ((*itSocket)->isPolled() == true ) 
    303263                { 
    304                         if ((*itSocket)->getSocket() > maxsock) 
    305                         { 
    306                                 maxsock = (*itSocket)->getSocket(); 
    307                         } 
    308  
    309                         FD_SET((*itSocket)->getSocket(),&rfds); 
     264                        polls[i].fd = (*itSocket)->getSocket(); 
     265                        polls[i].events = POLLIN; 
    310266 
    311267                        if ((*itSocket)->wantSend() == true) 
    312268                        { 
    313                                 FD_SET((*itSocket)->getSocket(),&wfds); 
    314                         }/*else 
    315                                 logSpam("polling %s readonly\n",(*itSocket)->getDescription().c_str());*/ 
     269                                polls[i].events |= POLLOUT; 
     270                        } 
    316271                        i++; 
    317272                } 
    318273        } 
    319274 
    320         struct timeval tv; 
    321         tv.tv_sec = 2; 
    322         tv.tv_usec = 500000; 
    323  
    324         int32_t iPollRet = select(maxsock,&rfds,&wfds,NULL,&tv); 
     275        int32_t iPollRet = poll(polls,i,50); 
    325276 
    326277        if (iPollRet != 0) 
     
    337288                                   ) 
    338289                                { 
    339                                         if ( FD_ISSET((*itSocket)->getSocket(),&rfds) ) 
     290                                        if ( iPollRet == 0 ) 
     291                                                continue; 
     292 
     293                                        if ( polls[i].revents & POLLIN && polls[i].events & POLLIN ) 
    340294                                        { 
    341295                                                (*itSocket)->doRecv(); 
     296                                                iPollRet--; 
    342297                                        } 
    343298                                } 
     
    352307                        if ( (*itSocket)->isPolled() == true ) 
    353308                        { 
    354 //                              logSpam(" COuld write #%i\n",1); 
    355                                 // doRecv() can close sockets 
    356                                 // we need a valid way to verify we dont try to send on a closed socket,  
    357                                 // i think wantSend() is a good option here 
    358                                 // getStatus i just a cheap fix 
    359 //                              logInfo("SSS %s \n",(*itSocket)->getDescription().c_str()); 
    360309                                if ( 
    361310                    ( (*itSocket)->getStatus() == SS_CONNECTED || (*itSocket)->getStatus() == SS_CLEANQUIT ) &&   
     
    369318                                   ) 
    370319                                { 
    371 //                                      if ( iPollRet == 0 ) 
    372 //                                              continue; 
    373 //                                      logSpam(" COuld write #%i\n",2); 
    374                                         if (FD_ISSET((*itSocket)->getSocket(),&wfds)) 
    375                                         { 
    376 //                                              logSpam(" COuld write #%i\n",3); 
    377                                                 (*itSocket)->doSend(); 
    378  
    379                                         } 
    380                                 } 
    381                                 i++; 
    382                         } 
    383                 } 
    384  
    385  
    386                 // accept new, non udp clients as udp does not accept() 
    387                 i=0; 
    388                 for (itSocket = m_Sockets.begin();itSocket != m_Sockets.end(); itSocket++) 
    389                 { 
    390                                  
    391  
    392                         if ( (*itSocket)->isPolled() == true ) 
    393                         { 
    394                                 if ( (*itSocket)->isBind() ) 
    395                                 { 
    396                                         if ( !((*itSocket)->getType() & ST_UDP) ) // bound udp sockets dont accept, they recvfrom 
    397                                         { 
    398                                                 if (FD_ISSET((*itSocket)->getSocket(),&rfds)) 
    399                                                 { 
    400                                                         logDebug("%s could Accept a Connection\n",(*itSocket)->getDescription().c_str()); 
    401                                                         Socket * socket = (*itSocket)->acceptConnection(); 
    402                                                         if ( socket == NULL ) 
    403                                                         { 
    404                                                                 logCrit("%s","Accept returned NULL ptr \n"); 
    405                                                         } else 
    406                                                         { 
    407                                                                 m_Sockets.push_back(socket); 
    408                                                                 logDebug("Accepted Connection %s \n%i Sockets in list\n",socket->getDescription().c_str(), m_Sockets.size()); 
    409                                                         } 
    410                                                 } 
    411                                         } 
    412                                 } 
    413                                 i++; 
    414                         } 
    415                 } 
    416         } 
    417 //      free(polls); 
    418 //      sleep(1); 
    419         return true; 
    420 } 
    421 #else 
    422  
    423 /** 
    424  * poll the sockets 
    425  *  
    426  * @param polltimeout 
    427  *               the polltimeout 
    428  *  
    429  * @return returns true 
    430  */ 
    431 bool SocketManager::doLoop(uint32_t polltimeout) 
    432 {// FIXME .. 
    433  
    434  
    435  
    436  
    437         list <Socket *>::iterator itSocket; 
    438  
    439 //      check socket timeouts and remove dead sockets 
    440         for (itSocket = m_Sockets.begin();itSocket != m_Sockets.end(); itSocket++) 
    441         { 
    442                 (*itSocket)->checkTimeout(); 
    443                 if ((*itSocket)->getStatus() == SS_TIMEOUT ) 
    444                 { 
    445                         logDebug("Deleting Socket %s due to timeout \n",(*itSocket)->getDescription().c_str()); 
    446                         Socket *delsocket = *itSocket; 
    447                         m_Sockets.erase(itSocket); 
    448                         delete delsocket; 
    449  
    450                         itSocket = m_Sockets.begin(); // FIXME ? 
    451                         if(m_Sockets.size() == 0) 
    452                 return false; 
    453                          
    454  
    455                 } 
    456  
    457                 if ( (*itSocket)->getStatus() == SS_CLOSED ) 
    458                 { 
    459                         logDebug("Deleting %s due to closed connection \n",(*itSocket)->getDescription().c_str()); 
    460                         Socket *delsocket = *itSocket; 
    461                         m_Sockets.erase(itSocket); 
    462                         delete delsocket; 
    463                         itSocket = m_Sockets.begin(); // FIXME ? 
    464                 } 
    465         } 
    466  
    467         pollfd *polls = (pollfd *) malloc( (m_Sockets.size())* sizeof(pollfd)); 
    468         memset(polls,0,(m_Sockets.size())* sizeof(pollfd)); 
    469         int32_t i=0; 
    470         for (itSocket = m_Sockets.begin();itSocket != m_Sockets.end(); itSocket++) 
    471         { 
    472                 int32_t iError = 0; 
    473                 int32_t iSize = sizeof(iError); 
    474                 if((*itSocket)->getType() & ST_FILE) 
    475                 { 
    476                         (*itSocket)->setPolled(); 
    477                 }else 
    478                 if ((*itSocket)->getsockOpt(SOL_SOCKET, SO_ERROR, &iError,(socklen_t *) &iSize) != 0 ) 
    479                 { 
    480                         // socket is dead 
    481                         logSpam("Socket %s is Dead\n",(*itSocket)->getDescription().c_str()); 
    482                         (*itSocket)->unsetPolled(); 
    483  
    484                 } else 
    485                 { 
    486                         switch (iError) 
    487                         { 
    488                         case 0: // der socket is soweit okay 
    489                         case EISCONN: 
    490 //                              logSpam("EISCONN State %i \n",(*itSocket)->getStatus()); 
    491                                 if ((*itSocket)->getStatus() == SS_CONNECTING) 
    492                                 { 
    493                                         (*itSocket)->setStatus(SS_CONNECTED); 
    494                                 } 
    495                                 (*itSocket)->setPolled();       // der socket ist am start 
    496                                 break; 
    497  
    498                         case EINPROGRESS: // der socket versuchts 
    499                                 (*itSocket)->unsetPolled(); 
    500                                 break; 
    501  
    502  
    503                         default: 
    504                                 (*itSocket)->unsetPolled();             // der is defekt 
    505                         } 
    506                 } 
    507         } 
    508  
    509         i=0; 
    510         for (itSocket = m_Sockets.begin();itSocket != m_Sockets.end(); itSocket++) 
    511         { 
    512                 polls[i].events = 0; 
    513                 if ((*itSocket)->isPolled() == true ) 
    514                 { 
    515                         polls[i].fd = (*itSocket)->getSocket(); 
    516  
    517                         polls[i].events = POLLIN; 
    518  
    519                          
    520  
    521                         if ((*itSocket)->wantSend() == true) 
    522                         { 
    523 //                              logSpam("polling %s read|write\n",(*itSocket)->getDescription().c_str()); 
    524                                          
    525                                 polls[i].events |= POLLOUT; 
    526                         }/*else 
    527                                 logSpam("polling %s readonly\n",(*itSocket)->getDescription().c_str());*/ 
    528                         i++; 
    529                 } 
    530         } 
    531  
    532         int32_t iPollRet = poll(polls,i,50); 
    533  
    534         if (iPollRet != 0) 
    535         { 
    536                 // read sockets 
    537                 i=0; 
    538                 for (itSocket = m_Sockets.begin();itSocket != m_Sockets.end(); itSocket++) 
    539                 { 
    540                         if ( (*itSocket)->isPolled() == true ) 
    541                         { 
    542                                 if (  
    543                                    ( (*itSocket)->isAccept()  || (*itSocket)->isConnect() ) || 
    544                                    (  (*itSocket)->isBind() && (*itSocket)->getType() & ST_UDP)   // bound udp sockets dont accept, they recvfrom 
    545                                    ) 
    546                                 { 
    547                                         if ( iPollRet == 0 ) 
    548                                                 continue; 
    549  
    550                                         if ( polls[i].revents & POLLIN && polls[i].events & POLLIN ) 
    551                                         { 
    552                                                 (*itSocket)->doRecv(); 
    553                                                 iPollRet--; 
    554                                         } 
    555                                 } 
    556                                 i++; 
    557                         } 
    558                 } 
    559  
    560                 // write sockets 
    561                 i=0; 
    562                 for (itSocket = m_Sockets.begin();itSocket != m_Sockets.end(); itSocket++) 
    563                 { 
    564                         if ( (*itSocket)->isPolled() == true ) 
    565                         { 
    566 //                              logSpam(" COuld write #%i\n",1); 
    567                                 // doRecv() can close sockets 
    568                                 // we need a valid way to verify we dont try to send on a closed socket,  
    569                                 // i think wantSend() is a good option here 
    570                                 // getStatus i just a cheap fix 
    571 //                              logDebug("SSS %s \n",(*itSocket)->getDescription().c_str()); 
    572                                 if ( 
    573                     ( (*itSocket)->getStatus() == SS_CONNECTED || (*itSocket)->getStatus() == SS_CLEANQUIT ) &&   
    574                                     ( 
    575                                      (*itSocket)->isAccept() || 
    576                                      (*itSocket)->isConnect() ||  
    577                                      ( 
    578                                       (*itSocket)->isBind() && (*itSocket)->getType() & ST_UDP 
    579                                      ) 
    580                                     )   
    581                                    ) 
    582                                 { 
    583 //                                      if ( iPollRet == 0 ) 
    584 //                                              continue; 
    585 //                                      logSpam(" COuld write #%i\n",2); 
    586320                                        if ( polls[i].revents & POLLOUT && polls[i].events & POLLOUT ) 
    587321                                        { 
    588 //                                              logSpam(" COuld write #%i\n",3); 
    589322                                                (*itSocket)->doSend(); 
    590323                                                iPollRet--; 
     
    632365        } 
    633366        free(polls); 
    634 //      sleep(1); 
     367 
    635368        return true; 
    636369} 
    637 #endif 
    638370 
    639371/**