home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_02_05 / 2n05035a < prev    next >
Text File  |  1991-03-27  |  5KB  |  193 lines

  1. /*
  2.  *  File:    Server.C
  3.  *  Purpose:    Contains CDROM Server program
  4.  */
  5.  
  6. #include    <io.h>
  7. #include    <fcntl.h>
  8. #include    <stdio.h>
  9. #include    <stdlib.h>
  10. #include    <malloc.h>
  11. #include    <dos.h>
  12. #include    <bios.h>
  13. #include    "device.h"
  14.  
  15.  
  16. /*  External Functions  */
  17.  
  18. /*  Net work functions return 0 if all went ok  */
  19. int NetAddname (char *name);
  20. int NetDelname (char *name);
  21. int NetListen (char *local, char *remote, BYTE *session);
  22. int NetHangUp (BYTE session);
  23. int NetSend (FPTR buf, int size, BYTE session);
  24. int NetReceive (FPTR buf, int size, BYTE session);
  25.  
  26. /*  Set es:bx to value of far pointer x  */
  27. void LoadESBX (FPTR x);
  28.  
  29. /*  Server data to control driver  */
  30. void     (far *DevStrategy)(void);
  31. void    (far *DevInterrupt)(void);
  32.  
  33. /*  Server data buffers  */
  34. char    rhBuf[64];
  35. char    huge dtaBuf[2532 * 48];
  36.  
  37. /*  Server network data  */
  38. char    serverName[] = "CDROM_SERVER";
  39. char    clientName[17] = "*";
  40. BYTE    localSession = 0;
  41. int    dtaInSize[] = { 5, 6, 1, 1,
  42.             9, 130, 5, 4,
  43.             5, 2, 7, 7,
  44.             11, 13, 11, 11 };
  45. int    dtaOutSize[] = { 1, 2, 1, 9, 130, 1 };
  46.  
  47.  
  48. /*
  49.  * main : run the server
  50.  */
  51. int main (int argc, char *argv[])
  52. {
  53.     int            handle;
  54.     int            ret;
  55.     char        ioctlCmd;
  56.     int              far *devHeader;
  57.     char        far *ptr;
  58.     union REGS        regs;
  59.     struct SREGS    sregs;
  60.     RH_CDREAD        far *rh;
  61.     FPTR        dtaSave;
  62.  
  63.  
  64.     /**  Validate usage  **/
  65.     if (argc != 2) {
  66.     printf ("Usage:  Server driver-name\n");
  67.     exit (1);
  68.     }
  69.  
  70.     /**  Simulate IOCTL Input sub-command 0  **/
  71.     handle = open (argv[1], O_BINARY | O_RDONLY);
  72.     if (handle != -1) {
  73.     dtaBuf[0] = '\0';    
  74.     ptr = (char far *) dtaBuf;
  75.  
  76.     /**  Use DOS Device IOCTL call  **/
  77.     regs.h.ah = 0x44;
  78.     regs.h.al = 0x02;
  79.     regs.x.bx = handle;
  80.     regs.x.cx = 5;
  81.     sregs.ds = FP_SEG (ptr);
  82.     regs.x.dx = FP_OFF (ptr);
  83.     int86x (0x21, ®s, ®s, &sregs);
  84.     close (handle);
  85.     }
  86.     if (handle == -1 || regs.x.cflag) {
  87.     printf ("Unable to access %s\n", argv[1]);
  88.     exit (1);
  89.     }
  90.  
  91.     /**  build devHeader pointer **/
  92.     devHeader = (int far *) *((long far *)(dtaBuf+1));
  93.  
  94.     /**  Print name from device header as  **/
  95.     /**  verification of devHeader.        **/
  96.     /**  '%Fs' = Far string pointer        **/
  97.     printf ("\nConnected to device driver %Fs\n",
  98.         devHeader + 5);
  99.  
  100.     /**  Save the desired function pointers  **/
  101.     FP_SEG (DevStrategy) = FP_SEG (devHeader);
  102.     FP_OFF (DevStrategy) = devHeader[3];
  103.     FP_SEG (DevInterrupt) = FP_SEG (devHeader);
  104.     FP_OFF (DevInterrupt) = devHeader[4];
  105.  
  106.     /**  Let people know I am here  **/
  107.     if (NetAddname (serverName)) {
  108.     printf ("Unable to add server name\n");
  109.     exit (1);
  110.     }
  111.  
  112.     /**  Wait for a connection  **/
  113.     printf ("Waiting for a connection...\n\n");
  114.     if (NetListen (serverName, clientName, &localSession))
  115.     printf ("No session could be established\n");
  116.  
  117.     else 
  118.     /**  Process remote requests while the  **/
  119.     /**  session remains intact             **/
  120.                             
  121.     while (1) {
  122.         printf ("\n\n");
  123.         printf ("Waiting...\r");
  124.  
  125.         /**  Get a request header  **/
  126.         if (NetReceive (rhBuf, 128, localSession))
  127.         break;
  128.             
  129.         rh = (FPTR) rhBuf;
  130.         printf ("Request Header has been received\n");
  131.         printf ("  Command code        : %d\n", 
  132.             rh->rh.command);
  133.  
  134.         /** If command is IOCTL, get the DTA now  **/
  135.         if (rh->rh.command==3 || rh->rh.command==12) {
  136.         if (NetReceive(dtaBuf,256,localSession))
  137.             break;
  138.  
  139.         ioctlCmd = *dtaBuf;
  140.             printf ("  IOCTL Subcommand    : %d\n", 
  141.             ioctlCmd);
  142.         }
  143.  
  144.         /**  Set request header to my DTA  **/
  145.         dtaSave = rh->dta;
  146.         rh->dta = (FPTR) dtaBuf;
  147.  
  148.         /**  If ReadLong, check for enough  **/
  149.         /**  buffer space                   **/
  150.         if (rh->rh.command == 128 && rh->count > 48)
  151.             rh->rh.status = DEV_DONE|DEV_ERROR|DEV_EGEN;
  152.  
  153.         else {
  154.            /**  Run command  **/
  155.            LoadESBX (rh);
  156.            (*DevStrategy)();
  157.            (*DevInterrupt)();
  158.         }
  159.         printf ("  Return status       : %x\n", 
  160.             rh->rh.status);
  161.  
  162.         /**  Send back the request header  **/
  163.         rh->dta = dtaSave;
  164.         if (NetSend (rhBuf,rh->rh.length,localSession))
  165.         break;
  166.  
  167.         /**  Does the DTA go back?  **/
  168.         if (rh->rh.status == DEV_DONE) {
  169.         /**  Determine DTA size to send  **/
  170.         handle = 0;
  171.         if (rh->rh.command == 3)
  172.             handle = dtaInSize[ioctlCmd];
  173.  
  174.         else if (rh->rh.command == 12)
  175.             handle = dtaOutSize[ioctlCmd];
  176.  
  177.         else if (rh->rh.command != 131)
  178.             handle = (304 * rh->readMode +
  179.                   2048) * rh->count;
  180.         
  181.         if (handle &&
  182.             NetSend (dtaBuf,handle,localSession))
  183.             break;
  184.         }
  185.     
  186.     } /*  while (1)  */
  187.  
  188.     printf ("\n\nCommunication has terminated\n");
  189.  
  190.     NetHangUp (localSession);
  191.     NetDelname (serverName);
  192. }
  193.