Changeset 491
- Timestamp:
- 04/08/06 19:29:40 (2 years ago)
- Files:
-
- library/trunk/library-core/include/SQLHandler.hpp (modified) (1 diff)
- library/trunk/library-core/include/SQLManager.hpp (modified) (2 diffs)
- library/trunk/library-core/src/ModuleManager.cpp (modified) (3 diffs)
- library/trunk/library-core/src/SQLManager.cpp (modified) (2 diffs)
- library/trunk/library-core/src/SocketManager.cpp (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
library/trunk/library-core/include/SQLHandler.hpp
r474 r491 46 46 47 47 virtual bool runQuery(SQLQuery *query)=0; 48 49 virtual string escapeBinary(string *str)=0; 50 virtual string unescapeBinary(string *str)=0; 51 48 52 49 53 virtual string getSQLHandlerName() library/trunk/library-core/include/SQLManager.hpp
r474 r491 36 36 #include "Manager.hpp" 37 37 38 #define SQL_ESCAPE_BINARY(x) g_Library->getSQLMgr()->escapeBinary((x)) 39 #define SQL_UNESCAPE_BINARY(x) g_Library->getSQLMgr()->unescapeBinary((x)) 40 38 41 using namespace std; 39 42 … … 51 54 52 55 virtual SQLQuery *addQuery(string *query, SQLCallback *callback, void *obj); 56 virtual string escapeBinary(string *str); 57 virtual string unescapeBinary(string *str); 53 58 54 59 virtual bool setSQLHandler(SQLHandler *handler); library/trunk/library-core/src/ModuleManager.cpp
r474 r491 202 202 bool retval=true; 203 203 204 #ifdef WIN32205 HMODULE handle;206 #else207 204 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 *); 211 207 module_init_proc module_init; 212 208 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); 237 210 238 211 if ( handle == NULL ) … … 242 215 return false; 243 216 } 244 #endif 245 246 #ifdef WIN32 247 (FARPROC&) module_init = GetProcAddress(handle, "module_init"); 248 #else 217 218 249 219 module_init = (module_init_proc)dlsym(handle, "module_init"); 250 #endif 251 if ( module_init == NULL )220 221 if ( module_init == NULL ) 252 222 { 253 223 logCrit("%s\n","module_init == NULL" ); 254 224 255 #ifdef WIN32256 FreeLibrary((HMODULE) handle);257 #else258 225 dlclose (handle); 259 #endif260 261 226 return false; 262 227 } … … 265 230 if ( module_init (MODULE_IFACE_VERSION, &newmodule, m_Library) != 1 ) 266 231 { 267 268 232 logCrit("%s\n","module_init() != 1" ); 269 #ifdef WIN32270 271 #else272 233 dlclose (handle); 273 #endif274 234 return false; 275 235 } library/trunk/library-core/src/SQLManager.cpp
r474 r491 55 55 { 56 56 logPF(); 57 logSpam("Query %s\nCallback %x\n",query->c_str(),callback); 57 58 SQLQuery *sqlquery = new SQLQuery(query,callback, obj); 58 59 m_SQLHandler->runQuery(sqlquery); … … 95 96 } 96 97 } 98 99 string SQLManager::escapeBinary(string *str) 100 { 101 return m_SQLHandler->escapeBinary(str); 102 } 103 104 string SQLManager::unescapeBinary(string *str) 105 { 106 return m_SQLHandler->unescapeBinary(str); 107 } 108 library/trunk/library-core/src/SocketManager.cpp
r474 r491 28 28 /* $Id$ */ 29 29 30 #ifdef WIN3231 #include <winsock2.h>32 #include <ws2tcpip.h>33 #include <iostream.h>34 #else35 30 36 31 #include <poll.h> … … 42 37 #include <arpa/inet.h> 43 38 #include <sys/ioctl.h> 44 #endif45 39 46 40 … … 112 106 bool SocketManager::Init() 113 107 { 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 125 108 try { 126 109 string bindAddressString = m_Library->getConfig()->getValString("library.socketmanager.bind_address"); 127 110 128 #ifdef __linux__111 #ifdef __linux__ 129 112 if(bindAddressString.substr(0, 3) == string("if:")) 130 113 { … … 149 132 150 133 } else 151 #endif134 #endif 152 135 { 153 136 m_BindAddress = inet_addr(bindAddressString.c_str()); … … 162 145 } 163 146 164 165 #ifdef WIN32166 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 }else179 {180 logDebug("%s\n","WSAStartup worked");181 }182 #endif183 147 184 148 return true; … … 214 178 * else false 215 179 */ 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 220 181 bool SocketManager::doLoop(uint32_t polltimeout) 221 {// FIXME .. 182 { 183 184 185 186 222 187 list <Socket *>::iterator itSocket; 223 188 … … 250 215 } 251 216 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)); 259 219 int32_t i=0; 260 220 for (itSocket = m_Sockets.begin();itSocket != m_Sockets.end(); itSocket++) … … 277 237 { 278 238 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 281 245 break; 282 246 283 case WSAEINPROGRESS: // der socket versuchts247 case EINPROGRESS: // der socket versuchts 284 248 (*itSocket)->unsetPolled(); 285 249 break; 286 250 287 case WSAEISCONN:288 (*itSocket)->setPolled(); // der socket ist am start289 break;290 291 251 292 252 default: … … 297 257 298 258 i=0; 299 int32_t maxsock=-1;300 259 for (itSocket = m_Sockets.begin();itSocket != m_Sockets.end(); itSocket++) 301 { 260 { 261 polls[i].events = 0; 302 262 if ((*itSocket)->isPolled() == true ) 303 263 { 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; 310 266 311 267 if ((*itSocket)->wantSend() == true) 312 268 { 313 FD_SET((*itSocket)->getSocket(),&wfds); 314 }/*else 315 logSpam("polling %s readonly\n",(*itSocket)->getDescription().c_str());*/ 269 polls[i].events |= POLLOUT; 270 } 316 271 i++; 317 272 } 318 273 } 319 274 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); 325 276 326 277 if (iPollRet != 0) … … 337 288 ) 338 289 { 339 if ( FD_ISSET((*itSocket)->getSocket(),&rfds) ) 290 if ( iPollRet == 0 ) 291 continue; 292 293 if ( polls[i].revents & POLLIN && polls[i].events & POLLIN ) 340 294 { 341 295 (*itSocket)->doRecv(); 296 iPollRet--; 342 297 } 343 298 } … … 352 307 if ( (*itSocket)->isPolled() == true ) 353 308 { 354 // logSpam(" COuld write #%i\n",1);355 // doRecv() can close sockets356 // we need a valid way to verify we dont try to send on a closed socket,357 // i think wantSend() is a good option here358 // getStatus i just a cheap fix359 // logInfo("SSS %s \n",(*itSocket)->getDescription().c_str());360 309 if ( 361 310 ( (*itSocket)->getStatus() == SS_CONNECTED || (*itSocket)->getStatus() == SS_CLEANQUIT ) && … … 369 318 ) 370 319 { 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 recvfrom397 {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 } else406 {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 #else422 423 /**424 * poll the sockets425 *426 * @param polltimeout427 * the polltimeout428 *429 * @return returns true430 */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 sockets440 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 }else478 if ((*itSocket)->getsockOpt(SOL_SOCKET, SO_ERROR, &iError,(socklen_t *) &iSize) != 0 )479 {480 // socket is dead481 logSpam("Socket %s is Dead\n",(*itSocket)->getDescription().c_str());482 (*itSocket)->unsetPolled();483 484 } else485 {486 switch (iError)487 {488 case 0: // der socket is soweit okay489 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 start496 break;497 498 case EINPROGRESS: // der socket versuchts499 (*itSocket)->unsetPolled();500 break;501 502 503 default:504 (*itSocket)->unsetPolled(); // der is defekt505 }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 }/*else527 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 sockets537 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 recvfrom545 )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 sockets561 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 sockets568 // we need a valid way to verify we dont try to send on a closed socket,569 // i think wantSend() is a good option here570 // getStatus i just a cheap fix571 // 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_UDP579 )580 )581 )582 {583 // if ( iPollRet == 0 )584 // continue;585 // logSpam(" COuld write #%i\n",2);586 320 if ( polls[i].revents & POLLOUT && polls[i].events & POLLOUT ) 587 321 { 588 // logSpam(" COuld write #%i\n",3);589 322 (*itSocket)->doSend(); 590 323 iPollRet--; … … 632 365 } 633 366 free(polls); 634 // sleep(1); 367 635 368 return true; 636 369 } 637 #endif638 370 639 371 /**
