Example #1
0
        private void RemoveContext(GameServerContext context, IGameServerPeer peer = null)
        {
            if (log.IsDebugEnabled)
            {
                log.DebugFormat("Removing context. context={0},p:{1}", context, peer);
            }

            if (this.gameServerContexts.TryGetValue(context.Key, out ContextKeeper keeper))
            {
                if (keeper.Context == context)
                {
                    this.gameServerContexts.Remove(context.Key);

                    if (log.IsInfoEnabled)
                    {
                        log.InfoFormat("Context was removed. context={0}, p:{1}", context, peer);
                    }
                    return;
                }

                if (log.IsInfoEnabled)
                {
                    log.InfoFormat("Context removal skiped. context found but it belongs " +
                                   "to different server. context4remove={0}, context in index:{1}", context, keeper.Context);
                }
            }
            else
            {
                log.WarnFormat("Removing of context failed. context={0},p:{1}", context, peer);
            }
        }
Example #2
0
        private void ScheduleContextRemoval(GameServerContext context, IGameServerPeer peer)
        {
            if (log.IsDebugEnabled)
            {
                log.DebugFormat("Scheduling context for context removal. context={0},p:{1}", context, peer);
            }

            if (!this.gameServerContexts.TryGetValue(context.Key, out ContextKeeper keeper))
            {
                log.WarnFormat("Scheduling removal failed to find context. key:'{0}', id:'{1}',p:{2}", context.Key, context.ServerId, peer);
                return;
            }

            if (keeper.Context == context)
            {
                keeper.DisposeTimer = this.fiber.Schedule(() => ScheduledDestroyContext(keeper), this.contextTTL);
                if (log.IsInfoEnabled)
                {
                    log.InfoFormat("Context was scheduled for removal. context={0}, p:{1}", context, peer);
                }
                return;
            }

            if (log.IsInfoEnabled)
            {
                log.InfoFormat("Removal scheduling skiped. context found but it belongs " +
                               "to different server. context4remove={0}, context in index:{1}", context, keeper.Context);
            }
        }
Example #3
0
        private void RegisterGameServer(IRegisterGameServer request, IGameServerPeer peer, bool registerByRequet)
        {
            var key = GameServerContext.GetKey(request);

            if (log.IsInfoEnabled)
            {
                if (registerByRequet)
                {
                    log.Info($"Registering GS by request. key:'{key}', id:'{request.ServerId}', p:{peer}");
                }
                else
                {
                    log.Info($"Registering GS by InitRequest. key:'{key}', id:'{request.ServerId}', p:{peer}");
                }
            }

            lock (this.gameServerContexts)
            {
                ContextKeeper keeper;
                if (this.gameServerContexts.TryGetValue(key, out keeper))
                {
                    keeper.KillDisposeTimer();
                    if (keeper.Context.ServerId == request.ServerId)
                    {
                        if (log.IsInfoEnabled)
                        {
                            log.InfoFormat("Context for GS found and reused. key:'{0}', id:'{1}',p:{2}", key, request.ServerId, peer);
                        }

                        keeper.Context.AttachPeerAndHandleRegisterRequest(peer, request, true);
                        return;
                    }

                    if (log.IsDebugEnabled)
                    {
                        log.DebugFormat("Context for GS found but belongs to other server." +
                                        " key:'{0}', old_id:'{1}',new_id:{2}", key, keeper.Context.ServerId, request.ServerId);
                    }

                    this.RemoveContext(keeper.Context);
                    keeper.Context.DetachPeerAndClose();
                }

                keeper = new ContextKeeper
                {
                    Context = this.CreateContext(request),
                };

                keeper.Context.AttachPeerAndHandleRegisterRequest(peer, request, false);

                this.gameServerContexts.Add(key, keeper);

                if (log.IsInfoEnabled)
                {
                    log.InfoFormat("GS is added. key:'{0}', id:'{1}',p:{2}", key, request.ServerId, peer);
                }
                return;
            }
        }
Example #4
0
        public void OnGameServerLeft(GameServerContext context, IGameServerPeer peer)
        {
            lock (this.gameServerContexts)
            {
                context.DetachPeerAndClose();

                this.RemoveContext(context, peer);
            }
        }
Example #5
0
        public void AttachPeerAndHandleRegisterRequest(IGameServerPeer peer, IRegisterGameServer request, bool reconnect)
        {
            this.AttachPeer(peer);
            this.SetStateAndLoadPrediction(request, reconnect);

            if (reconnect)
            {
                this.OnGameServerReconnected();
            }
        }
Example #6
0
 public void OnGameServerDisconnect(GameServerContext context, IGameServerPeer peer)
 {
     lock (this.gameServerContexts)
     {
         if (context.DetachPeer(peer))
         {
             this.ScheduleContextRemoval(context, peer);
         }
     }
 }
Example #7
0
        public bool DetachPeer(IGameServerPeer peer)
        {
            if (this.Peer == null || this.Peer != peer)
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug($"Context is bound to another peer. No detach done for peer:{peer}. bound peer:{this.Peer}");
                }
                return(false);
            }

            this.Peer.DettachFromContext();
            this.Peer = null;

            this.OnPeerDettached();
            return(true);
        }
Example #8
0
        private void AttachPeer(IGameServerPeer peer)
        {
            if (log.IsInfoEnabled)
            {
                log.Info($"Attaching new peer to context. old peer:{this.Peer}, new peer:{peer}, ctx:{this}");
            }

            if (this.Peer != null)
            {
                var p = this.Peer;
                this.DetachPeer(p);
                p.Disconnect();
            }

            this.Peer = peer;

            this.Peer.AttachToContext(this);

            this.OnPeerAttached();
        }
Example #9
0
 public void OnGameServerDisconnect(IGameServerPeer peer, DisconnectReason reason)
 {
     this.contextManager.OnGameServerDisconnect(this, peer);
 }
Example #10
0
 public void RegisterGameServerOnInit(IRegisterGameServer request, IGameServerPeer peer)
 {
     this.RegisterGameServer(request, peer, registerByRequet: false);
 }
Example #11
0
 public void RegisterGameServer(IRegisterGameServer request, IGameServerPeer peer)
 {
     this.RegisterGameServer(request, peer, registerByRequet: true);
 }