home *** CD-ROM | disk | FTP | other *** search
- /*
- l_incom.c
- */
- /* Copyright (c) 1994 Christian F. Tschudin. All rights reserved.
-
- Distributed under the terms of the GNU General Public License
- version 2 of june 1991 as published by the Free Software
- Foundation, Inc.
-
- This file is part of M0.
-
- M0 is distributed in the hope that it will be useful, but WITHOUT ANY
- WARRANTY. No author or distributor accepts responsibility to anyone for
- the consequences of using it or for whether it serves any particular
- purpose or works at all, unless he says so in writing. Refer to the GNU
- General Public License for full details.
-
- Everyone is granted permission to copy, modify and redistribute M0, but
- only under the conditions described in the GNU General Public License.
- A copy of this license is supposed to have been given to you along with
- M0 so you can know your rights and responsibilities. It should be in a
- file named LICENSE. Among other things, the copyright notice and this
- notice must be preserved on all copies. */
-
- #ifdef unix
-
- #include <sys/types.h>
- #include <sys/time.h>
-
- #include "l_proto.h"
-
-
- static receivefct rcv[FD_SETSIZE];
- static eindex chan_name[FD_SETSIZE];
- static sint chan_no[FD_SETSIZE];
-
- static void set_fdset(fd_set *sp)
- {
- int i;
-
- FD_ZERO(sp);
- for (i = 0; i < FD_SETSIZE; i++)
- if (rcv[i])
- FD_SET(i, sp);
- }
-
-
- void
- add_incoming(int fd, receivefct fct, eindex name, sint no)
- {
- rcv[fd] = fct;
- chan_name[fd] = name;
- chan_no[fd] = no;
- }
-
-
- int
- incoming_wouldblock()
- {
- static struct timeval NODELAY = {0,0};
- fd_set r;
-
- set_fdset(&r);
- return select(FD_SETSIZE, &r, 0, 0, &NODELAY) <= 0;
- }
-
-
- int
- serve_incoming(long usec)
- {
- fd_set r;
- int cnt, i;
-
- set_fdset(&r);
-
- if (usec > 0) {
- struct timeval delay;
- delay.tv_sec = usec / 1000000;
- delay.tv_usec = usec % 1000000;
- cnt = select(FD_SETSIZE, &r, 0, 0, &delay);
- } else
- cnt = select(FD_SETSIZE, &r, 0, 0, 0);
- if (cnt <= 0)
- return 0;
-
- for (i = 0; i < FD_SETSIZE; i++)
- if (FD_ISSET(i, &r)) {
- eindex msgr, orig_addr;
- retcode rc;
-
- rcv[i](i, &msgr, &orig_addr);
- if (msgr) {
- eindex o = new_array(0, 3);
- eindex n = new_element(0, T_INT);
-
- array_put(0, o, 0, chan_name[i]);
- increfp(gaddr(chan_name[i]));
- gaddr(n)->V.i = chan_no[i];
- array_put(0, o, 1, n);
- array_put(0, o, 2, orig_addr);
- epattr(gaddr(o)) &= ~A_WRITE;
-
- rc = new_proc(msgr, o);
- decref(0, msgr);
- decref(0, o);
- TRACE(3, if (rc != OK && rc != YIELD_CPU)
- printf("error %d creating a process\n", rc))
- }
- }
-
- return cnt;
- }
-
- #else /* __MSDOS__ */
-
- #include <conio.h>
- #include "l_proto.h"
-
- static receivefct read0;
- static c_name;
-
- void
- add_incoming(int fd, receivefct fct, eindex name, sint dummy)
- {
- if (fd == 0) {
- read0 = fct;
- c_name = name;
- }
- }
-
-
- int
- incoming_wouldblock()
- {
- return !kbhit();
- }
-
- int
- serve_incoming(long msec)
- {
- if (msec > 0) {
- eindex now = time_now(0), then = time_addint(0, now, msec);
- eptr tp = gaddr(then);
- for (;;) {
- decref(0, now);
- now = time_now(0);
- if (!time_gt(&(tp->V.tim),&(gaddr(now)->V.tim)))
- break;
- }
- decref(0, now);
- decref(0, then);
- if (!kbhit())
- return 0;
- }
-
- if (read0) {
- eindex msgr, orig;
- retcode rc;
-
- read0(0, &msgr, &orig);
- if (msgr) {
- rc = new_proc(msgr, 0);
- decref(0, msgr);
- decref(0, orig);
- TRACE(3, if (rc != OK && rc != YIELD_CPU)
- printf("error %d creating a process\n", rc))
- }
- return 1;
- }
- return 0;
- }
-
- #endif
-