home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mandlcpp.zip / server.cpp < prev    next >
C/C++ Source or Header  |  1993-06-26  |  4KB  |  205 lines

  1. #include "server.h"
  2.  
  3.  
  4. serverMainThread::serverMainThread(serverProcess *pProcessNew)
  5.     :thread(FALSE, pProcessNew)
  6. {    pServerProcess = pProcessNew;
  7. }
  8.  
  9.  
  10. void serverMainThread::doSomething(void)
  11. {       serverThread *pThread;
  12.  
  13.     if (!(pThread = new serverThread(pServerProcess)))
  14.         return;
  15.     else
  16.         if (!pThread->isSuccessfull())
  17.         {    delete pThread;
  18.             return;
  19.         }
  20.     pThread->startThread();
  21.     while (1)
  22.         DosSleep(1000*60);
  23. }
  24.  
  25.  
  26. serverThread::serverThread(serverProcess *pProcessNew)
  27.     :thread(TRUE, pProcessNew)
  28. {    pServerProcess = pProcessNew;
  29. }
  30.  
  31.  
  32. Boolean serverThread::create(void)
  33. {    APIRET iRet;
  34.  
  35.     if (iRet = DosCreateNPipe((unsigned char*)pServerProcess->pPipeName,
  36.         &hPipe,
  37.         NP_NOWRITEBEHIND | NP_NOINHERIT | NP_ACCESS_DUPLEX,
  38.         NP_WAIT | NP_TYPE_MESSAGE | NP_READMODE_MESSAGE | NP_UNLIMITED_INSTANCES,
  39.         1024*16,
  40.        1024*16,
  41.        0))
  42.     {    fprintf(stderr,
  43.             "DosCreateNPipe() returned error code %u!\n",
  44.             iRet);
  45.         return FALSE;
  46.     }
  47.     DosSetPriority(PRTYS_THREAD,
  48.         PRTYC_IDLETIME,
  49.         -pTIB->tib_ptib2->tib2_ulpri,
  50.         pTIB->tib_ptib2->tib2_ultid);
  51.     return TRUE;
  52. }
  53.  
  54.  
  55. void serverThread::destruct(void)
  56. {    DosClose(hPipe);
  57. }
  58.  
  59.  
  60. Boolean serverThread::calculate(structRequest *pStructR)
  61. {       unsigned int xi;
  62.     double x, y;
  63.     unsigned int iSize;
  64.     unsigned long iBytesWritten;
  65.     APIRET iRet;
  66.     unsigned char *paiData;
  67.  
  68.     if (pStructR->iSizeX > 16384)
  69.         return FALSE;
  70.     if (!(paiData
  71.         = (unsigned char*)alloca(iSize = pStructR->iSizeX*sizeof*paiData)))
  72.         return FALSE;
  73.     (void)memset((void*)paiData, 0, iSize);
  74.     for (xi = 0, x = pStructR->dXa, y = pStructR->dYa;
  75.        xi < pStructR->iSizeX;
  76.        xi++, y += pStructR->dYdx, x += pStructR->dXdx)
  77.     {    double y2 = y*y;
  78.         double x_1_2 = x + 1.0;
  79.  
  80.         x_1_2 *= x_1_2;
  81.         if (y2 + x_1_2 > 1.0/16.0)
  82.         {    double r = x*x + y2;
  83.             double s = r - x*0.5 + 1.0/16.0;
  84.             if ((r*16 - 5.0)*s + 4*x > 1)
  85.             {    double xit = x, yit = y;
  86.                 unsigned short it;
  87.  
  88.                 for (it = 0;
  89.                     it < pStructR->iMaxIterations;
  90.                     it++)
  91.                 {    double x2 = xit*xit;
  92.                     double y2 = yit*yit;
  93.                     double r = y2 + x2;
  94.  
  95.                     if (r > pStructR->dLimit)
  96.                     {    paiData[xi]
  97.                             = it
  98.                             % (pStructR->iMaxColors - 1)
  99.                             + 1;
  100.                         break;
  101.                     }
  102.                     r = 2*xit*yit + y;
  103.                     xit = x2 - y2 + x;
  104.                     yit = r;
  105.                 }
  106.             }
  107.         }
  108.     }
  109.     if (iRet = DosWrite(hPipe, paiData, iSize, &iBytesWritten))
  110.     {    fprintf(stderr,
  111.             "DosRead() returned error code %u!\n",
  112.             iRet);
  113.         return FALSE;
  114.     }
  115.     if (iBytesWritten != iSize)
  116.         {    fprintf(stderr,
  117.             "Wrong number of bytes written (%u)!\n",
  118.             iBytesWritten);
  119.         return FALSE;
  120.     }
  121.     return TRUE;
  122. }
  123.  
  124.  
  125. void serverThread::doSomething(void)
  126. {       structRequest structR;
  127.     unsigned long iBytesRead;
  128.     unsigned int iRet;
  129.  
  130.     if (iRet = DosConnectNPipe(hPipe))
  131.     {    fprintf(stderr,
  132.             "DosConnectNPipe() returned error code %u!\n",
  133.             iRet);
  134.         return;
  135.     }
  136.     {    serverThread *pThread = new serverThread(pServerProcess);
  137.  
  138.         if (pThread)
  139.             if (pThread->isSuccessfull())
  140.                 pThread->startThread();
  141.             else
  142.             {    delete pThread;
  143.                 fprintf(stderr,
  144.                     "Could not successfull create thread!\n");
  145.             }
  146.         else
  147.             fprintf(stderr, "Could not create thread!\n");
  148.     }
  149.     while (!(iRet = DosRead(hPipe,
  150.         &structR,
  151.         sizeof structR,
  152.         &iBytesRead))
  153.         && iBytesRead == sizeof structR)
  154.         if (!calculate(&structR))
  155.             break;
  156.     if (iRet)
  157.         fprintf(stderr,
  158.             "DosRead() returned error code %u!\n",
  159.             iRet);
  160.     else
  161.         if (iBytesRead != sizeof structR)
  162.             fprintf(stderr,
  163.                 "Invalid number of bytes in request!\n");
  164.     if (iRet = DosDisConnectNPipe(hPipe))
  165.         fprintf(stderr,
  166.             "DosDisConnectNPipe() returned error code %u!\n",
  167.             iRet);
  168. }
  169.  
  170.  
  171. serverProcess::serverProcess(int argc, char **argv):process(argc, argv)
  172. {    if (argc == 2)
  173.         pPipeName = argv[1];
  174.     else
  175.     {    fprintf(stderr, "Usage: %s \\pipe\\myPipeName", argv[0]);
  176.         setNoSuccess();
  177.     }
  178. }
  179.  
  180.  
  181. Boolean serverProcess::create(void)
  182. {    if (!(pProcessThread = new serverMainThread(this)))
  183.         return FALSE;
  184.     else
  185.         if (pProcessThread->isSuccessfull())
  186.             return TRUE;
  187.         else
  188.         {    delete pProcessThread;
  189.             pProcessThread = (thread*)0;
  190.             return FALSE;
  191.         }
  192. }
  193.  
  194.  
  195. int main(int argc, char **argv)
  196. {    serverProcess *pProcess = new serverProcess(argc, argv);
  197.  
  198.     if (pProcess)
  199.         pProcess->run();
  200.     delete pProcess;
  201.     DosExit(EXIT_PROCESS, 0);
  202.     return 0;
  203. }
  204.  
  205.