mirror of
https://github.com/osmarks/ngircd.git
synced 2025-03-04 08:28:10 +00:00
Conn_Handler(): cleanup code, add/translate comments.
In addition, the "timeout" variable has been removed because it is unnecessary today: Handle_Buffer() handles all the data it can handle, and io_dispatch() returns immediately when new data is available. So we don't have to double-check but better sleep. Pointed out by Florian.
This commit is contained in:
parent
b90f71ca2a
commit
d360871394
@ -480,104 +480,114 @@ NewListener(const char *listen_addr, UINT16 Port)
|
|||||||
} /* NewListener */
|
} /* NewListener */
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* "Main Loop": Loop until shutdown or restart is signalled.
|
||||||
|
* This function loops until a shutdown or restart of ngIRCd is signalled and
|
||||||
|
* calls io_dispatch() to check for readable and writable sockets every second.
|
||||||
|
* It checks for status changes on pending connections (e. g. when a hostname
|
||||||
|
* has been resolved), checks for "penalties" and timeouts, and handles the
|
||||||
|
* input buffers.
|
||||||
|
*/
|
||||||
GLOBAL void
|
GLOBAL void
|
||||||
Conn_Handler(void)
|
Conn_Handler(void)
|
||||||
{
|
{
|
||||||
/* "Main Loop.": Loop until a signal (for shutdown or restart) arrives.
|
|
||||||
* Call io_dispatch() to check for read/writeable sockets every second
|
|
||||||
* Wait for status change on pending connections (e.g: when the hostname has been resolved)
|
|
||||||
* check for penalty/timeouts
|
|
||||||
* handle input buffers
|
|
||||||
*/
|
|
||||||
int i;
|
int i;
|
||||||
unsigned int wdatalen;
|
unsigned int wdatalen;
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
time_t t;
|
time_t t;
|
||||||
bool timeout;
|
|
||||||
|
|
||||||
while(( ! NGIRCd_SignalQuit ) && ( ! NGIRCd_SignalRestart )) {
|
while (!NGIRCd_SignalQuit && !NGIRCd_SignalRestart) {
|
||||||
timeout = true;
|
t = time(NULL);
|
||||||
|
|
||||||
#ifdef ZEROCONF
|
#ifdef ZEROCONF
|
||||||
Rendezvous_Handler();
|
Rendezvous_Handler();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Should the configuration be reloaded? */
|
/* Should the configuration be reloaded? */
|
||||||
if (NGIRCd_SignalRehash) {
|
if (NGIRCd_SignalRehash)
|
||||||
NGIRCd_Rehash();
|
NGIRCd_Rehash();
|
||||||
}
|
|
||||||
|
|
||||||
/* Check configured servers and established links */
|
/* Check configured servers and established links */
|
||||||
Check_Servers();
|
Check_Servers();
|
||||||
Check_Connections();
|
Check_Connections();
|
||||||
|
|
||||||
t = time( NULL );
|
/* Look for non-empty read buffers ... */
|
||||||
|
|
||||||
/* noch volle Lese-Buffer suchen */
|
|
||||||
for (i = 0; i < Pool_Size; i++) {
|
for (i = 0; i < Pool_Size; i++) {
|
||||||
if(( My_Connections[i].sock > NONE ) && ( array_bytes(&My_Connections[i].rbuf) > 0 ) &&
|
if ((My_Connections[i].sock > NONE)
|
||||||
( My_Connections[i].delaytime < t ))
|
&& (array_bytes(&My_Connections[i].rbuf) > 0)
|
||||||
{
|
&& (My_Connections[i].delaytime < t)) {
|
||||||
/* Kann aus dem Buffer noch ein Befehl extrahiert werden? */
|
/* ... and try to handle the received data */
|
||||||
if (Handle_Buffer( i )) timeout = false;
|
Handle_Buffer(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* noch volle Schreib-Puffer suchen */
|
/* Look for non-empty write buffers ... */
|
||||||
for (i = 0; i < Pool_Size; i++) {
|
for (i = 0; i < Pool_Size; i++) {
|
||||||
if (My_Connections[i].sock <= NONE)
|
if (My_Connections[i].sock <= NONE)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
wdatalen = (unsigned int)array_bytes(&My_Connections[i].wbuf);
|
wdatalen = (unsigned int)array_bytes(&My_Connections[i].wbuf);
|
||||||
|
|
||||||
#ifdef ZLIB
|
#ifdef ZLIB
|
||||||
if (( wdatalen > 0 ) || ( array_bytes(&My_Connections[i].zip.wbuf)> 0 ))
|
if (wdatalen > 0 ||
|
||||||
|
array_bytes(&My_Connections[i].zip.wbuf) > 0)
|
||||||
#else
|
#else
|
||||||
if (wdatalen > 0)
|
if (wdatalen > 0)
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
/* Socket der Verbindung in Set aufnehmen */
|
/* Set the "WANTWRITE" flag on this socket */
|
||||||
io_event_add( My_Connections[i].sock, IO_WANTWRITE );
|
io_event_add(My_Connections[i].sock,
|
||||||
|
IO_WANTWRITE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* von welchen Sockets koennte gelesen werden? */
|
/* Check from which sockets we possibly could read ... */
|
||||||
for (i = 0; i < Pool_Size; i++) {
|
for (i = 0; i < Pool_Size; i++) {
|
||||||
if (My_Connections[i].sock <= NONE)
|
if (My_Connections[i].sock <= NONE)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (Resolve_INPROGRESS(&My_Connections[i].res_stat)) {
|
if (Resolve_INPROGRESS(&My_Connections[i].res_stat)) {
|
||||||
/* wait for completion of Resolver Sub-Process */
|
/* Wait for completion of resolver sub-process ... */
|
||||||
io_event_del( My_Connections[i].sock, IO_WANTREAD );
|
io_event_del(My_Connections[i].sock,
|
||||||
|
IO_WANTREAD);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Conn_OPTION_ISSET(&My_Connections[i], CONN_ISCONNECTING))
|
if (Conn_OPTION_ISSET(&My_Connections[i], CONN_ISCONNECTING))
|
||||||
continue; /* wait for completion of connect() */
|
/* Wait for completion of connect() ... */
|
||||||
|
continue;
|
||||||
|
|
||||||
if (My_Connections[i].delaytime > t) {
|
if (My_Connections[i].delaytime > t) {
|
||||||
/* Fuer die Verbindung ist eine "Penalty-Zeit" gesetzt */
|
/* There is a "penalty time" set: ignore socket! */
|
||||||
io_event_del( My_Connections[i].sock, IO_WANTREAD );
|
io_event_del(My_Connections[i].sock,
|
||||||
|
IO_WANTREAD);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
io_event_add(My_Connections[i].sock, IO_WANTREAD);
|
io_event_add(My_Connections[i].sock, IO_WANTREAD);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (re-)set timeout - tv_sec/usec are undefined after io_dispatch() returns */
|
/* Set the timeout for reading from the network to 1 second,
|
||||||
|
* which is the granularity with witch we handle "penalty
|
||||||
|
* times" for example.
|
||||||
|
* Note: tv_sec/usec are undefined(!) after io_dispatch()
|
||||||
|
* returns, so we have to set it beforce each call to it! */
|
||||||
tv.tv_usec = 0;
|
tv.tv_usec = 0;
|
||||||
tv.tv_sec = timeout ? 1 : 0;
|
tv.tv_sec = 1;
|
||||||
|
|
||||||
/* wait for activity */
|
/* Wait for activity ... */
|
||||||
i = io_dispatch(&tv);
|
i = io_dispatch(&tv);
|
||||||
if (i == -1 && errno != EINTR) {
|
if (i == -1 && errno != EINTR) {
|
||||||
Log(LOG_EMERG, "Conn_Handler(): io_dispatch(): %s!", strerror(errno));
|
Log(LOG_EMERG, "Conn_Handler(): io_dispatch(): %s!",
|
||||||
Log(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
|
strerror(errno));
|
||||||
|
Log(LOG_ALERT, "%s exiting due to fatal errors!",
|
||||||
|
PACKAGE_NAME);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if( NGIRCd_SignalQuit ) Log( LOG_NOTICE|LOG_snotice, "Server going down NOW!" );
|
if (NGIRCd_SignalQuit)
|
||||||
else if( NGIRCd_SignalRestart ) Log( LOG_NOTICE|LOG_snotice, "Server restarting NOW!" );
|
Log(LOG_NOTICE | LOG_snotice, "Server going down NOW!");
|
||||||
|
else if (NGIRCd_SignalRestart)
|
||||||
|
Log(LOG_NOTICE | LOG_snotice, "Server restarting NOW!");
|
||||||
} /* Conn_Handler */
|
} /* Conn_Handler */
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user