home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / os2 / queues / svrqthrd.c < prev    next >
C/C++ Source or Header  |  1999-05-11  |  6KB  |  175 lines

  1. /*==============================================================*\
  2.  *  Svrqthrd.c - queue processing routine: initialization and
  3.  *               processing thread.
  4.  *      Copyright 1992, IBM Corp.
  5.  *
  6.  *--------------------------------------------------------------
  7.  *
  8.  *  DISCLAIMER OF WARRANTIES.  The following [enclosed] code is
  9.  *  sample code created by IBM Corporation. This sample code is not
  10.  *  part of any standard or IBM product and is provided to you solely
  11.  *  for  the purpose of assisting you in the development of your
  12.  *  applications.  The code is provided "AS IS", without
  13.  *  warranty of any kind.  IBM shall not be liable for any damages
  14.  *  arising out of your use of the sample code, even if they have been
  15.  *  advised of the possibility of   such damages.
  16.  *
  17. \*==============================================================*/
  18.  
  19.  
  20. #define INCL_DOSPROCESS
  21. #define INCL_DOSSEMAPHORES
  22. #define INCL_DOSQUEUES
  23. #define INCL_DOSMEMMGR
  24. #define INCL_WINMESSAGEMGR
  25. #define INCL_DOSERRORS
  26.  
  27. #include <os2.h>
  28.  
  29. #include <stdlib.h>
  30. #include <stddef.h>
  31. #include <string.h>
  32. #include <stdio.h>
  33.  
  34. #include "svrqglbl.h"
  35. #include "svrqmain.h"
  36. #include "queue.h"
  37. #include "svrqxtrn.h"
  38.  
  39.  
  40. #define STACKSIZE    8192
  41.  
  42. static HQUEUE hqQ;
  43.  
  44. HQUEUE InitQ(PID  pidParent,   /* proccess that will send WM_CLOSE command */
  45.              HWND hwndParent)  /* window to post to */
  46. {
  47.    ULONG              ulErrorMsg;
  48.    BOOL               fError=FALSE;
  49.    static THREADPARAM tpThrdParm;
  50.  
  51.    if (!DosCreateQueue(&hqQ,
  52.                        QUE_PRIORITY,
  53.                        "\\queues\\" Q_NAME))
  54.    {
  55.       tpThrdParm.pidParent = pidParent;
  56.       tpThrdParm.hwndParent = hwndParent;
  57.  
  58.       if( _beginthread(ReadQMessage,
  59.                            NULL,
  60.                            STACKSIZE,
  61.                            (VOID *)&tpThrdParm) == -1 )
  62.       {
  63.          ulErrorMsg = IDMSG_CREATE_THREAD_FAILED;
  64.          fError = TRUE;
  65.       }
  66.    }
  67.  
  68.    else
  69.    {
  70.       ulErrorMsg = IDMSG_CREATE_Q_FAILED;
  71.       fError = TRUE;
  72.    }
  73.  
  74.    if (fError)
  75.    {
  76.       MessageBox(HWND_DESKTOP,
  77.                  ulErrorMsg,
  78.                  MB_OK | MB_ICONEXCLAMATION | MB_APPLMODAL,
  79.                  TRUE);
  80.       return(0);
  81.    }
  82.  
  83.    else
  84.    {
  85.       return (hqQ);
  86.    }
  87. }
  88.  
  89.  
  90. #define  MAXBUFF  10
  91.  
  92. VOID ReadQMessage(VOID *tpThrdParm)
  93. {
  94.    PVOID        pvdQMsg;
  95.    BYTE         bPriority;
  96.    REQUESTDATA  reqQdata;
  97.    ULONG        cbRead;
  98.    BOOL         fClose=FALSE, fError=FALSE;
  99.    int          index=0;
  100.    QDATA        qdataBuffer[MAXBUFF];
  101.    PID          pidParent;
  102.    HWND         hwndParent;
  103.  
  104.    pidParent = ((THREADPARAM *)tpThrdParm)->pidParent;
  105.    hwndParent = ((THREADPARAM *)tpThrdParm)->hwndParent;
  106.  
  107.    while (!fClose && !fError)
  108.    {
  109.       if (!(DosReadQueue(hqQ,             /* queue handle                               */
  110.                          &reqQdata,       /* queue result, incl. process id and request */
  111.                          &cbRead,         /* count of bytes in element                  */
  112.                 (VOID **)&pvdQMsg,        /* address of element                         */
  113.                          0,               /* element number to read                     */
  114.                          DCWW_WAIT,       /* wait for element                           */
  115.                          &bPriority,      /* received element priority                  */
  116.                          (HEV) NULL))) {  /* semaphore handle (not used)                */
  117.  
  118.          /* if parent process sends message to die... */
  119.  
  120.          if (pidParent == reqQdata.pid && WM_CLOSE == reqQdata.ulData)
  121.          {
  122.             fClose = TRUE;
  123.          }
  124.  
  125.          /* copy the string to some internal buffer, and post a message to the window
  126.           * to copy the new text to its own buffer and do what it will.
  127.           */
  128.          else
  129.          {
  130.             /* format the message */
  131.             switch(reqQdata.ulData)
  132.             {
  133.                case Q_MSG_DATE:
  134.                   sprintf(qdataBuffer[index].pszMsg,
  135.                           "Date -- Day: %d Month: %d Year: %d",
  136.                           ((Q_DATE *)pvdQMsg)->usDay,
  137.                           ((Q_DATE *)pvdQMsg)->usMonth,
  138.                           ((Q_DATE *)pvdQMsg)->usYear);
  139.                   break;
  140.  
  141.                case Q_MSG_TEXT:
  142.                default:
  143.                   strncpy(qdataBuffer[index].pszMsg, (CHAR *)pvdQMsg, cbRead);
  144.                   break;
  145.             }
  146.  
  147.             qdataBuffer[index].pidProcess = reqQdata.pid;
  148.             qdataBuffer[index].usPriority = bPriority;
  149.             WinPostMsg(hwndParent, WM_NEWQMSG, MPFROMP(&(qdataBuffer[index])), MPVOID);
  150.             index = (index < MAXBUFF-1) ? index+1 : 0;
  151.  
  152.          /* after posting the message, unmark the memory and start the loop again
  153.           *
  154.           * CAVEAT: We are getting the offset by zeroing out the lower 16 bits
  155.           * of the block offset.  This can ONLY be done if the memory allocated
  156.           * is less than 64K.  Otherwise, the client must somehow (in the queue
  157.           * message would probably be a good idea) pass the offset to the server.
  158.           *
  159.           */
  160.             DosSubFreeMem((VOID *)((ULONG)pvdQMsg & ~(ULONG)0xFFFF), pvdQMsg, cbRead);
  161. /*            DosSubFreeMem(MAKEP(SELECTOROF(pvdQMsg), 0), pvdQMsg, cbRead);*/
  162.          }
  163.       }
  164.  
  165.       else
  166.       {
  167.          WinPostMsg(hwndMain, WM_MSG, MPFROMSHORT(IDMSG_READ_Q_ERROR), MPVOID);
  168.          fError = TRUE;
  169.       }
  170.    }
  171.  
  172.    DosCloseQueue(hqQ);
  173.    fThrdsDead=TRUE;
  174. }
  175.