Beispiel #1
0
        public void AppendPkg(PkgReader pkg, int offset)
        {
            //WriteByteArray(pkg.Ptr, pkg.DataPtr() + offset, pkg.GetSize() - offset);
            unsafe
            {
                var src = (byte *)pkg.Ptr;

                WritePtr(src + pkg.DataPtr() + offset, pkg.GetSize() - offset);
            }
        }
Beispiel #2
0
        public virtual RPCParameter CreateParameter(PkgReader pkg)
        {
            //反射读取包
            //如果生成代码,这里就直接new ,然后按顺序read包数据

            var param = System.Activator.CreateInstance(ParamType) as RPCParameter;

            if (param == null)
            {
                return(null);
            }

            param.ReadObject(pkg);

            return(param);
        }
Beispiel #3
0
        public void DoCallBack(UInt16 serialId, PkgReader data)
        {
            lock (WaitCallBacks)
            {
                RPCWait wait;
                if (WaitCallBacks.TryGetValue(serialId, out wait))
                {
                    WaitCallBacks.Remove(serialId);
#if PWindow
                    var saved = System.Threading.SynchronizationContext.Current;
                    System.Threading.SynchronizationContext.SetSynchronizationContext(null);
                    wait.RetCallBack(data, false);
                    System.Threading.SynchronizationContext.SetSynchronizationContext(saved);
#else
                    wait.RetCallBack(data, false);
#endif
                    RPCWaitAllocator.ReleaseObject(wait);
                }
            }
        }
        public object Execute(PkgReader pkg, Net.NetConnection connect, ref RPCRouter.RouteData routeInfo)
        {
            UInt16 rpcIndex    = 0;
            UInt32 rpcHash     = 0;
            bool   hasReturn   = false;
            bool   isHashIndex = pkg.IsHashIndex();

            if (isHashIndex)
            {
                hasReturn = pkg.IsHasReturn();
                pkg.Read(out rpcHash);
            }
            else
            {
                pkg.Read(out rpcIndex);
                hasReturn = (rpcIndex & WaitFlag) != 0 ? true : false;
            }
            if (hasReturn != pkg.IsHasReturn())
            {
                System.Diagnostics.Debug.Assert(false);
            }
            UInt16 serialId = 0;

            if (hasReturn)
            {
                pkg.Read(out serialId);
                rpcIndex = (UInt16)(rpcIndex & MaxRPC);
            }
            RPCExec exec = null;

            if (isHashIndex)
            {
                exec = GetExecByHash(rpcHash);
            }
            else
            {
                exec = GetExecByIndex(rpcIndex);
            }
            if (exec == null)
            {
                System.Diagnostics.Debug.WriteLine($"RPC GetExecByIndex is null:{rpcIndex}");
                return(null);
            }
            var parameter = exec.CreateParameter(pkg);

            parameter.SerialId    = serialId;
            parameter.RouteInfo   = routeInfo;
            parameter.Connect     = connect;
            parameter.ExtraReader = pkg;
            object host = parameter.GetHostObject();

            if (host == null)
            {
                exec.DestroyParameter(parameter);
                System.Diagnostics.Debug.WriteLine($"RPC HostObject is null:{parameter.GetType().FullName}");
                return(null);
            }
            var authority = parameter.GetAuthority(host);

            if (authority < exec.CallAttr.LimitLevel)
            {//超越权限
                exec.DestroyParameter(parameter);
                System.Diagnostics.Debug.WriteLine($"Over Authority[{authority}<{exec.CallAttr.LimitLevel.ToString()}]:{parameter.GetHostObjectName()}=>{parameter.GetRPCMethod().Name}");
                return(null);
            }

            try
            {
                return(parameter.DoExecute(exec, host));
            }
            catch (Exception ex)
            {
                Profiler.Log.WriteException(ex);
                return(null);
            }
            finally
            {
                exec.DestroyParameter(parameter);
            }
        }
        public void ReceiveData(Net.NetConnection sender, byte[] pData, int nLength, Int64 recvTime)
        {
            unsafe
            {
                if (nLength < sizeof(RPCHeader))
                {
                    return;
                }
            }

            var router = sender.Router as RPCRouter;

            if (router == null)
            {
                System.Diagnostics.Debug.WriteLine($"connect bind a invalid data");
                return;
            }
            var pkg = new PkgReader(pData, nLength, recvTime);

            if (PkgRcver != null)
            {
                PkgRcver.OnReceivePackage(sender, pkg);
            }

            byte route = 0;

            pkg.Read(out route);
            var isReturn = (route & (byte)ERouteTarget.ReturnFlag) != 0 ? true : false;

            route = (byte)(route & (byte)ERouteTarget.ReturnMask);
            ERouteTarget target = (ERouteTarget)route;

            switch (target)
            {
            case ERouteTarget.Self:
            {
                if (isReturn)
                {
                    UInt16 serialId = 0;
                    pkg.Read(out serialId);
                    DoCallBack(serialId, pkg);
                }
                else
                {
                    Execute(pkg, sender, ref router.RouteInfo);
                }
            }
            break;

            case ERouteTarget.Routed:
            {
                var routeInfo = new RPCRouter.RouteData();
                routeInfo.Load(pkg);
                Execute(pkg, sender, ref routeInfo);
            }
            break;

            case ERouteTarget.Client:
            {        //只有GateServer才有转发到客户端的需求
#if Server
                var routePkg  = new PkgWriter();
                var routeInfo = new RPCRouter.RouteData();
                routeInfo.Load(pkg);
                if (Vise3D.Server.IGateServer.Instance != null)
                {
                    if (isReturn)
                    {
                        route = (byte)((byte)ERouteTarget.Self | (byte)ERouteTarget.ReturnFlag);
                        routePkg.Write(route);
                        UInt16 seriaId;
                        pkg.Read(out seriaId);
                        routePkg.Write(seriaId);
                    }
                    else
                    {
                        route = (byte)ERouteTarget.Self;
                        routePkg.Write(route);
                    }

                    routePkg.AppendPkg(pkg, pkg.GetPosition());
                    routePkg.SendBuffer(Vise3D.Server.IGateServer.Instance.GetClientConnect(routeInfo.RouteSlot));
                }
#endif
            }
            break;

            case ERouteTarget.Hall:
            {
                var routePkg = new PkgWriter(nLength + 64);
                routePkg.Write((byte)ERouteTarget.Routed);
                router.RouteInfo.Save(routePkg, ERouteTarget.Hall);
                routePkg.SetFlags(pkg.GetFlags());
                routePkg.AppendPkg(pkg, pkg.GetPosition());
                routePkg.SendBuffer(RouterTargetConnect(target, router));
                routePkg.Dispose();
            }
            break;

            case ERouteTarget.Data:
            case ERouteTarget.Keep:
            case ERouteTarget.Reg:
            case ERouteTarget.Path:
            case ERouteTarget.Log:
            {
                var routePkg = new PkgWriter(nLength + 64);
                routePkg.Write((byte)ERouteTarget.Routed);
                router.RouteInfo.Save(routePkg, target);
                routePkg.SetFlags(pkg.GetFlags());
                routePkg.AppendPkg(pkg, pkg.GetPosition());
                routePkg.SendBuffer(GetServerConnect(target));
                routePkg.Dispose();
            }
            break;
            }

            pkg.Dispose();
        }