Separate send buffer and fifo send buffer

This commit is contained in:
Alex D. 2021-01-09 21:35:10 +00:00
parent af191b4364
commit f4b2cb90f3
Signed by: caskd
GPG Key ID: F92BA85F61F4C173
1 changed files with 28 additions and 25 deletions

View File

@ -128,7 +128,9 @@ main(int argc, char* argv[])
time_t ctime;
struct timespec sleep = { 0, 50000000L };
struct {
Buffer_Info recv, send, fifo, log;
Buffer_Info recv, send, // Network receive and send (automatic)
fiforecv, fifosend, // FIFO receive and send (manual)
log; // Log writing buffer (automatic)
} buf;
init_chanarray(&connection.info.channels);
@ -172,7 +174,7 @@ main(int argc, char* argv[])
errno);
}
close(buf.send.fd);
close(buf.fifo.fd);
close(buf.fiforecv.fd);
connection.info.state = CONN_CLOSED;
LOG(LOG_VERBOSE, "Connection to " ADDRFMT " was closed.", connection.data.address, connection.data.service);
break;
@ -180,7 +182,7 @@ main(int argc, char* argv[])
break;
} else if (connection.info.state == CONN_RECONNECTING) {
close(buf.send.fd);
close(buf.fifo.fd);
close(buf.fiforecv.fd);
connection.info.state = CONN_PENDING;
if (connection.info.reconinter <= 300) connection.info.reconinter += 5;
continue;
@ -190,11 +192,11 @@ main(int argc, char* argv[])
/* Reset all state-dependent values to empty */
memset(&buf, '\0', sizeof(buf));
buf.recv.fd = buf.send.fd = buf.fifo.fd = -1;
connection.info.l_message = 0;
buf.recv.fd = buf.send.fd = buf.fiforecv.fd = buf.fifosend.fd = -1;
connection.info.l_message = 0;
if ((buf.send.fd = init_connection(&connection)) > 0) {
buf.recv.fd = buf.send.fd;
buf.recv.fd = buf.fifosend.fd = buf.send.fd;
add_socket_flags(buf.send.fd, O_NONBLOCK);
signed long temp;
@ -284,18 +286,19 @@ main(int argc, char* argv[])
connection.info.state = CONN_RECONNECTING;
continue;
}
if (buf.fifo.fd != -1) {
if (buf.fiforecv.fd != -1) {
ssize_t brd, len;
/* FIFO reader */
if ((brd =
read(buf.fifo.fd, buf.fifo.buffer + buf.fifo.append_pos, sizeof(buf.fifo.buffer) - buf.fifo.append_pos - 1))
if ((brd = read(buf.fiforecv.fd,
buf.fiforecv.buffer + buf.fiforecv.append_pos,
sizeof(buf.fiforecv.buffer) - buf.fiforecv.append_pos - 1))
> 0) {
*(buf.fifo.buffer + (buf.fifo.append_pos += (size_t) brd)) = '\0';
*(buf.fiforecv.buffer + (buf.fiforecv.append_pos += (size_t) brd)) = '\0';
LOG(LOG_DEBUG,
"Read %li bytes from FIFO buffer. Now appending at %li with \"%s\" so far.",
brd,
buf.fifo.append_pos,
buf.fifo.buffer);
buf.fiforecv.append_pos,
buf.fiforecv.buffer);
connection.info.state = CONN_ACTIVE;
} else if (brd == -1 && errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) {
LOG(LOG_ERROR, "Failed to read FIFO buffer. " ERRNOFMT, strerror(errno), errno);
@ -304,12 +307,12 @@ main(int argc, char* argv[])
}
memset((void*) &buffer, '\0', sizeof(IRC_Message));
if ((len = get_buffer_line(buf.fifo.buffer)) > 0) {
LOG(LOG_DEBUG, "Got IRC message: %s", buf.fifo.buffer);
if (Tok_mesg(buf.fifo.buffer, &buffer) == 1) {
if ((len = get_buffer_line(buf.fiforecv.buffer)) > 0) {
LOG(LOG_DEBUG, "Got IRC message: %s", buf.fiforecv.buffer);
if (Tok_mesg(buf.fiforecv.buffer, &buffer) == 1) {
signed long temp;
if ((temp = Assm_mesg(buf.send.buffer, &buffer, sizeof(buf.send.buffer))) > 0) {
if (flush_buffer(buf.send.buffer, (size_t) temp, buf.send.fd) == -1) {
if ((temp = Assm_mesg(buf.fifosend.buffer, &buffer, sizeof(buf.fifosend.buffer))) > 0) {
if (flush_buffer(buf.fifosend.buffer, (size_t) temp, buf.fifosend.fd) == -1) {
LOG(LOG_WARN,
"Couldn't send FIFO contents to " ADDRFMT ". " ERRNOFMT,
connection.data.address,
@ -323,19 +326,19 @@ main(int argc, char* argv[])
} else {
LOG(LOG_WARN, "%s", "Received invalid IRC message on FIFO (see RFC2812).");
}
for (long unsigned int x = 0; x < sizeof(buf.fifo.buffer) && *(buf.fifo.buffer + len + x); x++)
*(buf.fifo.buffer + x) = *(buf.fifo.buffer + x + len);
buf.fifo.append_pos -= (unsigned long) len;
*(buf.fifo.buffer + buf.fifo.append_pos) = '\0';
} else if (buf.fifo.append_pos == sizeof(buf.fifo.fd) - 1) {
for (long unsigned int x = 0; x < sizeof(buf.fiforecv.buffer) && *(buf.fiforecv.buffer + len + x); x++)
*(buf.fiforecv.buffer + x) = *(buf.fiforecv.buffer + x + len);
buf.fiforecv.append_pos -= (unsigned long) len;
*(buf.fiforecv.buffer + buf.fiforecv.append_pos) = '\0';
} else if (buf.fiforecv.append_pos == sizeof(buf.fiforecv.fd) - 1) {
LOG(LOG_WARN, "%s.", "FIFO buffer is full and no message could be parsed. Cleared buffer.");
memset(buf.fifo.buffer, '\0', sizeof(buf.fifo));
buf.fifo.append_pos = 0;
memset(buf.fiforecv.buffer, '\0', sizeof(buf.fiforecv));
buf.fiforecv.append_pos = 0;
}
} else {
char pathbuf[UIRCD_LIMITS_PATH];
if (get_path(pathbuf, sizeof(pathbuf), &connection, NULL, true, true, false) >= 0) {
if (mkdir_bottomup(pathbuf)) buf.fifo.fd = makeinput(pathbuf);
if (mkdir_bottomup(pathbuf)) buf.fiforecv.fd = makeinput(pathbuf);
}
}
}