home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c004 / 3.ddi / OS2LAN / CTAPIP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-04-18  |  7.1 KB  |  232 lines

  1. /*
  2.  *    OS/2 Lan Manager using named pipes version of c-tree server
  3.  *
  4.  *    This program is the CONFIDENTIAL and PROPRIETARY property 
  5.  *    of FairCom(R) Corporation. Any unauthorized use, reproduction or
  6.  *    transfer of this program is strictly prohibited.
  7.  *
  8.  *      Copyright (c) 1987, 1988, 1989 FairCom Corporation
  9.  *    (Subject to limited distribution and
  10.  *     restricted disclosure only.)
  11.  *    *** ALL RIGHTS RESERVED ***
  12.  *
  13.  *    4006 West Broadway
  14.  *    Columbia, MO 65203
  15.  *
  16.  *
  17.  *    c-tree(R)    Version 4.3
  18.  *            Release C
  19.  *            February 7, 1989 17:30
  20.  *
  21.  */
  22.  
  23. #define INCL_DOS
  24. #define INCL_DOSERRORS
  25. #include <os2def.h>       /* Common definitions */
  26. #include <bsedos.h>       /* Base definitions */
  27. #include <bseerr.h>       /* Base error code definitions */
  28.  
  29. #include <net\netcons.h>
  30. #include <net\neterr.h>
  31. #ifndef INCL_DOSNMPIPES   /* This define i set if MS SDK 1.06 include files */
  32. #include <net\nmpipe.h>
  33. #endif
  34.  
  35. #include <stdlib.h>
  36. #include <stdio.h>
  37. #include <string.h>
  38. #include <malloc.h>
  39.  
  40. #include "ctpipes.h"
  41.  
  42.  
  43. /*
  44.  * Receive buffer for call DosTransactNmPipe 
  45.  * in and out buffer could not be the same ??????!!!!!!?????
  46.  */
  47. /* char P_outp_buffer[PIPESIZE];
  48.  * char far *P_outp = (char far*)P_outp_buffer;
  49.  ***********************/
  50. char far *P_outp = 0L;
  51.  
  52. unsigned pipe_errcode;
  53.  
  54. /*
  55.  * values for type_of_named_pipe_call:
  56.  *  0    = OS/2 1.0 with device=NAMPIPES.SYS from 3Com 3+Open is working
  57.  *         GOOD with DosCallNmPipe
  58.  *  1    = OS/2 1.1 with device=NAMPIPES.SYS from 3Com 3+Open is working
  59.  *  0xFF = init value to test for version;
  60.  */
  61. static int type_of_named_pipe_call = 0xFF;
  62.  
  63. /* 
  64.  * PC-SERVER variable:
  65.  *----------------------
  66.  * en int for hver server prosses, med folgende verdi:
  67.  *  - 0 for local use (running as background task)  "\PIPE\pipename"
  68.  *  - 1 for LAN-server is to be used                "\\SERVER\PIPE\pipename"
  69.  */ 
  70. int    ps_srv_isam_use = 0; 
  71. int    ps_srv_print_use = 0; 
  72. char   ps_servname[NAMESIZE] = SRV_NETNAME;   /* server network name */
  73.  
  74.  
  75. unsigned startusepipe(char *pipename, char *prog, char *prog_param,
  76.                       int local)
  77. {
  78. int i;
  79. long are_you_there = PIPE_TEST, yes_i_am;
  80.  
  81.    if (!dopipe(pipename, (char far *)&are_you_there, sizeof(long),
  82.                          (char far *)&yes_i_am)) {
  83.      /*
  84.       * Pipe is not started, we will then start it localy in a
  85.       * background task.
  86.       */
  87.       if (!local)
  88.          return (PIPE_ERR_COULD_NOT_START);
  89.  
  90.       if (startpipe(prog,prog_param))
  91.          return (PIPE_ERR_COULD_NOT_START);
  92.       /* try to access pipe for a while until server is properly loaded */
  93.       for (i = 0 ; i < 50 ; i++ ) {
  94.          if (dopipe(pipename, (char far *)&are_you_there, sizeof(long),
  95.                                (char far *)&yes_i_am))
  96.             return(PIPE_STARTED_LOCAL);  /* pipe is startet, go on */
  97.          DosSleep(500L);
  98.       } /* end waiting for pipe to start */
  99.    }
  100.    else
  101.       /* test if server answered correctly */
  102.       if (yes_i_am != PIPE_ANSW)
  103.          pipe_errcode = PIPE_ERR_NO_ANSW; /* no answer */
  104.  
  105.    return(pipe_errcode);
  106. }
  107.  
  108. unsigned dopipe(char *PipeName, char far *pipe_inp, 
  109.                 unsigned ilen, char far *pipe_outp)
  110. {
  111. unsigned short bytes;
  112. HFILE    handle;
  113. unsigned action;
  114. unsigned short status;
  115. char     dummy[4];
  116.  
  117.    if (P_outp == 0L)  /* no buffer allocated */
  118.        P_outp = (char far *)_fmalloc(PIPESIZE);  
  119.  
  120.    if (type_of_named_pipe_call == 0xFF) {    /* Init type of Pipe Call ? */
  121.      /* Get type of OS/2 operating system:
  122.       *        0 = OS/2 1.0 med 3Com Named Pipes 
  123.       *        1 = OS/2 1.1 Standard or Extended Edition
  124.       *        9 = newer versions not supported
  125.       */
  126.       type_of_named_pipe_call = get_os2_version();
  127.       if (type_of_named_pipe_call == 9) {
  128.           DosBeep(800,6);
  129.           DosBeep(2000,10);
  130.           DosBeep(800,6);
  131.           type_of_named_pipe_call = 1; /* we then try use TransactNmPipe */
  132.       }
  133.    }
  134.  
  135.   /*
  136.    *  OS/2 1.1 Standard or Extended Edition is not working with
  137.    *  DosCallNmPipe.
  138.    *  We then use  DosOpen - DosTransactNmPipe - DosClose instead
  139.    */
  140.    if (type_of_named_pipe_call) {
  141.       while ((pipe_errcode = DosOpen(PipeName, &handle, &action, 0L, 0, 
  142.                                      CLI_OPENFLAG, CLI_DOSOPENMODE, 0L))
  143.                            == ERROR_PIPE_BUSY) {
  144.         /* wait until pipe is ready to be opened
  145.          * Wait for default time for pipe to be ready
  146.          */
  147.          pipe_errcode = DosWaitNmPipe(PipeName,0L); 
  148.       } /* end while DosOpen */
  149.       if (!pipe_errcode) {
  150.          for (;;) {
  151.             if (pipe_errcode = DosPeekNmPipe(handle, dummy, 0, 
  152.                                              &bytes, &bytes, &status)) {
  153.                break;
  154.             }
  155.             if (status == NP_CONNECTED) {
  156.                bytes = 0;
  157.                if (pipe_errcode =
  158.                         DosTransactNmPipe(handle,
  159.                                (char far *) pipe_inp, (unsigned short) ilen,
  160.                                (char far *) P_outp,
  161.                                PIPESIZE, &bytes) ) {
  162.                   break;
  163.                }
  164.                break;
  165.             }
  166.          } /* end waiting for pipe to be ready */
  167.          DosClose(handle);
  168.       }
  169.    }
  170.  
  171.    else
  172.  
  173.   /*
  174.    *  OS/2 1.0 with device=NAMPIPES.SYS from 3Com 3+Open is working
  175.    *  GOOD with DosCallNmPipe
  176.    */
  177.    {
  178.       do {
  179.  
  180.          pipe_errcode = DosCallNmPipe(PipeName,
  181.                            (char far *) pipe_inp, (unsigned short) ilen,
  182.                            (char far *) P_outp, PIPESIZE, &bytes, 10000L);
  183.  
  184.       } while(pipe_errcode == ERROR_PIPE_BUSY || 
  185.               pipe_errcode == ERROR_SEM_TIMEOUT);
  186.    }
  187.   /*
  188.    *    ERROR_FILE_NOT_FOUND    Specified pipe does not exist.
  189.    *    ERROR_PIPE_BUSY         All available instances are in use.
  190.    *    ERROR_PIPE_NOT_CONNECTED Pipe disconnected during transaction.
  191.    *    ERROR_BROKEN_PIPE       Write to single-ended pipe.
  192.    *    ERROR_MORE_DATA         More data exists in message.  All
  193.    *                            data requested has been transferred.
  194.    *    ERROR_SEM_TIMEOUT       Wait timed out.
  195.    *    ERROR_INTERRUPT         Interrupted wait.
  196.    */
  197.  
  198.    if (!pipe_errcode) {
  199.       movedata (SELECTOROF(P_outp),
  200.                  OFFSETOF(P_outp),
  201.                  SELECTOROF(pipe_outp), OFFSETOF(pipe_outp), bytes);
  202.    }
  203.  
  204.    return(pipe_errcode ? 0 : bytes);
  205. }
  206.  
  207.  
  208. unsigned startpipe(char *pname, char *pargs)
  209. {
  210. char        tbuf[128];
  211. RESULTCODES rc;
  212. PID pidWait;
  213. USHORT wc, prio;
  214.  
  215.    pipe_errcode = DosExecPgm(tbuf, sizeof(tbuf), EXEC_BACKGROUND,
  216.                              pargs, 0, &rc, pname);
  217.    pidWait = rc.codeTerminate;
  218.    DosSleep(500L);
  219.    /* see if Process still is running */
  220.    wc = DosGetPrty(PRTYS_PROCESS, &prio, pidWait);
  221.    switch (wc) {
  222.       case ERROR_INVALID_SCOPE:
  223.            /* The parameter must indicate either a thread or process.*/
  224.       case ERROR_INVALID_PROCID:
  225.            /* The process identifier is invalid or nonexistent.*/
  226.       case ERROR_NO_CHILD_PROCESS:
  227.       default:
  228.            pipe_errcode = wc;
  229.    }
  230.    return(pipe_errcode);
  231. }
  232.