From c02fa99188c868496f864a20b1139ec4e6f93c2a Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Fri, 14 Apr 2017 20:43:29 +0200 Subject: server: Factor out net related functions from command.c --- command.c | 137 ++++++++++++++++---------------------------------------------- net.c | 75 ++++++++++++++++++++++++++++++++++ net.h | 10 +++++ 3 files changed, 119 insertions(+), 103 deletions(-) create mode 100644 net.c create mode 100644 net.h diff --git a/command.c b/command.c index 4e7180d..92556de 100644 --- a/command.c +++ b/command.c @@ -6,6 +6,7 @@ #include #include "command.h" #include "db.h" +#include "net.h" #include "user_data.h" @@ -16,90 +17,20 @@ static i64 make_timestamp(void){ } -static bool send_raw_text(int fd,const char *text,i64 len){ - i64 cursor=0; - while(cursor0){ - i64 bufsz=64; - buf=malloc(bufsz,char); - - for(i64 i=0;i=bufsz){ - bufsz=len+512; - buf=realloc(buf,bufsz,char); - } - memcpy(buf+1,list[i],len); - buf[0]=' '; - if(send_raw_text(fd,buf,len+1)){ - free(buf); - return true; - } - } - - free(buf); - } - - return send_raw_text(fd,"\n",1); -} - - static bool cmd_register(struct conn_data *data,const char *tag,const char **args){ i64 userid=db_find_user(args[0]); if(userid!=-1){ - send_error(data->fd,tag,"Username already exists"); + net_send_error(data->fd,tag,"Username already exists"); return false; } db_create_user(args[0],args[1]); - return send_ok(data->fd,tag); + return net_send_ok(data->fd,tag); } static bool cmd_login(struct conn_data *data,const char *tag,const char **args){ i64 userid=db_find_user(args[0]); if(userid==-1){ - send_error(data->fd,tag,"User not found"); + net_send_error(data->fd,tag,"User not found"); return false; } char *pass=db_get_pass(userid); @@ -111,10 +42,10 @@ static bool cmd_login(struct conn_data *data,const char *tag,const char **args){ if(success){ data->userid=userid; userdata_register(userid,data->fd); - send_ok(data->fd,tag); + net_send_ok(data->fd,tag); } else { data->userid=-1; - send_error(data->fd,tag,"Incorrect password"); + net_send_error(data->fd,tag,"Incorrect password"); } return false; } @@ -122,48 +53,48 @@ static bool cmd_login(struct conn_data *data,const char *tag,const char **args){ static bool cmd_list_rooms(struct conn_data *data,const char *tag,const char **args){ (void)args; if(data->userid==-1){ - send_error(data->fd,tag,"Not logged in"); + net_send_error(data->fd,tag,"Not logged in"); return false; } struct db_room_list rl=db_list_rooms(data->userid); if(rl.count<=0){ db_nullify_room_list(rl); - return send_list(data->fd,tag,0,NULL); + return net_send_list(data->fd,tag,0,NULL); } const char *names[rl.count]; for(i64 i=0;ifd,tag,rl.count,names); + bool closed=net_send_list(data->fd,tag,rl.count,names); db_nullify_room_list(rl); return closed; } static bool cmd_list_members(struct conn_data *data,const char *tag,const char **args){ if(data->userid==-1){ - send_error(data->fd,tag,"Not logged in"); + net_send_error(data->fd,tag,"Not logged in"); return false; } i64 roomid=db_find_room(args[0]); if(roomid==-1){ - send_error(data->fd,tag,"Room not found"); + net_send_error(data->fd,tag,"Room not found"); return false; } if(!db_is_member(roomid,data->userid)){ - send_error(data->fd,tag,"Not in that room"); + net_send_error(data->fd,tag,"Not in that room"); return false; } struct db_user_list ul=db_list_members(roomid); if(ul.count<=0){ db_nullify_user_list(ul); - return send_list(data->fd,tag,0,NULL); + return net_send_list(data->fd,tag,0,NULL); } const char *names[ul.count]; for(i64 i=0;ifd,tag,ul.count,names); + bool closed=net_send_list(data->fd,tag,ul.count,names); db_nullify_user_list(ul); return closed; } @@ -171,39 +102,39 @@ static bool cmd_list_members(struct conn_data *data,const char *tag,const char * static bool cmd_create_room(struct conn_data *data,const char *tag,const char **args){ (void)args; if(data->userid==-1){ - send_error(data->fd,tag,"Not logged in"); + net_send_error(data->fd,tag,"Not logged in"); return false; } struct db_name_id room=db_create_room(); db_add_member(room.id,data->userid); - bool closed=send_name(data->fd,tag,room.name); + bool closed=net_send_name(data->fd,tag,room.name); db_nullify_name_id(room); return closed; } static bool cmd_invite(struct conn_data *data,const char *tag,const char **args){ if(data->userid==-1){ - send_error(data->fd,tag,"Not logged in"); + net_send_error(data->fd,tag,"Not logged in"); return false; } i64 roomid=db_find_room(args[0]); if(roomid==-1){ - send_error(data->fd,tag,"Room not found"); + net_send_error(data->fd,tag,"Room not found"); return false; } i64 user2=db_find_user(args[1]); if(user2==-1){ - send_error(data->fd,tag,"User not found"); + net_send_error(data->fd,tag,"User not found"); return false; } if(!db_is_member(roomid,data->userid)){ - send_error(data->fd,tag,"Not in that room"); + net_send_error(data->fd,tag,"Not in that room"); return false; } if(db_is_member(roomid,user2)){ - send_error(data->fd,tag,"User already in that room"); + net_send_error(data->fd,tag,"User already in that room"); return false; } @@ -223,9 +154,9 @@ static bool cmd_invite(struct conn_data *data,const char *tag,const char **args) for(i64 j=0;jfd){ if(members.list[i].id==user2){ - send_raw_text(fds[j],invitebuf,invitebuflen); + net_send_raw_text(fds[j],invitebuf,invitebuflen); } else { - send_raw_text(fds[j],joinbuf,joinbuflen); + net_send_raw_text(fds[j],joinbuf,joinbuflen); } } } @@ -235,27 +166,27 @@ static bool cmd_invite(struct conn_data *data,const char *tag,const char **args) free(joinbuf); free(invitebuf); - return send_ok(data->fd,tag); + return net_send_ok(data->fd,tag); } static bool cmd_send(struct conn_data *data,const char *tag,const char **args){ if(data->userid==-1){ - send_error(data->fd,tag,"Not logged in"); + net_send_error(data->fd,tag,"Not logged in"); return false; } i64 roomid=db_find_room(args[0]); if(roomid==-1){ - send_error(data->fd,tag,"Room not found"); + net_send_error(data->fd,tag,"Room not found"); return false; } if(!db_is_member(roomid,data->userid)){ - send_error(data->fd,tag,"Not in that room"); + net_send_error(data->fd,tag,"Not in that room"); return false; } i64 timestamp=make_timestamp(); db_create_message(roomid,data->userid,make_timestamp(),args[1]); - bool closed=send_ok(data->fd,tag); + bool closed=net_send_ok(data->fd,tag); char *pushbuf=NULL; char *username=db_get_username(data->userid); @@ -270,7 +201,7 @@ static bool cmd_send(struct conn_data *data,const char *tag,const char **args){ if(nfds<=0)continue; for(i64 j=0;jfd){ - send_raw_text(fds[j],pushbuf,pushbuflen); + net_send_raw_text(fds[j],pushbuf,pushbuflen); } } } @@ -290,23 +221,23 @@ static bool cmd_history(struct conn_data *data,const char *tag,const char **args } if(data->userid==-1){ - send_error(data->fd,tag,"Not logged in"); + net_send_error(data->fd,tag,"Not logged in"); return false; } i64 roomid=db_find_room(args[0]); if(roomid==-1){ - send_error(data->fd,tag,"Room not found"); + net_send_error(data->fd,tag,"Room not found"); return false; } if(!db_is_member(roomid,data->userid)){ - send_error(data->fd,tag,"Not in that room"); + net_send_error(data->fd,tag,"Not in that room"); return false; } struct db_message_list ml=db_get_messages(roomid,nrequested); char *buf=NULL; i64 len=asprintf(&buf,"%s history %" PRIi64 "\n",tag,ml.count); - bool closed=send_raw_text(data->fd,buf,len); + bool closed=net_send_raw_text(data->fd,buf,len); free(buf); if(closed){ @@ -318,7 +249,7 @@ static bool cmd_history(struct conn_data *data,const char *tag,const char **args char *username=db_get_username(ml.list[i].userid); len=asprintf(&buf,"%s history_message %" PRIi64 " %s %s %" PRIi64 " %s\n", tag,ml.count-1-i,args[0],username,ml.list[i].timestamp,ml.list[i].message); - closed=send_raw_text(data->fd,buf,len); + closed=net_send_raw_text(data->fd,buf,len); free(buf); if(closed)break; } diff --git a/net.c b/net.c new file mode 100644 index 0000000..8e6644c --- /dev/null +++ b/net.c @@ -0,0 +1,75 @@ +#include +#include +#include +#include +#include "net.h" + + +bool net_send_raw_text(int fd,const char *text,i64 len){ + i64 cursor=0; + while(cursor0){ + i64 bufsz=64; + buf=malloc(bufsz,char); + + for(i64 i=0;i=bufsz){ + bufsz=len+512; + buf=realloc(buf,bufsz,char); + } + memcpy(buf+1,list[i],len); + buf[0]=' '; + if(net_send_raw_text(fd,buf,len+1)){ + free(buf); + return true; + } + } + + free(buf); + } + + return net_send_raw_text(fd,"\n",1); +} diff --git a/net.h b/net.h new file mode 100644 index 0000000..de78c12 --- /dev/null +++ b/net.h @@ -0,0 +1,10 @@ +#pragma once + +#include "global.h" + + +bool net_send_raw_text(int fd,const char *text,i64 len); +bool net_send_ok(int fd,const char *tag); +bool net_send_error(int fd,const char *tag,const char *msg); +bool net_send_name(int fd,const char *tag,const char *name); +bool net_send_list(int fd,const char *tag,i64 count,const char **list); -- cgit v1.2.3-70-g09d2