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

  1. // OCL - OS/2 Class Library
  2. // (c) Cubus 1995
  3. // All Rights Reserved
  4. // OPipeSvr.cpp
  5.  
  6. // members of OPipeSvr
  7.  
  8.  
  9. /*
  10.  * Redistribution and use in source and binary forms, with or without
  11.  * modification, are permitted provided that the following conditions
  12.  * are met:
  13.  * 1. Redistributions of source code must retain the above copyright
  14.  *    notice, this list of conditions and the following disclaimer.
  15.  * 2. Neither the name Cubus nor the name Team OCL may be used to
  16.  *    endorse or promote products derived from this software
  17.  *    without specific prior written permission.
  18.  * 3. See OCL.INF for a detailed copyright notice.
  19.  *
  20.  *              THIS SOFTWARE IS PROVIDED ``AS IS'' AND
  21.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  24.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30.  * SUCH DAMAGE.
  31.  */
  32.  
  33. // $Header: W:/Projects/OCL/Source/rcs/OPipeSvr.cpp 1.50 1996/08/11 23:49:27 B.STEIN Release $
  34.  
  35. #define __OCL_SOURCE__
  36.  
  37. #define OINCL_OSTRING
  38. #define OINCL_BASE
  39.  
  40. #include <ocl.hpp>
  41. #include <OPipeSvr.hpp>
  42.  
  43. #if defined(__EMX__)
  44.   template class OThread<OPipeSvr>;
  45. #endif
  46.  
  47.  
  48. OPipeSvr::OPipeSvr(const ULONG inSize,
  49.                    const ULONG outSize,
  50.                    const ULONG Size,
  51.                    const ULONG openMode,
  52.                    const ULONG pipeMode,
  53.                    const ULONG timeout,
  54.                    const ULONG inst)
  55.   : pipeThread(this, &OPipeSvr::Pipe_IO)
  56. {
  57.  oMode = openMode;
  58.  pMode = pipeMode;
  59.  timeOut = timeout;
  60.  instances = inst;
  61.  inBuf = inSize;
  62.  outBuf = outSize;
  63.  BufSize = Size;
  64. }
  65.  
  66.  
  67. OPipeSvr::~OPipeSvr()
  68. {
  69.  destroyPipe();
  70.  pipes.reset();
  71. }
  72.  
  73.  
  74. PSZ OPipeSvr::isOfType() const
  75.  return("OPipeSvr"); 
  76. }
  77.  
  78.  
  79.  
  80. BOOL OPipeSvr::beginPiping(PCSZ pipeName)
  81. {
  82.  return(createPipe(pipeName)); 
  83. }
  84.  
  85.  
  86. BOOL OPipeSvr::stopPiping() 
  87. {
  88.  return(destroyPipe()); 
  89. }
  90.  
  91.  
  92. void OPipeSvr::setBufSizes(const ULONG in, const ULONG out, const ULONG Size)
  93. {
  94.  inBuf = in;
  95.  outBuf = out;
  96.  BufSize = Size;
  97. }
  98.  
  99.  
  100. BOOL OPipeSvr::createPipe(PCSZ pipeName)
  101. {
  102.  if ((!oMode) || (!pMode) || (!timeOut) || (!inBuf) || (!outBuf) || (!Buffer) || (!BufSize))
  103.    return(FALSE);
  104.  
  105.  pMode |= instances;
  106.  connectedClients = 0;
  107.  
  108.  for (ULONG i=0; i < instances; i++) {
  109.    pPipeStatus status = new PipeStatus;
  110.  
  111.    status->connected = FALSE;
  112.    if (DosCreateNPipe((PSZ) pipeName,  &status->pipe,  oMode, pMode, outBuf, inBuf, timeOut))
  113.      delete status;
  114.    else
  115.      pipes.addLast(status);
  116.   }
  117.  
  118.  if (pipes.isEmpty())
  119.    return(FALSE);
  120.  
  121.  return(pipeThread.run());
  122. }
  123.  
  124.  
  125. BOOL OPipeSvr::destroyPipe()
  126. {
  127.  close = TRUE;
  128.  pipeThread.waitFor();
  129.  actual = pipes.getFirst();
  130.  while(actual) {
  131.    actual->connected = FALSE;
  132.    DosDisConnectNPipe(actual->pipe);
  133.    DosClose(actual->pipe); 
  134.    actual = pipes.getNext(); }
  135.  return(TRUE);
  136. }
  137.  
  138.  
  139. BOOL OPipeSvr::getPipeData()
  140. {
  141.  ULONG read = 0;
  142.  if ((!actual->connected) || (DosRead(actual->pipe, Buffer, BufSize, &read)))
  143.    return(FALSE);
  144.  if (read == 0)
  145.    return(FALSE);
  146.  OPipeCommand(Buffer);
  147.  return(TRUE);
  148. }
  149.  
  150. BOOL OPipeSvr::postPipe(PVOID pvData, ULONG client)
  151. {
  152.  ULONG                written = 0;
  153.  pPipeStatus          postTo = NULL;
  154.  OListItem<PipeStatus> *tmp = (OListItem<PipeStatus>*)pipes.last();
  155.  
  156.  if (client == 0)
  157.   {
  158.    BOOL fSuccess = TRUE;
  159.  
  160.    for(ULONG client2post = 1; 
  161.       ((client2post <= connectedClients) && (fSuccess));
  162.       client2post++)
  163.      fSuccess = postPipe(pvData, client2post);
  164.    return(fSuccess);
  165.   }
  166.  
  167.  if ((pipes.isEmpty()) || (client < 1))
  168.    return(FALSE);
  169.  if (client > 1)
  170.   {
  171.    for (ULONG i=1; i < client; i++)
  172.     {
  173.      tmp = tmp->prev;
  174.      if (!tmp)
  175.        return(FALSE);
  176.     }
  177.   }
  178.  
  179.  if (tmp)
  180.    postTo = tmp->item;
  181.  
  182.  return((postTo != NULL) &&
  183.         (postTo->connected) && 
  184.         (!DosWrite(postTo->pipe, pvData, BufSize, &written)) && 
  185.         (written != 0)); 
  186. }
  187.  
  188.  
  189. void OPipeSvr::Pipe_IO()
  190. {
  191.  APIRET     rc;
  192.  
  193.  pipeThread.setPrty(PRTYC_IDLETIME, 0);  // initially run at idle priority
  194.  
  195.  actual = pipes.getFirst();
  196.  
  197.  while((!close) && (actual))
  198.   {
  199.    rc = DosConnectNPipe(actual->pipe);
  200.    switch(rc) 
  201.     {
  202.      case NO_ERROR:
  203.        if (actual->connected == FALSE) {
  204.            actual->connected = TRUE;
  205.            connectedClients++; }
  206.        if (connectedClients > 0)
  207.          pipeThread.setPrty(PRTYC_REGULAR, 0);
  208.        getPipeData();
  209.        break;
  210.  
  211.      case ERROR_BROKEN_PIPE:
  212.        actual->connected = FALSE;
  213.        connectedClients--;
  214.        DosDisConnectNPipe(actual->pipe);
  215.        if (connectedClients == 0)
  216.          pipeThread.setPrty(PRTYC_IDLETIME, 0);
  217.        break;
  218.  
  219.      case ERROR_PIPE_NOT_CONNECTED:
  220.        actual->connected = FALSE;
  221.        break;
  222.     }
  223.  
  224.    actual = pipes.getNext();
  225.    if (!actual) {
  226.       actual = pipes.getFirst();
  227.       DosSleep(100); }
  228.   }
  229.  
  230.  _endthread();
  231. }
  232.  
  233. // end of source
  234.