home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ocl150a.zip / OCL / Source / OPipeCli.cpp < prev    next >
C/C++ Source or Header  |  1996-08-12  |  4KB  |  168 lines

  1. // OCL - OS/2 Class Library
  2. // (c) Cubus 1995
  3. // All Rights Reserved
  4. // OPipeCli.cpp
  5.  
  6. /*
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  * 1. Redistributions of source code must retain the above copyright
  11.  *    notice, this list of conditions and the following disclaimer.
  12.  * 2. Neither the name Cubus nor the name Team OCL may be used to
  13.  *    endorse or promote products derived from this software
  14.  *    without specific prior written permission.
  15.  * 3. See OCL.INF for a detailed copyright notice.
  16.  *
  17.  *              THIS SOFTWARE IS PROVIDED ``AS IS'' AND
  18.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27.  * SUCH DAMAGE.
  28.  */
  29.  
  30.  
  31. // $Header: W:/Projects/OCL/Source/rcs/OPipeCli.cpp 1.50 1996/08/11 23:49:27 B.STEIN Release $
  32.  
  33. #define __OCL_SOURCE__
  34.  
  35. #define OINCL_OSTRING
  36. #define OINCL_BASE
  37.  
  38. #include <ocl.hpp>
  39. #include <OPipeCli.hpp>
  40.  
  41. #if defined(__EMX__)
  42.   template class OThread<OPipeCli>;
  43. #endif
  44.  
  45.  
  46. // members of OPipeCli
  47.  
  48.  
  49. OPipeCli::OPipeCli(const ULONG Size,
  50.                    const ULONG openMode,
  51.                    const ULONG pipeMode,
  52.                    const ULONG timeout,
  53.                    const BOOL  waitFor)
  54.   : pipeThread(this, &OPipeCli::Pipe_IO)
  55. {
  56.  oMode = openMode;
  57.  pMode = pipeMode;
  58.  timeOut = timeout;
  59.  BufSize = Size;
  60.  waitForInstance = waitFor;
  61.  connected = FALSE;
  62.  Pipe = (HPIPE)NULL;
  63. }
  64.  
  65.  
  66.  
  67. OPipeCli::~OPipeCli()
  68. {
  69.  if (connected) disconnectPipe();
  70. }
  71.  
  72.  
  73. PSZ OPipeCli::isOfType() const
  74.  return("OPipeCli"); 
  75. }
  76.  
  77.  
  78. BOOL OPipeCli::beginPiping(PCSZ pipeName) 
  79. {
  80.  return(connectPipe(pipeName)); 
  81. }
  82.  
  83.  
  84. BOOL OPipeCli::stopPiping() 
  85. {
  86.  return(disconnectPipe()); 
  87. }
  88.  
  89.  
  90. void OPipeCli::setSize(const ULONG Size) 
  91. {
  92.  BufSize = Size; 
  93. }
  94.  
  95.  
  96. BOOL OPipeCli::connectPipe(PCSZ pipeName)
  97. {
  98.  ULONG     Action;
  99.  APIRET    rc;
  100.  
  101.  if ((!oMode) || (connected) || (!Buffer) || (!BufSize))
  102.    return(FALSE);
  103.  
  104.  while(!close)
  105.   {
  106.    rc = DosOpen((PSZ)pipeName, (PHFILE)&Pipe, &Action, 0L, 0L, 
  107.                 OPEN_ACTION_OPEN_IF_EXISTS, oMode, NULL);
  108.    switch(rc)
  109.     {
  110.      case NO_ERROR:
  111.        connected = TRUE;
  112.        return(pipeThread.run());
  113.  
  114.      case ERROR_PIPE_BUSY:
  115.        if (!waitForInstance)
  116.          return(FALSE);
  117.        DosWaitNPipe((PSZ)pipeName, 0);
  118.        break;
  119.     }
  120.   }
  121.  return(FALSE);
  122. }
  123.  
  124. BOOL OPipeCli::disconnectPipe()
  125. {
  126.  close = TRUE;
  127.  DosClose(Pipe);
  128.  connected = FALSE;
  129.  if (pipeThread.isRunning())
  130.    pipeThread.stop();
  131.  return(TRUE);
  132. }
  133.  
  134. BOOL OPipeCli::postPipe(PVOID pvData, ULONG client)
  135. {
  136.  ULONG written = 0;
  137.  
  138.  return((connected) && 
  139.         (!DosWrite(Pipe, pvData, BufSize, &written)) &&
  140.         (written != 0));
  141. #ifdef __BCPLUSPLUS__
  142.   #pragma warn -par
  143. #endif
  144. }
  145. #ifdef __BCPLUSPLUS__
  146.   #pragma warn .par
  147. #endif
  148.  
  149.  
  150. void OPipeCli::Pipe_IO()
  151. {
  152.  ULONG read = 0;
  153.  
  154.  while (!close)
  155.    {
  156.     if (!DosRead(Pipe, Buffer, BufSize, &read) && read > 0)
  157.       OPipeCommand(Buffer); 
  158.     else
  159.       DosSleep(100); 
  160.    }
  161.  
  162.  connected = FALSE;
  163.  _endthread();
  164. }
  165.  
  166. // end of source
  167.