コード例 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="actionId"></param>
        /// <param name="sessionList"></param>
        /// <param name="package"></param>
        /// <param name="complateHandle"></param>
        /// <param name="onlineInterval"></param>
        /// <returns></returns>
        public static async System.Threading.Tasks.Task BroadcastAction(int actionId, List <GameSession> sessionList, RequestPackage package, Action <GameSession, SocketAsyncResult> complateHandle, int onlineInterval)
        {
            try
            {
                if (sessionList.Count == 0)
                {
                    return;
                }
                if (sessionList.Exists(s => Equals(s, null)))
                {
                    throw new ArgumentNullException("Session is a null value.");
                }

                IActionDispatcher actionDispatcher = new ScutActionDispatcher();
                byte[]            sendBuffer       = GetActionResponse(actionDispatcher, actionId, sessionList[0], package);

                foreach (var session in sessionList)
                {
                    GameSession temp = session;
                    try
                    {
                        if (onlineInterval <= 0 || session.LastActivityTime > MathUtils.Now.AddSeconds(-onlineInterval))
                        {
                            await session.SendAsync(package.OpCode, sendBuffer, 0, sendBuffer.Length, result =>
                            {
                                if (complateHandle != null)
                                {
                                    complateHandle(temp, result);
                                }
                            });
                        }
                        else
                        {
                            if (complateHandle != null)
                            {
                                complateHandle(temp, new SocketAsyncResult(sendBuffer)
                                {
                                    Result = ResultCode.Close
                                });
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        if (complateHandle != null)
                        {
                            complateHandle(temp, new SocketAsyncResult(sendBuffer)
                            {
                                Result = ResultCode.Error, Error = ex
                            });
                        }
                        TraceLog.WriteError("BroadcastAction  action:{0} userId:{1} error:{2}", actionId, session.UserId, ex);
                    }
                }
            }
            catch (Exception ex)
            {
                TraceLog.WriteError("BroadcastAction  action:{0} error:{1}", actionId, ex);
            }
        }
コード例 #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="session"></param>
        /// <param name="actionId"></param>
        /// <param name="package"></param>
        /// <param name="complateHandle"></param>
        /// <param name="onlineInterval"></param>
        /// <returns></returns>
        public static async System.Threading.Tasks.Task SendAction(GameSession session, int actionId, RequestPackage package, Action <GameSession, SocketAsyncResult> complateHandle, int onlineInterval)
        {
            IActionDispatcher actionDispatcher = new ScutActionDispatcher();
            GameSession       temp             = session;

            byte[] sendBuffer = null;
            try
            {
                sendBuffer = GetActionResponse(actionDispatcher, actionId, session, package);
                if ((onlineInterval <= 0 || session.LastActivityTime > MathUtils.Now.AddSeconds(-onlineInterval)))
                {
                    await session.SendAsync(package.OpCode, sendBuffer, 0, sendBuffer.Length, result =>
                    {
                        if (complateHandle != null)
                        {
                            complateHandle(temp, result);
                        }
                    });
                }
                else
                {
                    if (complateHandle != null)
                    {
                        complateHandle(temp, new SocketAsyncResult(sendBuffer)
                        {
                            Result = ResultCode.Close
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                if (complateHandle != null)
                {
                    complateHandle(temp, new SocketAsyncResult(sendBuffer)
                    {
                        Result = ResultCode.Error, Error = ex
                    });
                }
                TraceLog.WriteError("SendToClient action:{0} userId:{1} error:{2}", actionId, session.UserId, ex);
            }
        }