From 8e1a1c1f01aef52ba8b2af47503461320a0abc20 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Sat, 15 Apr 2017 18:09:48 +0200 Subject: server: Send _push online messages on online change --- broadcast.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ broadcast.h | 6 +++++ command.c | 3 +++ main.c | 11 ++++++-- 4 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 broadcast.c create mode 100644 broadcast.h diff --git a/broadcast.c b/broadcast.c new file mode 100644 index 0000000..0517dc3 --- /dev/null +++ b/broadcast.c @@ -0,0 +1,89 @@ +#include +#include "broadcast.h" +#include "db.h" +#include "net.h" +#include "user_data.h" + + +// Returns whether value was new and therefore inserted into the list. +// If already present, does nothing and returns false. +// Assumes enough space for the possible new item. +static bool sorted_insert(i64 *buf,i64 len,i64 value){ + if(len==0){ + buf[0]=value; + return true; + } + i64 L=0,H=len-1; + if(buf[L]==value||buf[H]==value)return false; + if(valuebuf[H]){ + buf[len]=value; + return true; + } + while(Lvalue)break; + } else if(buf[M]>value){ + H=M-1; + if(buf[H] #include #include +#include "broadcast.h" #include "command.h" #include "db.h" #include "net.h" @@ -38,11 +39,13 @@ static bool cmd_login(struct conn_data *data,const char *tag,const char **args){ free(pass); if(data->userid!=-1){ userdata_unregister(data->userid,data->fd); + broadcast_online_change(data->userid); } if(success){ data->userid=userid; userdata_register(userid,data->fd); net_send_ok(data->fd,tag); + broadcast_online_change(userid); } else { data->userid=-1; net_send_error(data->fd,tag,"Incorrect password"); diff --git a/main.c b/main.c index 622e1b9..5afece8 100644 --- a/main.c +++ b/main.c @@ -7,6 +7,7 @@ #include #include #include +#include "broadcast.h" #include "command.h" #include "conn_data.h" #include "db.h" @@ -53,7 +54,10 @@ static void delete_conn_data(int fd){ struct hash_item *item=conn_hash[fd%CONN_HASH_SIZE]; assert(item); if(item->cd.fd==fd){ - if(item->cd.userid!=-1)userdata_unregister(item->cd.userid,fd); + if(item->cd.userid!=-1){ + userdata_unregister(item->cd.userid,fd); + broadcast_online_change(item->cd.userid); + } conn_hash[fd%CONN_HASH_SIZE]=item->next; conn_data_nullify(&item->cd); free(item); @@ -66,7 +70,10 @@ static void delete_conn_data(int fd){ } assert(parent); assert(item); - if(item->cd.userid!=-1)userdata_unregister(item->cd.userid,fd); + if(item->cd.userid!=-1){ + userdata_unregister(item->cd.userid,fd); + broadcast_online_change(item->cd.userid); + } conn_data_nullify(&item->cd); parent->next=item->next; free(item); -- cgit v1.2.3-70-g09d2