diff options
Diffstat (limited to 'tcp.c')
-rw-r--r-- | tcp.c | 33 |
1 files changed, 31 insertions, 2 deletions
@@ -31,7 +31,7 @@ i64 tcp_read_line(int sock,char **buf,i64 *bufsz){ } } -i64 sock_read_data(int sock,char *buf,i64 length){ +i64 tcp_read_data(int sock,char *buf,i64 length){ i64 got=0; while(got<length){ i64 ret=recv(sock,buf+got,length-got,0); @@ -64,7 +64,7 @@ i64 tcp_send_line(int sock,const char *str){ } __attribute__((format (printf,2,3))) -i64 sock_send_line_f(int sock,const char *format,...){ +i64 tcp_send_line_f(int sock,const char *format,...){ va_list ap; va_start(ap,format); char *buf; @@ -77,6 +77,35 @@ i64 sock_send_line_f(int sock,const char *format,...){ } +i64 tcp_send_list(int sock,const char *tag,const char *const *list,i64 len){ + char *buf; + asprintf(&buf,"list %s %" PRIi64,tag,len); + if(buf==NULL)throw("asprintf: allocation failure"); + if(len==0){ + i64 ret=tcp_send_line(sock,buf); + free(buf); + return ret; + } + + if(tcp_send_str(sock,buf)==-1){free(buf); return -1;} + for(i64 i=0;i<len;i++){ + if(tcp_send_str(sock," ")==-1){free(buf); return -1;} + if(tcp_send_str(sock,list[i])==-1){free(buf); return -1;} + } + return tcp_send_str(sock,"\n"); +} + +//Returns -1 on error or connection closure. +i64 tcp_send_int(int sock,const char *tag,i64 value){ + char *buf; + asprintf(&buf,"int %s %" PRIi64,tag,value); + if(buf==NULL)throw("asprintf: allocation failure"); + i64 ret=tcp_send_line(sock,buf); + free(buf); + return ret; +} + + i64 tcp_read_ok(int sock,const char *tag){ char *buf=NULL; i64 bufsz=0; |