From 54d371e4e897fa58c316fcc3b6dc7bde5af6868c Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Wed, 11 Oct 2017 22:28:00 +0200 Subject: Third --- server/data_stream.c | 23 ++++++++++++++++++----- server/main.c | 7 +------ 2 files changed, 19 insertions(+), 11 deletions(-) (limited to 'server') diff --git a/server/data_stream.c b/server/data_stream.c index 3d25104..2719d2a 100644 --- a/server/data_stream.c +++ b/server/data_stream.c @@ -5,10 +5,13 @@ #include #include #include "data_stream.h" +#include "util.h" struct data_stream { int fd; + FILE *mirrorfile; + i64 pidbufsize; pid_t *pids; i64 namebufsize; @@ -21,11 +24,11 @@ struct data_stream { static bool callback_error=false; -static void write_data_fd(u8 *data,i64 len,void *fdp){ - int fd=*(int*)fdp; +static void write_data_callback(u8 *data,i64 len,void *streamp){ + struct data_stream *s=(struct data_stream*)streamp; i64 cursor=0; while(cursorfd,data+cursor,len-cursor); if(nwr<0){ perror("write"); callback_error=true; @@ -33,7 +36,8 @@ static void write_data_fd(u8 *data,i64 len,void *fdp){ } cursor+=nwr; } - fprintf(stderr,"write_data_fd(len=%" PRIi64 ")\n",len); + fwrite(data,1,len,s->mirrorfile); + fprintf(stderr,"write_data_callback(len=%" PRIi64 ")\n",len); } static void bufappend(u8 **buf,i64 *cursor,i64 *cap,u8 *data,i64 datalen){ @@ -57,6 +61,14 @@ static void serialise4(u8 *buf,i32 value){ struct data_stream* data_stream_init(int fd){ struct data_stream *s=malloc(1,struct data_stream); s->fd=fd; + char fnamebuf[128]; + snprintf(fnamebuf,sizeof fnamebuf,"logmirror_%" PRIi64 ".zz",make_timestamp()); + printf("Using log mirror file '%s'\n",fnamebuf); + s->mirrorfile=fopen(fnamebuf,"w"); + if(s->mirrorfile==NULL){ + fprintf(stderr,"Cannot open log mirror!\n"); + exit(1); + } s->pidbufsize=1024; s->pids=malloc(s->pidbufsize,pid_t); s->namebufsize=64; @@ -64,7 +76,7 @@ struct data_stream* data_stream_init(int fd){ s->writebufsize=4096; s->writebuf=malloc(s->writebufsize,u8); s->cursor=0; - s->z=zbuffer_init(write_data_fd,&s->fd); + s->z=zbuffer_init(write_data_callback,s); callback_error=false; @@ -74,6 +86,7 @@ struct data_stream* data_stream_init(int fd){ int data_stream_finish_destroy(struct data_stream *s){ callback_error=false; zbuffer_finish_destroy(s->z); + fclose(s->mirrorfile); free(s->pids); free(s->namebuf); free(s->writebuf); diff --git a/server/main.c b/server/main.c index ffa16cd..cb4e2b4 100644 --- a/server/main.c +++ b/server/main.c @@ -12,6 +12,7 @@ #include "data_stream.h" #include "line_reader.h" #include "zbuffer.h" +#include "util.h" #define PORT 57575 @@ -38,12 +39,6 @@ int32_t pti_priority; task priority };*/ -static i64 make_timestamp(void){ - struct timeval tv; - gettimeofday(&tv,NULL); - return tv.tv_sec*(i64)1000000+tv.tv_usec; -} - static void connection_handler(int sock){ struct data_stream *stream=data_stream_init(sock); struct line_reader *reader=line_reader_init(sock); -- cgit v1.2.3-70-g09d2