#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
-
- # Ifndef message_struct_h
-
- # Define message_struct_h
-
- # Ifdef _ cplusplus
-
- Extern "C"
-
- {
- # Include <pthread. h>
-
- # Include <semaphore. h>
-
- # Include <stdio. h>
-
- # Include <stdlib. h>
-
- # Include <string. h>
-
- # Include <ctype. h>
-
- }
-
- # Endif // _ cplusplus
-
- # Include "msg_macro.h"
-
- Typedef unsigned char u_char;
-
- Struct msg
-
- {
-
- Unsigned int msg_code; // message code
-
- U_char msg_para [max_msg_para_size]; // Message Parameter
-
- };
-
- Class msg_queue
-
- {
-
- Public:
-
- Msg_queue () {initialized = false ;};
-
- ~ Msg_queue ();
-
- Int initialize ();
-
- Int gethead (struct MSG * p_msg );
- Int addtail (struct MSG * p_msg );
-
- PRIVATE:
-
- Pthread_mutex_t mutex_wr;
-
- Bool initialized;
-
- Int msg_count;
-
- Int head;
-
- Int tail;
-
- Struct MSG msg_array [max_queue_length];
-
- };
-
- # Endif // message_struct_h
CopyCode
-
- # Include "MSG. H"
-
- Extern "C"
-
- {
-
- # Include <error. h>
-
- # Include <errno. h>
-
- }
-
- Int msg_queue: Initialize ()
-
- {
-
- Int error;
-
-
- If (initialized) return 0;
-
- Error = pthread_mutex_init (& mutex_wr, null );
-
- Switch (error ){
-
- Case eagain: initialized = false; Return-1;
-
- Case enomem: initialized = false; Return-2;
-
- Case eperm: initialized = false; Return-3;
-
- }
-
- Initialized = true;
-
- Msg_count = 0;
-
- Head = 0;
-
- Tail = 0;
-
- Memset (msg_array, 0, sizeof (msg_array ));
-
- Return 0;
-
- }
-
- Int msg_queue: gethead (struct MSG * p_msg)
-
- {
-
- Int result;
-
- Int error;
-
- If (! Initialized) return un_ini;
-
- If (p_msg = NULL) return rw_error_que;
-
- If (error = pthread_mutex_lock (& mutex_wr ))
-
- Return-3; // lock retrieval failed
-
-
- If (msg_count> 0)
-
- {
-
- Memcpy (void *) p_msg, (void *) & msg_array [head], sizeof (struct MSG ));
-
- Memset (void *) & msg_array [head], 0, sizeof (struct MSG ));
-
- Head ++;
-
- If (Head = max_queue_length) Head = 0;
-
- Msg_count --;
-
- Result = rw_success;
-
- }
-
- Else
-
- {
-
- P_msg-> msg_code = empty_que;
-
- Result = empty_que;
-
- }
-
-
- If (error = pthread_mutex_unlock (& mutex_wr ))
-
- Return-4; // unlock failed
-
- Return result;
-
-
- }
-
- Int msg_queue: addtail (struct MSG * p_msg)
-
- {
-
- Int ilock;
-
- Int result;
-
- Int error;
-
- If (p_msg = NULL) return rw_error_que;
-
- If (! Initialized) return un_ini;
-
- If (error = pthread_mutex_lock (& mutex_wr ))
-
- Return-3;
-
-
- If (msg_count <max_queue_length)
-
- {
-
- Memcpy (void *) & msg_array [tail], (void *) p_msg, sizeof (struct MSG ));
-
- Tail ++;
-
- If (tail = max_queue_length)
-
- Tail = 0;
-
- Msg_count ++;
-
- Result = rw_success;
-
- }
-
- Else
- Result = full_que;
-
-
- If (error = pthread_mutex_unlock (& mutex_wr ))
-
- Result =-4;
-
-
- Return result;
-
- }
-
- Msg_queue ::~ Msg_queue ()
-
- {
-
- Pthread_mutex_destroy (& mutex_wr );
-
- }
Copy code
-
- # Ifndef msgmachine_h
-
- # Define msgmachine_h
-
- # Include "MSG. H"
-
- # Include "msg_macro.h"
-
- # Include "msg_input_buf.h"
-
- # Ifdef _ cplusplus
-
- Extern "C"
-
- {
-
- # Include <stdio. h>
-
- # Include <stdlib. h>
-
- # Include <string. h>
-
- # Include <ctype. h>
- # Include <signal. h>
-
- # Include <unistd. h>
-
- }
-
- # Endif // _ cplusplus
-
-
- //-----------------------------------------------------------------------------
-
-
- Class msgmachine
-
- {
-
-
- Public:
-
- Bool cmd_run;
-
- Msgmachine ();
-
- Virtual ~ Msgmachine ();
-
- Void msg_deal_loop ();
-
- /* Read messages from each buffer zone to the message queue and asynchronously check whether a buffer zone is readable */
-
- Int readinputbuf (inputbuff * pbuff );
-
- /*!
-
- * Start the message loop processing thread.
-
- * If the word class is changed to execute, the execute of the parent class must be called to start the message processing cycle.
-
- */
-
-
- Virtual void dispatch (struct MSG)
- {
-
- Unsigned int msg_code;
-
- Msg_code = msg. msg_code;
-
- Switch (msg_code)
-
- {
-
- Default: break;
-
- }
-
- }
-
-
- PRIVATE:
-
- Bool detecteffective;
-
- Msg_queue * pmsg_que;
-
-
- };
-
-
- # Endif // msgmachine_h
Copy code
- # Include "msg_machine.h"
-
- Msgmachine ::~ Msgmachine ()
-
- {
-
- Delete pmsg_que;
-
- }
-
- Msgmachine: msgmachine ()
-
- {
-
- Pmsg_que = new msg_queue;
-
- If (pmsg_que-> initialize () =-1)
-
- {
-
- Delete pmsg_que;
-
- // Printf ("initialize Message Queue error! \ N ");
-
- // U_log ("initialize Message Queue error! \ N ");
-
- Exit (-1 );
-
- }
-
- Detecteffective = true;
-
- }
-
-
- Void msgmachine: msg_deal_loop ()
-
- {
-
- Struct MSG;
-
- While (cmd_run)
-
- {
-
- If (pmsg_que-> gethead (& MSG) = 0)
-
- {
-
- If (msg. msg_code = eventmsg_quit)
-
- Break;
-
- Dispatch (MSG );
-
- }
-
- Else
-
- Continue;
-
- }
-
- }
-
- Int msgmachine: readinputbuf (inputbuff * pbuff)
-
- {
-
- Struct MSG;
-
-
- If (pbuff-> Read (& MSG) = 0)
-
- {
-
- If (pmsg_que-> addtail (& MSG) = rw_success)
-
- {
-
- Return 0;
-
- }
-
- Else
- Return-1;
-
- }
-
- Else
-
- {
-
- Return-1;
-
- }
-
- }
-
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.