Example #1
0
        protected RPCExecuter.RPCWait WritePkgHeader_Hash <T>(ref NetCore.PkgWriter pkg, ref T arg, long timeOut, RPCExecuter.FRPCReturnCallBack cb,
                                                              NetCore.ERouteTarget route, NetCore.NetConnection conn, NetCore.RPCRouter router)
            where T : IArgument
        {
            var hash = MethodHash;

            pkg.SetHashIndex(true);
            pkg.Write((byte)route);
            if (router != null)
            {
                router.RouteInfo.Save(ref pkg, route);
            }
            RPCExecuter.RPCWait waiter = null;
            if (cb != null)
            {
                pkg.SetHasReturn(true);
                waiter     = RPCExecuter.Instance.AddCalBack(cb);
                pkg.Waiter = waiter;
                if (timeOut < 0)
                {
                    timeOut = long.MaxValue;
                }
                waiter.Timout = timeOut;
                pkg.Write(hash);
                pkg.Write(waiter.SerialId);
            }
            else
            {
                pkg.SetHasReturn(false);
                pkg.Write(hash);
            }
            return(waiter);
        }
Example #2
0
        public void DoReturn(ref R arg, UInt16 serialId, NetCore.NetConnection connect)
        {
            var pkg = new NetCore.PkgWriter(arg.GetPkgSize());

            try
            {
                byte route = (byte)NetCore.ERouteTarget.Self | (byte)NetCore.ERouteTarget.ReturnFlag;
                pkg.Write((byte)route);
                pkg.Write(serialId);
                OnWriteReturn(ref pkg, ref arg);
                pkg.SendBuffer(connect);
            }
            finally
            {
                pkg.Dispose();
            }
        }
            protected override void OnWriteReturn(ref NetCore.PkgWriter pkg, ref S2C_TestAwait.ReturnData data)
            {
                pkg.Write(data.A1);

                var obj = new TestStructRLE();

                obj.A = 1;
                obj.D = Matrix.Translate(1, 2, 3);

                IO.StructCompress <TestStructRLE, NetCore.PkgWriter, NetCore.PkgReader> .Instance.Write(
                    ref pkg, ref obj);
            }
