Beispiel #1
0
        private void OnStateWaitResponse()
        {
            Status = SourceStreamStatus.Connecting;
            HTTPResponse response = null;

            if (Recv(stream => { response = HTTPResponseReader.Read(stream); }))
            {
                if (response.Status == 200)
                {
                    lastReceived = Environment.TickCount;
                    state        = State.Receiving;
                }
                else
                {
                    Logger.Error("Server responses {0} to GET {1}", response.Status, SourceUri.PathAndQuery);
                    EndConnection();
                    state = State.Retrying;
                }
            }
            else if (Environment.TickCount - waitResponseStart > 10000)
            {
                Logger.Error("Recv response timed out");
                EndConnection();
                state = State.Retrying;
            }
        }
        protected override async Task DoProcess(CancellationToken cancel_token)
        {
            try {
                this.Status = ConnectionStatus.Connecting;
                var host = SourceUri.DnsSafeHost;
                if (SourceUri.Port != -1 && SourceUri.Port != 80)
                {
                    host = String.Format("{0}:{1}", SourceUri.DnsSafeHost, SourceUri.Port);
                }
                var request = String.Format(
                    "GET {0} HTTP/1.1\r\n" +
                    "Host: {1}\r\n" +
                    "User-Agent: NSPlayer ({2})\r\n" +
                    "Connection: close\r\n" +
                    "Pragma: stream-switch\r\n" +
                    "\r\n",
                    SourceUri.PathAndQuery,
                    host,
                    PeerCast.AgentName);
                await connection.Stream.WriteAsync(System.Text.Encoding.UTF8.GetBytes(request)).ConfigureAwait(false);

                Logger.Debug("Sending request:\n" + request);

                response = null;
                response = await HTTPResponseReader.ReadAsync(connection.Stream, cancel_token).ConfigureAwait(false);

                if (response.Status != 200)
                {
                    Logger.Error("Server responses {0} to GET {1}", response.Status, SourceUri.PathAndQuery);
                    Stop(response.Status == 404 ? StopReason.OffAir : StopReason.UnavailableError);
                }

                this.Status = ConnectionStatus.Connected;
                await contentReader.ReadAsync(contentSink, connection.Stream, cancel_token).ConfigureAwait(false);

                Stop(StopReason.OffAir);
            }
            catch (InvalidDataException) {
                Stop(StopReason.ConnectionError);
            }
            catch (OperationCanceledException) {
                Logger.Error("Recv content timed out");
                Stop(StopReason.ConnectionError);
            }
            catch (IOException) {
                Stop(StopReason.ConnectionError);
            }
            this.Status = ConnectionStatus.Error;
        }
        protected override async Task DoProcess(CancellationToken cancel_token)
        {
            try {
                this.Status = ConnectionStatus.Connecting;
                var host = SourceUri.DnsSafeHost;
                if (SourceUri.Port != -1 && SourceUri.Port != 80)
                {
                    host = $"{SourceUri.DnsSafeHost}:{SourceUri.Port}";
                }
                var request =
                    $"GET {SourceUri.PathAndQuery} HTTP/1.1\r\n" +
                    $"Host: {host}\r\n" +
                    "Accept: */*\r\n" +
                    "User-Agent: NSPlayer/12.0\r\n" +
                    "Connection: close\r\n" +
                    "Cache-Control: no-cache\r\n" +
                    "Pragma: xPlayStrm=1\r\n" +
                    "Pragma: rate=1.000,stream-time=0\r\n" +
                    "\r\n";
                await connection.Stream.WriteAsync(System.Text.Encoding.UTF8.GetBytes(request)).ConfigureAwait(false);

                Logger.Debug("Sending request:\n" + request);

                response = null;
                response = await HTTPResponseReader.ReadAsync(connection.Stream, cancel_token).ConfigureAwait(false);

                if (response.Status != 200)
                {
                    Logger.Error("Server responses {0} to GET {1}", response.Status, SourceUri.PathAndQuery);
                    Stop(response.Status == 404 ? StopReason.OffAir : StopReason.UnavailableError);
                }

                this.Status = ConnectionStatus.Connected;
                await contentReader.ReadAsync(contentSink, connection.Stream, cancel_token).ConfigureAwait(false);

                Stop(StopReason.OffAir);
            }
            catch (InvalidDataException) {
                Stop(StopReason.ConnectionError);
            }
            catch (OperationCanceledException) {
                Logger.Error("Recv content timed out");
                Stop(StopReason.ConnectionError);
            }
            catch (IOException) {
                Stop(StopReason.ConnectionError);
            }
            this.Status = ConnectionStatus.Error;
        }
        private State WaitResponse()
        {
            response = null;
            bool longresponse = false;

            try {
                connection.Recv(stream => {
                    longresponse = stream.Length >= 2048;
                    response     = HTTPResponseReader.Read(stream);
                });
            }
            catch (IOException) {
                Stop(StopReason.ConnectionError);
                return(State.Disconnected);
            }
            if (response != null)
            {
                if (response.Status == 200)
                {
                    return(State.Receiving);
                }
                else
                {
                    Logger.Error("Server responses {0} to GET {1}", response.Status, SourceUri.PathAndQuery);
                    Stop(response.Status == 404 ? StopReason.OffAir : StopReason.UnavailableError);
                    return(State.Disconnected);
                }
            }
            else if (longresponse)
            {
                Stop(StopReason.ConnectionError);
                return(State.Disconnected);
            }
            else
            {
                return(State.WaitingResponse);
            }
        }