Application of MFC message ing mechanism in Linux C ++ programming environment)

Source: Internet
Author: User

#IfndefMsg_constant_h
#DefineMsg_constant_h

#DefineMax_msg_parta_size 2048/* Message Parameter size */
#DefineMax_queue_length 3000/* Maximum queue length */

#DefineEventmsg_quit 100/* Exit */

# Define Rw_success 0
# Define Empty_que 1
# Define Rw_error_que - 1
# Define Full_que - 2
# Define Lock_error - 3
# Define Unlock_error - 4
# Define Realese_lock_error - 5
# Define Un_ini - 6

#DefinePer_buff_length 1000/* Message input buffer size (entries )*/

/*!
*
* For macro definitions of items in the message ing segment, refer to the MFC message ing mechanism.
*/

/* When calling the message processing function, add this, which must be a member function of the message machine */
# Define Msg_map_fun ( Msg_code , Msg_action_fun ) Case Msg_code : This - > Msg_action_fun ( MSG ) ; Break ;

/* Implement the pointer to forward messages to other message processing objects, which are used in cases where messages need to be processed for a long time. The receiving object must be a subclass of msg_machine */
# Define Msg_map_obj ( Msg_code , Msg_action_obj ) Case Msg_code : Sendmessage ( MSG , Msg_action_obj ) ; Break ;

/*! Message ing macro
*
* Begin_msg_map is the macro that defines the start of the message ing segment.
* The message ing segment must appear in the class body, not the implementation part.
* The format is as follows:
* Begin_msg_map
* Msg_map_fun (message code, member function that must return void and have a MSG type parameter)
* Msg_map_obj (message code, message machine class instance)
* End_msg_map
*/

# Define Begin_msg_map Virtual Void Dispatch ( Struct MSG msg ) \
{ \
\
Unsigned Int Msg_code ; \
Msg_code = MSG . Msg_code ; \
Switch ( Msg_code ) \
{

/*!
*
* Macro that defines the end of the message ing segment
*/
# Define End_msg_map ( Base ) Default : \
Base : : Dispatch ( MSG) ; \
Break ; \
} \
}

#Endif

  1. # Ifndef message_struct_h
  2. # Define message_struct_h
  3. # Ifdef _ cplusplus
  4. Extern "C"
  5. {
  6. # Include <pthread. h>
  7. # Include <semaphore. h>
  8. # Include <stdio. h>
  9. # Include <stdlib. h>
  10. # Include <string. h>
  11. # Include <ctype. h>
  12. }
  13. # Endif // _ cplusplus
  14. # Include "msg_macro.h"
  15. Typedef unsigned char u_char;
  16. Struct msg
  17. {
  18. Unsigned int msg_code; // message code
  19. U_char msg_para [max_msg_para_size]; // Message Parameter
  20. };
  21. Class msg_queue
  22. {
  23. Public:
  24. Msg_queue () {initialized = false ;};
  25. ~ Msg_queue ();
  26. Int initialize ();
  27. Int gethead (struct MSG * p_msg );
  28. Int addtail (struct MSG * p_msg );
  29. PRIVATE:
  30. Pthread_mutex_t mutex_wr;
  31. Bool initialized;
  32. Int msg_count;
  33. Int head;
  34. Int tail;
  35. Struct MSG msg_array [max_queue_length];
  36. };
  37. # Endif // message_struct_h

