home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 3: The Continuation / 17-Bit_The_Continuation_Disc.iso / amigan / amigan 21 / asynch / example.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-27  |  5.9 KB  |  207 lines

  1. /* asendpacket.c - (asynchronous)        send multiple packets to a dos-      */
  2. /* 05-SEP-86                    handler                  */
  3. /* Phillip Lindsay - Commodore-Amiga                          */
  4.  
  5. #include "exec/types.h"
  6. #include "exec/ports.h"
  7. #include "exec/io.h"
  8. #include "exec/memory.h"
  9. #include "libraries/dos.h"
  10. #include "libraries/dosextens.h"
  11. #include <stdio.h>
  12.  
  13. #ifdef MANX
  14. #include "functions.h"           /* aztec C include */
  15. #endif
  16.  
  17.  
  18. #define DOSTRUE         -1L  /* AmigaDos TRUE */
  19. #define MAXARGS          7L  /* limit in packet structure (dosextens.h) */
  20.  
  21. /*
  22.    asynchronous sendpkt routine
  23.    you must supply a port for packet replies...This function returns the
  24.    address of the pending packet.
  25.  
  26. */
  27. long asendpkt(replyport,pid,action,args,nargs)
  28.  
  29. struct MsgPort *replyport; /* where all packet replies are sent */
  30. struct MsgPort *pid;      /* process indentifier ... (handlers message port ) */
  31. long action,         /* packet type ... (what you want handler to do )   */
  32.      args[],        /* a pointer to a argument list */
  33.      nargs;           /* number of arguments in list  */
  34. {
  35.  
  36.  struct StandardPacket *packet;
  37.  
  38.  long    count, *pargs;
  39.  
  40.  if(nargs > MAXARGS) exit(FALSE);
  41.  
  42.  packet = (struct StandardPacket *)
  43.    AllocMem((long)sizeof(*packet),MEMF_PUBLIC | MEMF_CLEAR);
  44.  if(!packet)
  45.    {
  46.     FreeMem(packet,(long)sizeof(*packet));
  47.     return(NULL);
  48.    }
  49.  
  50.  packet->sp_Msg.mn_Node.ln_Name = (char *) &(packet->sp_Pkt); /* link packet- */
  51.  packet->sp_Pkt.dp_Link     = &(packet->sp_Msg);        /* to message    */
  52.  packet->sp_Pkt.dp_Port     = replyport;         /* set-up reply port   */
  53.  packet->sp_Pkt.dp_Type     = action;        /* what to do... */
  54.  
  55.  /* move all the arguments to the packet */
  56.  pargs = &(packet->sp_Pkt.dp_Arg1);        /* address of first argument */
  57.  for(count=NULL;count < nargs;count++)
  58.    pargs[count]=args[count];
  59.  
  60.  PutMsg(pid,packet); /* send packet */
  61.  
  62.  return((long)packet);   /* everything went ok...so far... give-em the packet */
  63.  
  64. }
  65.  
  66. /* end of asendpkt.c */
  67.  
  68.  
  69. /*
  70.  
  71.    simple packet flush with error detection ... returns ZERO for error
  72.    or DOSTRUE (-1) for a-o-k.
  73.  
  74.    A packet error can be detected in most cases by "Res1" being equal
  75.    to zero--"Res2" will hold more information pertaining to the error.
  76.    The problem with handling multiple packets is detecting an error for a
  77.    particular packet. What makes things a little more difficult is the fact
  78.    that the "Res1" member of the packet structure is not generally an
  79.    indicator of an error. So depending on what type of packets you'll be
  80.    handling you might want to deal with packet replies differently.
  81.  
  82.    ( I was designing a elegant packet handling tool, but I found that
  83.       AmigaDos doesn't handle ports like EXEC does. So I couldn't have
  84.       a software interrupt generated on packet arrvial [sigh...] Tim?  )
  85.  
  86. */
  87.  
  88. long pktflush(rport,pkts)
  89. struct MsgPort *rport;        /* reply port for packets      */
  90. long           pkts;        /* number of packets to flush */
  91.  
  92. {
  93.  struct StandardPacket *apkt;
  94.  long res1,cres;
  95.  
  96.  res1 = DOSTRUE;
  97.  
  98.  while(pkts--)                          /* received all packets? */
  99.   {
  100.    WaitPort(rport);                     /* sleep until a packet arrives */
  101.    apkt = (struct StandardPacket *) GetMsg(rport);       /* get packet */
  102.    cres = apkt->sp_Pkt.dp_Res1;     /* get result */
  103.    FreeMem(apkt,(long)sizeof(*apkt));   /* free packet structure from memory  */
  104.    res1 = (!cres ? cres : res1);        /* error?     */
  105.   }
  106.  
  107.  return(res1);
  108. }
  109.  
  110.  
  111. /*
  112.    for all those people interested in implementing AmigaDos file I/O
  113.    with an asynchronous design.
  114. */
  115.  
  116.  
  117. /* start of example */
  118.  
  119. #define NARGS     3L             /* number of arguments */
  120. #define NBUFFS     3L             /* number of buffers    */
  121. #define BUFFLEN 60L             /* buffer length    */
  122. #define ESC    27L
  123.  
  124. main()
  125. {
  126.  
  127.  struct MsgPort        *filehdlr;      /* for process id  handler  */
  128.  struct MsgPort        *rport;           /* where all packets return */
  129.  long               arg[NARGS],     /* array of arguments       */
  130.                rpkt,           /* holds returned packet    */
  131.                count;           /* count messages       */
  132.  struct FileHandle     *filehandle;    /* our file handle       */
  133.  BPTR               fh,           /* AmigaDos file handle       */
  134.                fharg1;           /* Arg1 from filehandle       */
  135.  UBYTE               *buff;           /* buffer pointer       */
  136.  struct StandardPacket *pkt;           /* our packet           */
  137.  
  138.  
  139. /* get buffers */
  140.  buff = (UBYTE *) AllocMem((BUFFLEN * NBUFFS),MEMF_PUBLIC | MEMF_CLEAR);
  141.  if(!buff) exit(TRUE);
  142.  
  143.  rport = (struct MsgPort *) CreatePort(NULL,NULL); /* make reply port */
  144.  if(!rport)
  145.   {
  146.    FreeMem(buff,(BUFFLEN * NBUFFS));
  147.    exit(TRUE);
  148.   }
  149.  
  150. /* here we open a dummy file */
  151.  fh = (BPTR) Open("df1:temp",MODE_NEWFILE);
  152.  if(!fh)
  153.   {
  154.    FreeMem(buff,(BUFFLEN * NBUFFS));
  155.    DeletePort(rport);
  156.    exit(TRUE);
  157.   }
  158.  
  159. /* bring our AmigaDos file handle into the real world... */
  160.  filehandle = (struct FileHandle *)  (fh << 2);
  161.  
  162. /* read your AmigaDOS Technical Reference Manual for packet requirements */
  163.  
  164.  fharg1     = filehandle->fh_Arg1;  /* get Arg1 */
  165.  
  166.  filehdlr   = filehandle->fh_Type;  /* get handler for file */
  167.  
  168. /*
  169.    you could get process id of the handler this way also ...
  170.       filehdlr = (struct MsgPort *) DeviceProc("DF1:");
  171. */
  172.  
  173. /* give each buffer unique data */
  174.  
  175.  for(count=0;count < (BUFFLEN * NBUFFS);count ++)
  176.   buff[count]= 0x31 + (count / BUFFLEN);
  177.  
  178. /* set-up arguments and send packets */
  179.  
  180.  arg[0]= (long) fharg1;        /* file handle Arg1  */
  181.  arg[1]= (long) &buff[0];     /* buffer            */
  182.  arg[2]=    BUFFLEN;     /* buffer length      */
  183.  puts("one");
  184.  rpkt = asendpkt(rport,filehdlr,ACTION_WRITE,arg,NARGS);
  185.  
  186.  arg[1]= (long) &buff[BUFFLEN];
  187.  puts("two");
  188.  rpkt = asendpkt(rport,filehdlr,ACTION_WRITE,arg,NARGS);
  189.  
  190.  arg[1]= (long) &buff[BUFFLEN + BUFFLEN];
  191.  puts("three");
  192.  rpkt = asendpkt(rport,filehdlr,ACTION_WRITE,arg,NARGS);
  193.  
  194. /* a more elegant packet flush routine would be nice */
  195.  
  196. if(!pktflush(rport,3L)) puts("Error in packets sent.");
  197.  
  198. /* done clean up... */
  199.  Close(fh);
  200.  FreeMem(buff,(BUFFLEN * NBUFFS));
  201.  DeletePort(rport);
  202.  exit(FALSE);
  203.  
  204. }
  205.  
  206. /* eof */
  207.