home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / fossdumm.zip / SOURCE.ZIP / SERVDUMM.C < prev   
C/C++ Source or Header  |  1993-06-15  |  5KB  |  169 lines

  1. /* Author Michael Mrosowski*/
  2. /* Version 0.1*/
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <fcntl.h>
  6. #include <ctype.h>
  7.  
  8. #define INCL_WHATEVER
  9. #define INCL_DOSDEVIOCTL
  10. #include <os2.h>
  11.  
  12. #define maxbufflen 4000
  13.  
  14. HFILE comhandle=0;
  15. int delay=100;
  16.  
  17. unsigned char sigin;
  18.  
  19. void tocom(void *buffer,int len)
  20. {
  21.   ULONG written;
  22.  
  23.   DosWrite(comhandle,buffer,len,&written);
  24. }
  25.  
  26. char localkey=0;
  27.  
  28. int fromcom(char *buffer)
  29. { ULONG read=0;
  30.   int lc;
  31.  
  32.   if(comhandle)
  33.     DosRead(comhandle,buffer,1,&read);
  34.  
  35.   lc=_read_kbd(0,0,0);         /*local keyboard*/
  36.   if(lc!=-1)
  37.   {
  38.     buffer[0]=(char)lc;
  39.     read=1;
  40.     localkey=(char)lc;
  41.   }
  42.   return read;
  43. }
  44.  
  45. int carrierlost(void)
  46. { APIRET        rc;
  47.   long          dummy;
  48.   unsigned char modeminfo;
  49.  
  50.   if(comhandle) {
  51.     rc=DosDevIOCtl(comhandle, IOCTL_ASYNC, ASYNC_GETMODEMINPUT, 0,0,0,&modeminfo,
  52.                    1,&dummy);
  53.     if(rc==0)
  54.       return !((modeminfo>>7) &1);
  55.   }
  56.   return 0;
  57. }
  58.  
  59.  
  60. char inbuffer[maxbufflen],outbuffer[maxbufflen];
  61. int inbufflen=0;
  62.  
  63.  #define  NPIPEIN_NAME   "\\PIPE\\DUMMSIG."         /* Pipe name             */
  64.  #define  NPIPEOUT_NAME   "\\PIPE\\DUMMOUT."         /* Pipe name             */
  65.  
  66.  HFILE      hSignal,hPipe;                    /* Pipe handle           */
  67.  ULONG      ulAction;                            /* Open action           */
  68.  APIRET     rc;                                  /* Return code           */
  69.  
  70. void initpipe(char *name,HFILE *hpipe)
  71. {    /*Dosmakenmpipe*/
  72.  printf("Dos Create Pipe name : %s\n",name);fflush(stdout);
  73.  rc = DosCreateNPipe(name,              /* Create named pipe     */
  74.                      hpipe,                  /* Pipe handle           */
  75.                      NP_ACCESS_DUPLEX,        /* Allow duplex access   */
  76.                      NP_NOWAIT        |       /* Blocking mode         */
  77.  
  78.                      NP_TYPE_BYTE     |       /* Msg oriented pipe     */
  79.                      NP_READMODE_BYTE |       /* Msg oriented read     */
  80.                      255,                     /* max 255 instances*/
  81.                      sizeof(outbuffer),       /* Outbound buffer size  */
  82.                      sizeof(inbuffer),        /* Inbound buffer size   */
  83.                      0);/*should be 0*/       /* Default timeout value */
  84.  printf("Dos Create Pipe returns : %d\n",rc);fflush(stdout);
  85.  do {
  86.    rc = DosConnectNPipe(*hpipe);              /* Connect to requester  */
  87.    if(rc) DosSleep(100);
  88.  } while(rc);
  89.  printf("Pipe connected\n",rc);fflush(stdout);
  90. }
  91.  
  92.  
  93. ULONG bytesread,byteswritten;
  94.  
  95. int main(int argc, char *argv[], char *envp[])
  96. {
  97.   char inname[128],outname[128];
  98.   int sleepval=100;
  99.   int lastcarriercheckago=0;
  100.   int nocarrier=0;
  101.  
  102.   if(argc>=3)
  103.   {
  104.    strcpy(inname,NPIPEIN_NAME);
  105.    strcpy(outname,NPIPEOUT_NAME);
  106.    strcat(inname,argv[1]); /*add node id to pipenames*/
  107.    strcat(outname,argv[1]);
  108.    comhandle=atoi(argv[2]);
  109.    printf("ServDumm, Michael Mrosowski is to blame for this.\n");
  110.    printf("Node : %s, Comhandle : %d\n",argv[1],comhandle);fflush(stdout);
  111.  
  112.    initpipe(outname,&hPipe);
  113.    initpipe(inname,&hSignal);
  114.    do
  115.    {
  116.      if(sleepval) {
  117.        DosSleep(sleepval);
  118.        lastcarriercheckago+=sleepval;
  119.      } else lastcarriercheckago+=2;
  120.      if (lastcarriercheckago>=1000) {
  121.        nocarrier=carrierlost();
  122.        lastcarriercheckago=0;
  123.      }
  124.  
  125.      /*get outputdata*/
  126.      rc = DosRead(hPipe,                      /* Read request          */
  127.                   outbuffer,                  /* Request buffer        */
  128.                   maxbufflen,                 /* Size of buffer        */
  129.                   &bytesread);                /* No. of bytes read     */
  130.  
  131.      if(bytesread) /*send outputdata to com-handle*/
  132.        tocom(outbuffer,bytesread);
  133.  
  134.      if(!inbufflen)
  135.        inbufflen=fromcom(inbuffer); /*get inputdata from com-handle*/
  136.  
  137.      if(!bytesread && !inbufflen) /*increase wait-delay if no data send/rec*/
  138.        {if(sleepval<100) sleepval+=5;}
  139.      else sleepval=0;
  140.  
  141.                       /*check signal pipe*/
  142.      sigin=0;
  143.      do {             /* get signals last signal */
  144.        rc = DosRead(hSignal,                   /* Read signal           */
  145.                     &sigin,                    /* Request buffer        */
  146.                     sizeof(sigin),             /* Size of buffer        */
  147.                     &bytesread);               /* No. of bytes read     */
  148.      } while(bytesread);
  149.  
  150.      if(inbufflen) {
  151.         rc = DosWrite(hPipe,                   /* Write buffer to pipe   */
  152.                       inbuffer,                   /* Reply buffer          */
  153.                       inbufflen,                  /* Size of buffer        */
  154.                       &byteswritten);             /* No. of bytes written  */
  155.         inbufflen-=byteswritten;
  156.      }
  157.    } while (sigin!=2 && !nocarrier);
  158.    if(nocarrier) {printf("Carrier lost !!\n\n");fflush(stdout);}
  159.    rc = DosDisConnectNPipe(hPipe);
  160.    rc = DosDisConnectNPipe(hSignal);
  161.    return 0;
  162.  }
  163.  else
  164.  {
  165.    printf("Please give me the nodeid and comhandle as Parameter !\n");
  166.    return 1;
  167.  }
  168. }
  169.