CopyCode

  1. # Include "MSG. H"
  2. Extern "C"
  3. {
  4. # Include <error. h>
  5. # Include <errno. h>
  6. }
  7. Int msg_queue: Initialize ()
  8. {
  9. Int error;
  10. If (initialized) return 0;
  11. Error = pthread_mutex_init (& mutex_wr, null );
  12. Switch (error ){
  13. Case eagain: initialized = false; Return-1;
  14. Case enomem: initialized = false; Return-2;
  15. Case eperm: initialized = false; Return-3;
  16. }
  17. Initialized = true;
  18. Msg_count = 0;
  19. Head = 0;
  20. Tail = 0;
  21. Memset (msg_array, 0, sizeof (msg_array ));
  22. Return 0;
  23. }
  24. Int msg_queue: gethead (struct MSG * p_msg)
  25. {
  26. Int result;
  27. Int error;
  28. If (! Initialized) return un_ini;
  29. If (p_msg = NULL) return rw_error_que;
  30. If (error = pthread_mutex_lock (& mutex_wr ))
  31. Return-3; // lock retrieval failed
  32. If (msg_count> 0)
  33. {
  34. Memcpy (void *) p_msg, (void *) & msg_array [head], sizeof (struct MSG ));
  35. Memset (void *) & msg_array [head], 0, sizeof (struct MSG ));
  36. Head ++;
  37. If (Head = max_queue_length) Head = 0;
  38. Msg_count --;
  39. Result = rw_success;
  40. }
  41. Else
  42. {
  43. P_msg-> msg_code = empty_que;
  44. Result = empty_que;
  45. }
  46. If (error = pthread_mutex_unlock (& mutex_wr ))
  47. Return-4; // unlock failed
  48. Return result;
  49. }
  50. Int msg_queue: addtail (struct MSG * p_msg)
  51. {
  52. Int ilock;
  53. Int result;
  54. Int error;
  55. If (p_msg = NULL) return rw_error_que;
  56. If (! Initialized) return un_ini;
  57. If (error = pthread_mutex_lock (& mutex_wr ))
  58. Return-3;
  59. If (msg_count <max_queue_length)
  60. {
  61. Memcpy (void *) & msg_array [tail], (void *) p_msg, sizeof (struct MSG ));
  62. Tail ++;
  63. If (tail = max_queue_length)
  64. Tail = 0;
  65. Msg_count ++;
  66. Result = rw_success;
  67. }
  68. Else
  69. Result = full_que;
  70. If (error = pthread_mutex_unlock (& mutex_wr ))
  71. Result =-4;
  72. Return result;
  73. }
  74. Msg_queue ::~ Msg_queue ()
  75. {
  76. Pthread_mutex_destroy (& mutex_wr );
  77. }

Copy code

  1. # Ifndef msgmachine_h
  2. # Define msgmachine_h
  3. # Include "MSG. H"
  4. # Include "msg_macro.h"
  5. # Include "msg_input_buf.h"
  6. # Ifdef _ cplusplus
  7. Extern "C"
  8. {
  9. # Include <stdio. h>
  10. # Include <stdlib. h>
  11. # Include <string. h>
  12. # Include <ctype. h>
  13. # Include <signal. h>
  14. # Include <unistd. h>
  15. }
  16. # Endif // _ cplusplus
  17. //-----------------------------------------------------------------------------
  18. Class msgmachine
  19. {
  20. Public:
  21. Bool cmd_run;
  22. Msgmachine ();
  23. Virtual ~ Msgmachine ();
  24. Void msg_deal_loop ();
  25. /* Read messages from each buffer zone to the message queue and asynchronously check whether a buffer zone is readable */
  26. Int readinputbuf (inputbuff * pbuff );
  27. /*!
  28. * Start the message loop processing thread.
  29. * If the word class is changed to execute, the execute of the parent class must be called to start the message processing cycle.
  30. */
  31. Virtual void dispatch (struct MSG)
  32. {
  33. Unsigned int msg_code;
  34. Msg_code = msg. msg_code;
  35. Switch (msg_code)
  36. {
  37. Default: break;
  38. }
  39. }
  40. PRIVATE:
  41. Bool detecteffective;
  42. Msg_queue * pmsg_que;
  43. };
  44. # Endif // msgmachine_h

Copy code

  1. # Include "msg_machine.h"
  2. Msgmachine ::~ Msgmachine ()
  3. {
  4. Delete pmsg_que;
  5. }
  6. Msgmachine: msgmachine ()
  7. {
  8. Pmsg_que = new msg_queue;
  9. If (pmsg_que-> initialize () =-1)
  10. {
  11. Delete pmsg_que;
  12. // Printf ("initialize Message Queue error! \ N ");
  13. // U_log ("initialize Message Queue error! \ N ");
  14. Exit (-1 );
  15. }
  16. Detecteffective = true;
  17. }
  18. Void msgmachine: msg_deal_loop ()
  19. {
  20. Struct MSG;
  21. While (cmd_run)
  22. {
  23. If (pmsg_que-> gethead (& MSG) = 0)
  24. {
  25. If (msg. msg_code = eventmsg_quit)
  26. Break;
  27. Dispatch (MSG );
  28. }
  29. Else
  30. Continue;
  31. }
  32. }
  33. Int msgmachine: readinputbuf (inputbuff * pbuff)
  34. {
  35. Struct MSG;
  36. If (pbuff-> Read (& MSG) = 0)
  37. {
  38. If (pmsg_que-> addtail (& MSG) = rw_success)
  39. {
  40. Return 0;
  41. }
  42. Else
  43. Return-1;
  44. }
  45. Else
  46. {
  47. Return-1;
  48. }
  49. }

Copy code

These are some of our projects (C ++ Development Implementation in RedHat Linux and interfaces based on UDP messages of a Portal Server ).
For more information about the daemon, see the message ing mechanism of MFC. Currently, the above Code has been implemented.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.