The R Project SVN R

Rev

Rev 26371 | Rev 36770 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 26371 Rev 26380
Line 234... Line 234...
234
#endif
234
#endif
235
	FD_ZERO(&wfd);
235
	FD_ZERO(&wfd);
236
	if(write) FD_SET(sockfd, &wfd); else FD_SET(sockfd, &rfd);
236
	if(write) FD_SET(sockfd, &wfd); else FD_SET(sockfd, &rfd);
237
	if(maxfd < sockfd) maxfd = sockfd;
237
	if(maxfd < sockfd) maxfd = sockfd;
238
 
238
 
-
 
239
	/* increment used value _before_ the select in case select
-
 
240
	   modifies tv (as Linux does) */
-
 
241
	used += tv.tv_sec + 1e-6 * tv.tv_usec;
-
 
242
 
239
	howmany = R_SelectEx(maxfd+1, &rfd, &wfd, NULL, &tv, NULL);
243
	howmany = R_SelectEx(maxfd+1, &rfd, &wfd, NULL, &tv, NULL);
240
 
244
 
241
	if (howmany < 0) {
245
	if (howmany < 0) {
242
	    return -1;
246
	    return -1;
243
	}
247
	}
244
	if (howmany == 0) {
248
	if (howmany == 0) {
245
	    used += tv.tv_sec + 1e-6 * tv.tv_usec;
-
 
246
	    if(used >= timeout) return 1;
249
	    if(used >= timeout) return 1;
247
	    continue;
250
	    continue;
248
	}
251
	}
249
 
252
 
250
#ifdef Unix
253
#ifdef Unix
Line 261... Line 264...
261
	break;
264
	break;
262
    }
265
    }
263
    return 0;
266
    return 0;
264
}
267
}
265
 
268
 
-
 
269
/**** FIXME: merge with R_SocketWait */
-
 
270
/**** FIXME: add timeout argument instead of using global?? */
-
 
271
int R_SocketWaitMultiple(int nsock, int *insockfd, int *ready, int *write,
-
 
272
			 double mytimeout)
-
 
273
{
-
 
274
    fd_set rfd, wfd;
-
 
275
    struct timeval tv;
-
 
276
    double used = 0.0;
-
 
277
    int nready = 0;
-
 
278
 
-
 
279
    while(1) {
-
 
280
	int maxfd = 0, howmany, i;
-
 
281
#ifdef Unix
-
 
282
	InputHandler *what;
-
 
283
 
-
 
284
	if(R_wait_usec > 0) {
-
 
285
	    int delta;
-
 
286
	    if (mytimeout < 0 || R_wait_usec / 1e-6 < mytimeout - used)
-
 
287
		delta = R_wait_usec;
-
 
288
	    else
-
 
289
		delta = 1e6 * (mytimeout - used);
-
 
290
	    R_PolledEvents();
-
 
291
	    tv.tv_sec = 0;
-
 
292
	    tv.tv_usec = delta;
-
 
293
	} else if (mytimeout >= 0) {
-
 
294
	    tv.tv_sec = mytimeout - used;
-
 
295
	    tv.tv_usec = 1e6 * (mytimeout - used - tv.tv_sec);
-
 
296
	} else {  /* always poll occationally--not really necessary */
-
 
297
	    tv.tv_sec = timeout;
-
 
298
	    tv.tv_usec = 0;
-
 
299
	}
-
 
300
#elif defined(Win32)
-
 
301
	tv.tv_sec = 0;
-
 
302
	tv.tv_usec = 2e5;
-
 
303
	R_ProcessEvents();
-
 
304
#else
-
 
305
	if (mytimeout >= 0) {
-
 
306
	    tv.tv_sec = mytimeout - used;
-
 
307
	    tv.tv_usec = 1e6 * (mytimeout - used - tv.tv_sec);
-
 
308
	} else {  /* always poll occationally--not really necessary */
-
 
309
	    tv.tv_sec = timeout;
-
 
310
	    tv.tv_usec = 0;
-
 
311
	}
-
 
312
#endif
-
 
313
 
-
 
314
 
-
 
315
#ifdef Unix
-
 
316
	maxfd = setSelectMask(R_InputHandlers, &rfd);
-
 
317
#else
-
 
318
	FD_ZERO(&rfd);
-
 
319
#endif
-
 
320
	FD_ZERO(&wfd);
-
 
321
	for (i = 0; i < nsock; i++) {
-
 
322
	    if(write[i]) FD_SET(insockfd[i], &wfd);
-
 
323
	    else FD_SET(insockfd[i], &rfd);
-
 
324
	    if(maxfd < insockfd[i]) maxfd = insockfd[i];
-
 
325
	}
-
 
326
 
-
 
327
	/* increment used value _before_ the select in case select
-
 
328
	   modifies tv (as Linux does) */
-
 
329
	used += tv.tv_sec + 1e-6 * tv.tv_usec;
-
 
330
 
-
 
331
	howmany = R_SelectEx(maxfd+1, &rfd, &wfd, NULL, &tv, NULL);
-
 
332
 
-
 
333
	if (howmany < 0) {
-
 
334
	    return -1;
-
 
335
	}
-
 
336
	if (howmany == 0) {
-
 
337
	    if(mytimeout >= 0 && used >= mytimeout) {
-
 
338
		for (i = 0; i < nsock; i++)
-
 
339
		    ready[i] = 0; /* FALSE */
-
 
340
		return 0;
-
 
341
	    }
-
 
342
	    continue;
-
 
343
	}
-
 
344
 
-
 
345
	for (i = 0; i < nsock; i++)
-
 
346
	    if ((!write[i] && FD_ISSET(insockfd[i], &rfd)) ||
-
 
347
		(write[i] && FD_ISSET(insockfd[i], &wfd))) {
-
 
348
		ready[i] = 1; /* TRUE */
-
 
349
		nready++;
-
 
350
	    }
-
 
351
	    else ready[i] = 0; /* FALSE */
-
 
352
 
-
 
353
#ifdef Unix
-
 
354
	if(howmany > nready) {
-
 
355
	    /* one of the extras is ready */
-
 
356
	    what = getSelectedHandler(R_InputHandlers, &rfd);
-
 
357
	    if(what != NULL) what->handler((void*) NULL);
-
 
358
	    continue;
-
 
359
	}
-
 
360
#endif
-
 
361
	/* some sockets are ready */
-
 
362
	break;
-
 
363
    }
-
 
364
    return nready;
-
 
365
}
-
 
366
 
-
 
367
int in_Rsockselect(int nsock, int *insockfd, int *ready, int *write,
-
 
368
		   double timeout)
-
 
369
{
-
 
370
    return R_SocketWaitMultiple(nsock, insockfd, ready, write, timeout);
-
 
371
}
-
 
372
 
266
void R_SockTimeout(int delay)
373
void R_SockTimeout(int delay)
267
{
374
{
268
    timeout = (unsigned int) delay;
375
    timeout = (unsigned int) delay;
269
}
376
}
270
 
377