Пример #1
0
        /// <summary>
        /// 加锁  只有成功后,才会有 返回值
        /// </summary>
        /// <param name="self"></param>
        /// <returns></returns>
        public static async Task Lock(this LockComponent self)
        {
            ++self.lockCount;                     //加锁次数++

            if (self.status == LockStatus.Locked) //如果已经锁定 跳出
            {
                return;
            }
            if (self.status == LockStatus.LockRequesting)              //如过当前是待加锁状态
            {
                await self.WaitLock();

                return;
            }

            self.status = LockStatus.LockRequesting;              //待加锁状态

            // 真身直接本地请求锁,镜像需要调用Rpc获取锁
            MasterComponent masterComponent = self.Entity.GetComponent <MasterComponent>();

            if (masterComponent != null)
            {
                await masterComponent.Lock(self.address);
            }
            else
            {
                self.RequestLock();
                await self.WaitLock();
            }
        }
Пример #2
0
        public static async ETTask Lock(this LockComponent self)
        {
            ++self.lockCount;

            if (self.status == LockStatus.Locked)
            {
                return;
            }
            if (self.status == LockStatus.LockRequesting)
            {
                await self.WaitLock();

                return;
            }

            self.status = LockStatus.LockRequesting;

            // 真身直接本地请求锁,镜像需要调用Rpc获取锁
            MasterComponent masterComponent = self.Entity.GetComponent <MasterComponent>();

            if (masterComponent != null)
            {
                await masterComponent.Lock(self.address);
            }
            else
            {
                self.RequestLock().NoAwait();
                await self.WaitLock();
            }
        }
Пример #3
0
        public static Task <bool> Lock(this MasterComponent self, IPEndPoint address)
        {
            if (self.lockedAddress == null)
            {
                self.lockedAddress = address;
                return(Task.FromResult(true));
            }

            TaskCompletionSource <bool> tcs = new TaskCompletionSource <bool>();
            LockInfo lockInfo = new LockInfo(address, tcs);

            self.queue.Enqueue(lockInfo);
            return(tcs.Task);
        }
Пример #4
0
        /// <summary>
        /// 锁一个IP,如果当前有锁的地址 将其放入缓存中,待当前锁的IP释放后,在锁它
        /// </summary>
        /// <param name="self"></param>
        /// <param name="address"></param>
        /// <returns></returns>
        public static Task <bool> Lock(this MasterComponent self, IPEndPoint address)
        {
            if (self.lockedAddress == null)     //如当前没有锁IP
            {
                self.lockedAddress = address;   //设置当前的锁IP地址
                return(Task.FromResult(true));  //返回true
            }
            //下面就是说当前有锁IPlockedAddress!=空格
            TaskCompletionSource <bool> tcs = new TaskCompletionSource <bool>();
            LockInfo lockInfo = new LockInfo(address, tcs);

            self.queue.Enqueue(lockInfo);             //加入到堆栈 等到 堆栈返回tcs.Task的值
            return(tcs.Task);
        }
        protected override void Run(Session session, S2M_RegisterService message, Action <M2S_RegisterService> reply)
        {
            M2S_RegisterService response = new M2S_RegisterService();

            try
            {
                NetInnerComponent    netInnerComponent    = Game.Scene.GetComponent <NetInnerComponent>();
                StartConfigComponent startConfigComponent = Game.Scene.GetComponent <StartConfigComponent>();
                MasterComponent      masterComponent      = Game.Scene.GetComponent <MasterComponent>();

                StartConfig startConfig = (StartConfig)message.Component;
                if (!masterComponent.AddConfig(startConfig))
                {
                    response.Error = ErrorCode.ERR_RegisterServerRepeatly;
                    reply(response);
                    return;
                }
                else
                {
                    Log.Info($"Server[{startConfig.AppType}:{startConfig.AppId}] is online.");
                }

                startConfigComponent.AddConfig(startConfig);

                var startConfigs = masterComponent.GetAll();
                response.Components = startConfigs.Select(e => (ComponentWithId)e).ToList();
                reply(response);

                foreach (StartConfig v in startConfigs)
                {
                    // 不傳給自己
                    if (v.AppId == startConfig.AppId)
                    {
                        continue;
                    }
                    InnerConfig innerConfig   = v.GetComponent <InnerConfig>();
                    Session     serverSession = netInnerComponent.Get(innerConfig.IPEndPoint);
                    serverSession.Send(new M2A_RegisterService
                    {
                        Component = startConfig
                    });
                }
            }
            catch (Exception e)
            {
                ReplyError(response, e, reply);
            }
        }
Пример #6
0
        public static void Release(this MasterComponent self, IPEndPoint address)
        {
            if (!self.lockedAddress.Equals(address))
            {
                Log.Error($"解锁地址与锁地址不匹配! {self.lockedAddress} {address}");
                return;
            }
            if (self.queue.Count == 0)
            {
                self.lockedAddress = null;
                return;
            }
            LockInfo lockInfo = self.queue.Dequeue();

            self.lockedAddress = lockInfo.Address;
            lockInfo.Tcs.SetResult(true);
        }
Пример #7
0
        /// <summary>
        /// 解锁当前锁住的IP地址
        /// </summary>
        /// <param name="self"></param>
        /// <param name="address"></param>
        public static void Release(this MasterComponent self, IPEndPoint address)
        {
            if (!self.lockedAddress.Equals(address))              //看当前锁定的地址是否和解锁的地址匹配
            {
                Log.Error($"解锁地址与锁地址不匹配! {self.lockedAddress} {address}");
                return;
            }
            if (self.queue.Count == 0)              //如果没有待加锁的地址  退出
            {
                self.lockedAddress = null;
                return;
            }
            LockInfo lockInfo = self.queue.Dequeue();             //从堆栈中取出待加锁的地址 加锁

            self.lockedAddress = lockInfo.Address;
            lockInfo.Tcs.SetResult(true);
        }
Пример #8
0
 public static void AddGhost(this MasterComponent self, IPEndPoint address)
 {
     self.ghostsAddress.Add(address);
 }
Пример #9
0
 public static void RemoveGhost(this MasterComponent self, IPEndPoint address)
 {
     self.ghostsAddress.Remove(address);
 }