aboutsummaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c53
1 files changed, 26 insertions, 27 deletions
diff --git a/main.c b/main.c
index 03bf7b0..b925cd1 100644
--- a/main.c
+++ b/main.c
@@ -19,6 +19,7 @@
#include "plugin.h"
#include "runloop.h"
#include "user_data.h"
+#include "hashtable.h"
#define PORT (29536) // python: int("msg",36)
@@ -39,39 +40,35 @@ static int create_server_socket(void){
return sock;
}
-struct hash_item{
- struct conn_data cd;
- struct hash_item *next;
-};
+static struct hashtable *conn_hash = NULL;
-#define CONN_HASH_SIZE (16)
-static struct hash_item *conn_hash[CONN_HASH_SIZE];
+static void conn_hash_init(void) {
+ conn_hash = ht_alloc();
+}
+
+static void add_conn_data(int fd, struct conn_data *item) {
+ ht_insert(conn_hash, fd, item);
+}
static struct conn_data* find_conn_data(int fd){
- struct hash_item *item=conn_hash[fd%CONN_HASH_SIZE];
- while(item&&item->cd.fd!=fd)item=item->next;
+ struct conn_data *item = (struct conn_data*)ht_find(conn_hash, fd);
assert(item);
- return &item->cd;
+ return item;
}
static void delete_conn_data(int fd){
debug("Deleting conn_data for fd=%d",fd);
- struct hash_item *item=conn_hash[fd%CONN_HASH_SIZE];
- assert(item);
- struct hash_item *parent=NULL;
- while(item&&item->cd.fd!=fd){
- parent=item;
- item=item->next;
- }
- assert(item);
- if(item->cd.userid!=-1){
- userdata_unregister(item->cd.userid,fd);
- broadcast_online_change(item->cd.userid);
+
+ struct conn_data *item = find_conn_data(fd);
+
+ if (item->userid != -1) {
+ userdata_unregister(item->userid, fd);
+ broadcast_online_change(item->userid);
}
- conn_data_nullify(&item->cd);
- if(parent)parent->next=item->next;
- else conn_hash[fd%CONN_HASH_SIZE]=item->next;
+ conn_data_nullify(item);
free(item);
+
+ ht_delete(conn_hash, fd);
}
static bool client_socket_callback(int fd){
@@ -116,10 +113,10 @@ static bool server_socket_callback(int fd){
if(sock<0)die_perror("accept");
runloop_add_fd(sock,client_socket_callback,true);
- struct hash_item *item=malloc(1,struct hash_item);
- conn_data_init(&item->cd,sock);
- item->next=conn_hash[sock%CONN_HASH_SIZE];
- conn_hash[sock%CONN_HASH_SIZE]=item;
+ struct conn_data *item = malloc(1, struct conn_data);
+ conn_data_init(item, sock);
+ add_conn_data(sock, item);
+
debug("Added conn_data for fd=%d",sock);
return false;
}
@@ -167,6 +164,8 @@ int main(int argc,char **argv){
signal(SIGPIPE,signal_handler);
+ conn_hash_init();
+
plugin_init();
for(int i=1;i<argc;i++){
plugin_register(argv[i]);