home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 2: PC / frozenfish_august_1995.bin / bbs / d02xx / d0248.lha / NetHandler / server / io.c < prev    next >
C/C++ Source or Header  |  1989-09-16  |  8KB  |  237 lines

  1. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  2. /* |_o_o|\\ Copyright (c) 1987, 1988 The Software Distillery.  All Rights  */
  3. /* |. o.| || Reserved.  This program may not be distributed without the    */
  4. /* | .  | || permission of the authors:                            BBS:    */
  5. /* | o  | ||   John Toebes     Doug Walker    Dave Baker                   */
  6. /* |  . |//                                                                */
  7. /* ======                                                                  */
  8. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  9. /* File Access:            */
  10. /* RmtRead RmtWrite RmtSeek RmtWaitForChar    */
  11. /* RmtFindwrite RmtFindin RmtFindout RmtEnd   */
  12.  
  13. #include "server.h"
  14.  
  15. static long CurrentPos U_ARGS((GLOBAL, struct DosPacket *));
  16.  
  17. static long CurrentPos(global, pkt)
  18. GLOBAL global;
  19. struct DosPacket *pkt;
  20. {
  21.    pkt->dp_Type = ACTION_SEEK;
  22.    pkt->dp_Arg1 = ((struct FileHandle *)global->RP.Arg1)->fh_Arg1;
  23.    pkt->dp_Arg2 = 0L;
  24.    pkt->dp_Arg3 = OFFSET_CURRENT;
  25.    Dispatch(global);
  26.    return(pkt->dp_Res1);
  27. }
  28.  
  29. /*-------------------------------------------------------------------------*/
  30. /*                                                                         */
  31. /*                 RmtRead( global, pkt )                                  */
  32. /*                                                                         */
  33. /*-------------------------------------------------------------------------*/
  34.  
  35. void RmtRead(global,pkt)
  36. GLOBAL global;
  37. struct DosPacket *pkt;
  38. /* Arg1: APTR EFileHandle */
  39. /* Arg2: APTR Buffer      */
  40. /* Arg3: Length           */
  41. {
  42.    long toread, amount, total, offset;
  43.  
  44.    BUG(("RmtRead of %d bytes\n", global->RP.Arg3));
  45.  
  46.    /* 1. Seek 0 to get current position in file
  47.     * 2. Dispatch read (max NETBUFSIZE bytes)
  48.     * 3. If error, seek back to original pos
  49.     * 4. Reply to other side
  50.     * 5. If more to read, go back to (2)
  51.     */
  52.    if((offset = CurrentPos(global, pkt))<0)
  53.    {
  54.       BUG(("RmtRead: Seek failed, code %d\n", pkt->dp_Res2));
  55.       global->RP.Arg1 = pkt->dp_Res1;
  56.       global->RP.Arg2 = pkt->dp_Res2;
  57.       return;
  58.    }
  59.  
  60.    BUG(("RmtRead: Seek done, position is %d\n", offset));
  61.  
  62.    /* Arg1 was set by the CurrentPos function to be the filehandle */
  63.    pkt->dp_Type = ACTION_READ;
  64.    pkt->dp_Arg2 = (LONG)global->RP.Data;
  65.    toread       = global->RP.Arg3;
  66.  
  67.    for(total=0; toread; total+=amount, toread-=amount)
  68.    {
  69.       /* If this isn't the first time, wait for confirmation */
  70.       if(total) GetRPacket(global, global->n.devptr);
  71.  
  72.       pkt->dp_Arg3 = min(toread, NETBUFSIZE);
  73.       BUG(("RmtRead: Amount is %d, to read is %d\n", pkt->dp_Arg3, toread));
  74.       Dispatch(global);
  75.  
  76.       global->RP.DLen = pkt->dp_Res1;
  77.       if(PutRPacket(global, global->n.devptr)) pkt->dp_Res1 = -1;
  78.  
  79.       /* If there was EOF or some kind of error, quit */
  80.  
  81.       if((amount=pkt->dp_Res1) == 0) break;  /* Normal EOF */
  82.  
  83.       if(amount < 0)
  84.       {
  85.          if(offset >= 0)
  86.          {
  87.             /* Seek back to the original pos */
  88.             pkt->dp_Type = ACTION_SEEK;
  89.             pkt->dp_Arg2 = offset;
  90.             pkt->dp_Arg3 = OFFSET_BEGINNING;
  91.             Dispatch(global);
  92.          }
  93.          break;
  94.       }
  95.    }
  96.    BUG(("RmtRead: Done reading, returning\n"));
  97.    global->n.reply = 0;  /* Don't resend the last packet */
  98. }
  99.  
  100. /*-------------------------------------------------------------------------*/
  101. /*                                                                         */
  102. /*                      RmtWrite( global, pkt )                            */
  103. /*                                                                         */
  104. /*-------------------------------------------------------------------------*/
  105.  
  106. void RmtWrite(global,pkt)
  107. GLOBAL global;
  108. struct DosPacket *pkt;
  109. /* Arg1: APTR EFileHandle */
  110. /* Arg2: APTR Buffer */
  111. /* Arg3: Length */
  112. {
  113.    long offset;
  114.    BUG(("RmtWrite\n"));
  115.  
  116.    if((offset = CurrentPos(global, pkt))<0)
  117.    {
  118.       BUG(("RmtWrite: Seek failed, code %d\n", pkt->dp_Res2));
  119.       global->RP.Arg1 = pkt->dp_Res1;
  120.       global->RP.Arg2 = pkt->dp_Res2;
  121.       return;
  122.    }
  123.  
  124.    pkt->dp_Type = ACTION_WRITE;
  125.    pkt->dp_Arg2 = (LONG)global->RP.Data;
  126.  
  127.    while(1)
  128.    {
  129.       pkt->dp_Arg3 = global->RP.Arg3;
  130.       Dispatch(global);
  131.  
  132.       global->RP.DLen = 0;
  133.       if(PutRPacket(global, global->n.devptr)) pkt->dp_Res1 = -1;
  134.       if(pkt->dp_Res1 == -1)
  135.       {
  136.          if(offset >= 0)
  137.          {
  138.             /* Seek back to where we started */
  139.             pkt->dp_Type = ACTION_SEEK;
  140.             pkt->dp_Arg2 = offset;
  141.             pkt->dp_Arg3 = OFFSET_BEGINNING;
  142.             Dispatch(global);
  143.          }
  144.          break;
  145.       }
  146.       if(global->RP.Arg4 == 0) break;  /* no more to write */
  147.  
  148.       GetRPacket(global, global->n.devptr);
  149.    }
  150.    global->n.reply = 0;  /* Don't reply twice */
  151. }
  152.  
  153. /*-------------------------------------------------------------------------*/
  154. /*                                                                         */
  155. /*                       RmtSeek( global, pkt )                            */
  156. /*                                                                         */
  157. /*-------------------------------------------------------------------------*/
  158.  
  159. void RmtSeek(global,pkt)
  160. GLOBAL global;
  161. struct DosPacket *pkt;              /* a pointer to the dos packet sent      */
  162. /* Arg1: APTR EFileHandle */
  163. /* Arg2: Position */
  164. /* Arg3: Mode */
  165. {
  166.    BUG(("RmtSeek\n"));
  167.    pkt->dp_Arg1 = ((struct FileHandle *)global->RP.Arg1)->fh_Arg1;
  168.    pkt->dp_Arg2 = global->RP.Arg2;
  169.    pkt->dp_Arg3 = global->RP.Arg3;
  170.  
  171.    Dispatch(global);
  172.  
  173.    global->RP.DLen = 0;
  174. }
  175.  
  176. /*-------------------------------------------------------------------------*/
  177. /*                                                                         */
  178. /*                    RmtFindwrite( global, pkt )                          */
  179. /*                                                                         */
  180. /*-------------------------------------------------------------------------*/
  181.  
  182. void RmtFindwrite(global,pkt)
  183. GLOBAL global;
  184. struct DosPacket *pkt;              /* a pointer to the dos packet sent    */
  185. /* ARG1: FileHandle to fill in */
  186. /* ARG2: Lock for file relative to */
  187. /* Arg3: Name of file */
  188. {
  189.    struct FileHandle *fh;
  190.    BUG(("RmtFindwrite, lock %lx\n", global->RP.Arg2));
  191.    BUGBSTR("Filename = ", global->RP.Data);
  192.  
  193.    if(!(fh=(struct FileHandle *)
  194.               DosAllocMem(global, sizeof(struct FileHandle))))
  195.    {
  196.       global->RP.Arg1 = DOS_FALSE;
  197.       global->RP.Arg2 = ERROR_NO_FREE_STORE;
  198.       return;
  199.    }
  200.    BUG(("Allocated FileHandle = %lx\n", fh));
  201.  
  202.    pkt->dp_Arg1 = (LONG)MKBADDR(fh);
  203.    pkt->dp_Arg2 = global->RP.Arg2;
  204.    MBSTR(global->RP.Data, global->fib);
  205.    pkt->dp_Arg3 = (LONG)MKBADDR(global->fib);
  206.  
  207.    Dispatch(global);
  208.  
  209.    /* If the open was successful, return the allocated filehandle as Arg3 */
  210.    if(pkt->dp_Res1 == DOS_FALSE) DosFreeMem((char *)fh);
  211.    else global->RP.Arg3 = (LONG)fh;
  212.  
  213.    global->RP.DLen = 0;
  214. }
  215.  
  216. /*-------------------------------------------------------------------------*/
  217. /*                                                                         */
  218. /*                       RmtEnd( global, pkt )                             */
  219. /*                                                                         */
  220. /*-------------------------------------------------------------------------*/
  221.  
  222. void RmtEnd( global, pkt )
  223. GLOBAL global;
  224. struct DosPacket *pkt;              /* a pointer to the dos packet sent    */
  225. {
  226.    struct FileHandle *fh;
  227.    BUG(("RmtEnd, freeing %lx\n", global->RP.Arg1));
  228.  
  229.    pkt->dp_Arg1 = (fh=(struct FileHandle *)global->RP.Arg1)->fh_Arg1;
  230.  
  231.    Dispatch(global);
  232.  
  233.    DosFreeMem((char *)fh);
  234.  
  235.    global->RP.DLen = 0;
  236. }
  237.