home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / msj / msjv3_3 / dllcode / dll_chat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-02  |  4.3 KB  |  160 lines

  1. /********************************************************************
  2. *    DLL_CHAT.C  - A demonstration program using a demo DLL
  3. *
  4. *    (C) 1988, By Ross M. Greenberg for Microsoft Systems Journal
  5. *
  6. *    This is the main body of the CHAT program, interfacing with
  7. *    and calling the DLL as if it were a bunch of routines 
  8. *    available with a far call:  which it is!
  9. *
  10. *    Compile with:
  11. *    cl /c chat.c
  12. *    link chat,chat,,slibce+doscalls,chat
  13. *
  14. *    Remember: move the DLL itself into your DLL library directory
  15. *
  16. ********************************************************************/
  17.  
  18. #include    <stdio.h>
  19. #include    <stdlib.h>
  20. #include    "chat.h"
  21.  
  22. #define    TRUE    1
  23. #define    FALSE    0
  24. #define    OK    TRUE
  25. #define    NOT_OK    FALSE
  26.  
  27. #define    MAX_MSG        100
  28. #define    MAX_MSG_LEN    80
  29. #define    NULLP        (void *)NULL
  30.  
  31. /* The following OS/2 system calls are made in this module: */
  32.  
  33. extern far pascal dosexitlist();
  34. extern far pascal dossleep();
  35.  
  36.  
  37. /* The following DLL system calls are made in this module: */
  38.  
  39. extern far _loadds pascal login();
  40. extern far _loadds pascal logout();
  41. extern far _loadds pascal get_msg_cnt();
  42. extern far _loadds pascal get_msg();
  43. extern far _loadds pascal send_msg();
  44.  
  45.  
  46. /********************************************************************
  47. *    This is where the messages are stored, once received and 
  48. *    formatted. This could probably be replaced easily with a call 
  49. *    to calloc(), but then we wouldn't have to block on being a 
  50. *    background process
  51. ********************************************************************/
  52.  
  53. char    msg_array[MAX_MSG + 1][MAX_MSG_LEN];
  54.  
  55. /* Must be global so that before_death() can access it to logout */
  56.  
  57. int    my_id = 0;
  58.  
  59. #define    MAX_SLEEP    2
  60.  
  61. /********************************************************************
  62. *    before_death()
  63. *
  64. *    Called the coins are lowered onto the eyes of this invokation 
  65. *    of CHAT. Any exit will cause this routine to be called. After 
  66. *    this routine calls the DLL logout procedure, it removes calls 
  67. *    the next exitroutine in the exit list.
  68. ********************************************************************/
  69.  
  70. void    far    before_death()
  71. {
  72.     logout(my_id);
  73.     dosexitlist(3, before_death);
  74. }
  75.  
  76. /********************************************************************
  77. *    main()
  78. *
  79. *    After logging in (which returns a unique login id), the 
  80. *    before_death() routine is added onto the exitlist.  Then the 
  81. *    main loop:
  82. *
  83. *    If there are any messages, read them into out memory buffer 
  84. *    (provided there is room) and kick up the count.  After 
  85. *    retrieving all the msgs which will fit, call the display 
  86. *    routine for each msg. Zero the count of messages when done.
  87. *
  88. *    Every couple of seconds, sleep for a little while (as if a 
  89. *    human typing on a keyboard), then send the message to all 
  90. *    other members of CHAT.
  91. *
  92. *    CHAT can only be exited (in its current form) with a 
  93. *    control-C or error condition.
  94. ********************************************************************/
  95.  
  96. main()
  97. {
  98. int    msg_cnt = 0;
  99. int    msg_id = 0;
  100. int    lp_cnt;
  101. char    tmp_buf[MAX_MSG_LEN];
  102. int    m_cnt;
  103.  
  104.     printf("Logged into CHAT as user:%d\n", my_id = login());
  105.  
  106.     dosexitlist(1, before_death);
  107.  
  108.     while    (TRUE)
  109.     {
  110.         for (m_cnt = get_msg_cnt(my_id); m_cnt ; m_cnt--)
  111.         {
  112.             get_msg(my_id, (char far *)tmp_buf);
  113.              if    (msg_cnt <= MAX_MSG)
  114.                 sprintf(msg_array[msg_cnt++],"(%d)%s",my_id,tmp_buf);
  115.     }
  116.  
  117.         if    (ok_to_disp())
  118.         {
  119.             for (lp_cnt = 0 ; lp_cnt < msg_cnt; lp_cnt++)
  120.                 disp_msg(msg_array[lp_cnt]);
  121.             if    (msg_cnt > MAX_MSG)
  122.                 disp_msg("Looks like you might have lost some") 
  123.                 disp_msg(" messages while you were away\n");
  124.             msg_cnt = NULL;
  125.         }
  126.  
  127.         if    (rand() % (my_id + 1))
  128.         {
  129.             dossleep((long)(rand() % MAX_SLEEP) * 1000L);
  130.             sprintf(tmp_buf, "Test message #%d from Session #%d\n", 
  131.                     msg_id++, my_id);
  132.             if    (send_msg(my_id, (char far *)tmp_buf) == NOT_OK)
  133.                 printf("?Can't send a message....\n");
  134.         }
  135.     }
  136. }
  137.  
  138. disp_msg(ptr)
  139. char    *ptr;
  140. {
  141.     printf("%s", ptr);
  142.     fflush(stdout);
  143. }
  144.  
  145. extern far pascal dosgetinfoseg();
  146.  
  147. ok_to_disp()
  148. {
  149. struct gdtinfoarea far *gdt;
  150. struct ldtinfoarea far *ldt;
  151. unsigned    gseg;
  152. unsigned    lseg;
  153.  
  154.     dosgetinfoseg((char far *)&gseg, (char far *)&lseg);
  155.     gdt = (struct gdtinfoarea far *)((long)gseg << 16);
  156.     ldt = (struct ldtinfoarea far *)((long)lseg << 16);
  157.  
  158.     return( gdt->foreground_process_id == ldt->parent_pid);
  159. }
  160.