Example #4
0
        //protected async Task<R> C2S_AwaitCall<R,T>(T arg, long timeOut, ERouteTarget route = ERouteTarget.Hall)
        //    where R : struct, IReturnValue
        //    where T : struct, IArgument
        //{
        //    AssertReturnType(this.GetType(), typeof(R));
        //    if (route == ERouteTarget.Gate)
        //        route = ERouteTarget.Self;

        //    RPCExecuter.RPCWait waiter = null;
        //    if (timeOut == 0)
        //        waiter = WaitDoCallImpl<T>(ref arg, 0, null, route, RPCExecuter.Instance.C2SConnect);
        //    else
        //        waiter = WaitDoCallImpl<T>(ref arg, timeOut, RPCAwaiter.NullCB, route, RPCExecuter.Instance.C2SConnect);

        //    var result = await RPCAwaiter.RPCWaitReturn<R>(waiter);
        //    return result;
        //}
        //protected RPCExecuter.RPCWait C2S_Call<R, T>(ref T arg, long timeOut, ERouteTarget route = ERouteTarget.Hall, FRPCTypeReturnCallBack<R> cb = null)
        //    where R : struct, IReturnValue
        //    where T : struct, IArgument
        //{
        //    AssertReturnType(this.GetType(), typeof(R));
        //    if (route == ERouteTarget.Gate)
        //        route = ERouteTarget.Self;
        //    if (cb == null)
        //        return WaitDoCallImpl<T>(ref arg, 0, null, route, RPCExecuter.Instance.C2SConnect);
        //    RPCExecuter.FRPCReturnCallBack fn = (PkgReader data, bool isTimeOut) =>
        //    {
        //        if (isTimeOut)
        //        {
        //            cb(default(R), true);
        //            return;
        //        }
        //        R retValue = new R();
        //        IO.Serializer.SerializerHelper.ReadObject(retValue, data);
        //        cb(retValue, false);
        //    };
        //    return WaitDoCallImpl<T>(ref arg, timeOut, fn, route, RPCExecuter.Instance.C2SConnect);
        //}

        //protected RPCExecuter.RPCWait S2C_Call<T>(ref T arg, RPCRouter router, ERouteTarget sender = ERouteTarget.Unknown)
        //    where T : struct, IArgument
        //{
        //    if (sender == ERouteTarget.Unknown)
        //    {
        //        //sender = RPCExecuter.Instance.AppTarget;
        //        sender = ERouteTarget.Self;
        //    }

        //    switch (sender)
        //    {
        //        case ERouteTarget.Gate:
        //            return WaitDoCallImpl<T>(ref arg, 0, null, ERouteTarget.Self, router.C2GConnect);
        //        case ERouteTarget.Hall:
        //            return WaitDoCallImpl<T>(ref arg, 0, null, ERouteTarget.Client, router.ToGConnect, router);
        //        case ERouteTarget.Keep:
        //            return WaitDoCallImpl<T>(ref arg, 0, null, ERouteTarget.Client, router.ToGConnect, router);
        //        case ERouteTarget.Data:
        //            return WaitDoCallImpl<T>(ref arg, 0, null, ERouteTarget.Client, router.ToGConnect, router);
        //        case ERouteTarget.Reg:
        //            return WaitDoCallImpl<T>(ref arg, 0, null, ERouteTarget.Client, router.ToGConnect, router);
        //        default:
        //            return null;
        //    }
        //}

        //protected async Task<R> S2S_AwaitCall<R, T>(T arg, long timeOut, ERouteTarget target = ERouteTarget.Data)
        //    where R : struct, IReturnValue
        //    where T : struct, IArgument
        //{
        //    AssertReturnType(this.GetType(), typeof(R));

        //    RPCExecuter.RPCWait writer = null;
        //    if (timeOut == 0)
        //        writer = WaitDoCallImpl<T>(ref arg, timeOut, null, ERouteTarget.Self, RPCExecuter.Instance.GetServerConnect(target));
        //    else
        //        writer = WaitDoCallImpl<T>(ref arg, timeOut, RPCAwaiter.NullCB, ERouteTarget.Self, RPCExecuter.Instance.GetServerConnect(target));

        //    var result = await RPCAwaiter.RPCWaitReturn<R>(writer);
        //    return result;
        //}

        //protected RPCExecuter.RPCWait S2S_Call<R,T>(ref T arg, long timeOut, ERouteTarget target = ERouteTarget.Data, FRPCTypeReturnCallBack<R> cb = null)
        //    where R : struct, IReturnValue
        //    where T : struct, IArgument
        //{
        //    AssertReturnType(this.GetType(), typeof(R));
        //    if (cb == null)
        //    {
        //        return WaitDoCallImpl<T>(ref arg, timeOut, null, ERouteTarget.Self, RPCExecuter.Instance.GetServerConnect(target));
        //    }

        //    RPCExecuter.FRPCReturnCallBack fn = (PkgReader data, bool isTimeOut) =>
        //    {
        //        if (isTimeOut)
        //        {
        //            cb(default(R), true);
        //            return;
        //        }
        //        R retValue = new R();
        //        IO.Serializer.SerializerHelper.ReadObject(retValue, data);
        //        cb(retValue, false);
        //    };
        //    return WaitDoCallImpl<T>(ref arg, timeOut, fn, ERouteTarget.Self, RPCExecuter.Instance.GetServerConnect(target));
        //}
        #endregion

        #region Pkg Implement
        protected RPCExecuter.RPCWait WritePkgHeader <T>(ref NetCore.PkgWriter pkg, ref T arg, long timeOut, RPCExecuter.FRPCReturnCallBack cb,
                                                         NetCore.ERouteTarget route, NetCore.NetConnection conn, NetCore.RPCRouter router) where T : IArgument
        {
            var Index = RPCIndex;

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

            RPCExecuter.RPCWait waiter = null;
            if (cb != null)
            {
                waiter     = RPCExecuter.Instance.AddCalBack(cb);
                pkg.Waiter = waiter;
                if (timeOut < 0)
                {
                    timeOut = long.MaxValue;
                }
                waiter.Timout = timeOut;
                Index        |= RPCExecuter.WaitFlag;
                pkg.SetHasReturn(true);
                pkg.Write(Index);
                pkg.Write(waiter.SerialId);
            }
            else
            {
                pkg.SetHasReturn(false);
                pkg.Write(Index);
            }
            return(waiter);
        }
Example #5
0
        public void ReceiveData(NetCore.NetConnection sender, byte[] pData, int nLength, Int64 recvTime)
        {
            unsafe
            {
                if (nLength < sizeof(NetCore.PkgHeader))
                {
                    return;
                }
            }

            var router = sender.Router as NetCore.RPCRouter;

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

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

                byte route = 0;
                pkg.Read(out route);
                var isReturn = (route & (byte)NetCore.ERouteTarget.ReturnFlag) != 0 ? true : false;
                route = (byte)(route & (byte)NetCore.ERouteTarget.ReturnMask);
                var target = (NetCore.ERouteTarget)route;
                switch (target)
                {
                case NetCore.ERouteTarget.Self:
                {
                    if (isReturn)
                    {
                        UInt16 serialId = 0;
                        pkg.Read(out serialId);
                        DoCallBack(serialId, ref pkg);
                    }
                    else
                    {
                        Execute(ref pkg, sender, ref router.RouteInfo);
                    }
                }
                break;

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

                case NetCore.ERouteTarget.Client:
                {        //只有GateServer才有转发到客户端的需求
#if Server
                    var routePkg  = new PkgWriter();
                    var routeInfo = new RPCRouter.RouteData();
                    try
                    {
                        routeInfo.Load(pkg);
                        if (Titan3D.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(Titan3D.Server.IGateServer.Instance.GetClientConnect(routeInfo.RouteSlot));
                        }
                    }
                    finally
                    {
                        routePkg.Dispose();
                    }
#endif
                }
                break;

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

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