/// <summary> /// Sends a Rtsp Response on the given client session /// </summary> /// <param name="message">The RtspResponse to send</param> If this was byte[] then it could handle http /// <param name="ci">The session to send the response on</param> internal void ProcessSendRtspMessage(RtspMessage message, ClientSession session, bool sendResponse = true) { //Todo have a SupportedFeatures and RequiredFeatures HashSet. //Check Require Header // And /* Add Unsupported Header if needed Require: play.basic, con.persistent (basic play, TCP is supported) setup.playing means that setup and teardown can be used in the play state. */ //If we have a session if (session == null || session.IsDisposed) return; try { //if there is a message to send if (message != null && false == message.IsDisposed) { //AddServerHeaders()-> if (false == message.ContainsHeader(RtspHeaders.Server)) message.SetHeader(RtspHeaders.Server, ServerName); if (false == message.ContainsHeader(RtspHeaders.Date)) message.SetHeader(RtspHeaders.Date, DateTime.UtcNow.ToString("r")); #region RFC2326 12.38 Timestamp / Delay /* 12.38 Timestamp The timestamp general header describes when the client sent the request to the server. The value of the timestamp is of significance only to the client and may use any timescale. The server MUST echo the exact same value and MAY, if it has accurate information about this, add a floating point number indicating the number of seconds that has elapsed since it has received the request. The timestamp is used by the client to compute the round-trip time to the server so that it can adjust the timeout value for retransmissions. Timestamp = "Timestamp" ":" *(DIGIT) [ "." *(DIGIT) ] [ delay ] delay = *(DIGIT) [ "." *(DIGIT) ] */ if (false == message.ContainsHeader(RtspHeaders.Timestamp) && session.LastRequest != null && session.LastRequest.ContainsHeader(RtspHeaders.Timestamp)) { ////Apparently not joined with ; ////message.SetHeader(RtspHeaders.Timestamp, session.LastRequest[RtspHeaders.Timestamp] + "delay=" + (DateTime.UtcNow - session.LastRequest.Created).TotalSeconds); //Set the value of the Timestamp header as given message.AppendOrSetHeader(RtspHeaders.Timestamp, session.LastRequest[RtspHeaders.Timestamp]); //Add a delay datum message.AppendOrSetHeader(RtspHeaders.Timestamp, "delay=" + (DateTime.UtcNow - session.LastRequest.Created).TotalSeconds); } #endregion string sess = message.GetHeader(RtspHeaders.Session); //Check for a session header if (false == string.IsNullOrWhiteSpace(sess)) { //Add the timeout header if there was a session header. if (RtspClientInactivityTimeout > TimeSpan.Zero && false == sess.Contains("timeout")) message.AppendOrSetHeader(RtspHeaders.Session, "timeout=" + (int)(RtspClientInactivityTimeout.TotalSeconds / 2)); } //Oops //if (session.m_RtspSocket.ProtocolType == ProtocolType.Tcp && session.Attached.Count > 0) response.SetHeader("Ignore", "$0\09\r\n$\0:\0"); //Dispose the last response if (session.LastResponse != null) session.LastResponse.Dispose(); //Todo //Content-Encoding should be the same as the request's if possible.. //If sending a response if (sendResponse) { //Log response if (Logger != null) Logger.LogResponse(message, session); session.SendRtspData((session.LastResponse = message).ToBytes()); }//Otherwise just update the property else session.LastResponse = message; //Indicate the session is not disconnected session.IsDisconnected = false; //Indicate the last response was sent now. session.LastResponse.Transferred = DateTime.UtcNow; } else { //Test the connectivity and start another receive session.SendRtspData(Media.Common.MemorySegment.EmptyBytes); } } catch (Exception ex) { if (Logger != null) Logger.LogException(ex); //if a socket exception occured then handle it. if (session != null && ex is SocketException) HandleClientSocketException((SocketException)ex, session); } }
internal void ProcessClientBuffer(ClientSession session, int received) { if (session == null || session.IsDisposed || received <= 0) return; try { //Use a segment around the data received which is already in the buffer. using (Common.MemorySegment data = new Common.MemorySegment(session.m_Buffer.Array, session.m_Buffer.Offset, received)) { if (data[0] == RtpClient.BigEndianFrameControl) { if (session.m_RtpClient == null) return; received -= session.m_RtpClient.ProcessFrameData(session.m_Buffer.Array, session.m_Buffer.Offset, received, session.m_RtspSocket); } if (received <= 0) return; //Ensure the message is really Rtsp RtspMessage request = new RtspMessage(data); //Check for validity if (request.MessageType != RtspMessageType.Invalid) { //Process the request when complete if (request.IsComplete) { ProcessRtspRequest(request, session); return; } } //Otherwise if LastRequest is not disposed then attempt completion with the invalid data else if (session.LastRequest != null && false == session.LastRequest.IsDisposed) { //Indicate a CompleteFrom is occuring Media.Common.ILoggingExtensions.Log(Logger, "Session:" + session.Id + " Attempting to complete previous mesage with buffer of " + data.Count + " bytes."); //Attempt to complete it received = session.LastRequest.CompleteFrom(null, data); //If nothing was recieved then do nothing. if (received == 0) { Media.Common.ILoggingExtensions.Log(Logger, "Session:" + session.Id + "Did not use buffer of " + data.Count + " bytes."); //return; } Media.Common.ILoggingExtensions.Log(Logger, "Session:" + session.Id + " used " + received + " of buffer bytes"); //Account for the received data session.m_Receieved += received; //Determine how to process the messge switch (session.LastRequest.MessageType) { case RtspMessageType.Response: case RtspMessageType.Invalid: { //Ensure the session is still connected. session.SendRtspData(Media.Common.MemorySegment.EmptyBytes); return; } case RtspMessageType.Request: { //If the request is complete now then process it if (session.LastRequest.IsComplete) { //Process the request (it may not be complete yet) ProcessRtspRequest(session.LastRequest, session); return; } goto case RtspMessageType.Invalid; } } } //Log the invalid request Media.Common.ILoggingExtensions.Log(Logger, "Received Invalid Message:" + request + " \r\nFor Session:" + session.Id); if (session.LastRequest != null) session.LastRequest.Dispose(); //Store it for now to allow completion. session.LastRequest = request; //Ensure the session is still connected. session.SendRtspData(Media.Common.MemorySegment.EmptyBytes); } } catch(Exception ex) { if (Logger != null) Logger.LogException(ex); } }