/// <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); }
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) { } }