Example #1
0
 public static void WriteLog(UserSession session, LogBase log)
 {
     if (log == null) return;
     log.worldid = zoneid;
     log.svrip = sip;
     if (session != null)
     {
         log.key = session.key;
         log.userip = session.IP.Address;
         log.domain = (int)(session.domain);
         if (log.opopenid == null)
         {
             log.opopenid = session.UserID;
         }
         if (log.opuid == 0)
         {
             PlayerBusiness player = session.Player;
             if (player != null)
             {
                 log.opuid = player.PID;
                 log.worldid = session.zoneid;
             }
         }
     }
     m_logs.Enqueue(log);
 }
Example #2
0
 /// <summary>
 ///  添加并获取旧值
 /// </summary>
 /// <param name="user"></param>
 /// <returns></returns>
 public static UserSession AddAndGetOld(UserSession user)
 {
     UserSession oldUser = null;
     m_users.AddOrUpdate(user.UserID, user, (x, y) =>
     {
         oldUser = y;
         return user;
     });
     return oldUser;
 }
Example #3
0
        /// <summary>
        /// 处理心跳包
        /// </summary>
        /// <param name="session"></param>
        public static void TResult(UserSession session)
        {
            int offset = Interlocked.Add(ref m_offset, size) & mask;
            long nowTicks = WriteTime(offset + 6);
            session.SendAsync(new Sinan.Collections.BytesSegment(m_bin, offset, len));

            TimeControl t = session.Buffer;
            if (t == null) return;

            //每个计时周期表示一百纳秒,即一千万分之一秒。1 毫秒内有 10,000 个计时周期
            //大于间隔,计数清零,连续8次间隔小于指定间隔,则断开客户端
            long tick = nowTicks - t.Ticks;
            if (tick > 5000 * 10000)
            {
                //重置警告
                t.SpeenWarn = 0;
            }
            //大于20毫秒,小于4900毫秒,则进行警告计数
            else if (tick > 20 * 10000 && tick < 4900 * 10000)
            {
                //连续8次警告,则断开客户端
                if (Interlocked.Increment(ref t.SpeenWarn) == 8)
                {
                    try
                    {
                        session.SendAsync(FastErr);
                        logger.Info(session.ToString("ForceExit|"));
                    }
                    finally
                    {
                        session.Close();
                    }
                    return;
                }
            }
            t.Ticks = nowTicks;
        }
Example #4
0
 public ISession CreateSession(Socket socket)
 {
     // 初始化session数据
     UserBuffer helper;
     if (m_bufferPool.TryDequeue(out helper))
     {
         int id = System.Threading.Interlocked.Increment(ref m_id);
         UserSession session = new UserSession(id, helper, socket, m_process);
         if (m_sessions.TryAdd(id, session))
         {
             SessionsProxy.Sessions.Enqueue(session);
             return session;
         }
         else
         {
             m_bufferPool.Enqueue(helper);
         }
     }
     return null;
 }
Example #5
0
        protected static string DecodeCommand(UserSession user, int command)
        {
            //心跳
            if (command == 103)
            {
                TResult(user);
                return string.Empty;
            }

            UserBuffer buffer = user.Buffer;
            if (buffer == null) return null;

            string name = null;
            //检查命令范围
            if (command > 100 && command <= CommandManager.MaxCommand)
            {
                //检查调用频繁度
                int ct = CommandManager.ControlTicks[command];
                if (ct > 0)
                {
                    long interval = buffer.LastInterval(command);
                    if (interval < ct)
                    {
                        if (string.IsNullOrEmpty(user.UserID))
                        {
                            user.Close();
                            return null;
                        }
                        else if (buffer.CallTicks[0]++ > 20000)
                        {
                            //发送操作太快信息
                            try
                            {
                                user.SendAsync(FastErr);
                                LogWrapper.Warn(user.ToString("Operating too fast|"));
                            }
                            finally
                            {
                                user.Close();
                            }
                            return null;
                        }
                        return string.Empty;
                    }
                }
                name = CommandManager.Instance.ReadString(command);
            }

            if (name == null)
            {
                //命令不存在时.如果用户未登录,或登录用户总计16次出错则断开
                if (string.IsNullOrEmpty(user.UserID))
                {
                    user.Close();
                }
                else if (buffer.CallTicks[1]++ > 16)
                {
                    LogWrapper.Warn(user.ToString("Protocol error|"));
                    user.Close();
                }
            }
            return name;
        }
Example #6
0
        /// <summary>
        /// 初始化玩家信息
        /// </summary>
        /// <param name="session"></param>
        public void InitPlayer(UserSession session)
        {
            m_session = session;
            if (session != null)
            {
                Online = true;
                FightTime = DateTime.UtcNow;
                this.AState = ActionState.Standing;
                //this.Coin = UserLogAccess.Instance.GetCoin(m_userID);
            }

            if (m_life == null)
            {
                setPlayerEx();
                this.m_maxExp = RoleManager.Instance.GetRoleExp(this.Level);
                m_life = CreateProperty();
                if (this.HP == 0) this.HP = m_life.ShengMing;
                if (this.MP == 0) this.MP = m_life.MoFa;
                InitWalk();
                InitFixBuffer();
            }
        }
Example #7
0
 public UserNote(UserSession session, string name, IList body = null)
     : base(name, body)
 {
     m_session = session;
     m_player = session.Player;
 }
Example #8
0
 public UserNote(PlayerBusiness player, string name, object[] body = null)
     : base(name, body)
 {
     m_player = player;
     m_session = player.Session;
 }
Example #9
0
 public UserNote(UserNote note, string name, object[] body = null)
     : base(name, body)
 {
     m_session = note.Session;
     m_player = note.Player;
 }
Example #10
0
 /// <summary>
 /// 重新连接
 /// </summary>
 /// <param name="session">新的连接</param>
 /// <returns>原来连接</returns>
 public UserSession Reconnection(UserSession session)
 {
     lock (m_locker)
     {
         Online = (session == null ? false : true);
         UserSession old = m_session;
         m_session = session;
         return old;
     }
 }
Example #11
0
 /// <summary>
 /// 断开连接
 /// </summary>
 /// <param name="session"></param>
 /// <returns></returns>
 public bool DisConnection(UserSession session)
 {
     return Interlocked.CompareExchange(ref m_session, null, session) == session;
 }
Example #12
0
 /// <summary>
 /// 处理重复登录用户
 /// </summary>
 /// <param name="oldUser"></param>
 private static void ProcessOldUser(UserSession oldUser)
 {
     try
     {
         //TODO: 需添加发送重复登录信息
         //Socket socket = oldUser.Socket;
         //if (socket != null)
         //{
         //    oldUser.Call(LoginCommand.UserLoginR, (int)LoginResult.OtherInto, null);
         //    oldUser.Socket.Shutdown(SocketShutdown.Send);
         //}
         oldUser.Close();
     }
     catch (System.Exception ex)
     {
         LogWrapper.Error(ex);
     }
 }