From: hackbard Date: Thu, 28 Oct 2004 13:51:28 +0000 (+0000) Subject: fixed drpintf for event + network api X-Git-Url: https://hackdaworld.org/gitweb/?a=commitdiff_plain;h=163431149f68f6d8e50665d74e7e31c45d662698;p=my-code%2Fapi.git fixed drpintf for event + network api --- diff --git a/event/event.c b/event/event.c index 9a1b6f9..e3d60ab 100644 --- a/event/event.c +++ b/event/event.c @@ -6,12 +6,14 @@ #include "event.h" -int event_init(t_event *event) { +int event_init(t_event *event,int outfd) { - fprintf(stderr,"[event] initializing event system ...\n"); + dprintf(outfd,"[event] initializing event system ...\n"); event->maxfd=0; + event->outfd=outfd; + FD_ZERO(&(event->rfds)); FD_ZERO(&(event->rfds_o)); FD_ZERO(&(event->wfds)); @@ -32,8 +34,8 @@ int event_math(int fd,t_event *event,char rw,char ar) { if(fd==event->maxfd) event->maxfd-=1; } - fprintf(stderr,"[event] %c fd %d to %c file descriptor set\n",ar==ADD?'+':'-', - fd,rw==READ?'r':'w'); + dprintf(event->outfd,"[event] %c fd %d to %c file descriptor set\n", + ar==ADD?'+':'-',fd,rw==READ?'r':'w'); return E_SUCCESS; } @@ -63,14 +65,14 @@ int event_start(t_event *event,void *ptr, if(s_ret) { /* 1+ fd(s) available for reading/writing */ if(callback0(event,ptr)==CALLBACK_ERROR) { - fprintf(stderr,"[event] callback0 returned error\n"); + dprintf(event->outfd,"[event] callback0 returned error\n"); return E_ERROR; } } /* usual stuff to be executed */ if(callback1(event,ptr)==CALLBACK_ERROR) { - fprintf(stderr,"[event] callback returned error\n"); + dprintf(event->outfd,"[event] callback returned error\n"); return E_ERROR; } @@ -81,7 +83,7 @@ int event_start(t_event *event,void *ptr, int event_stop(t_event *event) { - fprintf(stderr,"[event] shutdown\n"); + dprintf(event->outfd,"[event] shutdown\n"); event->status=DISABLED; return E_SUCCESS; diff --git a/event/event.h b/event/event.h index 03994c5..b17983b 100644 --- a/event/event.h +++ b/event/event.h @@ -28,6 +28,7 @@ /* event specific variables */ typedef struct s_event { + int outfd; int maxfd; fd_set rfds,rfds_o; fd_set wfds,wfds_o; @@ -36,7 +37,7 @@ typedef struct s_event { } t_event; /* function prototypes */ -int event_init(t_event *event); +int event_init(t_event *event,int outfd); 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), diff --git a/network/network.c b/network/network.c index 4cb539d..1160e7c 100644 --- a/network/network.c +++ b/network/network.c @@ -6,16 +6,17 @@ #include "network.h" -int network_init(t_net *net) { +int network_init(t_net *net,int outfd) { struct sockaddr_in addr; int true; - fprintf(stderr,"[network] initializing network ...\n"); + dprintf(outfd,"[network] initializing network ...\n"); memset(net->connection,0,MAX_CONNECTIONS*sizeof(t_connection)); net->c_count=0; net->sendmask=0; + net->outfd=outfd; if((net->l_fd=socket(AF_INET,SOCK_STREAM,0))==-1) { perror("[network] socket call"); @@ -35,7 +36,7 @@ int network_init(t_net *net) { perror("[network] setsockopt call"); return N_ERROR; } - fprintf(stderr,"[network] reused address\n"); + dprintf(event->outfd,"[network] reused address\n"); } if(listen(net->l_fd,MAX_LISTEN_QUEUE)==-1) { @@ -43,8 +44,8 @@ int network_init(t_net *net) { return N_ERROR; } - fprintf(stderr,"[network] listen on %s port %d\n",inet_ntoa(addr.sin_addr), - net->l_port); + dprintf(event->outfd,"[network] listen on %s port %d\n", + inet_ntoa(addr.sin_addr),net->l_port); return N_SUCCESS; } @@ -62,7 +63,7 @@ int network_shutdown(t_net *net) { return N_ERROR; } - fprintf(stderr,"[network] shutdown\n"); + dprintf(event->outfd,"[network] shutdown\n"); return N_SUCCESS; } @@ -88,7 +89,7 @@ int network_manage_connection(t_net *net) { perror("[network] close call"); return N_E_CLOSE; } - fprintf(stderr,"[network] connection %d closed\n",i); + dprintf(event->outfd,"[network] connection %d closed\n",i); net->connection[i].status=0; } @@ -119,9 +120,9 @@ int network_manage_connection(t_net *net) { return N_E_CONNECT; } - fprintf(stderr,"[network] established connection to "); - fprintf(stderr,"%s port %d on channel %d\n",net->connection[i].ip, - net->connection[i].port,i); + dprintf(event->outfd,"[network] established connection to "); + dprintf(event->outfd,"%s port %d on channel %d\n", + net->connection[i].ip,net->connection[i].port,i); net->connection[i].status|=C_ESTABL; } @@ -138,11 +139,13 @@ int network_manage_connection(t_net *net) { int network_connect(t_net *net,int channel) { if(net->connection[channel].status&C_IN_USE) { - fprintf(stderr,"[network] connect failed, channel %02d in use\n",channel); + dprintf(event->outfd,"[network] connect failed, channel %02d in use\n", + channel); return N_E_IN_USE; } if(!(net->connection[channel].status&C_INFO_A)) { - fprintf(stderr,"[network] connect failed, missing configuration for channel %02d\n",channel); + dprintf(event->outfd, + "[network] connect failed, missing config for chan %02d\n",channel); return N_E_NO_INFO; } @@ -154,7 +157,8 @@ int network_connect(t_net *net,int channel) { int network_close(t_net *net,int channel) { if(!(net->connection[channel].status&C_ESTABL)) { - fprintf(stderr,"[network] close failed, channel %02d not active\n",channel); + dprintf(event->outfd,"[network] close failed, channel %02d not active\n", + channel); return N_E_NC; } @@ -175,8 +179,8 @@ int network_close_all(t_net *net) { int network_set_connection_info(t_net *net,int channel,char *ip,int port) { if(net->connection[channel].status&C_IN_USE) { - fprintf(stderr,"[network] set connection failed, channel %02d in use\n", - channel); + dprintf(event->outfd, + "[network] set connection failed, channel %02d in use\n",channel); return N_E_IN_USE; } @@ -226,12 +230,15 @@ int network_manage_incoming(t_net *net) { strncpy(net->connection[channel].ip,inet_ntoa(addr.sin_addr),IP_DIGITS); net->connection[channel].port=ntohs(addr.sin_port); net->connection[channel].status=C_IN_USE|C_INFO_A|C_SOCKET|C_ESTABL; - fprintf(stderr,"[network] established connection from %s port %d on channel %d\n",net->connection[channel].ip,net->connection[channel].port,channel); + dprintf(event->outfd, + "[network] established connection from %s:%d, channel %d\n", + net->connection[channel].ip,net->connection[channel].port, + channel); return channel; } } - fprintf(stderr,"[network] maximum connections reached\n"); + dprintf(event->outfd,"[network] maximum connections reached\n"); return N_E_MAXC; } @@ -287,10 +294,11 @@ int network_udp_listen_init(t_net *net) { perror("[network] setsockopt call (udp)"); return N_ERROR; } - fprintf(stderr,"[network] reused address (udp)\n"); + dprintf(event->outfd,"[network] reused address (udp)\n"); } - fprintf(stderr,"[network] listening on port %d (udp)\n",net->l_udp_port); + dprintf(event->outfd,"[network] listening on port %d (udp)\n", + net->l_udp_port); if((net->s_udp_fd=socket(AF_INET,SOCK_DGRAM,0))==-1) { perror("[network] socket call (udp-send)"); @@ -312,7 +320,7 @@ int network_udp_receive(t_net *net,int channel, unsigned char *data,int count) { } if(strncmp(net->connection[channel].ip,inet_ntoa(addr.sin_addr),IP_DIGITS)) { - fprintf(stderr,"[network] packet from unknown: %s\n", + dprintf(event->outfd,"[network] packet from unknown: %s\n", inet_ntoa(addr.sin_addr)); return N_UDP_WRONG_SENDER; } diff --git a/network/network.h b/network/network.h index 079cea3..d234d9c 100644 --- a/network/network.h +++ b/network/network.h @@ -52,6 +52,7 @@ typedef struct s_connection { } t_connection; typedef struct s_net { + int outfd; int l_fd; /* fd for tcp conn */ int l_udp_fd; /* fd for udp data receive */ int s_udp_fd; /* fd for udp data send */ @@ -66,7 +67,7 @@ typedef struct s_net { } t_net; /* function prototypes */ -int network_init(t_net *net); +int network_init(t_net *net,int outfd); int network_shutdown(t_net *net); int network_set_listen_port(t_net *net,in_port_t port); int network_manage_connection(t_net *net);