Exemple #1
0
 public void SendBuffer(Net.NetConnection conn)
 {
     if (CurPtr() < 65535 - 2)
     {
         if (conn != null && conn.Connected == true)
         {
             conn.SendBuffer(this.Ptr, CurPtr());
         }
     }
     else
     {
         Profiler.Log.WriteLine(Profiler.ELogTag.Error, "RPC", "Error!!SendBuffer mPos>=65535-2");
     }
 }
Exemple #2
0
 public RPCExecManager.RPCWait WaitHashCall <T>(long timeOut, FRPCTypeReturnCallBack <T> cb,
                                                ERouteTarget route     = ERouteTarget.Self,
                                                Net.NetConnection conn = null,
                                                RPCRouter router       = null) where T : RPCReturnValue, new()
 {
     AssertReturnType(this.GetType(), typeof(T));
     if (cb == null)
     {
         return(WaitDoCallImpl_Hash(timeOut, null, route, conn, router));
     }
     RPCExecManager.FRPCReturnCallBack fn = (PkgReader data, bool isTimeOut) =>
     {
         if (isTimeOut)
         {
             cb(null, true);
             return;
         }
         T retValue = new T();
         retValue.ReadObject(data);
         cb(retValue, false);
     };
     return(WaitDoCallImpl_Hash(timeOut, fn, route, conn, router));
 }
Exemple #3
0
        public async Task <T> AwaitHashCall <T>(long timeOut, ERouteTarget route = ERouteTarget.Self,
                                                Net.NetConnection conn           = null,
                                                RPCRouter router = null) where T : RPCReturnValue, new()
        {
            AssertReturnType(this.GetType(), typeof(T));
            RPCExecManager.RPCWait pkg = null;
            if (timeOut == 0)
            {
                pkg = WaitDoCallImpl_Hash(timeOut, null, route, conn, router);
            }
            else
            {
                pkg = WaitDoCallImpl_Hash(timeOut, RPCAwaiter.NullCB, route, conn, router);
            }

            var result = await RPCAwaiter.RPCWaitReturn <T>(pkg);

            if (result == null)
            {
                System.Diagnostics.Debug.WriteLine($"RPC Time Out: {this.GetType().FullName}");
                return(null);
            }
            return(result);
        }
        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();
        }
Exemple #6
0
        private RPCExecManager.RPCWait WaitDoCallImpl_Hash(long timeOut, RPCExecManager.FRPCReturnCallBack cb, ERouteTarget route = ERouteTarget.Self, Net.NetConnection conn = null, RPCRouter router = null)
        {
            var hash = this.GetMethodBinder().MethordHash;
            var pkg  = new PkgWriter(this.GetPkgSize());

            pkg.SetHashIndex(true);
            pkg.Write((byte)route);
            if (router != null)
            {
                router.RouteInfo.Save(pkg, route);
            }
            RPCExecManager.RPCWait waiter = null;
            if (cb != null)
            {
                pkg.SetHasReturn(true);
                waiter        = RPCExecManager.Insance.AddCalBack(cb);
                this.SerialId = waiter.SerialId;
                pkg.Waiter    = waiter;
                if (timeOut < 0)
                {
                    timeOut = long.MaxValue;
                }
                waiter.Timout = timeOut;
                pkg.Write(hash);
                pkg.Write(this.SerialId);
            }
            else
            {
                pkg.SetHasReturn(false);
                pkg.Write(hash);
            }
            WriteObject(pkg);

            if (conn != null)
            {
                pkg.SendBuffer(conn);
            }

            pkg.Dispose();

            return(waiter);
        }
Exemple #7
0
        private RPCExecManager.RPCWait WaitDoCallImpl(long timeOut, RPCExecManager.FRPCReturnCallBack cb, ERouteTarget route = ERouteTarget.Self, Net.NetConnection conn = null, RPCRouter router = null)
        {
            var Index = this.GetMethodBinder().Index;
            var pkg   = new PkgWriter(this.GetPkgSize());

            if (Index >= RPCExecManager.MaxRPC)
            {
                System.Diagnostics.Debug.WriteLine($"RPC Index is invalid:{this.GetType().FullName}");
                return(null);
            }
            pkg.Write((byte)route);
            if (router != null)
            {
                router.RouteInfo.Save(pkg, route);
            }

            RPCExecManager.RPCWait waiter = null;
            if (cb != null)
            {
                waiter        = RPCExecManager.Insance.AddCalBack(cb);
                this.SerialId = waiter.SerialId;
                pkg.Waiter    = waiter;
                if (timeOut < 0)
                {
                    timeOut = long.MaxValue;
                }
                waiter.Timout = timeOut;
                Index        |= RPCExecManager.WaitFlag;
                pkg.SetHasReturn(true);
                pkg.Write(Index);
                pkg.Write(this.SerialId);
            }
            else
            {
                pkg.SetHasReturn(false);
                pkg.Write(Index);
            }
            WriteObject(pkg);

            if (conn != null)
            {
                pkg.SendBuffer(conn);
            }

            return(waiter);
        }
 public async System.Threading.Tasks.Task <R> AwaitHashCallReturn(long timeOut, ERouteTarget route = ERouteTarget.Self,
                                                                  Net.NetConnection conn           = null,
                                                                  RPCRouter router = null)
 {
     return(await AwaitHashCall <R>(timeOut, route, conn, router));
 }