summaryrefslogtreecommitdiff
path: root/src/ivyloop.c
blob: 92f6301249607820d821c63d3f766a43e74e9781 (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
/*
 *
 * Ivy, C interface
 *
 * Copyright 1997-1998 
 * Centre d'Etudes de la Navigation Aerienne
 *
 * Main loop handling around select
 *
 * $Id$
 *
 */

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

#ifdef WIN32
#else
#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;
	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 BusLoopChannelClose( Channel channel )
{
	channel->tobedeleted = 1;
}

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

	FD_CLR(channel->fd, &open_fds);
	LIST_REMOVE( channels_list, channel );
}
static void ChannelDefferedDelete()
{
	Channel channel,next;
	LIST_EACH_SAFE( channels_list, channel,next)
		{
		if ( channel->tobedeleted  )
			{
			BusLoopChannelDelete( channel );
			}
		}
}

Channel BusLoopChannelSetUp(HANDLE fd, void *data, 
				ChannelHandleDelete handle_delete,
				ChannelHandleRead handle_read
				)						
{
	Channel channel;

	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 BusLoopChannelHandleRead(fd_set *current)
{
	Channel channel,next;
	
	LIST_EACH_SAFE( channels_list, channel, next )
		{
		if ( FD_ISSET( channel->fd, current ) )
			{
			(*channel->handle_read)(channel,channel->fd,channel->data);
			}
		}
}

static void BusLoopChannelHandleExcpt(fd_set *current)
{
	Channel channel,next;
	LIST_EACH_SAFE( channels_list, channel, next )
		{
		if (FD_ISSET( channel->fd, current ) )
			{
			(*channel->handle_delete)(channel->data);
//			BusLoopChannelClose( channel );
			}
		}
}

void BusLoopChannelInit(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 BusLoopChannelStop(void)
{
	MainLoop = 0;
}

void BusLoopChannelMainLoop(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 ))
		{
		perror("select");
		return;
		}
	TimerScan();
    if ( ready > 0 )
		{
		BusLoopChannelHandleExcpt(&exset);
		BusLoopChannelHandleRead(&rdset);
		continue;
		}
	}
}