private IChannel Create(string uri, IPEndPoint endpoint)
        {
            var channel = new MulticastChannel(endpoint, true, !_loopback);
            lock (_channels)
            {
                _channels.Add(uri, channel);
            }

            return channel;
        }
Example #2
0
        /// <summary>
        /// Creates the specified host.
        /// </summary>
        /// <param name="host">The host.</param>
        /// <param name="port">The port.</param>
        /// <param name="certificate">The certificate.</param>
        /// <param name="replayBufferSize">Size of the replay buffer.</param>
        /// <returns>IEventChannel.</returns>
        public virtual IEventChannel Create(string host, int port = 3365, X509Certificate certificate = null, int replayBufferSize = 1)
        {
            var channel = new MulticastChannel(replayBufferSize);

            async Task handler(HttpContext ctx)
            {
                var req = ctx.HttpRequest;

                if (req.Uri == "/")
                {
                    var res = new HttpResponse(200, "OK");
                    res.Headers.Add("Content-Type", "text/html");
                    res.Headers.Add("Connection", "close");
                    res.Content = "OK";
                    await ctx.ResponseChannel.Send(res, ctx.Token)
                    .ContinueWith(t => ctx.ResponseChannel.Close());
                }
                else if (req.Uri == ".stream")
                {
                    var res = new HttpResponse(200, "OK");
                    res.Headers.Add("Content-Type", "text/event-stream");
                    res.Headers.Add("Cache-Control", "no-cache");
                    res.Headers.Add("Connection", "keep-alive");
                    res.Headers.Add("Access-Control-Allow-Origin", "*");
                    await ctx.ResponseChannel.Send(res, ctx.Token)
                    .ContinueWith(t =>
                    {
                        ctx.ResponseChannel.Send(new ServerSentEvent("INFO", $"Connected successfully on LOG stream from {host}:{port}"), ctx.Token);
                        channel.AddChannel(ctx.ResponseChannel, ctx.Token);
                    });
                }
                // ESTATE
                else if (!req.Headers.TryGetValue("Estate", out var estateName) || !Estates.TryGetValue(estateName, out var estate))
                {
                    var res = new HttpResponse(404, "Not Found");
                    res.Headers.Add("Content-Type", "text/html");
                    res.Headers.Add("Connection", "close");
                    await ctx.ResponseChannel.Send(res, ctx.Token)
                    .ContinueWith(t => ctx.ResponseChannel.Close());
                }
                else
                {
                    await HandleEstate(ctx, req, estate);
                }
            }

            var httpServer = new HttpServer(host, port, handler);

            channel.AttachServer(httpServer);
            httpServer.Run(certificate);
            return(channel);
        }
Example #3
0
        public virtual IEventChannel Create(string host, int port, int bufferSize)
        {
            var channel = new MulticastChannel(bufferSize);

            var js           = GetContent(Assembly.GetExecutingAssembly().GetManifestResourceStream("BrowserLog.BrowserLog.js"));
            var htmlTemplate = GetContent(Assembly.GetExecutingAssembly().GetManifestResourceStream("BrowserLog.homepage.html"));
            var html         = htmlTemplate.Replace("HOST", Dns.GetHostName()).Replace("PORT", port.ToString());

            Action <HttpContext> handler = ctx =>
            {
                var httpResponse = new HttpResponse(200, "OK");
                if (ctx.HttpRequest.Uri == "/")
                {
                    httpResponse.AddHeader("Content-Type", "text/html");
                    httpResponse.AddHeader("Connection", "close");
                    httpResponse.Content = html;
                    ctx.ResponseChannel.Send(httpResponse, ctx.Token).ContinueWith(t => ctx.ResponseChannel.Close());
                }
                else if (ctx.HttpRequest.Uri == "/BrowserLog.js")
                {
                    httpResponse.AddHeader("Content-Type", "text/javascript");
                    httpResponse.AddHeader("Connection", "close");
                    httpResponse.Content = js;
                    ctx.ResponseChannel.Send(httpResponse, ctx.Token).ContinueWith(t => ctx.ResponseChannel.Close());
                }
                else
                {
                    httpResponse.AddHeader("Content-Type", "text/event-stream");
                    httpResponse.AddHeader("Cache-Control", "no-cache");
                    httpResponse.AddHeader("Connection", "keep-alive");
                    httpResponse.AddHeader("Access-Control-Allow-Origin", "*");
                    ctx.ResponseChannel.Send(httpResponse, ctx.Token)
                    .ContinueWith(t =>
                    {
                        ctx.ResponseChannel.Send(new ServerSentEvent("INFO",
                                                                     "Connected successfully on LOG stream from " + host + ":" + port), ctx.Token);
                        channel.AddChannel(ctx.ResponseChannel, ctx.Token);
                    });
                }
            };
            var httpServer = new HttpServer(host, port, handler);

            channel.AttachServer(httpServer);
            httpServer.Run();
            return(channel);
        }