This project is read-only.

Tutorial

Follow in Example

1 Server Object

First write an class and define it as remote object.

    [RemoteObject()]  //Indicate this class is remote object 
    public class ExampleObject : Listener
    {
        [RemoteMethod] //indicate this method can be call from remote
        public int Add(int A, int B) //Be careful, complex type return value and parameters are not supported.
        {
            Console.WriteLine("{0} + {1} + C ={2}", A, B, A + B + C);
            return A + B + C;

        }

        [RemoteMethod]
        public void Increase()
        {

            C++;
            Console.WriteLine("C = {0}", C);
            if (OnIncreased != null)
                OnIncreased(this, EventArgs.Empty);
        }

        [RemoteMethod]
        public int Divide(int A, int B)
        {
            Console.WriteLine("A / B");
            return A / B;
        }


        [RemoteMethod]
        public int GetC()
        {
            return C;
        }

        int C = 0;

        [RemoteEvent]  // this event could be register from remote
        public event EventHandler OnIncreased;
    }

Then register it in Proxy
 Proxy.SetListener(1023); //open listener 
 ExampleObject sample = new ExampleObject();
 Proxy.Register(sample);

In this way, the sample object could be used from client.

OK, we compile the server side code and get a DLL/EXE file.

Then, we generate client by using Zap.Generate.exe. Simply click Load Assembly and get the compiled file. In the windows, we could get the generated code, which could be copied to client side project.

Here is the auto-generated code, please don't modify it:

public class ExampleObject : ProxyClass
    {
        public ExampleObject(ITunnel Tunnel) : base(Tunnel.Name) { }
        public ExampleObject(String TunnelName) : base(TunnelName) { }
        public Int32 Add(Int32 A, Int32 B)
        {
            var message = new SendMessage() { ObjectName = "ExampleObject", Method = "Add" };
            message.Parameters.Add("A", A.ToString());
            message.Parameters.Add("B", B.ToString());
            return Proxy.Send<Int32>(Tunnel, message);
        }
        public void Increase()
        {
            var message = new SendMessage() { ObjectName = "ExampleObject", Method = "Increase" };
            Proxy.SendVoid(Tunnel, message);
        }
        public int Divide(int A, int B)
        {
            var message = new SendMessage() { ObjectName = "ExampleObject", Method = "Divide" };
            message.Parameters.Add("A", A.ToString());
            message.Parameters.Add("B", B.ToString());
            return Proxy.Send<int>(Tunnel, message);
        }
        public Int32 GetC()
        {
            var message = new SendMessage() { ObjectName = "ExampleObject", Method = "GetC" };
            return Proxy.Send<Int32>(Tunnel, message);
        }
        public event EventHandler OnIncreased
        {
            add
            {
                AddEvent("ExampleObject", "OnIncreased", value);
            }
            remove
            {
                RemoveEvent("ExampleObject", "OnIncreased", value);
            }
        }
    }

2 Client Operation

Before we call remote object, there are some work to to. "Tunnel" is an very important concept in this framework. Tunnels have unique names, and each tunnel has one TCP connection. Messages will go through the tunnels by saying which name of tunnel they are using.

Code time:

TunnelConnector tunnel= new TunnelConnector("MyTunnel", "127.0.0.1", 1023);
Proxy.AddTunnel(tunnel);

Here we open up a tunnel named "MyTunnel".

ExampleObject call = new ExampleObject(tunnel);

A client mapped object is created in this way. Remote objects are specified with tunnels.

2.1 Invoke remote method

Let's take a look on the example code here:

//call without return value
call.Increase();

//call with return value
int dd = call.Add(1, 2);

The code looks normal. Actually, it gets the result from the server side.

2.2 Add/Remote remote event listener

Event listener is one important feature in this framework. Client can listen to the remote object.

Event registration


call.OnIncreased += new EventHandler(call_OnCAdded);

static void call_OnCAdded(object sender, EventArgs e) {
            Console.WriteLine("C Added");
 }


Event de-register


call.OnIncreased -= new EventHandler(call_OnCAdded);


Look quite normal usage again, isn't it? The framework hides the inner details.

2.3 Exception

You may think what if the server side occurs exceptions. The Zap framework will notify the client code and tell what is going in the server side.

The simple usage is:

            try
            {
                var result = call.Divide(10, 0);
                Console.WriteLine("Result = " + result);
            }
            catch (Exception e)
            {
                Console.WriteLine("Remote exception catched: " + e.Message);
            }

Thank you.
  • If you have any question, please contact me: neio.zhou(at)gmail.com


Last edited Mar 1, 2012 at 6:50 AM by Neio, version 10

Comments

No comments yet.