Commit 7a3d788e authored by Dustin L. Howett's avatar Dustin L. Howett

Make server a proper class.

parent 86171c63
......@@ -10,9 +10,9 @@
extern void *app_handle;
#include "commands.h"
#define _COMMAND(name) extern "C" int cmd_ ## name (struct server *server, int argc, char **argv, struct command_state *state)
#define _COMMAND(name) extern "C" int cmd_ ## name (server *server, int argc, char **argv, struct command_state *state)
int dispatch_command(struct server *server, const char *cmdname, int argc, char **argv, struct command_state *state) {
int dispatch_command(server *server, const char *cmdname, int argc, char **argv, struct command_state *state) {
char *namecmp = (char *)malloc(strlen(cmdname));
char *namep = namecmp;
while(*cmdname) {
......
......@@ -2,7 +2,7 @@
#define __COMMANDS_H
#include "oligos.h"
typedef int (*command_handler)(struct server *server, int argc, char **argv, struct command_state *state);
int dispatch_command(struct server *server, const char *command, int argc, char **argv, struct command_state *state);
typedef int (*command_handler)(server *server, int argc, char **argv, struct command_state *state);
int dispatch_command(server *server, const char *command, int argc, char **argv, struct command_state *state);
#endif //__COMMANDS_H
......@@ -11,13 +11,13 @@
#include <string>
#include <map>
#define _EVENT(name) void evt_ ## name (struct server *, struct sender *, char *); \
#define _EVENT(name) void evt_ ## name (server *, struct sender *, char *); \
static __attribute__((constructor)) void _evt_ ## name ## _init() { event_handlers[#name] = &evt_ ## name; } \
void evt_ ## name (struct server *server, struct sender *sender, char *msgbuf)
void evt_ ## name (server *server, struct sender *sender, char *msgbuf)
std::map<std::string, event_handler_func> event_handlers;
void dispatch_event(struct server *server, char *name, struct sender *sender, char *msgbuf) {
void dispatch_event(server *server, char *name, struct sender *sender, char *msgbuf) {
event_handler_func handler = event_handlers[name];
if(handler) {
......
#ifndef EVENT_H
#define EVENT_H
typedef void (*event_handler_func)(struct server *server, struct sender *sender, char *msgbuf);
typedef void (*event_handler_func)(server *server, struct sender *sender, char *msgbuf);
void dispatch_event(struct server *server, char *name, struct sender *sender, char *msgbuf);
void dispatch_event(server *server, char *name, struct sender *sender, char *msgbuf);
#endif /* end of include guard: EVENT_H */
......@@ -28,7 +28,7 @@ void *app_handle;
static char *progname;
static int control_fd = -1;
static std::list<struct server *> servers;
static std::list<server *> servers;
#define MAX(a,b) (a>b?a:b)
......@@ -60,10 +60,10 @@ void do_copyover(char *requester) {
uint8_t ntags = servers.size() + 1;
struct tag_info *copyovertags = (struct tag_info *)calloc(ntags, sizeof(struct tag_info));
int i = 0;
for(std::list<struct server *>::const_iterator iter = servers.begin(), end = servers.end(); iter != end; ++iter) {
struct server *server = *iter;
for(std::list<server *>::const_iterator iter = servers.begin(), end = servers.end(); iter != end; ++iter) {
server *server = *iter;
uint32_t server_size = 0;
uint8_t *server_data = server_freeze(server, &server_size);
uint8_t *server_data = server->freeze(&server_size);
copyovertags[i].type = 'SERV';
copyovertags[i].len = server_size;
copyovertags[i].bufp = server_data;
......@@ -90,8 +90,8 @@ void do_copyover(char *requester) {
static int _load_copyover_tag_cb(uint32_t tagtype, uint32_t len, uint8_t *data, struct dady_callback *cb, void *cbdata) {
switch(tagtype) {
case 'SERV': {
struct server *server = server_thaw(data, len);
servers.push_back(server);
server *newserver = new server(data, len);
servers.push_back(newserver);
break;
} case 'CTLD':
memcpy(&control_fd, data, len);
......@@ -141,23 +141,23 @@ void _new_server(char *hostname) {
exit(1);
}
struct server *server = server_alloc();
server->hostname = strdup(hostname);
server->fd = socket_fd;
server *newserver = new server();
newserver->hostname = strdup(hostname);
newserver->fd = socket_fd;
control_fd = 0;
_D("Got control fd %d\n", control_fd);
ircprintf(server, "USER 1 2 3 4"); // TODO: YEAH
ircprintf(server, "NICK oligosII");
servers.push_back(server);
ircprintf(newserver, "USER 1 2 3 4"); // TODO: YEAH
ircprintf(newserver, "NICK oligosII");
servers.push_back(newserver);
}
void init_irc(int argc, char **argv) {
_new_server(argv[1]);
}
void ircprintf(struct server *server, const char *format, ...) {
void ircprintf(server *server, const char *format, ...) {
char *intermediate;
va_list ap;
......@@ -178,7 +178,7 @@ void ircprintf(struct server *server, const char *format, ...) {
free(intermediate);
}
void handle_irc(struct server *server, char *msgbuf) {
void handle_irc(server *server, char *msgbuf) {
struct sender sender;
memset(&sender, 0, sizeof(sender));
char *cmd = NULL;
......@@ -199,7 +199,7 @@ void handle_irc(struct server *server, char *msgbuf) {
return;
}
void _read_irc(struct server *server) {
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)
......@@ -231,7 +231,7 @@ void _read_irc(struct server *server) {
}
}
void _write_irc(struct server *server) {
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;
......@@ -245,7 +245,7 @@ void event_loop() {
int nfds = 0;
FD_ZERO(&read_fds);
FD_ZERO(&write_fds);
for(std::list<struct server *>::const_iterator iter = servers.begin(), end = servers.end(); iter != end; ++iter) {
for(std::list<server *>::const_iterator iter = servers.begin(), end = servers.end(); iter != end; ++iter) {
int fd = (*iter)->fd;
if(fd > -1) {
FD_SET(fd, &read_fds);
......@@ -266,8 +266,8 @@ void event_loop() {
_D("Error selecting?\n");
exit(1);
}
for(std::list<struct server *>::const_iterator iter = servers.begin(), end = servers.end(); iter != end; ++iter) {
struct server *server = *iter;
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);
}
......
......@@ -22,7 +22,7 @@ struct command_state {
#define READ_BUF_LEN 4096
#define WRITE_BUF_LEN (200*1024)
struct server;
void ircprintf(struct server *server, const char *format, ...);
class server;
void ircprintf(server *server, const char *format, ...);
#endif /* end of include guard: OLIGOS_H */
......@@ -4,7 +4,7 @@
#include "server.h"
/* {{{ DADY */
static int _serv_internal_tag_cb(uint32_t tagtype, uint32_t len, uint8_t *data, struct dady_callback *cb, struct server *server) {
static int _serv_internal_tag_cb(uint32_t tagtype, uint32_t len, uint8_t *data, struct dady_callback *cb, server *server) {
switch(tagtype) {
case 'RBUF':
if(len > 0) {
......@@ -35,20 +35,36 @@ static int _serv_internal_tag_cb(uint32_t tagtype, uint32_t len, uint8_t *data,
static struct dady_callback _serv_internal_callbacks = { NULL, (tag_callback)&_serv_internal_tag_cb };
/* }}} */
struct server *server_alloc() {
struct server *newserver = (struct server *)calloc(1, sizeof(struct server));
newserver->readbuf = (char *)malloc(READ_BUF_LEN);
newserver->writebuf = (char *)malloc(WRITE_BUF_LEN);
return newserver;
server::server() :
hostname(NULL),
nick(NULL),
readbuf(new char[READ_BUF_LEN]),
nbytes_read(0),
writebuf(new char[WRITE_BUF_LEN]),
nbytes_write(0) {
}
uint8_t *server_freeze(struct server *server, uint32_t *outlen) {
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) {
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?
datap += len + _TAG_HEAD_LEN;
}
}
uint8_t *server::freeze(uint32_t *outlen) {
struct tag_info taglist[] = {
{ 'RBUF', server->nbytes_read, server->readbuf, },
{ 'WBUF', server->nbytes_write, server->writebuf, },
{ '_FD_', sizeof(server->fd), &server->fd, },
{ 'NICK', server->nick ? strlen(server->nick) : 0, server->nick, },
{ 'HOST', server->hostname ? strlen(server->hostname) : 0, server->hostname, },
{ 'RBUF', nbytes_read, readbuf, },
{ 'WBUF', nbytes_write, writebuf, },
{ '_FD_', sizeof(fd), &fd, },
{ 'NICK', nick ? strlen(nick) : 0, nick, },
{ 'HOST', hostname ? strlen(hostname) : 0, hostname, },
};
uint32_t ntags = sizeof(taglist)/sizeof(struct tag_info);
......@@ -56,20 +72,9 @@ uint8_t *server_freeze(struct server *server, uint32_t *outlen) {
return outbuf;
}
struct server *server_thaw(uint8_t *data, uint32_t size) {
struct server *out = NULL;
out = server_alloc();
for(uint8_t *datap = data; datap<data+size;) {
int len = bscantag(datap, size-(datap-data), &_serv_internal_callbacks, out);
if(len == -1) break; // Problem, Officer?
datap += len + _TAG_HEAD_LEN;
}
return out;
}
void server_dealloc(struct server *server) {
free(server->readbuf);
free(server->writebuf);
free(server->nick);
free(server);
server::~server() {
free(this->readbuf);
free(this->writebuf);
if(this->nick)
free(this->nick);
}
......@@ -2,7 +2,8 @@
#define SERVER_H
#include <stdint.h>
struct server {
class server {
public:
char *hostname;
char *nick;
......@@ -12,11 +13,12 @@ struct server {
char *writebuf;
unsigned long long int nbytes_write;
};
struct server *server_alloc();
uint8_t *server_freeze(struct server *server, uint32_t *outlen);
struct server *server_thaw(uint8_t *data, uint32_t size);
void server_dealloc(struct server *server);
server();
server(uint8_t *data, uint32_t size);
uint8_t *freeze(uint32_t *outlen);
~server();
};
#endif /* end of include guard: SERVER_H */
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