diff --git a/dap-sdk/net/core/dap_server.c b/dap-sdk/net/core/dap_server.c index fafda1a618675d7ee657a9e4ef3b1576abe4f347..39e43780728a10ba451deddca59d2b2e0316b34f 100644 --- a/dap-sdk/net/core/dap_server.c +++ b/dap-sdk/net/core/dap_server.c @@ -170,6 +170,26 @@ int32_t dap_server_init( uint32_t count_threads ) log_it( L_NOTICE, "Initialized socket server module" ); dap_client_remote_init( ); + + + pthread_t thread_listener[ DAP_MAX_THREADS ]; + + for( uint32_t i = 0; i < _count_threads; ++i ) { + + EPOLL_HANDLE efd = epoll_create1( 0 ); + if ( (intptr_t)efd == -1 ) { + log_it( L_ERROR, "Server wakeup no events / error" ); + goto error; + } + dap_server_threads[ i ].epoll_fd = efd; + dap_server_threads[ i ].thread_num = i; + } + + for( uint32_t i = 0; i < _count_threads; ++i ) { + pthread_create( &thread_listener[i], NULL, thread_loop, &dap_server_threads[i] ); + } + + return 0; err:; @@ -207,6 +227,14 @@ void dap_server_deinit( void ) pthread_mutex_destroy( &dap_server_threads[i].mutex_on_hash ); pthread_mutex_destroy( &dap_server_threads[i].mutex_dlist_add_remove ); + + if ( (intptr_t)dap_server_threads[ i ].epoll_fd != -1 ) { + #ifndef _WIN32 + close( dap_server_threads[ i ].epoll_fd ); + #else + epoll_close( dap_server_threads[ i ].epoll_fd ); + #endif + } } } moduleInit = false; @@ -737,106 +765,73 @@ void *thread_loop( void *arg ) */ int32_t dap_server_loop( dap_server_t *d_server ) { - static uint32_t pickthread = 0; // just for test - pthread_t thread_listener[ DAP_MAX_THREADS ]; + int errCode = 0; - for( uint32_t i = 0; i < _count_threads; ++i ) { - - EPOLL_HANDLE efd = epoll_create1( 0 ); -// log_it( L_ERROR, "EPOLL_HANDLE efd %u for thread %u created", efd, i ); - if ( (intptr_t)efd == -1 ) { - log_it( L_ERROR, "Server wakeup no events / error" ); - goto error; - } - dap_server_threads[ i ].epoll_fd = efd; - dap_server_threads[ i ].thread_num = i; - } - - for( uint32_t i = 0; i < _count_threads; ++i ) { - pthread_create( &thread_listener[i], NULL, thread_loop, &dap_server_threads[i] ); + if(d_server){ + log_it(L_ERROR, "Server is NULL"); + return -1; } _current_run_server = d_server; EPOLL_HANDLE efd = epoll_create1( 0 ); - if ( (intptr_t)efd == -1 ) - goto error; + if ( (intptr_t)efd == -1 ) { + return -10; + } struct epoll_event pev; struct epoll_event events[ 16 ]; - if(d_server){ - memset(&pev, 0, sizeof(pev)); - pev.events = EPOLLIN | EPOLLERR; - pev.data.fd = d_server->socket_listener; + memset(&pev, 0, sizeof(pev)); + pev.events = EPOLLIN | EPOLLERR; + pev.data.fd = d_server->socket_listener; - if( epoll_ctl( efd, EPOLL_CTL_ADD, d_server->socket_listener, &pev) != 0 ) { - log_it( L_ERROR, "epoll_ctl failed 004" ); - goto error; - } + if( epoll_ctl( efd, EPOLL_CTL_ADD, d_server->socket_listener, &pev) != 0 ) { + log_it( L_ERROR, "epoll_ctl failed 004" ); + return -20; } - while( !bQuitSignal ) { - if(d_server){ - int32_t n = epoll_wait( efd, &events[0], 16, -1 ); - - if ( bQuitSignal ) - break; - - if ( n <= 0 ) { - if ( errno == EINTR ) - continue; - log_it( L_ERROR, "Server wakeup no events / error" ); - break; - } + while( !bQuitSignal && errCode == 0 ) { + int32_t n = epoll_wait( efd, &events[0], 16, 1000 ); - for( int32_t i = 0; i < n; ++ i ) { + if ( bQuitSignal ) + break; - if ( events[i].events & EPOLLIN ) { + if ( n < 0 ) { + if ( errno == EINTR ) + continue; + log_it( L_ERROR, "Server wakeup on error: %i", errno ); + errCode = -30; + } - int client_fd = accept( events[i].data.fd, 0, 0 ); + for( int32_t i = 0; i < n && errCode == 0; ++i ) { - if ( client_fd < 0 ) { - log_it( L_ERROR, "accept_cb: error accept socket"); - continue; - } + if ( events[i].events & EPOLLIN ) { + int client_fd = accept( events[i].data.fd, 0, 0 ); - set_nonblock_socket( client_fd ); - dap_server_add_socket( client_fd, -1 ); - } - else if( events[i].events & EPOLLERR ) { - log_it( L_ERROR, "Server socket error event" ); - goto exit; + if ( client_fd < 0 ) { + log_it( L_ERROR, "accept_cb: error accept socket"); + continue; } - } // for - }else{ - static const int c_dap_server_client_mode_tick_rate = 200; - dap_usleep(DAP_USEC_PER_SEC / c_dap_server_client_mode_tick_rate); - } + set_nonblock_socket( client_fd ); + dap_server_add_socket( client_fd, -1 ); + } + else if( events[i].events & EPOLLERR ) { + log_it( L_ERROR, "Server socket error event" ); + errCode = -40; + } + } // for } // while -exit:; - - #ifndef _WIN32 - close( efd ); - #else - epoll_close( efd ); - #endif -error:; - - bQuitSignal = true; - - for( uint32_t i = 0; i < _count_threads; ++i ) { - if ( (intptr_t)dap_server_threads[ i ].epoll_fd != -1 ) { - #ifndef _WIN32 - close( dap_server_threads[ i ].epoll_fd ); - #else - epoll_close( dap_server_threads[ i ].epoll_fd ); - #endif - } + if (efd != -1) { + #ifndef _WIN32 + close( efd ); + #else + epoll_close( efd ); + #endif } - return 0; + return errCode; }