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

Make server a proper class.

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