diff options
| author | tomsmeding <tom.smeding@gmail.com> | 2017-01-19 18:29:55 +0100 | 
|---|---|---|
| committer | tomsmeding <tom.smeding@gmail.com> | 2017-01-19 18:29:55 +0100 | 
| commit | fb62a55d2fafa4df5dec7c730df635758db2dd70 (patch) | |
| tree | aedd6f700aa602628450ec9256bd4248988f1277 | |
| parent | 830a7b4ff5d08f408a3df56846d8b336d17fa979 (diff) | |
Add more tcp functions from roomserver
| -rw-r--r-- | tcp.c | 33 | ||||
| -rw-r--r-- | tcp.h | 5 | 
2 files changed, 35 insertions, 3 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; @@ -1,6 +1,6 @@  #pragma once -#include "global.h" +#include <stdint.h>  //Unless specified otherwise, functions return -1 on error or socket closure. @@ -15,6 +15,9 @@ i64 tcp_send_str(int sock,const char *str);  i64 tcp_send_line(int sock,const char *str);  i64 tcp_send_line_f(int sock,const char *format,...) __attribute__((format (printf,2,3))); +i64 tcp_send_list(int sock,const char *tag,const char *const *list,i64 len); +i64 tcp_send_int(int sock,const char *tag,i64 value); +  i64 tcp_read_ok(int sock,const char *tag);  | 
