private async Task ExecuteConnectModules(ITcpChannel channel, ClientContext context)
        {
            var result = ModuleResult.Continue;

            for (var i = 0; i < _modules.Length; i++)
            {
                var connectMod = _modules[i] as IConnectionEvents;
                if (connectMod == null)
                    continue;

                try
                {
                    result = await connectMod.OnClientConnected(context);
                }
                catch (Exception exception)
                {
                    context.Error = exception;
                    result = ModuleResult.SendResponse;
                }

                if (result != ModuleResult.Continue)
                    break;
            }

            switch (result)
            {
                case ModuleResult.Disconnect:
                    channel.Close();
                    break;
                case ModuleResult.SendResponse:
                    channel.Send(context.ResponseMessage);
                    break;
            }
        }
 private async Task EndRequestAsync(ClientContext context)
 {
     for (var i = 0; i < _modules.Length; i++)
     {
         await _modules[i].EndRequest(context);
     }
 }
Example #3
0
 private void OnClientMessage(ITcpChannel channel, object message)
 {
     var context = new ClientContext(channel, message);
     ExecuteModules(channel, context).Wait();
 }
Example #4
0
 private void OnClientDisconnect(object sender, ClientDisconnectedEventArgs e)
 {
     var context = new ClientContext(e.Channel, null);
     ExecuteDisconnectModules(context).Wait();
 }
Example #5
0
        private async Task ExecuteModules(ITcpChannel channel, ClientContext context)
        {
            var result = ModuleResult.Continue;

            for (var i = 0; i < _modules.Length; i++)
            {
                try
                {
                    await _modules[i].BeginRequestAsync(context);
                }
                catch (Exception exception)
                {
                    context.Error = exception;
                    result = ModuleResult.SendResponse;
                }
            }


            if (result == ModuleResult.Continue)
            {
                for (var i = 0; i < _modules.Length; i++)
                {
                    try
                    {
                        result = await _modules[i].ProcessAsync(context);
                    }
                    catch (Exception exception)
                    {
                        context.Error = exception;
                        result = ModuleResult.SendResponse;
                    }

                    if (result != ModuleResult.Continue)
                        break;
                }
            }


            try
            {
                await EndRequestAsync(context);
            }
            catch (Exception exception)
            {
                if (context.ResponseMessage == null)
                    context.ResponseMessage = exception;
                result = ModuleResult.Disconnect;
            }

            if (context.ResponseMessage != null)
                channel.Send(context.ResponseMessage);

            if (result == ModuleResult.Disconnect)
            {
                channel.Close();
            }
        }
        private async Task ExecuteDisconnectModules(ITcpChannel channel, ClientContext context)
        {
            for (var i = 0; i < _modules.Length; i++)
            {
                var connectMod = _modules[i] as IConnectionEvents;
                if (connectMod == null)
                    continue;

                try
                {
                    await connectMod.OnClientDisconnect(context);
                }
                catch (Exception exception)
                {
                    //TODO: Alert user of failure
                }
            }
        }