aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2017-03-16 21:42:24 +0100
committertomsmeding <tom.smeding@gmail.com>2017-03-16 21:42:24 +0100
commit9d21406d68ebdce18110431fb24c9963dcc44373 (patch)
tree954a0fdc48ef473f8c1faf7177c064c33d8ab4a0
parent047e1828901b5b08cd538a7b11c0de2149606797 (diff)
Send new messages to every fd of user
-rw-r--r--command.c11
-rw-r--r--user_data.c10
-rw-r--r--user_data.h4
3 files changed, 17 insertions, 8 deletions
diff --git a/command.c b/command.c
index 0f2cd06..bf6c29d 100644
--- a/command.c
+++ b/command.c
@@ -212,10 +212,13 @@ static bool cmd_send(struct conn_data *data,const char *tag,const char **args){
struct db_user_list members=db_get_members(roomid);
for(i64 i=0;i<members.count;i++){
- int fd=userdata_online(members.list[i].id);
- if(fd!=-1&&fd!=data->fd){
- send_push(fd,"message",args[1]);
- // TODO: unregister and close users that aren't reachable anymore; maybe ping instead?
+ i64 nfds;
+ const int *fds=userdata_online(members.list[i].id,&nfds);
+ if(nfds<=0)continue;
+ for(i64 j=0;j<nfds;j++){
+ if(fds[j]!=data->fd){
+ send_push(fds[j],"message",args[1]);
+ }
}
}
diff --git a/user_data.c b/user_data.c
index 77e09cc..65587ad 100644
--- a/user_data.c
+++ b/user_data.c
@@ -64,9 +64,13 @@ void userdata_unregister(i64 userid,int fd){
item->data.fds_len--;
}
-int userdata_online(i64 userid){
+const int* userdata_online(i64 userid,i64 *nfds){
struct hash_item *item=find_userdata(userid);
- if(!item)return false;
+ if(!item){
+ *nfds=0;
+ return NULL;
+ }
assert(item->data.fds_len!=0);
- return item->data.fds[0]; // TODO: return all fds in some way?
+ *nfds=item->data.fds_len;
+ return item->data.fds;
}
diff --git a/user_data.h b/user_data.h
index 4b4fc10..094ad75 100644
--- a/user_data.h
+++ b/user_data.h
@@ -5,4 +5,6 @@
void userdata_register(i64 userid,int fd);
void userdata_unregister(i64 userid,int fd);
-int userdata_online(i64 userid); // Returns fd
+
+// Returns array of nfds fd's; valid until next call to (un)register
+const int* userdata_online(i64 userid,i64 *nfds);