home *** CD-ROM | disk | FTP | other *** search
/ The Developer Connection…ice Driver Kit for OS/2 3 / DEV3-D1.ISO / source / devnews / vol2 / sample3 / os2thrd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-05  |  10.8 KB  |  312 lines

  1. /******************************************************************************/
  2. /* The following code fragments were published as part of                     */
  3. /* The Developer Connection News  Volume II                                   */
  4. /*                                                                            */
  5. /* COPYRIGHT:                                                                 */
  6. /* ----------                                                                 */
  7. /* Copyright (C) International Business Machines Corp., 1993.                 */
  8. /*                                                                            */
  9. /* DISCLAIMER OF WARRANTIES:                                                  */
  10. /* -------------------------                                                  */
  11. /* The following [enclosed] code is sample code created by IBM                */
  12. /* Corporation.  This sample code is not part of any standard IBM product     */
  13. /* and is provided to you solely for the purpose of assisting you in the      */
  14. /* development of your applications.  The code is provided "AS IS",           */
  15. /* without warranty of any kind.  IBM shall not be liable for any damages     */
  16. /* arising out of your use of the sample code, even if they have been         */
  17. /* advised of the possibility of such damages.                                */
  18. /*                                                                            */
  19. /******************************************************************************/
  20.  
  21.  
  22. #define INCL_DOSMVDM
  23. #define INCL_BASE
  24. #define INCL_DOSPROCESS
  25. #include <os2.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include "sty_main.h"
  29. #include "sty_xtrn.h"
  30. #include "sty_dlg.h"
  31. #include "common.h"
  32.  
  33.  
  34. /*
  35.  *Any data types that are private to this module
  36.  */
  37.  
  38.  
  39. /*
  40.  *any data declarations that are private to this module
  41.  */
  42. ULONG  HandleToVDD;
  43. MESSAGEBUFFER MessageBuffer;
  44.  
  45. /****************************************************************************/
  46. /*The following code fragments were enclosed due to erros in The Developer  */
  47. /*Connection News that we were not able to correct before press time.       */
  48. /*The following routines will be expanded and incoporated into the final    */
  49. /*release of the source code that will be released in a future release of   */
  50. /*The Developer Connection News - David Kenner                              */
  51. /****************************************************************************/
  52. /****************************************************************************/
  53. /*OpenVirtQueue                                                             */
  54. /*                                                                          */
  55. /*                                                                          */
  56. /*A service routine that will allow us to do our initilization with the vdd */
  57. /****************************************************************************/
  58. APIRET OpenVirtQueue(VOID)
  59. {
  60.      APIRET apiRet;
  61.  
  62.      /*
  63.       *see if our Vdd is alive and if so get
  64.       *a handle that we may use later
  65.       */
  66.      if(apiRet = DosOpenVDD(VDDNAME,&HandleToVDD) )
  67.      {
  68.           return(apiRet);
  69.      }
  70.  
  71.      /*
  72.       *Call into our Vdd so we perform any initialization
  73.       *we need to do at this time.
  74.       */
  75.      if(apiRet  = DosRequestVDD(HandleToVDD,
  76.                                 0,
  77.                                 INIT_COMMAND,
  78.                                 0,
  79.                                 NULL,
  80.                                 0,
  81.                                 NULL) )
  82.      {
  83.           return(apiRet);
  84.      }
  85.      return(apiRet);
  86. }
  87. /****************************************************************************/
  88. /*WriteVirtQueue                                                            */
  89. /*                                                                          */
  90. /*                                                                          */
  91. /*A service routine that will allow us to send our message data to the  vdd */
  92. /*                                                                          */
  93. /*                                                                          */
  94. /*                                                                          */
  95. /****************************************************************************/
  96.  
  97. APIRET WriteVirtQueue(PVOID pvMessageToSend,
  98.                       ULONG ulCount,
  99.                       SGID  SessionId)
  100. {
  101.      APIRET apiRet;
  102.      MESSAGEBUFFER MessageBuffer;
  103.      PTIB pptib;
  104.      PPIB ppib;
  105.  
  106.      memset(&MessageBuffer,0,sizeof(MessageBuffer));
  107.      DosGetInfoBlocks(&pptib,&ppib);
  108.  
  109.      MessageBuffer.ulMessageType  = TEXT_MESSAGE;
  110.      MessageBuffer.ulBufferSize   = strlen(pvMessageToSend);
  111.      MessageBuffer.pvMessageData  = pvMessageToSend;
  112.      MessageBuffer.ulSrcSessionId = ppib->pib_ulppid;
  113.  
  114.      if(apiRet = DosRequestVDD(HandleToVDD,
  115.                                SessionId,
  116.                                POST_MESSAGE,
  117.                                ulCount,
  118.                                pvMessageToSend,
  119.                                0,
  120.                                NULL) )
  121.      {
  122.           return(apiRet);
  123.      }
  124.      return(apiRet);
  125. }
  126.  
  127. /****************************************************************************/
  128. /*ReadVirtQueue                                                             */
  129. /*                                                                          */
  130. /*                                                                          */
  131. /*This routine is set up as separate thread that will read messages,process */
  132. /*them,and then continue to block until additional messages come in.The     */
  133. /*block mechansim is accomplished through the semaphore in our VDD          */
  134. /*                                                                          */
  135. /*Note:                                                                     */
  136. /*There is no error return. All error messages are handled before the       */
  137. /*thread terminates                                                         */
  138. /*                                                                          */
  139. /****************************************************************************/
  140. VOID ReadVirtQueue(VOID)
  141. {
  142.      APIRET apiRet;
  143.      PVOID  pvMessage;
  144.      CHAR   CommandToSend[SIZ_COMMAND_BUF];
  145.  
  146.  
  147.  
  148.      do
  149.      {
  150.           /*
  151.            *block until we get any message
  152.            *data
  153.            */
  154.           if(apiRet = DosRequestVDD(HandleToVDD,
  155.                                     0,
  156.                                     READ_QUEUE,
  157.                                     strlen(CommandToSend),
  158.                                     CommandToSend,
  159.                                     sizeof(MessageBuffer),
  160.                                     &MessageBuffer) )
  161.           {
  162.                break;
  163.           }
  164.           /*
  165.            *do all of our message processing
  166.            */
  167.            apiRet = ProcessMessageData(MessageBuffer);
  168.  
  169.      }while(!apiRet);
  170.      /*
  171.       *handle any error that was  encountered
  172.       *during our polling
  173.       */
  174.  
  175.  
  176.      /*
  177.       *do any other clean up we need to do
  178.       *and get out
  179.       */
  180.  
  181.      _endthread();
  182. }
  183.  
  184. /****************************************************************************/
  185. /*Provide a stub to keep the linker from complaining                        */
  186. /*                                                                          */
  187. /*                                                                          */
  188. /*This is currently just a stub routine                                     */
  189. /****************************************************************************/
  190. #if 0
  191. INT main(void)
  192. {
  193.      APIRET apiRet;
  194.  
  195.  
  196.      do
  197.      {
  198.  
  199.           if(OpenVirtQueue() )
  200.           {
  201.                break;
  202.           }
  203.           /*
  204.            *start off our read thread
  205.            */
  206.            apiRet = StartReadThread();
  207.  
  208.      }ONCE;
  209.      return(0);
  210.  
  211. }
  212. #endif
  213.  
  214. /****************************************************************************/
  215. /*Process any message data we get from our vdd                              */
  216. /*                                                                          */
  217. /*                                                                          */
  218. /****************************************************************************/
  219. APIRET ProcessMessageData(MESSAGEBUFFER MessageBuffer)
  220. {
  221.  
  222.      APIRET apiRet;
  223.      CHAR   szBuffer[512];
  224.  
  225.      do
  226.      {
  227.           memset(szBuffer,0,sizeof(szBuffer) );
  228.           sprintf(szBuffer,"MessageType:%ul,MessageSubCommand:%ul,Message:%s",
  229.                             MessageBuffer.ulMessageType,
  230.                             MessageBuffer.ulMessageSubCommand,
  231.                             (PSZ)MessageBuffer.pvMessageData);
  232.  
  233.           /*
  234.            *display the message
  235.            *to the mle
  236.            */
  237.           DisPlayInComingMessage(szBuffer);
  238.  
  239.      }ONCE;
  240.      return(apiRet);
  241. }
  242. /****************************************************************\
  243.  *
  244.  *--------------------------------------------------------------
  245.  *
  246.  *  Name:
  247.  *
  248.  *  Purpose:
  249.  *
  250.  *
  251.  *
  252.  *  Usage:
  253.  *
  254.  *  Method:
  255.  *          -
  256.  *
  257.  *          -
  258.  *          -
  259.  *
  260.  *          -
  261.  *          -
  262.  *
  263.  *  Returns:
  264.  *          1 - if sucessful execution completed
  265.  *          0 - if error
  266. \****************************************************************/
  267. APIRET StartReadThread(VOID)
  268. {
  269.     APIRET ApiRet;
  270.  
  271.  
  272.      if( (ApiRet = _beginthread(ReadVirtQueue,
  273.                           NULL,
  274.                           8192,
  275.                           NULL)) == -1)
  276.      {
  277.           return(TRUE);
  278.      }
  279.  
  280.  
  281.      return(FALSE);
  282. }
  283.  
  284. /****************************************************************************/
  285. /****************************************************************************/
  286. /*OpenVirtQueue                                                             */
  287. /*                                                                          */
  288. /*                                                                          */
  289. /*A service routine that will allow us to do our initilization with the vdd */
  290. /****************************************************************************/
  291. APIRET CloseVirtQueue(VOID)
  292. {
  293.      APIRET apiRet;
  294.  
  295.      /*
  296.       *Call into our Vdd so we perform any initialization
  297.       *we need to do at this time.
  298.       */
  299.      if(apiRet  = DosRequestVDD(HandleToVDD,
  300.                                 0,
  301.                                 TERMINATE_COMMAND,
  302.                                 0,
  303.                                 NULL,
  304.                                 0,
  305.                                 NULL) )
  306.      {
  307.           return(apiRet);
  308.      }
  309.      return(apiRet);
  310. }
  311.  
  312.