Beispiel #1
0
 public void AddFactoryHandle(int type, ProtocolFactoryHandle factory)
 {
     if (false == Factorys.TryAdd(type, factory))
     {
         throw new Exception($"duplicate factory type={type} moduleid={(type >> 16) & 0x7fff} id={type & 0x7fff}");
     }
 }
Beispiel #2
0
 // 重载需要的方法。
 public override void DispatchProtocol(Zeze.Net.Protocol p, ProtocolFactoryHandle factoryHandle)
 {
     if (null != factoryHandle.Handle)
     {
         if (p.TypeId == gnet.Provider.Bind.TypeId_)
         {
             // Bind 的处理需要同步等待ServiceManager的订阅成功,时间比较长,
             // 不要直接在io-thread里面执行。
             global::Zeze.Util.Task.Run(() => factoryHandle.Handle(p), p);
         }
         else
         {
             // 不启用新的Task,直接在io-thread里面执行。因为其他协议都是立即处理的,
             // 直接执行,少一次线程切换。
             try
             {
                 var isReqeustSaved = p.IsRequest;
                 int result         = factoryHandle.Handle(p);
                 global::Zeze.Util.Task.LogAndStatistics(result, p, isReqeustSaved);
             }
             catch (System.Exception ex)
             {
                 logger.Log(SocketOptions.SocketLogLevel, ex, "Protocol.Handle. {0}", p);
             }
         }
     }
     else
     {
         logger.Log(SocketOptions.SocketLogLevel, "Protocol Handle Not Found. {0}", p);
     }
 }
Beispiel #3
0
 public override void DispatchProtocol(Protocol p, ProtocolFactoryHandle factoryHandle)
 {
     // Reduce 很重要。必须得到执行,不能使用默认线程池(Task.Run),防止饥饿。
     if (null != factoryHandle.Handle)
     {
         agent.Zeze.InternalThreadPool.QueueUserWorkItem(
             () => Util.Task.Call(() => factoryHandle.Handle(p), p));
     }
 }
Beispiel #4
0
        public override void DispatchProtocol(Protocol p, ProtocolFactoryHandle factoryHandle)
        {
            // 防止Client不进入加密,直接发送用户协议。
            if (false == IsHandshakeProtocol(p.TypeId))
            {
                p.Sender.VerifySecurity();
            }

            base.DispatchProtocol(p, factoryHandle);
        }
Beispiel #5
0
        public override void DispatchRpcResponse(Protocol p,
                                                 Func <Protocol, int> responseHandle,
                                                 ProtocolFactoryHandle factoryHandle)
        {
            if (IsImportantProtocol(p.TypeId))
            {
                // 不能在默认线程中执行,使用专用线程池,保证这些协议得到处理。
                Raft.ImportantThreadPool.QueueUserWorkItem(
                    () => Util.Task.Call(() => responseHandle(p), p));
                return;
            }

            base.DispatchRpcResponse(p, responseHandle, factoryHandle);
        }
Beispiel #6
0
        public override void DispatchProtocol(Protocol p, ProtocolFactoryHandle factoryHandle)
        {
            // 防止Client不进入加密,直接发送用户协议。
            if (false == IsHandshakeProtocol(p.TypeId))
            {
                p.Sender.VerifySecurity();
            }

            if (p.TypeId == gnet.Provider.ModuleRedirect.TypeId_)
            {
                if (null != factoryHandle.Handle)
                {
                    var modureRecirect = p as gnet.Provider.ModuleRedirect;
                    if (null != Zeze && false == factoryHandle.NoProcedure)
                    {
                        Zeze.TaskOneByOneByKey.Execute(
                            modureRecirect.Argument.HashCode,
                            () => global::Zeze.Util.Task.Call(
                                Zeze.NewProcedure(
                                    () => factoryHandle.Handle(p),
                                    p.GetType().FullName,
                                    p.UserState),
                                p,
                                (p, code) => p.SendResultCode(code)
                                )
                            );
                    }
                    else
                    {
                        Zeze.TaskOneByOneByKey.Execute(modureRecirect.Argument.HashCode,
                                                       () => global::Zeze.Util.Task.Call(
                                                           () => factoryHandle.Handle(p),
                                                           p,
                                                           (p, code) => p.SendResultCode(code)
                                                           )
                                                       );
                    }
                }
                else
                {
                    logger.Log(SocketOptions.SocketLogLevel, "Protocol Handle Not Found. {0}", p);
                }
                return;
            }

            base.DispatchProtocol(p, factoryHandle);
        }
