home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume28 / m0 / part02 / l_incom.c < prev    next >
C/C++ Source or Header  |  1994-06-05  |  3KB  |  174 lines

  1. /*
  2.     l_incom.c
  3. */
  4. /*  Copyright (c) 1994 Christian F. Tschudin. All rights reserved.
  5.  
  6.     Distributed under the terms of the GNU General Public License
  7.     version 2 of june 1991 as published by the Free Software
  8.     Foundation, Inc.
  9.  
  10.              This file is part of M0.
  11.  
  12. M0 is distributed in the hope that it will be useful, but WITHOUT ANY
  13. WARRANTY.  No author or distributor accepts responsibility to anyone for
  14. the consequences of using it or for whether it serves any particular
  15. purpose or works at all, unless he says so in writing.  Refer to the GNU
  16. General Public License for full details. 
  17.  
  18. Everyone is granted permission to copy, modify and redistribute M0, but
  19. only under the conditions described in the GNU General Public License. 
  20. A copy of this license is supposed to have been given to you along with
  21. M0 so you can know your rights and responsibilities.  It should be in a
  22. file named LICENSE.  Among other things, the copyright notice and this
  23. notice must be preserved on all copies.  */
  24.  
  25. #ifdef unix
  26.  
  27. #include <sys/types.h>
  28. #include <sys/time.h>
  29.  
  30. #include "l_proto.h"
  31.  
  32.  
  33. static receivefct    rcv[FD_SETSIZE];
  34. static eindex        chan_name[FD_SETSIZE];
  35. static sint        chan_no[FD_SETSIZE];
  36.  
  37. static void set_fdset(fd_set *sp)
  38. {
  39.     int i;
  40.  
  41.         FD_ZERO(sp);
  42.     for (i = 0; i < FD_SETSIZE; i++)
  43.         if (rcv[i])
  44.                 FD_SET(i, sp);
  45. }
  46.  
  47.  
  48. void
  49. add_incoming(int fd, receivefct fct, eindex name, sint no)
  50. {
  51.     rcv[fd] = fct;
  52.     chan_name[fd] = name;
  53.     chan_no[fd] = no;
  54. }
  55.  
  56.  
  57. int
  58. incoming_wouldblock()
  59. {
  60. static struct timeval NODELAY = {0,0};
  61.     fd_set r;
  62.  
  63.     set_fdset(&r);
  64.     return select(FD_SETSIZE, &r, 0, 0, &NODELAY) <= 0;
  65. }
  66.  
  67.  
  68. int
  69. serve_incoming(long usec)
  70. {
  71.     fd_set r;
  72.     int cnt, i;
  73.  
  74.     set_fdset(&r);
  75.  
  76.     if (usec > 0) {
  77.         struct timeval delay;
  78.         delay.tv_sec = usec / 1000000;
  79.         delay.tv_usec = usec % 1000000;
  80.         cnt = select(FD_SETSIZE, &r, 0, 0, &delay);
  81.     } else
  82.         cnt = select(FD_SETSIZE, &r, 0, 0, 0);
  83.     if (cnt <= 0)
  84.         return 0;
  85.  
  86.     for (i = 0; i < FD_SETSIZE; i++)
  87.         if (FD_ISSET(i, &r)) {
  88.         eindex msgr, orig_addr;
  89.         retcode rc;
  90.  
  91.         rcv[i](i, &msgr, &orig_addr);
  92.         if (msgr) {
  93.             eindex o = new_array(0, 3);
  94.             eindex n = new_element(0, T_INT);
  95.  
  96.             array_put(0, o, 0, chan_name[i]);
  97.             increfp(gaddr(chan_name[i]));
  98.             gaddr(n)->V.i = chan_no[i];
  99.             array_put(0, o, 1, n);
  100.             array_put(0, o, 2, orig_addr);
  101.             epattr(gaddr(o)) &= ~A_WRITE;
  102.  
  103.             rc = new_proc(msgr, o);
  104.             decref(0, msgr);
  105.             decref(0, o);
  106.             TRACE(3, if (rc != OK && rc != YIELD_CPU)
  107.                 printf("error %d creating a process\n", rc))
  108.         }
  109.         }
  110.  
  111.     return cnt;
  112. }
  113.  
  114. #else /* __MSDOS__ */
  115.  
  116. #include <conio.h>
  117. #include "l_proto.h"
  118.  
  119. static receivefct read0;
  120. static c_name;
  121.  
  122. void
  123. add_incoming(int fd, receivefct fct, eindex name, sint dummy)
  124. {
  125.     if (fd == 0) {
  126.         read0 = fct;
  127.         c_name = name;
  128.     }
  129. }
  130.  
  131.  
  132. int
  133. incoming_wouldblock()
  134. {
  135.     return !kbhit();
  136. }
  137.  
  138. int
  139. serve_incoming(long msec)
  140. {
  141.     if (msec > 0) {
  142.         eindex now = time_now(0), then = time_addint(0, now, msec);
  143.         eptr tp = gaddr(then);
  144.         for (;;) {
  145.             decref(0, now);
  146.             now = time_now(0);
  147.             if (!time_gt(&(tp->V.tim),&(gaddr(now)->V.tim)))
  148.                 break;
  149.         }
  150.         decref(0, now);
  151.         decref(0, then);
  152.         if (!kbhit())
  153.             return 0;
  154.     }
  155.  
  156.     if (read0) {
  157.         eindex msgr, orig;
  158.         retcode rc;
  159.  
  160.         read0(0, &msgr, &orig);
  161.         if (msgr) {
  162.             rc = new_proc(msgr, 0);
  163.             decref(0, msgr);
  164.             decref(0, orig);
  165.             TRACE(3, if (rc != OK && rc != YIELD_CPU)
  166.                 printf("error %d creating a process\n", rc))
  167.         }
  168.         return 1;
  169.     }
  170.     return 0;
  171. }
  172.  
  173. #endif
  174.