Commit 294d42f8 authored by Dustin L. Howett's avatar Dustin L. Howett

Move all reading/writing logic and event dispatch (apart from the event loop)...

Move all reading/writing logic and event dispatch (apart from the event loop) into server. Make all members of server private and wrap some in accessors.
parent 59157651
......@@ -142,7 +142,7 @@ void _new_server(char *hostname) {
}
server *newserver = new server(hostname);
newserver->fd = socket_fd;
newserver->setFd(socket_fd);
control_fd = 0;
_D("Got control fd %d\n", control_fd);
......@@ -156,67 +156,6 @@ void init_irc(int argc, char **argv) {
_new_server(argv[1]);
}
void handle_irc(server *server, char *msgbuf) {
struct sender sender;
memset(&sender, 0, sizeof(sender));
char *cmd = NULL;
if(msgbuf[0] == ':') {
char *temp_sender;
temp_sender = strsep(&msgbuf, " ") + 1;
if(strchr(temp_sender, '!') != NULL) {
sender.nick = strsep(&temp_sender, "!");
sender.username = strsep(&temp_sender, "@");
}
sender.host = temp_sender;
}
cmd = strsep(&msgbuf, " ");
_D("[%s!%s@%s] %s %s\n", sender.nick, sender.username, sender.host, cmd, msgbuf);
dispatch_event(server, cmd, &sender, msgbuf);
return;
}
void _read_irc(server *server) {
static char irc_msg[513]; // RFC spec - messages are only 512 characters, and \0.
int readcount = 0;
if(server->nbytes_read < READ_BUF_LEN)
readcount = read(server->fd, server->readbuf + server->nbytes_read, READ_BUF_LEN-server->nbytes_read);
if(readcount > 0) {
// TODO: There is something wrong with this, I believe.
_D("IRC -> ringbuf[%llu]\n", server->nbytes_read);
server->nbytes_read += readcount;
_D("Total bytes in buffer: %llu\n", server->nbytes_read);
}
char *nl_ptr;
int nl;
while((nl_ptr = strstr(server->readbuf, "\r\n")) != NULL) {
nl = nl_ptr - server->readbuf;
if(nl > 0) {
strncpy(irc_msg, server->readbuf, nl);
irc_msg[nl] = '\0';
}
int consume = nl;
while(server->readbuf[consume] == '\n' || server->readbuf[consume] == '\r') ++consume;
memmove(server->readbuf, server->readbuf+consume, READ_BUF_LEN-consume);
server->nbytes_read -= consume;
_D("Chomped %d bytes. Total bytes in buffer: %llu\n", consume, server->nbytes_read);
if(nl > 0) {
handle_irc(server, irc_msg);
}
}
}
void _write_irc(server *server) {
int written = write(server->fd, server->writebuf, server->nbytes_write);
_D("Wrote %d/%llu bytes.\n", written, server->nbytes_write);
server->nbytes_write -= written;
if(server->nbytes_write == 0) return;
memmove(server->writebuf, server->writebuf+written, server->nbytes_write);
}
void event_loop() {
fd_set read_fds, write_fds;
while(true) {
......@@ -224,7 +163,7 @@ void event_loop() {
FD_ZERO(&read_fds);
FD_ZERO(&write_fds);
for(std::list<server *>::const_iterator iter = servers.begin(), end = servers.end(); iter != end; ++iter) {
int fd = (*iter)->fd;
int fd = (*iter)->getFd();
if(fd > -1) {
FD_SET(fd, &read_fds);
FD_SET(fd, &write_fds);
......@@ -246,12 +185,12 @@ void event_loop() {
}
for(std::list<server *>::const_iterator iter = servers.begin(), end = servers.end(); iter != end; ++iter) {
server *server = *iter;
if(FD_ISSET(server->fd, &read_fds)) {
_read_irc(server);
int fd = server->getFd();
if(FD_ISSET(fd, &read_fds)) {
server->read();
}
if(server->nbytes_write > 0 && FD_ISSET(server->fd, &write_fds)) {
_D("Socket ready for writing and we have %llu bytes of data.\n", server->nbytes_write);
_write_irc(server);
if(FD_ISSET(fd, &write_fds) && server->writePending()) {
server->write();
}
}
if(FD_ISSET(control_fd, &read_fds)) {
......
......@@ -2,7 +2,9 @@
#include "dady/dady.h"
#include "server.h"
#include "event.h"
#include <unistd.h>
#include <stdarg.h>
/* {{{ DADY */
......@@ -81,6 +83,8 @@ server::~server() {
free(this->nick);
}
const int server::getFd() { return fd; }
void server::setFd(const int newFd) { fd = newFd; }
const char *server::getHostname() { return hostname; }
const char *server::getNick() { return nick; }
void server::setNick(const char *newNick) {
......@@ -91,6 +95,71 @@ void server::setNick(const char *newNick) {
nick = strdup(newNick);
}
void server::_handle_message(char *msgbuf) {
struct sender sender;
memset(&sender, 0, sizeof(sender));
char *cmd = NULL;
if(msgbuf[0] == ':') {
char *temp_sender;
temp_sender = strsep(&msgbuf, " ") + 1;
if(strchr(temp_sender, '!') != NULL) {
sender.nick = strsep(&temp_sender, "!");
sender.username = strsep(&temp_sender, "@");
}
sender.host = temp_sender;
}
cmd = strsep(&msgbuf, " ");
_D("[%s!%s@%s] %s %s\n", sender.nick, sender.username, sender.host, cmd, msgbuf);
dispatch_event(this, cmd, &sender, msgbuf);
return;
}
void server::read() {
static char irc_msg[513]; // RFC spec - messages are only 512 characters, and \0.
int readcount = 0;
if(nbytes_read < READ_BUF_LEN)
readcount = ::read(fd, readbuf + nbytes_read, READ_BUF_LEN-nbytes_read);
if(readcount > 0) {
// TODO: There is something wrong with this, I believe.
_D("IRC -> ringbuf[%llu]\n", nbytes_read);
nbytes_read += readcount;
_D("Total bytes in buffer: %llu\n", nbytes_read);
}
char *nl_ptr;
int nl;
while((nl_ptr = strstr(readbuf, "\r\n")) != NULL) {
nl = nl_ptr - readbuf;
if(nl > 0) {
strncpy(irc_msg, readbuf, nl);
irc_msg[nl] = '\0';
}
int consume = nl;
while(readbuf[consume] == '\n' || readbuf[consume] == '\r') ++consume;
memmove(readbuf, readbuf+consume, READ_BUF_LEN-consume);
nbytes_read -= consume;
_D("Chomped %d bytes. Total bytes in buffer: %llu\n", consume, nbytes_read);
if(nl > 0) {
_handle_message(irc_msg);
}
}
}
bool server::writePending() {
return nbytes_write > 0;
}
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::printf(const char *format, ...) {
char *intermediate;
......
......@@ -7,7 +7,6 @@ private:
char *hostname;
char *nick;
public:
int fd;
char *readbuf;
unsigned long long int nbytes_read;
......@@ -20,11 +19,16 @@ public:
server(uint8_t *data, uint32_t size);
/* {{{ Accessors */
const int getFd();
void setFd(const int newFd);
const char *getHostname();
const char *getNick();
void setNick(const char *newNick);
/* }}} */
void read();
bool writePending();
void write();
void printf(const char *format, ...);
uint8_t *freeze(uint32_t *outlen);
......@@ -32,6 +36,7 @@ public:
private:
server();
void _handle_message(char *msgbuf);
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