From 94b96aec6bbd5397e10f45fcf2419fad2f2ec800 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Thu, 25 May 2017 11:38:38 +0200 Subject: server: Client can mark fd's active/inactive --- command.c | 22 +++++++++++++++++++--- user_data.c | 9 +++++---- user_data.h | 2 +- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/command.c b/command.c index 1b70369..53d65f5 100644 --- a/command.c +++ b/command.c @@ -120,7 +120,7 @@ static bool cmd_create_room(struct conn_data *data,const char *tag,const char ** net_send_error(data->fd,tag,"Not logged in"); return false; } - userdata_mark_active(data->userid,data->fd); + userdata_mark_active(data->userid,data->fd,true); struct db_name_id room=db_create_room(); db_add_member(room.id,data->userid); bool closed=net_send_name(data->fd,tag,room.name); @@ -133,7 +133,7 @@ static bool cmd_invite(struct conn_data *data,const char *tag,const char **args) net_send_error(data->fd,tag,"Not logged in"); return false; } - userdata_mark_active(data->userid,data->fd); + userdata_mark_active(data->userid,data->fd,true); const char *roomname=args[0]; i64 roomid=db_find_room(roomname); if(roomid==-1){ @@ -194,7 +194,7 @@ static bool cmd_send(struct conn_data *data,const char *tag,const char **args){ net_send_error(data->fd,tag,"Not logged in"); return false; } - userdata_mark_active(data->userid,data->fd); + userdata_mark_active(data->userid,data->fd,true); const char *roomname=args[0]; const char *message=args[1]; i64 roomid=db_find_room(roomname); @@ -318,6 +318,21 @@ static bool cmd_delete_firebase_token(struct conn_data *data,const char *tag,con return net_send_ok(data->fd,tag); } +static bool cmd_user_active(struct conn_data *data,const char *tag,const char **args){ + if(data->userid==-1){ + net_send_error(data->fd,tag,"Not logged in"); + return false; + } + char *endp; + i64 active=strtoll(args[0],&endp,10); + if(args[0][0]=='\0'||*endp!='\0'||active<0){ + debug("Connection fd=%d sent an invalid number for 'user_active': '%s'",data->fd,args[0]); + return true; + } + userdata_mark_active(data->userid,data->fd,active>0); + return net_send_ok(data->fd,tag); +} + struct cmd_info{ const char *cmdname; @@ -340,6 +355,7 @@ static const struct cmd_info commands[]={ {"is_online",1,false,cmd_is_online}, {"firebase_token",1,false,cmd_firebase_token}, {"delete_firebase_token",1,false,cmd_delete_firebase_token}, + {"user_active",1,false,cmd_user_active}, }; #define NCOMMANDS (sizeof(commands)/sizeof(commands[0])) diff --git a/user_data.c b/user_data.c index b6a8012..f92d4d4 100644 --- a/user_data.c +++ b/user_data.c @@ -97,19 +97,20 @@ void userdata_unregister(i64 userid,int fd){ } } -void userdata_mark_active(i64 userid,int fd){ +void userdata_mark_active(i64 userid,int fd,bool active){ struct hash_item *item=find_userdata(userid); if(!item){ - die("userdata_mark_active(%" PRIi64 ") while nonexistent",userid); + die("userdata_mark_active(%" PRIi64 ", %d, %d) with nonexistent userid",userid,fd,active); } i64 i; for(i=0;idata.len;i++){ if(item->data.fds[i]==fd)break; } if(i==item->data.len){ - die("userdata_mark_active(%" PRIi64 ", %d) while nonexistent",userid,fd); + die("userdata_mark_active(%" PRIi64 ", %d, %d) while nonexistent",userid,fd,active); } - item->data.last_active[i]=make_timestamp(); + + item->data.last_active[i]=active?make_timestamp():-1; } bool userdata_is_active(i64 userid){ diff --git a/user_data.h b/user_data.h index 5769ae9..1ac5307 100644 --- a/user_data.h +++ b/user_data.h @@ -6,7 +6,7 @@ void userdata_register(i64 userid,int fd); void userdata_unregister(i64 userid,int fd); -void userdata_mark_active(i64 userid,int fd); +void userdata_mark_active(i64 userid,int fd,bool active); bool userdata_is_active(i64 userid); -- cgit v1.2.3