summaryrefslogtreecommitdiff
path: root/src/ivyloop.c
blob: bda5cc7f1a52aad719cce7fd75040cbc54f1bc58 (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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
 *	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.dgac.fr>
 *		 Stéphane Chatty <chatty@cena.dgac.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;
  HANDLE fd;
  void *data;
  int tobedeleted;
  ChannelHandleDelete handle_delete;
  ChannelHandleRead handle_read;
  ChannelHandleWrite handle_write;
};

static Channel channels_list = NULL;

static int channel_initialized = 0;

static fd_set open_fds;
static fd_set wrdy_fds;
static HANDLE highestFd=0;

static int MainLoop = 1;

/* Hook callback & data */
static IvyHookPtr BeforeSelect = NULL;
static IvyHookPtr AfterSelect = NULL;

static void *BeforeSelectData = NULL;
static void *AfterSelectData = NULL;

#ifdef WIN32
WSADATA WsaData;
#endif

void
IvyChannelRemove (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);
  FD_CLR (channel->fd, &wrdy_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 IvyChannelAdd (HANDLE fd, void *data, 
		       ChannelHandleDelete handle_delete,
		       ChannelHandleRead handle_read,
		       ChannelHandleWrite handle_write
		       )						
{
  Channel channel;


  IVY_LIST_ADD_START (channels_list, channel)
    channel->fd = fd;
  channel->tobedeleted = 0;
  channel->handle_delete = handle_delete;
  channel->handle_read = handle_read;
  channel->handle_write = handle_write;
  channel->data = data;
  if (channel->fd >= highestFd)  
    highestFd = channel->fd+1 ;

  IVY_LIST_ADD_END (channels_list, channel)
    
    FD_SET (channel->fd, &open_fds);

  return channel;
}

void IvyChannelAddWritableEvent(Channel channel)
{
  if (channel->fd >= highestFd)  
    highestFd = channel->fd+1 ;

  FD_SET (channel->fd, &wrdy_fds);
}

void IvyChannelClearWritableEvent(Channel channel)
{
  FD_CLR (channel->fd, &wrdy_fds);
}

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

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
  MainLoop = 1;
  if (channel_initialized) return;

  FD_ZERO (&open_fds);
  FD_ZERO (&wrdy_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)
{

  fd_set rdset, exset, wrset;
  int ready;

  while (MainLoop) {
		
    ChannelDefferedDelete();
	   	
    if (BeforeSelect)
      (*BeforeSelect)(BeforeSelectData);
    rdset = open_fds;
    wrset = wrdy_fds;
    exset = open_fds;
    ready = select(highestFd, &rdset, &wrset,  &exset, 
		   TimerGetSmallestTimeout());
		
    if (AfterSelect) 
      (*AfterSelect)(AfterSelectData);
		
    if (ready < 0 && (errno != EINTR)) {
      fprintf (stderr, "select error %d\n",errno);
      perror("select");
      return;
    }
    TimerScan(); /* should be spliited in two part ( next timeout & callbacks */
    if (ready > 0) {
      IvyChannelHandleExcpt(&exset);
      IvyChannelHandleRead(&rdset);
      IvyChannelHandleWrite(&wrset);
    }
  }
}

void IvyIdle()
{
  fd_set rdset, exset, wrset;
  int ready;
  struct timeval timeout = {0,0}; 

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



void IvySetBeforeSelectHook(IvyHookPtr before, void *data )
{
  BeforeSelect = before;
  BeforeSelectData = data;
}
void IvySetAfterSelectHook(IvyHookPtr after, void *data )
{
  AfterSelect = after;
  AfterSelectData = data;
}