#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));
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;
}
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;
}
int event_stop(t_event *event) {
- fprintf(stderr,"[event] shutdown\n");
+ dprintf(event->outfd,"[event] shutdown\n");
event->status=DISABLED;
return E_SUCCESS;
#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");
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) {
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;
}
return N_ERROR;
}
- fprintf(stderr,"[network] shutdown\n");
+ dprintf(event->outfd,"[network] shutdown\n");
return N_SUCCESS;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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)");
}
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;
}