Main Page   Packages   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Search  

C:/temp/src/j2k/QNX4/MsgThread.cpp

Go to the documentation of this file.
00001 #include "MsgThread.hh"
00002 
00003 // Default Constructor
00004 MsgThread::MsgThread() : Thread() {}
00005 
00006 // Increase this if a message is bigger than 1 KB !
00007 #define MSG_BUFFER_MAX  1024
00008 
00009 // Runs the message loop and dispatches to the proper child function.
00010 void MsgThread::Run()
00011 {
00012    unsigned char   buf[ MSG_BUFFER_MAX ];
00013    MsgData*    msg;
00014    MsgData*    feedback;
00015    pid_t    rpid;
00016 
00017    while( 1 ) {
00018 
00019           if( (  rpid = Receive( 0, buf, sizeof(buf) )  )  < 0  ) {
00020             continue;           // Try again
00021 
00022        if ( (*buf) == MSG_KILL_THREAD ) 
00023       break;
00024    
00025        if ( (*buf) == MSG_DATA_ID ) {  // Got a good message of type (MsgData*)
00026 
00027           // convert message
00028              msg = (MsgData*)buf;
00029    
00030                // Destroy MsgThread, if index equal MSG_KILL_THREAD
00031           if( msg->index == MSG_KILL_THREAD )
00032          break;
00033 
00034 
00035           // Dispatch message into the children array of local function
00036           feedback = Dispatch( msg, this, rpid );
00037 
00038                Reply( rpid, &feedback, sizeof(feedback) );
00039 
00040        } else {  // Not a known type message, so reply the same message
00041 
00042                Reply( rpid, buf, sizeof(buf) );
00043        }
00044     }
00045 
00046     // Send confirmation for destruction
00047     Reply( rpid, NULL, 0 );
00048 }
00049 
00050 // Send a message to this MsgThread
00051 void MsgThread::SendMsg( void* buffer, void* reply )
00052 {
00053     if ( pid < 0 ) { return; }
00054     Send( pid, buffer, reply, sizeof(buffer), sizeof(reply) );
00055 }
00056 
00057 // Stops the current thread (if running)
00058 void MsgThread::Stop()
00059 {
00060 //  static const unsigned char Kill_Msg[1] = MSG_KILL_THREAD;
00061 
00062     MsgData* Kill_Msg = new MsgData();
00063 
00064     if ( pid < 0 ) { return; }
00065 
00066     // Try to send  MSG_KILL_THREAD  message...
00067     Send( pid, Kill_Msg, NULL, sizeof( Kill_Msg ), 0 );
00068 
00069     _endthread();
00070 
00071     pid = -1;
00072 }

Generated on Sun Oct 14 18:46:36 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001