From 4546108a81317af1135683e85b9340715e585339 Mon Sep 17 00:00:00 2001 From: hackbard Date: Wed, 5 May 2004 17:20:41 +0000 Subject: [PATCH] renamed inet.* -> network.*; bugfixes; began input system ... --- src/event.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++------ src/event.h | 36 +++++++++++++++++++++--- src/ivac.c | 65 +++++++++++++++++++++++++++++++++++++++++-- src/ivac.h | 16 ++++++++++- 4 files changed, 182 insertions(+), 15 deletions(-) diff --git a/src/event.c b/src/event.c index d7ea42d..bd703a5 100644 --- a/src/event.c +++ b/src/event.c @@ -6,16 +6,80 @@ #include "event.h" -int event_init(t_ivac *ivac) { +int event_init(t_event *event) { - puts("[ivac] event: initializing event system ..."); + puts("[event] initializing event system ..."); - ivac->event.maxfd=0; + event->maxfd=0; - FD_ZERO(ivac->event.rfds); - FD_ZERO(ivac->event.rfds_c); - FD_ZERO(ivac->event.wfds); - FD_ZERO(ivac->event.wfds_c); + FD_ZERO(&(event->rfds)); + FD_ZERO(&(event->rfds_o)); + FD_ZERO(&(event->wfds)); + FD_ZERO(&(event->wfds_o)); - return SUCCESS; + return E_SUCCESS; +} + +int event_math(int fd,t_event *event,char rw,char ar) { + + if(ar==ADD) { + if(rw==READ) FD_SET(fd,&(event->rfds_o)); + else FD_SET(fd,&(event->wfds_o)); + if(fd>event->maxfd) event->maxfd=fd; + } else { + if(rw==READ) FD_CLR(fd,&(event->rfds_o)); + else FD_CLR(fd,&(event->wfds_o)); + if(fd==event->maxfd) event->maxfd-=1; + } + + printf("[event] %c fd %d to %c file descriptor set\n",ar==ADD?'+':'-',fd, + rw==READ?'r':'w'); + + return E_SUCCESS; +} + +int event_start(t_event *event,void *ptr, + int (*callback0)(t_event *event,void *ptr), + int (*callback1)(t_event *event,void *ptr)) { + + int s_ret; + + /* switch on event system */ + event->status=ENABLED; + + while(event->status&ENABLED) { + + event->rfds=event->rfds_o; + event->wfds=event->wfds_o; + + if((s_ret=select(event->maxfd+1,&(event->rfds),&(event->wfds),NULL, + &(event->timeout)))==-1) { + perror("[event] select call"); + return E_ERROR; + } + + if(s_ret) { + /* 1+ fd(s) available for reading/writing */ + if(callback0(event,ptr)==CALLBACK_ERROR) { + puts("[event] callback0 returned error"); + return E_ERROR; + } + } + + /* usual stuff to be executed */ + if(callback1(event,ptr)==CALLBACK_ERROR) { + puts("[event] callback returned error"); + return E_ERROR; + } + + } + + return E_SUCCESS; +} + +int event_stop(t_event *event) { + + event->status=DISABLED; + + return E_SUCCESS; } diff --git a/src/event.h b/src/event.h index 00a134c..ac35535 100644 --- a/src/event.h +++ b/src/event.h @@ -4,14 +4,42 @@ #define EVENT_H /* includes */ -#include "ivac.h" +#include #include +/* defines */ + +/* return values */ +#define CALLBACK_ERROR -1 +#define E_ERROR -1 +#define E_SUCCESS 1 + +/* event status */ +#define DISABLED 0 +#define ENABLED 1 + +/* read write mode */ +#define READ 'r' +#define WRITE 'w' +/* add remove mode */ +#define ADD 'a' +#define REMOVE 'r' + /* event specific variables */ -typedef s_event { +typedef struct s_event { int maxfd; - fd_set rfds,rfds_c; - fd_set wfds,wfds_c; + fd_set rfds,rfds_o; + fd_set wfds,wfds_o; + struct timeval timeout; + unsigned char status; } t_event; +/* function prototypes */ +int event_init(t_event *event); +int event_math(int fd,t_event *event,char rw,char ar); +int event_start(t_event *event,void *ptr, + int (*callback0)(t_event *event,void *ptr), + int (*callback1)(t_event *event,void *ptr)); +int event_stop(t_event *event); + #endif diff --git a/src/ivac.c b/src/ivac.c index 610ac1c..c567f28 100644 --- a/src/ivac.c +++ b/src/ivac.c @@ -25,7 +25,68 @@ int main(int argc,char **argv) { + /* TESTING BY NOW */ - - return 1; + t_ivac ivac; + + /* set username (futur: read from config or entered later) */ + strcpy(ivac.username,"hackbard"); + + /* set event timeout */ + ivac.event.timeout.tv_sec=IVAC_S_SEC; + ivac.event.timeout.tv_usec=IVAC_S_USEC; + + /* set listen port (futur: read from config or entered later) */ + network_set_listen_port(&(ivac.net),IVAC_LISTEN_PORT); + + /* event init */ + event_init(&(ivac.event)); + + /* input init */ + input_init(&(ivac.input)); + + /* network init */ + network_init(&(ivac.net)); + + /* add listening port + stdin to (read) event system */ + event_math(ivac.net.l_fd,&(ivac.event),READ,ADD); + event_math(0,&(ivac.event),READ,ADD); + + /* start event system - callbacks used: ivac_event_cb + ivac_regular_cb */ + event_start(&(ivac.event),(void *)&ivac,ivac_event_cb,ivac_regular_cb); + + return SUCCESS; +} + +int ivac_event_cb(t_event *event,void *ptr) { + + t_ivac *ivac; + int channel; + + ivac=(t_ivac *)ptr; + + if(FD_ISSET(ivac->net.l_fd,&(event->rfds))) { + /* 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); + } + + /* 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)); + + /* user interaction */ + if(FD_ISSET(0,&(event->rfds))) input_get_char(ivac); + + return SUCCESS; +} + +int ivac_regular_cb(t_event *event,void *ptr) { + + /* usual jobs like audio & video transmit ... */ + + return SUCCESS; } diff --git a/src/ivac.h b/src/ivac.h index 2f14ab5..76844f5 100644 --- a/src/ivac.h +++ b/src/ivac.h @@ -7,17 +7,31 @@ #define _GNU_SOURCE #include +#include "network.h" +#include "event.h" +#include "input.h" + /* defines */ #define CHAR_USERNAME 32 #define ERROR -1 #define SUCCESS 1 +#define IVAC_LISTEN_PORT 1025 + +#define IVAC_S_SEC 2 +#define IVAC_S_USEC 500 + /* variables */ -typedef s_ivac { +typedef struct s_ivac { char username[CHAR_USERNAME]; t_net net; t_event event; + t_input input; } t_ivac; +/* function prototypes */ +int ivac_event_cb(t_event *event,void *ptr); +int ivac_regular_cb(t_event *event,void *ptr); + #endif -- 2.39.2