public void RegisterRPCHandler(ISession session, IRpcHandler handler)
 {
     foreach (var metho in handler.GetType().GetMethods())
     {
         string fullName = string.Format("{0}.{1}", handler.GetType().FullName, metho.Name);
         object instance = handler.GetInstance();
         ((Session)session)[metho.Name] = new MethodHandlerContext(instance, metho);
         ((Session)session)[fullName]   = new MethodHandlerContext(instance, metho);
     }
     //_handlers.Add(handler);
 }
        protected override void InternalProcessMessage(IOCPReaderWriter Context, byte[] Message)
        {
            base.InternalProcessMessage(Context, Message);
            var call = new IOCPRPCCall();
            //XmlSerializer ser = new XmlSerializer(call.GetType());
            var buf = Message; //ASCIIEncoding.ASCII.GetBytes(Message);
            var ms  = new MemoryStream();

            ms.Write(buf, 0, buf.Length);
            ms.Position = 0;
            //call = (IOCPRPCCall) ser.Deserialize(ms);
            call = (IOCPRPCCall)DeSerialize(ms, call.GetType());
            var rpcContext = new RpcContext(Context.Socket, Context, this, call);

            switch ((ResponseType)call.Response)
            {
            default:
            {
                _responseQ.Add(call);
                return;
            }
            break;

            case ResponseType.Message:
            {
                if (OnMessage != null)
                {
                    OnMessage(this, rpcContext, call.Params[0].ToString());
                }
                return;
            }
            break;

            case ResponseType.None:
            {
            }
            break;
            }

            /*            if (call.Response != (int)ResponseType.None)
             *          {
             *              _responseQ.Add(call);
             *              return;
             *          }*/

            MethodHandlerContext ctx = null;

            {
                var session = _sessions.GetSession();
                try
                {
                    if (session.TryGetValue(call.Method, out ctx))
                    {
                        try
                        {
                            // THIS MIGHT BE NESSESARY FOR MULTIPLE THREADS
                            object[] outp;
                            lock (ctx.Instance)
                            {
                                var inputParams = new object[call.Params.Count + 1];
                                inputParams[0] = rpcContext;
                                for (int i = 1; i <= call.Params.Count; i++)
                                {
                                    inputParams[i] = call.Params[i - 1];
                                }
                                outp = (object[])ctx.MethodInfo.Invoke(ctx.Instance, inputParams);
                                call.SetResponse(ResponseType.Success, "");
                                call.Params.Clear();
                                foreach (object p in outp)
                                {
                                    call.Params.Add(p);
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            if (e is TargetInvocationException)
                            {
                                e = ((TargetInvocationException)e).InnerException;
                            }
                            call.SetException(e);
                        }
                    }
                    else
                    {
                        call.SetResponse(ResponseType.Error, string.Format("Method not found: {0}", call.Method));
                    }
                }
                finally
                {
                    _sessions.ReleaseSession(session);
                }
            }
            Execute(Context, call);

            /* foreach (var handler in _handlers)
             * {
             *
             *   foreach (var metho in handler.GetType().GetMethods())
             *   {
             *       if (metho.Name == call.Method)
             *       {
             *           object[] outp = (object[])metho.Invoke(handler, call.Params.ToArray());
             *           call.Params.Clear();
             *           foreach (object p in outp)
             *           {
             *               call.Params.Add(p);
             *           }
             *           call.IsResponse = true;
             *           Execute(Context, call);
             *       }
             *   }
             * }*/
        }