Commit 79e0ea65 authored by Dustin L. Howett's avatar Dustin L. Howett

Kill the write buffer and just write directly to the socket. Catch errors and...

Kill the write buffer and just write directly to the socket. Catch errors and kill the fd when necessary.
parent d5a0aa58
......@@ -166,7 +166,6 @@ void event_loop() {
int fd = (*iter)->getFd();
if(fd > -1) {
FD_SET(fd, &read_fds);
FD_SET(fd, &write_fds);
}
nfds = MAX(nfds, fd);
}
......@@ -189,9 +188,6 @@ void event_loop() {
if(FD_ISSET(fd, &read_fds)) {
server->read();
}
if(FD_ISSET(fd, &write_fds) && server->writePending()) {
server->write();
}
}
if(FD_ISSET(control_fd, &read_fds)) {
char buf[1024];
......@@ -216,6 +212,8 @@ int main(int argc, char **argv, char **envp) {
progname = strdup(argv[0]);
app_handle = dlopen(NULL, RTLD_NOW);
signal(SIGPIPE, SIG_IGN);
if(argc > 1 && strcmp(argv[1], "-copyover") == 0) {
init_copyover(argc, argv);
} else {
......
......@@ -16,12 +16,6 @@ int _serv_internal_tag_cb(uint32_t tagtype, uint32_t len, uint8_t *data, struct
server->nbytes_read = len;
}
break;
case 'WBUF':
if(len > 0) {
memcpy(server->writebuf, data, len);
server->nbytes_write = len;
}
break;
case '_FD_':
memcpy(&server->fd, data, len);
_D("Got file descriptor %d for IRC.\n", server->fd);
......@@ -43,18 +37,14 @@ server::server(const char *hostname) :
hostname(strdup(hostname)),
nick(NULL),
readbuf(new char[READ_BUF_LEN]),
nbytes_read(0),
writebuf(new char[WRITE_BUF_LEN]),
nbytes_write(0) {
nbytes_read(0) {
}
server::server(uint8_t *data, uint32_t size) :
hostname(NULL),
nick(NULL),
readbuf(new char[READ_BUF_LEN]),
nbytes_read(0),
writebuf(new char[WRITE_BUF_LEN]),
nbytes_write(0) {
nbytes_read(0) {
for(uint8_t *datap = data; datap<data+size;) {
int len = bscantag(datap, size-(datap-data), &_serv_internal_callbacks, this);
if(len == -1) break; // Problem, Officer?
......@@ -65,7 +55,6 @@ server::server(uint8_t *data, uint32_t size) :
uint8_t *server::freeze(uint32_t *outlen) {
struct tag_info taglist[] = {
{ 'RBUF', nbytes_read, readbuf, },
{ 'WBUF', nbytes_write, writebuf, },
{ '_FD_', sizeof(fd), &fd, },
{ 'NICK', nick ? strlen(nick) : 0, nick, },
{ 'HOST', hostname ? strlen(hostname) : 0, hostname, },
......@@ -123,6 +112,10 @@ void server::read() {
int readcount = 0;
if(nbytes_read < READ_BUF_LEN)
readcount = ::read(fd, readbuf + nbytes_read, READ_BUF_LEN-nbytes_read);
if(readcount == -1) {
_socket_error();
return;
}
if(readcount > 0) {
// TODO: There is something wrong with this, I believe.
......@@ -150,16 +143,19 @@ void server::read() {
}
}
bool server::writePending() {
return nbytes_write > 0;
void server::write(const char *buf, size_t nbyte) {
ssize_t written = ::write(fd, buf, nbyte);
if(written == -1) {
_socket_error();
return;
}
_D("Wrote %ld bytes.\n", written);
}
void server::write() {
int written = ::write(fd, writebuf, nbytes_write);
_D("Wrote %d/%llu bytes.\n", written, nbytes_write);
nbytes_write -= written;
if(nbytes_write == 0) return;
memmove(writebuf, writebuf+written, nbytes_write);
void server::_socket_error() {
int old_fd = fd;
fd = -1;
close(old_fd);
}
void server::printf(const char *format, ...) {
......@@ -171,14 +167,11 @@ void server::printf(const char *format, ...) {
va_end(ap);
_D("-> \"%s\"\n", intermediate);
if(WRITE_BUF_LEN - nbytes_write < strlen(intermediate)+2) {
fprintf(stderr, "ERROR: Write buffer filled up!\n");
free(intermediate);
return;
}
nbytes_write += snprintf(writebuf + nbytes_write, WRITE_BUF_LEN - nbytes_write, "%s\r\n", intermediate);
_D("SENDQ: %llu/%d bytes consumed.\n", nbytes_write, WRITE_BUF_LEN);
int sendsize = strlen(intermediate)+2;
char *send = new char[sendsize+1];
snprintf(send, sendsize+1, "%s\r\n", intermediate);
write(send, sendsize);
delete send;
free(intermediate);
}
#ifndef SERVER_H
#define SERVER_H
#include <stdint.h>
#include <sys/types.h>
class server {
private:
......@@ -11,9 +12,6 @@ private:
char *readbuf;
unsigned long long int nbytes_read;
char *writebuf;
unsigned long long int nbytes_write;
public:
server(const char *hostname);
server(uint8_t *data, uint32_t size);
......@@ -27,8 +25,7 @@ public:
/* }}} */
void read();
bool writePending();
void write();
void write(const char *buf, size_t nbyte);
void printf(const char *format, ...);
uint8_t *freeze(uint32_t *outlen);
......@@ -37,6 +34,7 @@ public:
private:
server();
void _handle_message(char *msgbuf);
void _socket_error();
friend int _serv_internal_tag_cb(uint32_t tagtype, uint32_t len, uint8_t *data, struct dady_callback *cb, server *server);
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment