Friday, June 10, 2011

Foray into Photon - Part 5 Operation Dispatcher

So Photon has some really nice features built into it for us to use. They have pre-built encryption, the Operation Dispatcher, and Operations and Events to base our code off of. This segment we will be covering the Operation Dispatcher. Its job is to use reflection to determine what functions get which operations. This reduces the amount of work compared to what we had to do with SmartFoxServer. At first it seems like the same thing. SmartFoxServer allows us to add handlers which then pass us the user and the datapacket. This is exactly the same thing Photon does except that we only have to add the class that holds each of the handler functions instead of adding a new handlers for each message. Lets take a look. All of this code will be going into AegisBornPeer. First we add 2 new properties to our class:


        private static readonly OperationMethodInfoCache Operations = new OperationMethodInfoCache();


        private readonly OperationDispatcher _dispatcher;

The cache provides all the function lookup data and the dispatcher makes the calls and passes the information necessary.

Next we need to add a static constructor that will instantiate our cache and add all the functions that belong to each class that will handle our operations:

        static AegisBornPeer()
        {
            Operations = new OperationMethodInfoCache();

            try
            {
                Operations.RegisterOperations(typeof(AegisBornPeer));
            }
            catch (Exception e)
            {
                ErrorHandler.OnUnexpectedException(Operations, e);
            }
        }

The cool thing about this setup is that you can create handlers in groups. I can create a class called CraftingHandler and put all crafting operations in it. We'll have to explore this option later.

Next we will add a the dispatcher creation code to the AegisBornPeer(PhotonPeer) constructor immediately above the SetCurrentOperationHandler call:

            _dispatcher = new OperationDispatcher(Operations, this);

The last thing we need to do is call the dispatcher when we receive an Operation. So drop down to our "We get signal" call and add the following above it:

            OperationResponse result;
            if (_dispatcher.DispatchOperationRequest(peer, operationRequest, out result))
            {
                return result;
            }

What this does is attempt to find a function that has our operation code to handle the operation. if it doesn't find it, it will call our "We get signal" return. If it does handle it, it returns the result of the function call. Thats all there is to it. Now we can start adding operations and handling them without adding much more code.

So that's it for this segment. The next segment we are going to take a look at how the demo handles connection states. This is a piece of the client to handle specific things and will lead us into the server version of the same thing.

2 comments:

brodgers said...

Awesome tutorial. I got stuck where you wanted to insert into the "we got signal" area. Where exactly is that?

Unknown said...

The "we got signal" area is in the AegisBornPeer OnOperationRequest. 3 lines from the bottom.