private void socketLintener_DataReceived(object sender, ConnectionEventArgs e)
 {
     try
     {
         OnReceivedBefore(e);
         RequestPackage package;
         if (!ActionDispatcher.TryDecodePackage(e, out package))
         {
             return;
         }
         var session = GetSession(e, package);
         CheckSpecialPackge(package, session);
         package.Bind(session);
         //检查是否已连接路由服
         var switchSession = ServerSsMgr.GetSwitchSession();
         if (switchSession == null)
         {
             ProcessNotReady(package, session).Wait();
         }
         else
         {
             SendAsyncToSwitch(e, session.SessionId).Wait();
         }
     }
     catch (Exception ex)
     {
         TraceLog.WriteError("Received to Host:{0} error:{1}", e.Socket.RemoteEndPoint, ex);
     }
 }
Ejemplo n.º 2
0
 /// <summary>
 /// 业务逻辑处理
 /// </summary>
 /// <returns>false:中断后面的方式执行并返回Error</returns>
 public override bool TakeAction()
 {
     if (ServerSsMgr.Get(Current.SessionId) != null)
     {
         ServerHelper.OnCloseCSBefore(Current);
     }
     return(true);
 }
Ejemplo n.º 3
0
 /// <summary>
 /// 社交服准备关闭
 /// </summary>
 static public void OnCloseSocialSBefore(GameSession session)
 {
     if (SocialSession != null && SocialSession.SessionId == session.SessionId)
     {
         Console.WriteLine("社交服准备关闭");
         ServerSsMgr.DelSession(session);
         SocialSession = null;
     }
 }
 /// <summary>
 /// 挖豹子服准备关闭
 /// </summary>
 static public void OnCloseWabaoziSBefore(GameSession session)
 {
     if (WabaoziSession != null && WabaoziSession.SessionId == session.SessionId)
     {
         Console.WriteLine("挖豹子服准备关闭");
         ServerSsMgr.DelSession(session);
         WabaoziSession = null;
     }
 }
