This project is read-only.

How are MsgHandler objects used?

Oct 19, 2012 at 1:05 PM

Could someone tell me how MsgHandler objects are used? Specifically, when are their HandleMsg and Send methods called? Is Send called whenever Client.Send() or Server.Send() is called? I read the source code,  but I couldn't figure out where they fit into the call graph.

Also, why do Clients need/have Handlers? As Servers don't have Send() methods i.e. Servers can't send messages.

Oct 19, 2012 at 1:43 PM
Edited Oct 19, 2012 at 1:45 PM

ok the message handlers work like WCF contracts

you can ask the server for something and the server then responds to the client

 

so yes the server does call a .Send() to the client but it is initiated from the client

like for example take the default message handlers the client asks for the server version to which the server responds

the reason the client has a message handler is to know how to handle the messages coming back from the server.

 

so when the server responds to the Version() call with its own .Send() then the client needs to know what data it just received and what to do with it.

 

does that make sense to you?

 

also the handle message is called everytime the server or client receives a message that message needs to be handled some how so that is why it is called

Oct 19, 2012 at 2:06 PM

you also after creating a message handler you have to add the message handler to the server and the client by calling client.Handlers.add or server.handlers.add

so that they will get called when you receive a message

Oct 19, 2012 at 2:12 PM
Edited Oct 19, 2012 at 2:15 PM

custom behavior can be implemented by creating an object that implements the IMsgHandler interface and then added that object to the list of handlers on both the client and the server side. but calling the .Handlers.Add() funtion. custom behaviour then lives inside the new class you created that implemented the IMsgHandler interface. how this works is there is a call in the interface called HandleMessage and that method is called everytime the client or server receives a message. so you can do you custom behavior handlers in there what to do when that message is received.

you could have a client handler and a server handler or they could both be the same handler. it all depends on how you want to implement your handlers. it was done like this so that you can easily extend what you can send and receive without having to rewrite or change the way messages are sent.

 

The IMsgHandler interface handles that for you with the HandleMessage call. when the server or client gets a message for the other party the HandleMessage method in all the .Handlers list is called so you can have as many message handlers as you wnat to do many different things.

 

Oct 19, 2012 at 2:18 PM

you could also extend the basehandler you can take a look at the BaseHandler.cs file to see the base message handler

also the default handlers are example handlers you can model your handlers to work in that way or not or just extend the basehandler class