Command injection in SuperSocket

Command injection in SuperSocket

SuperSocket uses reflection by default to inject commands into AppServers. Here is a complete sample of how to create your own CommandLoader instead of the default ReflectLoader, adding it to your AppServer without modifying any settings file.

We start by creating a custom commandloader class.

namespace Some.NameSpace
{
    public class InjectCommandLoader<TCommand> : CommandLoaderBase<TCommand> where TCommand : class, ICommand
    {
        public override bool Initialize(IRootConfig rootConfig, IAppServer appServer)
        {
            return true;
        }

        private readonly IEnumerable<TCommand> _commands;

        public InjectCommandLoader(IEnumerable<TCommand> commands)
        {
            _commands = commands;
        }

        public override bool TryLoadCommands(out IEnumerable<TCommand> commands)
        {
            commands = _commands;
            return true;
        }
    }
}

Then we create a custom AppServer class in which we override (TAppSession specifies your custom AppSession type).

namespace Some.NameSpace
{
    public class MyServer<TAppSession> : AppServer<TAppSession>, where TAppSession : AppSession<TAppSession>, new()
    {
        private readonly IEnumerable<ICommand<TAppSession, StringRequestInfo>> _commands;

        protected MyServer(IEnumerable<ICommand<TAppSession, StringRequestInfo>> commands)
        {
            _commands = commands;
        }

        protected override bool SetupCommandLoaders(List<ICommandLoader<ICommand<TAppSession, StringRequestInfo>>> commandLoaders)
        {
            commandLoaders.Add(new InjectCommandLoader<ICommand<TAppSession, StringRequestInfo>>(_commands));
            return true;
        }
    }
}

Now we have everything in place. When injecting your AppServer you can also inject all commands using your preferred dependency injection library (i like AutoFac).

Leave a Reply

Your email address will not be published. Required fields are marked *