Loading src/event/kqueue.cpp +55 −21 Changes for src/event/kqueue.cpp: 55 added lines, 21 removed lines. Original line number Diff line number Diff line Loading @@ -172,62 +172,96 @@ namespace netplus { void ConnectEventHandler(int pos, const int tid, ULONG_PTR args) { NetException exception; // DRAIN THE LISTEN QUEUE: Loop to accept all waiting connections. while (true) { // The ccon unique_ptr scope is inside the loop, ensuring it is destroyed // if setup fails before being released into the CONNECTIONS map. std::unique_ptr<con> ccon; // 1. Create the base connection object evconnection->CreateConnection(ccon); // 1. Allocate the socket object if(!ccon) { // Should not happen, but exit if connection wrapper creation failed return; } // 2. Initialize the socket wrapper based on server type if(_ServerSocket->_Type==sockettype::TCP){ ccon->csock=std::make_unique<tcp>(-1); } // ... handle other types ... // 2. Attempt to accept the connection int accept_status = _ServerSocket->accept(ccon->csock); } else if(_ServerSocket->_Type==sockettype::UDP){ ccon->csock=std::make_unique<udp>(-1); } else if(_ServerSocket->_Type==sockettype::SSL){ // Assuming an SSL type exists and is handled correctly // ccon->csock=std::make_unique<ssl>(-1); } // If no supported socket type was initialized, skip this iteration/exit function if (!ccon->csock) { // Since we don't know how other types are handled, we stop the loop // in a production environment, this should log a critical error. std::cerr << "ConnectEventHandler: Unsupported socket type on server." << std::endl; return; } // Check accept status *before* checking ccon validity if (accept_status < 0) { if (errno == EAGAIN || errno == EWOULDBLOCK) { // Queue drained. Exit the loop. break; try{ // 3. Attempt to accept the new connection _ServerSocket->accept(ccon->csock); }catch(NetException &e){ // Check for the non-fatal error (EAGAIN/EWOULDBLOCK) wrapped in NetException::Note. if (e.getErrorType()==NetException::Note) { // This means the listen queue is empty. Break the accept loop. return; } // Handle fatal accept errors // Handle fatal accept errors (e.g., resource exhaustion) char errstr[255]; strerror_r_netplus(errno,errstr,255); std::cerr << "ConnectEventHandler: Failed to accept connection: " << errstr << std::endl; return; } // 3. Post-Accept setup (original logic) // --- SETUP ACCEPTED CONNECTION (The rest of the original logic) --- // Set accepted socket to non-blocking ccon->csock->setFlag(O_NONBLOCK,1); // ... (logging, etc.) // Log IP std::string ip; ccon->csock->getAddress(ip); std::cout << "Connected: " << ip << std::endl; ccon->lasteventime = time(nullptr); // 4. Register client socket for reading (kqueue setup) // Register client socket with kqueue for read events struct kevent setevent {}; setevent.udata = ccon.get(); // EV_ONESHOT is used, so we must re-register after every R/W operation EV_SET(&setevent, ccon->csock->fd(), EVFILT_READ, EV_ADD | EV_ONESHOT,0,0,ccon.get()); int estate = kevent(_pollFD, &setevent, 1, nullptr, 0, nullptr); if ( estate < 0 ) { // Fatal kqueue error, must close the accepted socket and throw // ... (error handling code, which must clean up ccon) // For now, let's assume the accepted socket is cleaned up by ccon's unique_ptr if released fails continue; // Skip to next accept attempt char errstr[255]; strerror_r_netplus(errno,errstr,255); exception[NetException::Error] << "ConnectEventHandler: can't add socket to kqueue: " << errstr; // Throw error to close the worker thread throw exception; } // 5. Finalize connection (call event and insert into map) // Call user-defined connection event handler evconnection ->ConnectEvent(*ccon,tid,args); // Release the unique_ptr and insert the raw pointer into the global map { std::lock_guard<std::mutex> clock(ccon->event_mutex),global_lock(POLL_HANDLER_MUTEX); CONNECTIONS.emplace(ccon->csock->fd(),ccon.release()); } // Loop continues to accept the next connection from the queue } // end while(true) }; } } void ReadEventHandler(int pos,const int tid,ULONG_PTR args) { // Get pointer directly from udata. Loading src/posix/tcp.cpp +1 −1 Changes for src/posix/tcp.cpp: 1 added line, 1 removed line. Original line number Diff line number Diff line Loading @@ -162,7 +162,7 @@ void netplus::tcp::accept(std::unique_ptr<socket> &csock){ if(csock->_Socket<0){ int etype=NetException::Error; if(errno==EAGAIN) if(errno==EAGAIN || EWOULDBLOCK) etype=NetException::Note; char errstr[512]; strerror_r_netplus(errno,errstr,512); Loading Loading
src/event/kqueue.cpp +55 −21 Changes for src/event/kqueue.cpp: 55 added lines, 21 removed lines. Original line number Diff line number Diff line Loading @@ -172,62 +172,96 @@ namespace netplus { void ConnectEventHandler(int pos, const int tid, ULONG_PTR args) { NetException exception; // DRAIN THE LISTEN QUEUE: Loop to accept all waiting connections. while (true) { // The ccon unique_ptr scope is inside the loop, ensuring it is destroyed // if setup fails before being released into the CONNECTIONS map. std::unique_ptr<con> ccon; // 1. Create the base connection object evconnection->CreateConnection(ccon); // 1. Allocate the socket object if(!ccon) { // Should not happen, but exit if connection wrapper creation failed return; } // 2. Initialize the socket wrapper based on server type if(_ServerSocket->_Type==sockettype::TCP){ ccon->csock=std::make_unique<tcp>(-1); } // ... handle other types ... // 2. Attempt to accept the connection int accept_status = _ServerSocket->accept(ccon->csock); } else if(_ServerSocket->_Type==sockettype::UDP){ ccon->csock=std::make_unique<udp>(-1); } else if(_ServerSocket->_Type==sockettype::SSL){ // Assuming an SSL type exists and is handled correctly // ccon->csock=std::make_unique<ssl>(-1); } // If no supported socket type was initialized, skip this iteration/exit function if (!ccon->csock) { // Since we don't know how other types are handled, we stop the loop // in a production environment, this should log a critical error. std::cerr << "ConnectEventHandler: Unsupported socket type on server." << std::endl; return; } // Check accept status *before* checking ccon validity if (accept_status < 0) { if (errno == EAGAIN || errno == EWOULDBLOCK) { // Queue drained. Exit the loop. break; try{ // 3. Attempt to accept the new connection _ServerSocket->accept(ccon->csock); }catch(NetException &e){ // Check for the non-fatal error (EAGAIN/EWOULDBLOCK) wrapped in NetException::Note. if (e.getErrorType()==NetException::Note) { // This means the listen queue is empty. Break the accept loop. return; } // Handle fatal accept errors // Handle fatal accept errors (e.g., resource exhaustion) char errstr[255]; strerror_r_netplus(errno,errstr,255); std::cerr << "ConnectEventHandler: Failed to accept connection: " << errstr << std::endl; return; } // 3. Post-Accept setup (original logic) // --- SETUP ACCEPTED CONNECTION (The rest of the original logic) --- // Set accepted socket to non-blocking ccon->csock->setFlag(O_NONBLOCK,1); // ... (logging, etc.) // Log IP std::string ip; ccon->csock->getAddress(ip); std::cout << "Connected: " << ip << std::endl; ccon->lasteventime = time(nullptr); // 4. Register client socket for reading (kqueue setup) // Register client socket with kqueue for read events struct kevent setevent {}; setevent.udata = ccon.get(); // EV_ONESHOT is used, so we must re-register after every R/W operation EV_SET(&setevent, ccon->csock->fd(), EVFILT_READ, EV_ADD | EV_ONESHOT,0,0,ccon.get()); int estate = kevent(_pollFD, &setevent, 1, nullptr, 0, nullptr); if ( estate < 0 ) { // Fatal kqueue error, must close the accepted socket and throw // ... (error handling code, which must clean up ccon) // For now, let's assume the accepted socket is cleaned up by ccon's unique_ptr if released fails continue; // Skip to next accept attempt char errstr[255]; strerror_r_netplus(errno,errstr,255); exception[NetException::Error] << "ConnectEventHandler: can't add socket to kqueue: " << errstr; // Throw error to close the worker thread throw exception; } // 5. Finalize connection (call event and insert into map) // Call user-defined connection event handler evconnection ->ConnectEvent(*ccon,tid,args); // Release the unique_ptr and insert the raw pointer into the global map { std::lock_guard<std::mutex> clock(ccon->event_mutex),global_lock(POLL_HANDLER_MUTEX); CONNECTIONS.emplace(ccon->csock->fd(),ccon.release()); } // Loop continues to accept the next connection from the queue } // end while(true) }; } } void ReadEventHandler(int pos,const int tid,ULONG_PTR args) { // Get pointer directly from udata. Loading
src/posix/tcp.cpp +1 −1 Changes for src/posix/tcp.cpp: 1 added line, 1 removed line. Original line number Diff line number Diff line Loading @@ -162,7 +162,7 @@ void netplus::tcp::accept(std::unique_ptr<socket> &csock){ if(csock->_Socket<0){ int etype=NetException::Error; if(errno==EAGAIN) if(errno==EAGAIN || EWOULDBLOCK) etype=NetException::Note; char errstr[512]; strerror_r_netplus(errno,errstr,512); Loading