public async Task ProcessingLoop() { try { // Get a stream object for reading and writing using (var stream = _tcpClient.GetStream()) { byte[] buffer = new byte[1000]; int bytesRead = 0; while (_tcpClient.Connected) { StringBuilder message = new StringBuilder(); // Incoming message may be larger than the buffer size. do { bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length); // Translate data bytes to a ASCII string. message.AppendFormat("{0}", Encoding.ASCII.GetString(buffer, 0, bytesRead)); }while (stream.DataAvailable); string request = message.ToString(); if (String.IsNullOrEmpty(request)) { _logger.Error("Empty packet"); if (IsDisconnected(_tcpClient)) { _logger.Error("Client disconnected ungracefully"); goto close; } continue; } if (await ProcessIncomingRequest(request) == false) { break; } } } close: _tcpClient.Close(); } catch (Exception ex) { if (_logger.IsDebugEnabled) { _logger.Error(ex); } } finally { _session?.Close(); _session = null; _tcpClient.Close(); } if (_logger.IsDebugEnabled) { _logger.Debug("Network stream disconnected, thread exiting."); } }
public async Task <bool> ProcessIncomingRequest(string message) { bool retVal = false; if (_logger.IsDebugEnabled) { _logger.Debug("C->S:\n{0}", message); } if (_session != null) { _session.LastKeepAlive = DateTime.Now; } RTSPRequestParser parser = new RTSPRequestParser(); try { parser.Parse(message.ToUpper()); // TODO : Error handling } catch (Exception ex) { _logger.Error(ex); HandleBadRequest(); return(false); } if (!_commands.Contains(parser.Method)) { HandleCommandNotSupported(parser); return(false); } if (!String.IsNullOrEmpty(parser.TransportType)) { if (parser.TransportType != "RTP/AVP" && parser.TransportType != "RTP/AVP/TCP") { HandleUnsupportedTransport(parser); return(false); } } // Handle session less verbs first if (parser.Method == "OPTIONS") { retVal = Options(parser); return(retVal); } if (parser.Method == "DESCRIBE") { retVal = Describes(parser); return(retVal); } if (parser.Method == "SET_PARAMETER") { retVal = SetParameter(parser); return(retVal); } if (parser.Headers.ContainsKey("SESSION")) { _session = Server.GetSession(Convert.ToInt32(parser.Headers["SESSION"])); if (_session == null) { HandleSessionNotFound(parser); return(false); } } else { _session = Server.GenerateSession(this); if (_logger.IsDebugEnabled) { _logger.Debug("Generating new session {0}", _session.SessionId); } } _session.LastKeepAlive = DateTime.Now; if (parser.Method == "SETUP") { retVal = _session.Setup(_tcpClient, parser); } else if (parser.Method == "PAUSE") { retVal = _session.Pause(parser); } else if (parser.Method == "GET_PARAMETER") { retVal = _session.GetParameter(parser); } else if (parser.Method == "PLAY") { retVal = await _session.Play(parser); } else if (parser.Method == "TEARDOWN") { _session.Teardown(parser); return(false); } else { HandleBadRequest(); } return(retVal); }