Important Update: (Massive CPU usage improvement)
- Bug fix: Select timeout not constant, reduces to zero - Only scan FDs if Select event occured
This commit is contained in:
@@ -160,13 +160,14 @@ bool CSelect::Test()
|
|||||||
TSelectHandle ** HandlePtr = NULL;
|
TSelectHandle ** HandlePtr = NULL;
|
||||||
int TestFD = 0;
|
int TestFD = 0;
|
||||||
int Events = 0;
|
int Events = 0;
|
||||||
|
timeval STimeout = Timeout;
|
||||||
|
|
||||||
// Set Test sets
|
// Set Test sets
|
||||||
ReadFDS = ReadTestFDS;
|
ReadFDS = ReadTestFDS;
|
||||||
WriteFDS = WriteTestFDS;
|
WriteFDS = WriteTestFDS;
|
||||||
|
|
||||||
// Perform select
|
// Perform select
|
||||||
Events = select( MaxFD, &ReadFDS, &WriteFDS, (fd_set*)NULL, &Timeout );
|
Events = select( MaxFD, &ReadFDS, &WriteFDS, (fd_set*)NULL, &STimeout );
|
||||||
if (Events < 0)
|
if (Events < 0)
|
||||||
{
|
{
|
||||||
LogMessage( dlHigh, "Select: Select operation failed" );
|
LogMessage( dlHigh, "Select: Select operation failed" );
|
||||||
@@ -174,47 +175,49 @@ bool CSelect::Test()
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check all descriptors for events
|
// Check all descriptors for events
|
||||||
Handle = FirstHandle;
|
if (Events) {
|
||||||
while (Handle)
|
Handle = FirstHandle;
|
||||||
{
|
while (Handle)
|
||||||
// Check if to remove from list
|
|
||||||
if (!Handle->Read && !Handle->Write)
|
|
||||||
{
|
{
|
||||||
// Update Maximum Test FD
|
// Check if to remove from list
|
||||||
if (Handle->FD == MaxFD-1) {
|
if (!Handle->Read && !Handle->Write)
|
||||||
for (TestFD = MaxFD-1; TestFD >= 0; TestFD--) {
|
{
|
||||||
if (FD_ISSET( TestFD, &ReadTestFDS ) || FD_ISSET( TestFD, &WriteTestFDS )) {
|
// Update Maximum Test FD
|
||||||
break;
|
if (Handle->FD == MaxFD-1) {
|
||||||
|
for (TestFD = MaxFD-1; TestFD >= 0; TestFD--) {
|
||||||
|
if (FD_ISSET( TestFD, &ReadTestFDS ) || FD_ISSET( TestFD, &WriteTestFDS )) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
MaxFD = TestFD+1;
|
||||||
}
|
}
|
||||||
MaxFD = TestFD+1;
|
|
||||||
|
// Remove from list
|
||||||
|
HandlePtr = &FirstHandle;
|
||||||
|
while (*HandlePtr && (*HandlePtr != Handle))
|
||||||
|
HandlePtr = &((*HandlePtr)->Next);
|
||||||
|
*HandlePtr = (*HandlePtr)->Next;
|
||||||
|
|
||||||
|
// Destroy and go to next
|
||||||
|
free( Handle );
|
||||||
|
Handle = *HandlePtr;
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Check read Event
|
||||||
|
if (FD_ISSET( Handle->FD, &ReadFDS ) && Handle->Function) {
|
||||||
|
Handle->Function->Read( Handle->FD );
|
||||||
|
}
|
||||||
|
|
||||||
// Remove from list
|
// Check Write Event
|
||||||
HandlePtr = &FirstHandle;
|
if (FD_ISSET( Handle->FD, &WriteFDS ) && Handle->Function) {
|
||||||
while (*HandlePtr && (*HandlePtr != Handle))
|
Handle->Function->Write( Handle->FD );
|
||||||
HandlePtr = &((*HandlePtr)->Next);
|
}
|
||||||
*HandlePtr = (*HandlePtr)->Next;
|
}
|
||||||
|
// Next
|
||||||
// Destroy and go to next
|
Handle = Handle->Next;
|
||||||
free( Handle );
|
|
||||||
Handle = *HandlePtr;
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
// Check read Event
|
|
||||||
if (FD_ISSET( Handle->FD, &ReadFDS ) && Handle->Function) {
|
|
||||||
Handle->Function->Read( Handle->FD );
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check Write Event
|
|
||||||
if (FD_ISSET( Handle->FD, &WriteFDS ) && Handle->Function) {
|
|
||||||
Handle->Function->Write( Handle->FD );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Next
|
|
||||||
Handle = Handle->Next;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// return success
|
// return success
|
||||||
|
|||||||
Reference in New Issue
Block a user