aboutsummaryrefslogtreecommitdiff
path: root/user_data.c
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2017-05-24 11:24:54 +0200
committertomsmeding <tom.smeding@gmail.com>2017-05-24 11:24:54 +0200
commit66b3a7833a86ce868a1cad19cd5bbe2a62d10980 (patch)
tree4ae27e04eb6f27bfe2f1e26b47e5864da90e578a /user_data.c
parente4914ccdfa4d9eb6f71f7b5cc5b4881c4e911778 (diff)
server: Only send notifs when user is actually active
Diffstat (limited to 'user_data.c')
-rw-r--r--user_data.c69
1 files changed, 52 insertions, 17 deletions
diff --git a/user_data.c b/user_data.c
index e7d2e2a..b6a8012 100644
--- a/user_data.c
+++ b/user_data.c
@@ -1,13 +1,15 @@
#include <string.h>
#include <assert.h>
#include "user_data.h"
+#include "util.h"
struct user_data{
i64 userid;
int *fds;
- i64 fds_cap,fds_len;
+ i64 *last_active;
+ i64 cap,len;
};
struct hash_item{
@@ -48,23 +50,28 @@ static void remove_userdata(i64 userid){
void userdata_register(i64 userid,int fd){
struct hash_item *item=find_userdata(userid);
if(item){
- for(i64 i=0;i<item->data.fds_len;i++){
+ for(i64 i=0;i<item->data.len;i++){
if(item->data.fds[i]==fd){
die("userdata_register(%" PRIi64 ", %d) while pair already existed",userid,fd);
}
}
- if(item->data.fds_len==item->data.fds_cap){
- item->data.fds_cap*=2;
- item->data.fds=realloc(item->data.fds,item->data.fds_cap,int);
+ if(item->data.len==item->data.cap){
+ item->data.cap*=2;
+ item->data.fds=realloc(item->data.fds,item->data.cap,int);
+ item->data.last_active=realloc(item->data.last_active,item->data.cap,i64);
}
- item->data.fds[item->data.fds_len++]=fd;
+ item->data.fds[item->data.len]=fd;
+ item->data.last_active[item->data.len]=make_timestamp();
+ item->data.len++;
} else {
item=malloc(1,struct hash_item);
item->data.userid=userid;
- item->data.fds_cap=2;
- item->data.fds_len=1;
- item->data.fds=malloc(item->data.fds_cap,int);
+ item->data.cap=2;
+ item->data.len=1;
+ item->data.fds=malloc(item->data.cap,int);
item->data.fds[0]=fd;
+ item->data.last_active=malloc(item->data.cap,i64);
+ item->data.last_active[0]=make_timestamp();
item->next=user_hash[userid%USER_HASH_SIZE];
user_hash[userid%USER_HASH_SIZE]=item;
}
@@ -72,28 +79,56 @@ void userdata_register(i64 userid,int fd){
void userdata_unregister(i64 userid,int fd){
struct hash_item *item=find_userdata(userid);
- assert(item);
+ if(!item){
+ die("userdata_unregister(%" PRIi64 ", %d) while nonexistent",userid,fd);
+ }
i64 i;
- for(i=0;i<item->data.fds_len;i++){
+ for(i=0;i<item->data.len;i++){
if(item->data.fds[i]==fd)break;
}
- if(i==item->data.fds_len){
+ if(i==item->data.len){
die("userdata_unregister(%" PRIi64 ", %d) while nonexistent",userid,fd);
}
- memmove(item->data.fds+i,item->data.fds+i+1,item->data.fds_len-i-1);
- item->data.fds_len--;
- if(item->data.fds_len==0){
+ memmove(item->data.fds+i,item->data.fds+i+1,item->data.len-i-1);
+ memmove(item->data.last_active+i,item->data.last_active+i+1,item->data.len-i-1);
+ item->data.len--;
+ if(item->data.len==0){
remove_userdata(userid);
}
}
+void userdata_mark_active(i64 userid,int fd){
+ struct hash_item *item=find_userdata(userid);
+ if(!item){
+ die("userdata_mark_active(%" PRIi64 ") while nonexistent",userid);
+ }
+ i64 i;
+ for(i=0;i<item->data.len;i++){
+ if(item->data.fds[i]==fd)break;
+ }
+ if(i==item->data.len){
+ die("userdata_mark_active(%" PRIi64 ", %d) while nonexistent",userid,fd);
+ }
+ item->data.last_active[i]=make_timestamp();
+}
+
+bool userdata_is_active(i64 userid){
+ struct hash_item *item=find_userdata(userid);
+ if(!item)return false;
+ i64 last=0;
+ for(i64 i=0;i<item->data.len;i++){
+ if(item->data.last_active[i]>last)last=item->data.last_active[i];
+ }
+ return make_timestamp()-last<2*60*1000*1000;
+}
+
const int* userdata_online(i64 userid,i64 *nfds){
struct hash_item *item=find_userdata(userid);
if(!item){
if(nfds)*nfds=0;
return NULL;
}
- assert(item->data.fds_len!=0);
- if(nfds)*nfds=item->data.fds_len;
+ assert(item->data.len!=0);
+ if(nfds)*nfds=item->data.len;
return item->data.fds;
}