summaryrefslogtreecommitdiff
path: root/src/ivyloop.c
blob: d90fa6cf614857d072942ec69b2c9aa8f93cc211 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
 *	Ivy, C interface
 *
 *	Copyright (C) 1997-2000
 *	Centre d'Études de la Navigation Aérienne
 *
 * 	Main loop based on select
 *
 *	Authors: François-Régis Colin <fcolin@cena.fr>
 *		 Stéphane Chatty <chatty@cena.fr>
 *
 *	$Id$
 * 
 *	Please refer to file version.h for the
 *	copyright notice regarding this software
 */

#ifdef WIN32
#include <windows.h>
#endif
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>

#ifndef WIN32
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <signal.h>
#endif

#include "list.h"
#include "ivychannel.h"
#include "ivyloop.h"
#include "timer.h"

struct _channel {
	Channel next;
	IVY_HANDLE fd;
	void *data;
	int tobedeleted;
	ChannelHandleDelete handle_delete;
	ChannelHandleRead handle_read;
};


static Channel channels_list = NULL;

static int channel_initialized = 0;

static fd_set open_fds;
static int MainLoop = 1;

#ifdef WIN32
WSADATA WsaData;
#endif

void
IvyChannelClose (Channel channel)
{
	channel->tobedeleted = 1;
}

static void
IvyChannelDelete (Channel channel)
{
	if (channel->handle_delete)
		(*channel->handle_delete) (channel->data);

	FD_CLR (channel->fd, &open_fds);
	IVY_LIST_REMOVE (channels_list, channel);
}

static void
ChannelDefferedDelete ()
{
	Channel channel, next;
	IVY_LIST_EACH_SAFE (channels_list, channel,next)	{
		if (channel->tobedeleted ) {
			IvyChannelDelete (channel);
		}
	}
}

Channel IvyChannelOpen (IVY_HANDLE fd, void *data, 
				ChannelHandleDelete handle_delete,
				ChannelHandleRead handle_read
				)						
{
	Channel channel;

	IVY_LIST_ADD (channels_list, channel);
	if (!channel) {
		fprintf(stderr,"NOK Memory Alloc Error\n");
		exit(0);
	}
	channel->fd = fd;
	channel->tobedeleted = 0;
	channel->handle_delete = handle_delete;
	channel->handle_read = handle_read;
	channel->data = data;

	FD_SET (channel->fd, &open_fds);

	return channel;
}

static void
IvyChannelHandleRead (fd_set *current)
{
	Channel channel, next;
	
	IVY_LIST_EACH_SAFE (channels_list, channel, next) {
		if (FD_ISSET (channel->fd, current)) {
			(*channel->handle_read)(channel,channel->fd,channel->data);
		}
	}
}

static void
IvyChannelHandleExcpt (fd_set *current)
{
	Channel channel,next;
	IVY_LIST_EACH_SAFE (channels_list, channel, next) {
		if (FD_ISSET (channel->fd, current)) {
			if (channel->handle_delete)
				(*channel->handle_delete)(channel->data);
/*			IvyChannelClose (channel); */
		}
	}
}

void IvyChannelInit (void)
{
#ifdef WIN32
	int error;
#else 
	/* pour eviter les plantages quand les autres applis font core-dump */
	signal (SIGPIPE, SIG_IGN);
#endif
	if (channel_initialized) return;

	FD_ZERO (&open_fds);

#ifdef WIN32
	error = WSAStartup (0x0101, &WsaData);
	  if (error == SOCKET_ERROR) {
	      printf ("WSAStartup failed.\n");
	  }
#endif
	channel_initialized = 1;
}

void IvyChannelStop (void)
{
	MainLoop = 0;
}

void IvyMainLoop(void(*hook)(void))
{

	fd_set rdset;
	fd_set exset;
	int ready;

	while (MainLoop) {
		ChannelDefferedDelete();
	   	if (hook) (*hook)();
		rdset = open_fds;
		exset = open_fds;
		ready = select(64, &rdset, 0,  &exset, TimerGetSmallestTimeout());
		if (ready < 0 && (errno != EINTR)) {
         		fprintf (stderr, "select error %d\n",errno);
			perror("select");
			return;
		}
		TimerScan();
		if (ready > 0) {
			IvyChannelHandleExcpt(&exset);
			IvyChannelHandleRead(&rdset);
			continue;
		}
	}
}

void IvyIdle()
{

	fd_set rdset;
	fd_set exset;
	int ready;
	struct timeval timeout = {0,0}; 

	
	ChannelDefferedDelete();
	rdset = open_fds;
	exset = open_fds;
	ready = select(64, &rdset, 0,  &exset, &timeout);
	if (ready < 0 && (errno != EINTR)) {
         	fprintf (stderr, "select error %d\n",errno);
		perror("select");
		return;
	}
	if (ready > 0) {
		IvyChannelHandleExcpt(&exset);
		IvyChannelHandleRead(&rdset);
	}
	
}