Exemplo n.º 1
0
        /// <summary>
        /// Deserialise/parse an RTSP response.
        /// </summary>
        /// <param name="responseBytes">The raw response bytes.</param>
        /// <param name="responseByteCount">The number of valid bytes in the response.</param>
        /// <returns>a response object</returns>
        public static RtspResponse Deserialise(byte[] responseBytes, int responseByteCount)
        {
            var response       = new RtspResponse();
            var responseString = Encoding.UTF8.GetString(responseBytes, 0, responseByteCount);

            Logger.Info(responseString);
            var m = RegexStatusLine.Match(responseString);

            if (m.Success)
            {
                response._majorVersion = int.Parse(m.Groups[1].Captures[0].Value);
                response._minorVersion = int.Parse(m.Groups[2].Captures[0].Value);
                response._statusCode   = (RtspStatusCode)int.Parse(m.Groups[3].Captures[0].Value);
                response._reasonPhrase = m.Groups[4].Captures[0].Value;
                responseString         = m.Groups[5].Captures[0].Value;
            }

            var sections = responseString.Split(new[] { "\r\n\r\n" }, StringSplitOptions.None);

            response._body = sections[1];
            var headers = sections[0].Split(new[] { "\r\n" }, StringSplitOptions.None);

            response._headers = new Dictionary <string, string>();
            foreach (var headerInfo in headers.Select(header => header.Split(':')))
            {
                response._headers.Add(headerInfo[0], headerInfo[1].Trim());
            }
            return(response);
        }
Exemplo n.º 2
0
        private void ReceiveResponse(out RtspResponse response)
        {
            response = null;
            var responseBytesCount = 0;

            byte[] responseBytes = new byte[1024];
            try
            {
                responseBytesCount = _rtspSocket.Receive(responseBytes, responseBytes.Length, SocketFlags.None);
                response           = RtspResponse.Deserialise(responseBytes, responseBytesCount);
                string contentLengthString;
                int    contentLength = 0;
                if (response.Headers.TryGetValue("Content-Length", out contentLengthString))
                {
                    contentLength = int.Parse(contentLengthString);
                    if ((string.IsNullOrEmpty(response.Body) && contentLength > 0) || response.Body.Length < contentLength)
                    {
                        if (response.Body == null)
                        {
                            response.Body = string.Empty;
                        }
                        while (responseBytesCount > 0 && response.Body.Length < contentLength)
                        {
                            responseBytesCount = _rtspSocket.Receive(responseBytes, responseBytes.Length, SocketFlags.None);
                            response.Body     += System.Text.Encoding.UTF8.GetString(responseBytes, 0, responseBytesCount);
                        }
                    }
                }
            }
            catch (SocketException)
            {
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// Send an RTSP request and retrieve the response.
 /// </summary>
 /// <param name="request">The request.</param>
 /// <param name="response">The response.</param>
 /// <returns>the response status code</returns>
 public RtspStatusCode SendRequest(RtspRequest request, out RtspResponse response)
 {
     response = null;
     lock (_lockObject)
     {
         NetworkStream stream = null;
         try
         {
             stream = _client.GetStream();
             if (stream == null)
             {
                 throw new Exception();
             }
         }
         catch
         {
             _client.Close();
         }
         try
         {
             if (_client == null)
             {
                 _client = new TcpClient(_serverHost, 554);
             }
             // Send the request and get the response.
             request.Headers.Add("CSeq", _cseq.ToString(CultureInfo.InvariantCulture));
             byte[] requestBytes = request.Serialise();
             stream.Write(requestBytes, 0, requestBytes.Length);
             _cseq++;
             byte[] responseBytes = new byte[_client.ReceiveBufferSize];
             int    byteCount     = stream.Read(responseBytes, 0, responseBytes.Length);
             response = RtspResponse.Deserialise(responseBytes, byteCount);
             // Did we get the whole response?
             string contentLengthString;
             int    contentLength = 0;
             if (response.Headers.TryGetValue("Content-Length", out contentLengthString))
             {
                 contentLength = int.Parse(contentLengthString);
                 if ((string.IsNullOrEmpty(response.Body) && contentLength > 0) || response.Body.Length < contentLength)
                 {
                     if (response.Body == null)
                     {
                         response.Body = string.Empty;
                     }
                     while (byteCount > 0 && response.Body.Length < contentLength)
                     {
                         byteCount      = stream.Read(responseBytes, 0, responseBytes.Length);
                         response.Body += System.Text.Encoding.UTF8.GetString(responseBytes, 0, byteCount);
                     }
                 }
             }
             return(response.StatusCode);
         }
         finally
         {
             stream.Close();
         }
     }
 }