From d3c5d3d150e9f9368b2ba9f8d0f957dbb58c14d4 Mon Sep 17 00:00:00 2001 From: hackbard Date: Thu, 6 May 2004 13:43:07 +0000 Subject: [PATCH] more abstraction in network.* and input.*, first display stuff in ivac.* --- src/input.c | 30 +++++----- src/input.h | 6 +- src/ivac.c | 157 ++++++++++++++++++++++++++++++++++++++++++++++++-- src/ivac.h | 28 ++++++++- src/network.c | 68 ---------------------- src/network.h | 14 +---- 6 files changed, 202 insertions(+), 101 deletions(-) diff --git a/src/input.c b/src/input.c index a2886ee..1403017 100644 --- a/src/input.c +++ b/src/input.c @@ -18,11 +18,13 @@ int input_init(t_input *input) { } input->c_count=0; - tcgetattr(0,&tios); - /* switch off canonical mode */ - tios.c_lflag&=(~ICANON); - tios.c_lflag&=(~ECHO); - tcsetattr(0,TCSANOW,&tios); + if(!(input->mode&LINE_BUFFERED)) { + tcgetattr(0,&tios); + /* switch off canonical mode */ + tios.c_lflag&=(~ICANON); + tios.c_lflag&=(~ECHO); + tcsetattr(0,TCSANOW,&tios); + } return I_SUCCESS; } @@ -43,25 +45,27 @@ int input_shutdown(t_input *input) { return I_SUCCESS; } -int input_get_char(t_input *input,int (*callback)(t_input *input,void *ptr), +int input_get_event(t_input *input,int (*callback)(t_input *input,void *ptr), void *ptr) { - char data[1]; + char data[MAX_CONTENT]; + int count; - if(read(0,data,1)==-1) { + /* delete char counter if not buffered */ + if(!(input->mode&CONTENT_BUFFER)) input->c_count=0; + + if((count=read(0,data,MAX_CONTENT))==-1) { perror("[input] read call"); return I_ERROR; } - if(input->c_count==MAX_CONTENT) { + if(input->c_count>=MAX_CONTENT) { puts("[input] max input length reached"); return I_ERROR; } - input->content[input->c_count]=data[0]; - input->c_count++; - - if(data[0]=='\n') input->c_count=0; + strncpy(&(input->content[input->c_count]),data,count); + input->c_count+=count; callback(input,ptr); diff --git a/src/input.h b/src/input.h index e31ebe2..a3d8dd4 100644 --- a/src/input.h +++ b/src/input.h @@ -6,6 +6,7 @@ /* includes */ #include #include +#include #include #include #include @@ -19,6 +20,9 @@ #define MAX_CONTENT 128 +#define LINE_BUFFERED (1<<0) +#define CONTENT_BUFFER (1<<1) + /* input specific variables */ typedef struct s_input { char *content; @@ -29,7 +33,7 @@ typedef struct s_input { /* function prototypes */ int input_init(t_input *input); int input_shutdown(t_input *input); -int input_get_char(t_input *input,int (*callback)(t_input *input,void *ptr), +int input_get_event(t_input *input,int (*callback)(t_input *input,void *ptr), void *ptr); #endif diff --git a/src/ivac.c b/src/ivac.c index f4995a7..062fc56 100644 --- a/src/ivac.c +++ b/src/ivac.c @@ -32,6 +32,10 @@ int main(int argc,char **argv) { /* set username (futur: read from config or entered later) */ strcpy(ivac.username,"hackbard"); + /* set capabilities (futur: set by check routines) */ + ivac.g_cap=NETWORK; + ivac.av_cap=AUDIO|VIDEO|DUPLEX; + /* set event timeout */ ivac.event.timeout.tv_sec=IVAC_S_SEC; ivac.event.timeout.tv_usec=IVAC_S_USEC; @@ -43,6 +47,7 @@ int main(int argc,char **argv) { event_init(&(ivac.event)); /* input init */ + ivac.input.mode=CONTENT_BUFFER; input_init(&(ivac.input)); /* network init */ @@ -55,6 +60,9 @@ int main(int argc,char **argv) { event_math(ivac.net.l_fd,&(ivac.event),READ,ADD); event_math(0,&(ivac.event),READ,ADD); + /* display */ + ivac_display(&(ivac)); + /* start event system - callbacks used: ivac_event_cb + ivac_regular_cb */ event_start(&(ivac.event),(void *)&ivac,ivac_event_cb,ivac_regular_cb); @@ -65,6 +73,76 @@ int main(int argc,char **argv) { return SUCCESS; } +int ivac_send_info(int channel,t_ivac *ivac) { + + char data[SEND_N_MAX]; + int size; + + size=strlen(ivac->username); + + data[0]=IVAC_SEND_NAME; + data[1]=size; + strncpy(data+2,ivac->username,size); + size+=2; + + data[size]=IVAC_SEND_G_CAP; + data[size+1]=1; + data[size+2]=ivac->g_cap; + size+=3; + + data[size]=IVAC_SEND_AV_CAP; + data[size+1]=2; + data[size+2]=(ivac->av_cap)>>8; + data[size+3]=(ivac->av_cap)&0xff; + size+=4; + + if(network_send(ivac->net.connection[channel].fd,data,size)==N_ERROR) { + puts("[ivac] ivac_send_info failed"); + return ERROR; + } + + return SUCCESS; +} + +int ivac_receive_info(int channel,t_ivac *ivac) { + + char data[SEND_N_MAX]; + int count,length; + + count=0; + + if((length=network_receive(ivac->net.connection[channel].fd, + data,SEND_N_MAX))==N_ERROR) { + puts("[ivac] ivac_receive_info failed"); + return ERROR; + } + + while(length-count) { + switch(data[count]) { + case IVAC_SEND_NAME: + strncpy(ivac->challenger[channel].name,data+count+2,data[count+1]); + ivac->challenger[channel].name[data[count+1]]='\0'; + count+=(data[count+1]+2); + break; + case IVAC_SEND_G_CAP: + ivac->challenger[channel].g_cap=data[count+2]; + count+=3; + break; + case IVAC_SEND_AV_CAP: + ivac->challenger[channel].av_cap=data[count+2]<<8; + ivac->challenger[channel].av_cap|=data[count+3]; + count+=4; + break; + default: + puts("[ivac] ivac_receive_info, unknown character"); + return ERROR; + break; + } + } + + return SUCCESS; +} + int ivac_event_cb(t_event *event,void *ptr) { t_ivac *ivac; @@ -76,18 +154,21 @@ int ivac_event_cb(t_event *event,void *ptr) { /* manage incoming + send info */ channel=network_manage_incoming(&(ivac->net)); event_math(ivac->net.connection[channel].fd,event,READ,ADD); - send_info(channel,&(ivac->net),ivac->username); + ivac_send_info(channel,ivac); } /* receive info */ for(channel=0;channelnet.connection[channel].status&C_ESTABL) if(FD_ISSET(ivac->net.connection[channel].fd,&(event->rfds))) - receive_info(channel,&(ivac->net)); + ivac_receive_info(channel,ivac); /* user interaction */ if(FD_ISSET(0,&(event->rfds))) - input_get_char(&(ivac->input),ivac_display,ivac); + input_get_event(&(ivac->input),ivac_parse_command,ivac); + + /* display ivac gui */ + ivac_display(ivac); return SUCCESS; } @@ -99,13 +180,77 @@ int ivac_regular_cb(t_event *event,void *ptr) { return SUCCESS; } -int ivac_display(t_input *input,void *ptr) { +int ivac_parse_command(t_input *input,void *ptr) { t_ivac *ivac; + int channel; ivac=(t_ivac *)ptr; - if(input->content[input->c_count-1]=='q') ivac->event.status=DISABLED; + /* parse command routines */ - return SUCCESS; + if(input->content[input->c_count-1]=='\n') { + /* delete content buffer + reset counter */ + memset(input->content,0,input->c_count); + input->c_count=0; + } + + return SUCCESS; +} + +int ivac_display_head(void) { + + /* 23 x 80 */ + int column,line; + + for(column=0;columninput.c_count;column++) + printf("%c",ivac->input.content[column-12]); + for(column=12+ivac->input.c_count;columncap; - size+=(sizeof(unsigned char)+2); - - data[size]=SEND_N_AV_CAP; - data[size+1]=sizeof(unsigned short); - data[size+1+sizeof(unsigned short)]=net->avcap; - size+=(sizeof(unsigned short)+2); - - if(network_send(net->connection[channel].fd,data,size)==N_ERROR) { - puts("[network] send_info failed"); - return N_ERROR; - } - - return N_SUCCESS; -} - -int receive_info(int channel,t_net *net) { - - char data[CHAR_N_UNAME+2]; - int count,length; - - count=0; - - if((length=network_receive(net->connection[channel].fd, - data,SEND_N_MAX))==N_ERROR) { - puts("[network] receive_info failed"); - return N_ERROR; - } - - while(length-count) { - switch(data[count]) { - case SEND_N_NAME: - strncpy(net->connection[channel].name,&data[count+2],data[count+1]); - net->connection[channel].name[(int)data[count+2]]='\0'; - count+=(data[count+2]+2); - break; - case SEND_N_G_CAP: - net->connection[channel].cap=data[count+4]; - count+=(sizeof(unsigned char)+2); - break; - case SEND_N_AV_CAP: - net->connection[channel].avcap=data[count+3]<<8; - net->connection[channel].avcap|=data[count+4]; - count+=(sizeof(unsigned short)+2); - break; - default: - puts("[network] receive_info, unknown character"); - return N_ERROR; - } - } - - return N_SUCCESS; -} diff --git a/src/network.h b/src/network.h index 274811e..31ca335 100644 --- a/src/network.h +++ b/src/network.h @@ -25,12 +25,7 @@ #define C_ESTABL (1<<3) #define C_HANGUP (1<<4) -#define CHAR_N_UNAME 32 - #define SEND_N_MAX 128 -#define SEND_N_NAME 'n' -#define SEND_N_G_CAP 'g' -#define SEND_N_AV_CAP 'c' #define N_SUCCESS 1 #define N_ERROR -1 @@ -40,19 +35,16 @@ /* net specific variables */ typedef struct s_connection { int fd; - char name[CHAR_N_UNAME]; char ip[IP_DIGITS]; in_port_t port; unsigned char status; - unsigned char cap; /* general capabilities */ - unsigned short avcap; /* audio/video capabilities */ + unsigned short cap; /* capabilities */ } t_connection; typedef struct s_net { int l_fd; /* listen file descriptor */ in_port_t l_port; - unsigned char cap; - unsigned short avcap; + unsigned short cap; /* limited connections by now -- replaced by list management later */ int c_count; t_connection connection[MAX_CONNECTIONS]; @@ -67,7 +59,5 @@ int network_manage_connection(t_net *net); int network_manage_incoming(t_net *net); int network_send(int fd,unsigned char *data,int datasize); int network_receive(int fd,unsigned char *data,int datasize); -int send_info(int channel,t_net *net,char *name); -int receive_info(int channel,t_net *net); #endif -- 2.39.2