using System; using System.Collections.Generic; using System.Linq; using System.Text; using SuperSocket.SocketBase; using SuperSocket.SocketBase.Command; using SuperSocket.SocketBase.Protocol; namespace SuperWebSocket.SubProtocol { /// /// SubCommand base /// public abstract class SubCommandBase : SubCommandBase { } /// /// SubCommand base /// /// The type of the web socket session. public abstract class SubCommandBase : ISubCommand, ISubCommandFilterLoader where TWebSocketSession : WebSocketSession, new() { #region ISubCommand Members /// /// Gets the name. /// public virtual string Name { get { return this.GetType().Name; } } void ISubCommand.ExecuteCommand(TWebSocketSession session, SubRequestInfo requestInfo) { var filters = m_Filters; if (filters == null || filters.Length == 0) { ExecuteCommand(session, requestInfo); return; } var commandContext = new CommandExecutingContext(); commandContext.Initialize(session, requestInfo, this); for (var i = 0; i < filters.Length; i++) { var filter = filters[i]; filter.OnCommandExecuting(commandContext); if (commandContext.Cancel) break; } if (!commandContext.Cancel) { ExecuteCommand(session, requestInfo); for (var i = 0; i < filters.Length; i++) { var filter = filters[i]; filter.OnCommandExecuted(commandContext); } } } /// /// Executes the command. /// /// The session. /// The request info. public abstract void ExecuteCommand(TWebSocketSession session, SubRequestInfo requestInfo); #endregion private SubCommandFilterAttribute[] m_Filters; void ISubCommandFilterLoader.LoadSubCommandFilters(IEnumerable globalFilters) { var filters = new List(); if (globalFilters.Any()) { filters.AddRange(globalFilters); } var commandFilters = this.GetType().GetCustomAttributes(true).OfType().ToArray(); if (commandFilters.Any()) { filters.AddRange(commandFilters); } m_Filters = filters.OrderBy(f => f.Order).ToArray(); } } }