Ejemplo n.º 5
0
 /// <summary>
 /// 拉霸机服准备关闭
 /// </summary>
 static public void OnCloseSuperMonreySBefore(GameSession session)
 {
     if (SuperMonreySession != null && SuperMonreySession.SessionId == session.SessionId)
     {
         Console.WriteLine("拉霸机服准备关闭");
         ServerSsMgr.DelSession(session);
         SuperMonreySession = null;
     }
 }
        //////////////////////////////////////////////////////////////////////////////////////////
        public static GameSession GetConnectSession(string proxId)
        {
            var proxIdData = GetProxIdData(proxId);

            if (proxIdData == null)
            {
                return(null);
            }
            return(ServerSsMgr.Get(proxIdData.serverSid));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 大厅服准备关闭
        /// </summary>
        static public void OnCloseLSBefore(GameSession session)
        {
            var lobbySs = ServerSsMgr.GetLobbySession();

            if (lobbySs != null && lobbySs.SessionId == session.SessionId)
            {
                Console.WriteLine("大厅服断开连接");
                ServerSsMgr.SetLobbySession(null);
                ServerSsMgr.DelSession(session);
            }
        }
        /// <summary>
        /// 异步发送给路由服
        /// </summary>
        public async System.Threading.Tasks.Task SendAsyncToSwitch(ConnectionEventArgs e, string proxyId)
        {
            //转发给路由服,并通知路由服转发给大厅服
            var switchSession = ServerSsMgr.GetSwitchSession();
            var ssidStr       = HttpUtility.UrlEncode(string.Format("&proxyId={0}&proxyIp={1}", proxyId, e.Socket.RemoteEndPoint.ToString()));
            var ssidData      = Encoding.UTF8.GetBytes(ssidStr);
            var buffer        = new byte[e.Data.Length + ssidData.Length];

            Buffer.BlockCopy(e.Data, 0, buffer, 0, e.Data.Length);
            Buffer.BlockCopy(ssidData, 0, buffer, e.Data.Length, ssidData.Length);
            await switchSession.SendAsync(buffer, 0, buffer.Length);
        }
Ejemplo n.º 9
0
 /// <summary>
 /// 设置社交服
 /// </summary>
 public static void SetSocialS(GameSession ss)
 {
     //判断
     if (!ss.IsAuthorized || ss.UserId != (int)GlobalDefine.ServerIdType.SocialId)
     {
         TraceLog.WriteError("非法开启社交服");
         return;
     }
     ServerSsMgr.AddSession(ss);
     //
     TraceLog.WriteLine("社交服加入成功");
 }
Ejemplo n.º 10
0
 /// <summary>
 /// 设置大厅服
 /// </summary>
 public static void SetLS(int serverId, GameSession ss)
 {
     //判断
     if (!ss.IsAuthorized)
     {
         TraceLog.WriteError("非法开启大厅服,ServerId:{0}", serverId);
         return;
     }
     ServerSsMgr.SetLobbySession(ss);
     ServerSsMgr.AddSession(ss);
     //
     TraceLog.WriteLine("大厅服加入成功");
 }
Ejemplo n.º 11
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        /////////////////////////////////////////////////////大厅服/////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// 是否存在大厅服
        /// </summary>
        static public bool IsExistLS()
        {
            var lobbySs = ServerSsMgr.GetLobbySession();

            if (lobbySs != null)
            {
                if (lobbySs.IsHeartbeatTimeout)
                {
                    ServerSsMgr.SetLobbySession(null);
                    ServerSsMgr.DelSession(lobbySs);
                }
            }
            return(ServerSsMgr.GetLobbySession() != null);
        }
Ejemplo n.º 12
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        /////////////////////////////////////////////////////其它///////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// 其它服断开处理
        /// </summary>
        /// <param name="session"></param>
        public static void OnOtherServerDisconnected(GameSession session)
        {
            string serverSid = session.SessionId;

            do
            {
                var serverInfo = sConnectServerSet.Find(m => m.CSSid == serverSid);
                if (serverInfo != null)
                {
                    Console.WriteLine("连接服断开连接,ServerId:{0},剩余:{1}", serverInfo.ServerId, sConnectServerSet.FindAll(false).Count - 1);
                    sConnectServerSet.Delete(serverInfo);
                    break;
                }
                if (ServerSsMgr.GetLobbySession() != null &&
                    ServerSsMgr.GetLobbySession().SessionId == session.SessionId)
                {
                    Console.WriteLine("大厅服断开连接");
                    ServerSsMgr.SetLobbySession(null);
                    break;
                }
                if (session.UserId == GlobalDefine.ServerIdType.SocialId.ToInt())
                {
                    Console.WriteLine("社交服断开连接");
                    SocialHelper.SocialSession = null;
                    break;
                }
                if (session.UserId == GlobalDefine.ServerIdType.SuperMonreyId.ToInt())
                {
                    Console.WriteLine("拉霸机服断开连接");
                    SuperMonreyHelper.SuperMonreySession = null;
                    break;
                }
                if (session.UserId == GlobalDefine.ServerIdType.WabaoziId.ToInt())
                {
                    Console.WriteLine("挖豹子服断开连接");
                    WbzServerHelper.WabaoziSession = null;
                    break;
                }
                var rsInfo = sRoomServerSet.Find(m => m.RSSid == serverSid);
                if (rsInfo != null)
                {
                    Console.WriteLine("房间服断开连接,ServerId:{0},剩余:{1}", rsInfo.ServerId, sRoomServerSet.FindAll(false).Count - 1);
                    sRoomServerSet.Delete(rsInfo);
                    break;
                }
            } while (false);
            //
            ServerSsMgr.DelSession(session);
        }
        /// <summary>
        /// 异步通知路由服客户端断开
        /// </summary>
        public async System.Threading.Tasks.Task SendSwitchClientDisconnect(GameSession session)
        {
            //转发给路由服,并通知路由服转发给大厅服
            var switchSession = ServerSsMgr.GetSwitchSession();

            if (switchSession == null)
            {
                return;
            }
            RequestParam interruptParam = new RequestParam();

            interruptParam["ActionId"] = (int)ActionEnum.Interrupt;
            interruptParam["MsgId"]    = 0;
            interruptParam["proxyId"]  = session.SessionId;
            string post   = string.Format("?d={0}", HttpUtility.UrlEncode(interruptParam.ToPostString()));
            var    buffer = Encoding.ASCII.GetBytes(post);
            await switchSession.SendAsync(buffer, 0, buffer.Length);
        }
        /// <summary>
        /// 是否存在挖豹子服
        /// </summary>
        static public bool IsExistWabaoziS()
        {
            var wabaoziSs = WabaoziSession;

            if (wabaoziSs != null)
            {
                if (wabaoziSs.IsHeartbeatTimeout)
                {
                    ServerSsMgr.DelSession(wabaoziSs);
                    return(false);
                }
                else
                {
                    return(true);
                }
            }
            return(false);
        }
Ejemplo n.º 15
0
        /// <summary>
        /// 是否存在社交服
        /// </summary>
        static public bool IsExistSocialS()
        {
            var socialSs = ServerSsMgr.Get((int)GlobalDefine.ServerIdType.SocialId);

            if (socialSs != null)
            {
                if (socialSs.IsHeartbeatTimeout)
                {
                    ServerSsMgr.DelSession(socialSs);
                    return(false);
                }
                else
                {
                    return(true);
                }
            }
            return(false);
        }
        private GameSession GetSession(SocketEventArgs e, RequestPackage package)
        {
            //使用代理分发器时,每个ssid建立一个游服Serssion
            GameSession session       = null;
            var         isProxyClient = !string.IsNullOrEmpty(package.ProxyId);

            if (isProxyClient)
            {
                var proxyGuid = GameSession.SessionId2Guid(package.ProxyId);
                if (!proxyGuid.Equals(Guid.Empty))
                {
                    session = GameSession.Get(proxyGuid);
                    if (session == null)
                    {
                        var switchSession = ServerSsMgr.GetSwitchSession();
                        if (switchSession != null)
                        {
                            session = GameSession.CreateNew(proxyGuid, package.ProxyId, switchSession.KeyCode, package.ProxyIp);
                        }
                        else
                        {
                            TraceLog.WriteError("LobbySocketClient GetSession error, switchSession = null");
                        }
                    }
                }
            }
            else
            {
                session = GameSession.Get(package.SessionId) ?? GameSession.Get(e.Socket.HashCode);
            }
            if (session == null)
            {
                session = GameSession.CreateNew(e.Socket.HashCode, e.Socket, _socketLobby);
            }
            if (!isProxyClient && (!session.Connected || !Equals(session.RemoteAddress, e.Socket.RemoteEndPoint.ToString())))
            {
                GameSession.Recover(session, e.Socket.HashCode, e.Socket, _socketLobby);
            }
            else if (isProxyClient && !Equals(session.RemoteAddress, package.ProxyIp))
            {
                GameSession.RecoverProxy(session, package.ProxyIp);
            }
            return(session);
        }
Ejemplo n.º 17
0
        /// <summary>
        /// 增加连接服
        /// </summary>
        static public void AddCS(int serverId, GameSession ss)
        {
            //判断
            if (!ss.IsAuthorized)
            {
                TraceLog.WriteError("非法开启连接服,ServerId:{0}", serverId);
                return;
            }
            //加入
            var sConnectServer = new SConnectServer()
            {
                ServerId = serverId, Status = ServerStatus.Connected, CSSid = ss.SessionId, UserId = ss.UserId
            };

            sConnectServerSet.Add(sConnectServer);

            ServerSsMgr.AddSession(ss);
            //
            TraceLog.WriteLine("连接服加入成功,ServerId:{0},剩余:{1}", serverId, sConnectServerSet.FindAll(false).Count);
        }
Ejemplo n.º 18
0
        /// <summary>
        /// 是否存在连接服
        /// </summary>
        static public bool IsExistCS(int _serverId)
        {
            var connectServer = sConnectServerSet.FindKey(_serverId);

            if (connectServer != null)
            {
                var connectSs = ServerSsMgr.Get(connectServer.CSSid);
                if (connectSs != null)
                {
                    if (connectSs.IsHeartbeatTimeout)
                    {
                        ServerSsMgr.DelSession(connectSs);
                        sConnectServerSet.Delete(connectServer);
                        connectServer = null;
                    }
                }
            }

            return(connectServer != null);
        }
Ejemplo n.º 19
0
        /// <summary>
        /// 是否存在房间服
        /// </summary>
        static public bool IsExistRS(int _serverId)
        {
            var roomServer = sRoomServerSet.FindKey(_serverId);

            if (roomServer != null)
            {
                var roomSs = ServerSsMgr.Get(roomServer.RSSid);
                if (roomSs != null)
                {
                    if (roomSs.IsHeartbeatTimeout)
                    {
                        ServerSsMgr.DelSession(roomSs);
                        sRoomServerSet.Delete(roomServer);
                        roomServer = null;
                    }
                }
            }

            return(roomServer != null);
        }
Ejemplo n.º 20
0
 private void socketLintener_DataReceived(object sender, ConnectionEventArgs e)
 {
     try
     {
         OnReceivedBefore(e);
         RequestPackage package;
         if (!ActionDispatcher.TryDecodePackage(e, out package))
         {
             return;
         }
         var session = GetSession(e, package);
         if (string.IsNullOrEmpty(package.ProxyId) && CheckSpecialPackge(package, session))
         {
             return;
         }
         package.Bind(session);
         if (!string.IsNullOrEmpty(package.ProxyId))
         {
             //转发给大厅服
             var lobbySs = ServerSsMgr.GetLobbySession();
             if (lobbySs == null)
             {
                 TraceLog.WriteError("SwitchSocketHost socketLintener_DataReceived error, lobbSs == null");
                 return;
             }
             SwitchSessionMgr.AddOrUpdateProxIdData(package.ProxyId, session.SessionId);
             JumpToLobbyServer(lobbySs, e).Wait();
         }
         else
         {
             ProcessPackage(package, session);
         }
     }
     catch (Exception ex)
     {
         TraceLog.WriteError("Received to Host:{0} error:{1}", e.Socket.RemoteEndPoint, ex);
     }
 }
        private async System.Threading.Tasks.Task ProcessPackage(RequestPackage package, GameSession session)
        {
            if (package == null)
            {
                return;
            }

            try
            {
                ActionGetter actionGetter;
                byte[]       data = new byte[0];
                if (!string.IsNullOrEmpty(package.RouteName))
                {
                    actionGetter = ActionDispatcher.GetActionGetter(package, session);
                    if (CheckRemote(package.RouteName, actionGetter))
                    {
                        MessageStructure response = new MessageStructure();
                        OnCallRemote(package.RouteName, actionGetter, response);
                        data = response.PopBuffer();
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    SocketGameResponse response = new SocketGameResponse();
                    response.WriteErrorCallback += ActionDispatcher.ResponseError;
                    actionGetter = ActionDispatcher.GetActionGetter(package, session);
                    DoAction(actionGetter, response);
                    data = response.ReadByte();
                }
                try
                {
                    if (data.Length > 0)
                    {
                        var switchSession = ServerSsMgr.GetSwitchSession();
                        if (switchSession != null && !string.IsNullOrEmpty(package.ProxyId))
                        {
                            var    paramStr = "ActionId=101&MsgId=0&UserSid=" + package.ProxyId;
                            string sign     = SignUtils.EncodeSign(paramStr, RequestParam.SignKey);
                            paramStr += string.Format("&{0}={1}", "sign", sign);
                            var    postData   = Encoding.UTF8.GetBytes(string.Format("?d={0}", paramStr));
                            byte[] paramBytes = new byte[postData.Length + PackageReader.EnterChar.Length + data.Length];
                            Buffer.BlockCopy(postData, 0, paramBytes, 0, postData.Length);
                            Buffer.BlockCopy(PackageReader.EnterChar, 0, paramBytes, postData.Length, PackageReader.EnterChar.Length);
                            Buffer.BlockCopy(data, 0, paramBytes, postData.Length + PackageReader.EnterChar.Length, data.Length);

                            await switchSession.SendAsync(actionGetter.OpCode, paramBytes, 0, paramBytes.Length, OnSendCompleted);
                        }
                    }
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError("PostSend error:{0}", ex);
                }
            }
            catch (Exception ex)
            {
                TraceLog.WriteError("Task error:{0}", ex);
            }
            finally
            {
                if (session != null)
                {
                    session.ExitSession();
                }
            }
        }