home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Misc / Vector18.lha / ParNetExample / task.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-18  |  3.2 KB  |  137 lines

  1. /*
  2.  * $Header: DH0:src/asm/parallel/parnet/RCS/task.c,v 1.1 92/08/27 17:12:46 Barnard Exp $
  3.  *
  4.  */
  5.  
  6. /*
  7.  * This code was originally written by Matthew Dillon and put into Public Domain
  8.  *
  9.  * All changes concerning the adaption of Matt's original code to the
  10.  * Vector Connection I/O board are © 1991-1993 by Henning Schmiedehausen
  11.  * All rights for this changes are reserved. The original code is Public Domain
  12.  *
  13.  * This code is distributed with the expressed written permission of Matthew
  14.  * Dillon (Thank you very much, Matt)
  15.  *
  16.  */
  17.  
  18.  
  19. /*
  20.  *  TASK.C
  21.  *
  22.  *  -Accept packets to send to network
  23.  *  -Receive data from network
  24.  *
  25.  *        ParWrite(destaddr, buf, bytes)
  26.  */
  27.  
  28. #include "defs.h"
  29.  
  30. typedef struct {
  31.     uword   Port;   /*    destination port    */
  32.     uword   ChkSum; /*    data checksum        */
  33.     ulong   Bytes;  /*    # of bytes        */
  34. } Header;
  35.  
  36. char    DataBuf[MAXPKTSIZE];
  37. Header    Hdr;
  38. long    TLock[2] = { 0, 0 };
  39. static    short Cnt = 0;
  40.  
  41. void
  42. CParNetTask()
  43. {
  44.     long mask;
  45.     Unit *unit;
  46.     Packet *packet;
  47.     long n;
  48.  
  49.     for (;;) {
  50.         SetPIOInt();
  51.     mask = Wait(SIGBREAKF_CTRL_E|SIGBREAKF_CTRL_F);
  52.     LockAddr(TLock);
  53.     if (mask & SIGBREAKF_CTRL_F) {      /*  packet pending  */
  54. data:
  55.         if (ParDataReady() > 0) {
  56.         n = ParReadV(&Hdr, sizeof(Hdr), DataBuf, MAXPKTSIZE, NULL, NULL);
  57.  
  58.         /*sprintf(StickyPort->DebugBuf, "%03d READ  %6ld", Cnt++, n);*/
  59.  
  60.         if (n >= sizeof(Hdr) && n >= sizeof(Hdr) + Hdr.Bytes) {
  61.             if (unit = FindUnitForPort(Hdr.Port)) {
  62.             packet = AllocParPacket(NULL, unit, DataBuf, Hdr.Bytes, NULL, 0);
  63.             LockAddr(unit->UnitLock);
  64.             (*packet->io_Unit->DataFunc)('r', packet, n - sizeof(Hdr));
  65.             UnlockAddr(unit->UnitLock);
  66.             }
  67.         }
  68.         }
  69.     }
  70.     if (mask & SIGBREAKF_CTRL_E) {      /*  port            */
  71.         while (packet = (Packet *)GetMsg(&DevBase->Port)) {
  72.         n = OutputPacket(packet);
  73.         if (n == -2) {
  74.             Forbid();
  75.             AddHead(&DevBase->Port.mp_MsgList, (struct Node *)packet);
  76.             Permit();
  77.             goto data;
  78.         }
  79.         }
  80.     }
  81.     UnlockAddr(TLock);
  82.     }
  83. }
  84.  
  85. OutputPacket(packet)
  86. Packet *packet;
  87. {
  88.     long n;
  89.     Unit *unit = packet->io_Unit;
  90.  
  91.     Hdr.Port  = packet->DestPort;
  92.     Hdr.ChkSum= 0;
  93.     Hdr.Bytes = packet->DLen1 + packet->DLen2;
  94.  
  95.     n = ParWriteV(packet->DestAddr, &Hdr, sizeof(Hdr), packet->Data1, packet->DLen1, packet->Data2, packet->DLen2, NULL, NULL);
  96.  
  97.     /* sprintf(StickyPort->DebugBuf, "%03d WRITE %6ld", Cnt++, n); */
  98.  
  99.     if (n == -2)                /*  can't write, pending rcv    */
  100.     return(n);
  101.     LockAddr(unit->UnitLock);
  102.     if (n == sizeof(Hdr) + packet->DLen1 + packet->DLen2)
  103.     (*unit->DataFunc)('w', packet, n - sizeof(Hdr));
  104.     else
  105.     (*unit->DataFunc)('W', packet, n - sizeof(Hdr));
  106.     UnlockAddr(unit->UnitLock);
  107.     return(n);
  108. }
  109.  
  110.  
  111. /*
  112.  *  Queue packet for write.  If QUICKIO is requested and the packet can be
  113.  *  sent manually it is, else it is queued to the task.
  114.  */
  115.  
  116. void
  117. QueuePacketForWrite(packet)
  118. Packet *packet;
  119. {
  120.     Iob *iob = packet->iob;
  121.  
  122.     if ((iob->io_Flags & IOF_QUICK) && TryLockAddr(TLock) > 0) {
  123.     if (OutputPacket(packet) != -2) {
  124.         UnlockAddr(TLock);
  125.         return;
  126.     }
  127.     /*
  128.      *  Can't write packet, rcv packet pending.
  129.      */
  130.     UnlockAddr(TLock);
  131.     }
  132.     iob->io_Flags &= ~IOF_QUICK;
  133.     iob->io_Flags |= IOF_QUEUED;
  134.     PutMsg(&DevBase->Port, &packet->Msg);
  135. }
  136.  
  137.