Beispiel #7
0
 // 用来派发异步rpc回调。
 public virtual void DispatchRpcResponse(Protocol rpc,
                                         Func <Protocol, int> responseHandle,
                                         ProtocolFactoryHandle factoryHandle)
 {
     if (null != Zeze && false == factoryHandle.NoProcedure)
     {
         global::Zeze.Util.Task.Run(
             Zeze.NewProcedure(
                 () => responseHandle(rpc),
                 rpc.GetType().FullName + ":Response",
                 rpc.UserState));
     }
     else
     {
         global::Zeze.Util.Task.Run(() => responseHandle(rpc), rpc);
     }
 }
Beispiel #8
0
        public override void DispatchProtocol(Protocol p, ProtocolFactoryHandle factoryHandle)
        {
            // 防止Client不进入加密,直接发送用户协议。
            if (false == IsHandshakeProtocol(p.TypeId))
            {
                p.Sender.VerifySecurity();
            }

            if (IsImportantProtocol(p.TypeId))
            {
                // 不能在默认线程中执行,使用专用线程池,保证这些协议得到处理。
                // 内部协议总是使用明确返回值或者超时,不使用框架的错误时自动发送结果。
                Raft.ImportantThreadPool.QueueUserWorkItem(
                    () => Util.Task.Call(() => factoryHandle.Handle(p), p, null));
                return;
            }
            // User Request

            if (Raft.IsLeader)
            {
                // 默认0,如果没有配置多实例客户端,所有得请求都排一个队列,因为并发有风险。
                long appInstance = 0;
                //【防止重复的请求】
                // see Log.cs::LogSequence.TryApply
                if (p.UniqueRequestId > 0 && Raft.RaftConfig.AutoKeyLocalStep > 0)
                {
                    appInstance = p.UniqueRequestId % Raft.RaftConfig.AutoKeyLocalStep;
                }

                TaskOneByOne.Execute(appInstance,
                                     () => ProcessRequest(appInstance, p, factoryHandle),
                                     p.GetType().FullName,
                                     () => p.SendResultCode(Procedure.CancelExcption)
                                     );
                return;
            }

            TrySendLeaderIs(p.Sender);

            // 选举中
            // DONOT process application request.
        }
Beispiel #9
0
 public override void DispatchProtocol(Zeze.Net.Protocol p, ProtocolFactoryHandle factoryHandle)
 {
     if (null != factoryHandle.Handle)
     {
         try
         {
             var isRequestSaved = p.IsRequest;
             int result         = factoryHandle.Handle(p); // 不启用新的Task,直接在io-thread里面执行。
             global::Zeze.Util.Task.LogAndStatistics(result, p, isRequestSaved);
         }
         catch (System.Exception ex)
         {
             p.Sender.Close(ex); // link 在异常时关闭连接。
         }
     }
     else
     {
         logger.Log(SocketOptions.SocketLogLevel, "Protocol Handle Not Found. {0}", p);
         p.Sender.Close(null);
     }
 }
Beispiel #10
0
 public virtual void DispatchProtocol(Protocol p, ProtocolFactoryHandle factoryHandle)
 {
     if (null != factoryHandle.Handle)
     {
         if (null != Zeze && false == factoryHandle.NoProcedure)
         {
             global::Zeze.Util.Task.Run(
                 Zeze.NewProcedure(
                     () => factoryHandle.Handle(p),
                     p.GetType().FullName,
                     p.UserState), p);
         }
         else
         {
             global::Zeze.Util.Task.Run(() => factoryHandle.Handle(p), p);
         }
     }
     else
     {
         logger.Log(SocketOptions.SocketLogLevel, "Protocol Handle Not Found. {0}", p);
     }
 }
Beispiel #11
0
 private int ProcessRequest(long appInstance, Protocol p, ProtocolFactoryHandle factoryHandle)
 {
     return(Util.Task.Call(() =>
     {
         if (Raft.WaitLeaderReady())
         {
             if (Raft.LogSequence.LastAppliedAppRpcSessionId.TryGetValue(appInstance, out var exist))
             {
                 if (p.UniqueRequestId <= exist)
                 {
                     p.SendResultCode(Procedure.DuplicateRequest);
                     return Procedure.DuplicateRequest;
                 }
             }
             return factoryHandle.Handle(p);
         }
         TrySendLeaderIs(p.Sender);
         return Procedure.LogicError;
     },
                           p,
                           (p, code) => p.SendResultCode(code)
                           ));
 }