Beispiel #1
0
        /// <summary>
        /// 将指定的Action结果广播给指定范围的玩家
        /// </summary>
        /// <typeparam name="T">BaseUser对象</typeparam>
        /// <param name="actionId">指定的Action</param>
        /// <param name="userList">指定范围的玩家</param>
        /// <param name="parameters">请求参数</param>
        /// <param name="successHandle">成功回调</param>
        public static void BroadcastAction <T>(int actionId, List <T> userList, Parameters parameters, Action <T> successHandle) where T : BaseUser
        {
            StringBuilder shareParam = new StringBuilder();

            if (parameters != null)
            {
                foreach (var parameter in parameters)
                {
                    shareParam.AppendFormat("&{0}={1}", parameter.Key, parameter.Value);
                }
            }
            IActionDispatcher actionDispatcher = new ActionDispatcher();
            ActionGetter      actionParam;

            byte[] sendData = GetActionResponse(actionDispatcher, actionId, null, shareParam.ToString(), out actionParam);
            foreach (var user in userList)
            {
                if (user == default(T))
                {
                    continue;
                }
                try
                {
                    GameSession session = GameSession.Get(user.GetUserId());
                    if (session != null)
                    {
                        if (session.SendAsync(sendData, 0, sendData.Length))
                        {
                            if (successHandle != null)
                            {
                                successHandle(user);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError("BroadcastAction  action:{0} userId:{1} error:{2}", actionId, user.PersonalId, ex);
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// 
        /// </summary>
        protected GameSocketHost()
        {
            ActionDispatcher = new ActionDispatcher();
            int port = GameEnvironment.Setting.GamePort;
            IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, port);

            int maxConnections = ConfigUtils.GetSetting("MaxConnections", 10000);
            int backlog = ConfigUtils.GetSetting("Backlog", 1000);
            int maxAcceptOps = ConfigUtils.GetSetting("MaxAcceptOps", 1000);
            int bufferSize = ConfigUtils.GetSetting("BufferSize", 8192);
            int expireInterval = ConfigUtils.GetSetting("ExpireInterval", 600) * 1000;
            int expireTime = ConfigUtils.GetSetting("ExpireTime", 3600) * 1000;

            threadPool = new SmartThreadPool(180 * 1000, 100, 5);
            threadPool.Start();

            var socketSettings = new SocketSettings(maxConnections, backlog, maxAcceptOps, bufferSize, localEndPoint, expireInterval, expireTime);
            socketLintener = new SocketListener(socketSettings);
            socketLintener.DataReceived += new ConnectionEventHandler(socketLintener_DataReceived);
            socketLintener.Connected += new ConnectionEventHandler(socketLintener_OnConnectCompleted);
            socketLintener.Disconnected += new ConnectionEventHandler(socketLintener_Disconnected);

            httpListener = new HttpListener();
            var httpHost = ConfigUtils.GetSetting("Game.Http.Host");
            var httpPort = ConfigUtils.GetSetting("Game.Http.Port", 80);
            var httpName = ConfigUtils.GetSetting("Game.Http.Name", "Service.aspx");

            if (!string.IsNullOrEmpty(httpHost))
            {
                EnableHttp = true;
                var hosts = httpHost.Split(',');
                foreach (var host in hosts)
                {
                    string address = host.StartsWith("http", StringComparison.InvariantCultureIgnoreCase)
                                         ? host
                                         : "http://" + host;
                    httpListener.Prefixes.Add(string.Format("{0}:{1}/{2}/", address, httpPort, httpName));
                }
            }
            Interlocked.Exchange(ref _runningQueue, 1);
            queueProcessThread = new Thread(ProcessQueue);
            queueProcessThread.Start();
            _LockedQueueChecker = new Timer(LockedQueueChecker, null, 100, 100);
        }