Beispiel #1
0
        protected virtual void CreateOwinProps <TContext>(IHttpApplication <TContext> application, out Func <IDictionary <string, object>, Task> appFunc, out Dictionary <string, object> props)
        {
            appFunc = async env =>
            {
                FeatureCollection features = new FeatureCollection(new OwinFeatureCollection(env));

                TContext context = application.CreateContext(features);

                try
                {
                    await application.ProcessRequestAsync(context);
                }
                catch (Exception ex)
                {
                    application.DisposeContext(context, ex);
                    throw;
                }

                application.DisposeContext(context, null);
            };

            appFunc = OwinWebSocketAcceptAdapter.AdaptWebSockets(appFunc);

            props = new Dictionary <string, object>
            {
                ["host.Addresses"] =
                    Features.Get <IServerAddressesFeature>()
                    .Addresses.Select(add => new Uri(add))
                    .Select(add => new Address(add.Scheme, add.Host, add.Port.ToString(CultureInfo.InvariantCulture), add.LocalPath).Dictionary)
                    .ToList()
            };
        }
Beispiel #2
0
        public void Start <TContext>(IHttpApplication <TContext> application)
        {
            // Note that this example does not take into account of Nowin's "server.OnSendingHeaders" callback.
            // Ideally we should ensure this method is fired before disposing the context.
            _callback = async features =>
            {
                var context = application.CreateContext(features);
                try
                {
                    await application.ProcessRequestAsync(context);
                }
                catch (Exception ex)
                {
                    application.DisposeContext(context, ex);
                    throw;
                }
                application.DisposeContext(context, null);
            };

            var address       = Features.Get <IServerAddressesFeature>().Addresses.First();
            var port          = new Uri(address).Port;
            var serverBuilder = ServerBuilder.New().SetAddress(IPAddress.Loopback).SetPort(port);

            _nowinServer = serverBuilder.SetOwinApp(OwinWebSocketAcceptAdapter.AdaptWebSockets(HandleRequest)).Build();
            _nowinServer.Start();
        }
Beispiel #3
0
        public IServerInformation Initialize(IConfiguration configuration)
        {
            var builder = ServerBuilder.New()
                          .SetAddress(IPAddress.Any)
                          .SetPort(5000)
                          .SetOwinApp(OwinWebSocketAcceptAdapter.AdaptWebSockets(HandleRequest));

            return(new NowinServerInformation(builder));
        }
Beispiel #4
0
        public IFeatureCollection Initialize(IConfiguration configuration)
        {
            // TODO: Parse config
            var builder = ServerBuilder.New()
                          .SetAddress(IPAddress.Any)
                          .SetPort(configuration["port"] != null ? Int32.Parse(configuration["port"]) : 8080)
                          .SetOwinApp(OwinWebSocketAcceptAdapter.AdaptWebSockets(HandleRequest));
            var serverFeatures = new FeatureCollection();

            serverFeatures.Set <NowinServerInformation>(new NowinServerInformation(builder));
            return(serverFeatures);
        }
        public IDisposable Start(IFeatureCollection serverFeatures, Func <IFeatureCollection, Task> application)
        {
            var builder = ServerBuilder.New()
                          .SetAddress(IPAddress.Any)
                          .SetPort(5000)
                          .SetOwinApp(OwinWebSocketAcceptAdapter.AdaptWebSockets(HandleRequest));

            _callback = application;
            var server = builder.Build();

            server.Start();
            return(server);
        }
        public Task StartAsync <TContext>(IHttpApplication <TContext> application, CancellationToken cancellationToken)
        {
            return(Task.Run(() =>
            {
                Func <IDictionary <string, object>, Task> appFunc = async env =>
                {
                    var owinFeatures = new OwinFeatureCollection(env);
                    var features = new FeatureCollection(owinFeatures);

                    var owinHttpResponse = features.Get <IHttpResponseFeature>();
                    features.Set <IHttpResponseFeature>(new NoOnStartingHttpResponseFeature(owinHttpResponse));

                    var context = application.CreateContext(features);
                    try
                    {
                        await application.ProcessRequestAsync(context);
                    }
                    catch (Exception ex)
                    {
                        application.DisposeContext(context, ex);
                        throw;
                    }

                    application.DisposeContext(context, null);
                };

                // Convert OWIN WebSockets to ASP.NET Core WebSockets
                appFunc = OwinWebSocketAcceptAdapter.AdaptWebSockets(appFunc);

                // Wrap this into a middleware handler that Fos can accept
                Func <IDictionary <string, object>, Func <IDictionary <string, object>, Task>, Task> middlewareHandler = async(env, next) =>
                {
                    await appFunc(env);

                    if (next != null)
                    {
                        await next(env);
                    }
                };

                cgiServer = new FosSelfHost(builder =>
                {
                    builder.Use(middlewareHandler);
                });

                cgiServer.Bind(IPAddress.Loopback, 9000);
                cgiServer.Start(true);
            }, cancellationToken));
        }
Beispiel #7
0
        public Task StartAsync <TContext>(IHttpApplication <TContext> application, CancellationToken cancellationToken)
        {
            // Note that this example does not take into account of Nowin's "server.OnSendingHeaders" callback.
            // Ideally we should ensure this method is fired before disposing the context.
            Func <IDictionary <string, object>, Task> appFunc = async env =>
            {
                // The reason for 2 level of wrapping is because the OwinFeatureCollection isn't mutable
                // so features can't be added
                var features = new FeatureCollection(new OwinFeatureCollection(env));

                var context = application.CreateContext(features);
                try
                {
                    await application.ProcessRequestAsync(context);
                }
                catch (Exception ex)
                {
                    application.DisposeContext(context, ex);
                    throw;
                }

                application.DisposeContext(context, null);
            };

            // Add the web socket adapter so we can turn OWIN websockets into ASP.NET Core compatible web sockets.
            // The calling pattern is a bit different
            appFunc = OwinWebSocketAcceptAdapter.AdaptWebSockets(appFunc);

            // Get the server addresses
            var address = Features.Get <IServerAddressesFeature>().Addresses.First();

            var       uri  = new Uri(address);
            var       port = uri.Port;
            IPAddress ip;

            if (!IPAddress.TryParse(uri.Host, out ip))
            {
                ip = IPAddress.Loopback;
            }

            _nowinServer = _builder.SetAddress(ip)
                           .SetPort(port)
                           .SetOwinApp(appFunc)
                           .Build();
            _nowinServer.Start();

            return(Task.CompletedTask);
        }
Beispiel #8
0
        public void Start <TContext>(IHttpApplication <TContext> application)
        {
            Func <IDictionary <string, object>, Task> appFunc = async env =>
            {
                FeatureCollection features = new FeatureCollection(new OwinFeatureCollection(env));

                TContext context = application.CreateContext(features);

                try
                {
                    await application.ProcessRequestAsync(context);
                }
                catch (Exception ex)
                {
                    application.DisposeContext(context, ex);
                    throw;
                }

                application.DisposeContext(context, null);
            };

            appFunc = OwinWebSocketAcceptAdapter.AdaptWebSockets(appFunc);

            Dictionary <string, object> props = new Dictionary <string, object>
            {
                ["host.Addresses"] =
                    Features.Get <IServerAddressesFeature>()
                    .Addresses.Select(add => new Uri(add))
                    .Select(add => new Address(add.Scheme, add.Host, add.Port.ToString(), add.LocalPath).Dictionary)
                    .ToList()
            };


            Suave.Owin.OwinServerFactory.Initialize(props);

            _suaveServer = Suave.Owin.OwinServerFactory.Create(appFunc, props);
        }