Merge intervals into one and improve ping/reconnect logic

This commit is contained in:
Alex D. 2020-09-15 16:06:27 +02:00
parent 32de8270f4
commit c0eca28927
Signed by: caskd
GPG Key ID: F92BA85F61F4C173
2 changed files with 21 additions and 17 deletions

View File

@ -92,7 +92,7 @@ int main(int argc, char* argv[])
break;
}
} else if (cpid == 0) {
return run_main(&cons[actcon], delay, 10, quitmsg, timeout);
return run_main(&cons[actcon], delay, quitmsg, timeout);
} else {
LOG(LOG_VERBOSE, "Successfully forked for connection " ADDRFMT ".", cons[actcon].data.addr, cons[actcon].data.port);
actcon++;
@ -106,15 +106,15 @@ int main(int argc, char* argv[])
return EXIT_SUCCESS;
}
int run_main(Connection* conn, unsigned int recon_inter, unsigned int ping_inter, char* quitmsg, int timeout)
int run_main(Connection* conn, unsigned int recon_inter, char* quitmsg, int timeout)
{
IRC_Message buffer;
char sendbuf[MAXLINE + 1], recvbuf[MAXLINE + 1], fifobuf[MAXLINE + 1], /* Buffers */
connstr[100], *path[4] = {connstr, NULL, 0}; /* Path buffers */
size_t sendbufpos = 0, recvbufpos = 0, fifobufpos = 0; /* Positions */
signed int connstate = CONN_PENDING, fds[2] = {-1, -1}; /* Connection state and file descriptors */
unsigned int lastping = 0, lastconnect = 0, ctime = 0, lastpong = 0, /* Timestamps */
pinginter = (ping_inter) ? ping_inter : 10, reconinter = (recon_inter) ? recon_inter : 10; /* Intervals */
char sendbuf[MAXLINE + 1], recvbuf[MAXLINE + 1], fifobuf[MAXLINE + 1], /* Buffers */
connstr[100], *path[4] = {connstr, NULL, 0}; /* Path buffers */
size_t sendbufpos = 0, recvbufpos = 0, fifobufpos = 0; /* Positions */
signed int connstate = CONN_PENDING, fds[2] = {-1, -1}; /* Connection state and file descriptors */
unsigned int lastping = 0, lastconnect = 0, ctime = 0, lastpong = 0, lastmesg = 0, /* Timestamps */
reconinter = (recon_inter) ? recon_inter : 10; /* Intervals */
get_connstr(connstr, sizeof(connstr), conn);
srand(time(NULL));
@ -210,12 +210,12 @@ int run_main(Connection* conn, unsigned int recon_inter, unsigned int ping_inter
continue;
}
} else if (connstate == CONN_ACTIVE) {
if (lastpong < lastping - timeout) {
if (lastmesg < ctime - timeout && lastpong < lastping - timeout) {
LOG(LOG_WARN, "Server " ADDRFMT " didn't respond to a PING in time.", conn->data.addr, conn->data.port);
connstate = CONN_PENDING;
continue;
}
if (ctime - lastping >= ping_inter) {
if (ctime - lastmesg >= timeout / 4 && ctime - lastping >= timeout / 4) {
char mesg[] = "uIRC PING -- XXXXXX";
snprintf(mesg + sizeof(mesg) - 7, 7, "%.7i", rand());
if ((sendbufpos = Assm_mesg(sendbuf, Assm_cmd_PING(mesg, NULL), sizeof(sendbuf))) > 0) {
@ -249,6 +249,7 @@ int run_main(Connection* conn, unsigned int recon_inter, unsigned int ping_inter
if (ppoi > recvbuf && *(ppoi - 1) == '\r')
*(ppoi - 1) = '\0';
LOG(LOG_DEBUG, "Recieved line: %s", recvbuf);
lastmesg = ctime;
memset((void*)&buffer, '\0', sizeof(IRC_Message));
if (Tok_mesg(recvbuf, &buffer) == 1) {
char bufname[MAXPATH + 1], logpath[PATH_MAX], *bprint = "out";
@ -324,13 +325,16 @@ int run_main(Connection* conn, unsigned int recon_inter, unsigned int ping_inter
case (RPL_WELCOME): {
LOG(LOG_INFO, "Connection established to " ADDRFMT ".", conn->data.addr, conn->data.port);
connstate = CONN_ACTIVE;
LOG(LOG_VERBOSE, "Auto-joining channels \"%s\" on " ADDRFMT ".", conn->data.chans, conn->data.addr, conn->data.port);
if ((sendbufpos = Assm_mesg(sendbuf, Assm_cmd_JOIN(conn->data.chans, NULL), sizeof(sendbuf))) > 0)
if (flush_buffer(sendbuf, sendbufpos, fds[0]) == -1) {
LOG(LOG_WARN, "Couldn't auto-join channels \"%s\" " ADDRFMT ". " ERRNOFMT, conn->data.chans, conn->data.addr, conn->data.port, strerror(errno), errno);
connstate = CONN_PENDING;
continue;
if (conn->data.chans != NULL) {
LOG(LOG_VERBOSE, "Auto-joining channels \"%s\" on " ADDRFMT ".", conn->data.chans, conn->data.addr, conn->data.port);
if ((sendbufpos = Assm_mesg(sendbuf, Assm_cmd_JOIN(conn->data.chans, NULL), sizeof(sendbuf))) > 0) {
if (flush_buffer(sendbuf, sendbufpos, fds[0]) == -1) {
LOG(LOG_WARN, "Couldn't auto-join channels \"%s\" " ADDRFMT ". " ERRNOFMT, conn->data.chans, conn->data.addr, conn->data.port, strerror(errno), errno);
connstate = CONN_PENDING;
continue;
}
}
}
break;
}
case (ERROR): {

View File

@ -30,5 +30,5 @@
#include <unistd.h>
#define VERSION "indev - testing"
int run_main(Connection* conn, unsigned int recon_inter, unsigned int ping_inter, char* quitmsg, int timeout);
int run_main(Connection* conn, unsigned int recon_inter, char* quitmsg, int timeout);
void print_help(void);