Пример #1
0
        private void WebClientConnectionWorker(StreamClient client)
        {
            client.ClientPosition = _ringBuffer.NextAddPosition;
            var data = new byte[1500];

            while (true)
            {
                while (client.ClientPosition == _ringBuffer.NextAddPosition)
                {
                    Thread.Sleep(1);
                }

                if (client.ClientPosition > _ringBuffer.BufferSize)
                {
                    client.ClientPosition = 0;
                }

                int dataLen;
                if (_ringBuffer.Peek(client.ClientPosition, ref data, out dataLen) != 0)
                {
                    Console.WriteLine("Resizing buffer");
                    data = new byte[dataLen];
                    _ringBuffer.Peek(client.ClientPosition, ref data, out dataLen);
                }

                try
                {
                    if (dataLen != 0)
                    {
                        if (WebSocket != null)
                        {
                            if (WebSocket.State == WebSocketState.Open)
                            {
                                var arraySeg = WebSocket.CreateServerBuffer(data.Length);
                                Buffer.BlockCopy(data, RtpHeaderSize, arraySeg.Array, 0, data.Length - RtpHeaderSize);
                                WebSocket.SendAsync(arraySeg, WebSocketMessageType.Binary, true, new CancellationToken());
                            }
                            else
                            {
                                return;
                            }
                        }
                        else
                        {
                            client.OutputWriter.Write(data, RtpHeaderSize, dataLen - RtpHeaderSize);
                        }
                    }
                }
                catch (Exception)
                {
                    return;
                }
                client.ClientPosition++;
            }
        }
Пример #2
0
        private static void RunHttpListener()
        {
            _httpListener = new HttpListener();

            if (!IsNullOrEmpty(_options.AdapterAddress))
            {
            }

            var prefix = Format(UrlPrefix, "+", _options.ListenPort);

            _httpListener.Prefixes.Add(prefix + _options.UrlIdentifier + "/");

            try
            {
                _httpListener.Start();
            }
            catch (Exception ex)
            {
                PrintToConsole("Exception creating web listener: " + ex.Message);
                PrintToConsole("Probably the URL is not reserved - either reserve, or run as admin!");
                PrintToConsole("For reference, type from elevated command prompt:");
                PrintToConsole($@"netsh http add urlacl url={prefix} user=BUILTIN\users");
                PrintToConsole("");
                PrintToConsole("Hit any key to exit");
                Console.ReadLine();
                Environment.Exit((int)ExitCodes.UrlAccessDenied);
            }

            ThreadPool.QueueUserWorkItem(o =>
            {
                try
                {
                    while (_httpListener.IsListening)
                    {
                        ThreadPool.QueueUserWorkItem(c =>
                        {
                            var ctx = c as HttpListenerContext;
                            try
                            {
                                if (ctx == null)
                                {
                                    return;
                                }

                                ctx.Response.ContentType = "video/mp2t";
                                ctx.Response.Headers.Add("Access-Control-Allow-Origin", "*");

                                if (ctx.Request.RemoteEndPoint == null)
                                {
                                    return;
                                }

                                var streamClient = new StreamClient(_ringBuffer)
                                {
                                    ClientAddress = ctx.Request.RemoteEndPoint.ToString()
                                };

                                if (ctx.Request.IsWebSocketRequest)
                                {
                                    HttpListenerWebSocketContext webSocketContext = ctx.AcceptWebSocketAsync(null).Result;
                                    WebSocket webSocket    = webSocketContext.WebSocket;
                                    streamClient.WebSocket = webSocket;
                                }
                                else
                                {
                                    streamClient.OutputWriter = new BinaryWriter(ctx.Response.OutputStream);
                                }

                                lock (StreamingClients)
                                {
                                    StreamingClients.Add(streamClient);
                                }

                                streamClient.Start();
                            }
                            catch (Exception ex)
                            {
                                PrintToConsole($"Exception: {ex.Message}");
                            }
                        }, _httpListener.GetContext());
                    }
                }
                catch (Exception ex)
                {
                    PrintToConsole($"Exception: {ex.Message}");
                }
            });
        }