/// <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); } }
/// <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> /// <param name="onlineInterval">online interval second</param> public static void BroadcastAction <T>(int actionId, List <T> userList, Parameters parameters, Action <T> successHandle, int onlineInterval = 0) where T : IUser { try { if (userList.Count == 0) { return; } StringBuilder shareParam = new StringBuilder(); if (parameters != null) { foreach (var parameter in parameters) { shareParam.AppendFormat("&{0}={1}", parameter.Key, parameter.Value); } } IActionDispatcher actionDispatcher = new ScutActionDispatcher(); ActionGetter actionParam; byte[] sendData = GetActionResponse(actionDispatcher, actionId, userList[0], shareParam.ToString(), out actionParam); foreach (T user in userList) { if (object.Equals(user, null)) { continue; } try { GameSession session = GameSession.Get(user.GetUserId()); if (session != null) { if ((onlineInterval <= 0 || session.LastActivityTime > MathUtils.Now.AddSeconds(-onlineInterval)) && session.SendAsync(actionParam.OpCode, sendData, 0, sendData.Length)) { if (successHandle != null) { successHandle(user); } } } } catch (Exception ex) { TraceLog.WriteError("BroadcastAction action:{0} userId:{1} error:{2}", actionId, user.GetUserId(), ex); } } } catch (Exception ex) { TraceLog.WriteError("BroadcastAction action:{0} error:{1}", actionId, ex); } }
/// <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); } }
/// <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 : IUser { StringBuilder shareParam = new StringBuilder(); if (parameters != null) { foreach (var parameter in parameters) { shareParam.AppendFormat("&{0}={1}", parameter.Key, parameter.Value); } } IActionDispatcher actionDispatcher = new ScutActionDispatcher(); ActionGetter actionParam; byte[] sendData = GetActionResponse(actionDispatcher, actionId, null, shareParam.ToString(), out actionParam); foreach (T user in userList) { if (object.Equals(user, null)) { 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.GetUserId(), ex); } } }
/// <summary> /// Object Initialization. /// </summary> public EnvironmentSetting() { var appServer = GetServerSection(); var protocol = GetProtocolSection(); var cacheSection = GetCacheSection(); var scriptSection = GetScriptSection(); CacheGlobalPeriod = cacheSection.ShareExpirePeriod; CacheUserPeriod = cacheSection.PersonalExpirePeriod; ScriptSysAsmReferences = scriptSection.SysAssemblyReferences; ScriptAsmReferences = scriptSection.AssemblyReferences; GamePort = protocol.GamePort; GameIpAddress = string.IsNullOrEmpty(protocol.GameIpAddress) ? GetLocalIp() : protocol.GameIpAddress; try { if (!string.IsNullOrEmpty(appServer.EntityAssemblyName)) { EntityAssembly = Assembly.LoadFrom(appServer.EntityAssemblyName); } } catch (Exception ex) { TraceLog.WriteError("Load entity assembly error:\"{0}\" {1}", appServer.EntityAssemblyName, ex); } ActionDispatcher = new ScutActionDispatcher(); InitSerializer(); Reset(); }
/// <summary> /// Object Initialization. /// </summary> public EnvironmentSetting() { ProductDesEnKey = productDesEnKey; ClientDesDeKey = clientDesDeKey; ProductSignKey = productSignKey; ProductCode = productCode; ProductName = productName; ProductServerId = productServerId; CacheGlobalPeriod = cacheGlobalPeriod; CacheUserPeriod = cacheUserPeriod; ScriptSysAsmReferences = scriptSysAsmReferences; ScriptAsmReferences = scriptAsmReferences; ActionEnableGZip = enableActionGZip; ActionGZipOutLength = actionGZipOutLength; GamePort = gamePort; GameIpAddress = gameIpAddress; ActionTypeName = actionTypeName; ScriptTypeName = scriptTypeName; try { EntityAssembly = Assembly.LoadFrom(entityAssemblyName); } catch{} ActionDispatcher = new ScutActionDispatcher(); }