[muscle] Re: muscle client

  • From: "Jeremy Friesner" <jaf@xxxxxxxxxxxx>
  • To: muscle@xxxxxxxxxxxxx
  • Date: Fri, 27 May 2005 09:27:20 PDT (-0700)

Hi Wim,

MessageReceivedFromOwner() is called by the network I/O thread, whenever your 
main thread calls SendMessageToSessions().  It is involved with messages going 
to the server, not messages coming
back from the server, so it is not the method you want to use.

Typically a (Something)MessageTransceiverThread is used like this:

1) Your code creates a (Something)MessageTransceiverThread object, and calls 
StartInternalThread() and AddNewConnectSession() on it to connect to the server.
2) When your code wants to send a Message to the server, it calls 
SendMessageToSessions() on the object.
3) When the object receives a Message from the server, it notifies your code in 
the main thread that a Message has been received, and your code will then 
respond to that notification by running some code like this:

   uint32 code;
   MessageRef msgRef;
   while(myMessageTransceiverThread.GetNextEventFromInternalThread(code, 
&msgRef) =3D=3D B=5FNO=5FERROR)
   {
      switch(code)
      {
         case MTT=5FEVENT=5FINCOMING=5FMESSAGE:
            printf("Hey, I received a Message from the server!  The Message 
is:\n");
            if (msgRef()) msgRef()->PrintToStream();
         break;

         case MTT=5FEVENT=5FSESSION=5FCONNECTED:     
            printf("The TCP connection to the server has been connected!\n");
         break;

         case MTT=5FEVENT=5FSESSION=5FDISCONNECTED:  
            printf("The TCP connection to the server has been disconnected!\n");
         break;
      }
   }

So the only question is:  how can the MessageTransceiverThread object notify 
your main
thread that it is time to execute the above code=3F  If you were using 
Microsoft's Win32 API directly,
then you could use the Win32MessageTransceiverThread class to do this; it knows 
how to
notify a Win32 GUI thread using either the SetEvent() or PostThreadMessage() 
Win32 API
functions.

However, if you are using the wxWidgets toolkit instead of the Win32 API 
directly, you'll 
probably want/need to use a wxwidgets specific thread-signalling method 
instead.  I haven't
used wxWidgets myself (I've always used Qt instead), but from Googling around a 
bit, it
appears that the way to do this would be to have the MessageTransceiverThread 
object
call wxPostEvent(), and then your main GUI thread would respond to the received 
event
by executing the code shown above.

In order to get your MessageTransceiverThread class to do this, you'll want to
use your own wxWindowsMessageTransceiverThread subclass, instead of
using Win32MessageTransceiverThread.  It might look something like this:

enum {CHECK=5FFOR=5FNETWORK=5FEVENTS =3D 123456};  // some event code that your 
main thead can recognize

class wxWindowsMessageTransceiverThread : public MessageTransceiverThread
{
public:
   wxWindowsMessageTransceiverThread(wxEvtHandler * target) : =5Ftarget(target) 
{/* empty */}

   /** Override SignalOwner() to notify the main thread the wxWindows way! */
   virtual void SignalOwner()
   {
      wxEvent evt(CHECK=5FFOR=5FNETWORK=5FEVENTS);
      ::wxPostEvent(=5Ftarget, evt);
   }

private:
   wxEvtHandler * =5Ftarget;
};

If you do it as shown above, then there shouldn't be any problem with cleanup;
just remember to call Reset() on your wxWindowsMessageTransceiverThread object
before you delete it.

Hope that helps,

Jeremy

> I was thinking about a system of using the client of muscle  in the
> following way and I would ask if this would have a chance to work.
> If I subclass Win32MessageTransceiverThread to
> MyWin32MessageTransceiverThread and I override the virtual function
> MessageReceivedFromOwner(MessageRef msgRef, uint32 numLeft) I have a
> function that  will be called every time when a new message arrives, my
> first idea was to use this message directly, but I think there will be a
> problem in cleaning up the message afterwards, so maybe it is possible
> to call GetNextEventFromInternalThread in this overloaded function and
> do the things I want to do ( in fact create a wxwidgets event).
> Is it correct what I'm thinking  =3F  



Other related posts: