Example #1
0
        public static RtspResponseMessage Parse(ArraySegment <byte> byteSegment)
        {
            Debug.Assert(byteSegment.Array != null, "byteSegment.Array != null");

            var headersStream = new MemoryStream(byteSegment.Array, byteSegment.Offset, byteSegment.Count, false);
            var headersReader = new StreamReader(headersStream);

            string startLine = headersReader.ReadLine();

            if (startLine == null)
            {
                throw new RtspParseResponseException("Empty response");
            }

            string[] tokens = GetFirstLineTokens(startLine);

            string rtspVersion = tokens[0];

            Version        protocolVersion = ParseProtocolVersion(rtspVersion);
            RtspStatusCode statusCode      = ParseStatusCode(tokens[1]);

            NameValueCollection headers = HeadersParser.ParseHeaders(headersReader);

            uint   cSeq      = 0;
            string cseqValue = headers.Get("CSEQ");

            if (cseqValue != null)
            {
                uint.TryParse(cseqValue, out cSeq);
            }

            return(new RtspResponseMessage(statusCode, protocolVersion, cSeq, headers));
        }
        public override async Task ConnectAsync(CancellationToken token)
        {
            _commandCounter = 0;
            _sessionCookie  = Guid.NewGuid().ToString("N").Substring(0, 10);

            _streamDataClient = NetworkClientFactory.CreateTcpClient();

            Uri connectionUri = ConnectionParameters.ConnectionUri;

            int httpPort = connectionUri.Port != -1 ? connectionUri.Port : Constants.DefaultHttpPort;

            await _streamDataClient.ConnectAsync(connectionUri.Host, httpPort);

            _remoteEndPoint    = _streamDataClient.RemoteEndPoint;
            _dataNetworkStream = new NetworkStream(_streamDataClient, false);

            string request = ComposeGetRequest();

            byte[] requestBytes = Encoding.ASCII.GetBytes(request);

            await _dataNetworkStream.WriteAsync(requestBytes, 0, requestBytes.Length, token);

            var buffer = new byte[Constants.MaxResponseHeadersSize];
            int read   = await ReadUntilEndOfHeadersAsync(_dataNetworkStream, buffer);

            var ms           = new MemoryStream(buffer, 0, read);
            var streamReader = new StreamReader(ms, Encoding.ASCII);

            string responseLine = streamReader.ReadLine();

            if (responseLine == null)
            {
                throw new HttpBadResponseException("Empty response");
            }

            string[] tokens = responseLine.Split(' ');

            if (tokens.Length != 3)
            {
                throw new HttpRequestException("Invalid first response line");
            }

            HttpStatusCode statusCode = (HttpStatusCode)int.Parse(tokens[1]);

            if (statusCode == HttpStatusCode.OK)
            {
                return;
            }

            if (statusCode == HttpStatusCode.Unauthorized &&
                !ConnectionParameters.Credentials.IsEmpty() &&
                _authenticator == null)
            {
                NameValueCollection headers = HeadersParser.ParseHeaders(streamReader);

                string authenticateHeader = headers.Get(WellKnownHeaders.WwwAuthenticate);

                if (string.IsNullOrEmpty(authenticateHeader))
                {
                    throw new HttpBadResponseCodeException(statusCode);
                }

                _authenticator = Authenticator.Create(ConnectionParameters.Credentials, authenticateHeader);

                _streamDataClient.Dispose();

                await ConnectAsync(token);

                return;
            }

            throw new HttpBadResponseCodeException(statusCode);
        }