Changed playlist input timing to boottime-based instead of wallclock-based, split up realtime and regular main loops functions

# Conflicts:
#	src/input/input.cpp
This commit is contained in:
Thulinma 2018-05-09 12:21:03 +02:00
commit 9ce7c29813
5 changed files with 29 additions and 14 deletions

View file

@ -8,19 +8,24 @@ namespace HTTP{
Downloader::Downloader(){ Downloader::Downloader(){
progressCallback = 0; progressCallback = 0;
connectedPort = 0; connectedPort = 0;
dataTimeout = 5;
retryCount = 5;
ssl = false; ssl = false;
proxied = false; proxied = false;
char *p = getenv("http_proxy"); char *p = getenv("http_proxy");
if (p){ if (p){
proxyUrl = HTTP::URL(p); proxyUrl = HTTP::URL(p);
proxied = true; proxied = true;
INFO_MSG("Proxying through %s", proxyUrl.getUrl().c_str()); MEDIUM_MSG("Proxying through %s", proxyUrl.getUrl().c_str());
} }
} }
/// Returns a reference to the internal HTTP::Parser body element /// Returns a reference to the internal HTTP::Parser body element
std::string &Downloader::data(){return H.body;} std::string &Downloader::data(){return H.body;}
/// Returns a const reference to the internal HTTP::Parser body element
const std::string &Downloader::const_data() const{return H.body;}
/// Returns the status text of the HTTP Request. /// Returns the status text of the HTTP Request.
std::string &Downloader::getStatusText(){return H.method;} std::string &Downloader::getStatusText(){return H.method;}
@ -64,7 +69,6 @@ namespace HTTP{
void Downloader::doRequest(const HTTP::URL &link, const std::string &method, const std::string &body){ void Downloader::doRequest(const HTTP::URL &link, const std::string &method, const std::string &body){
if (!canRequest(link)){return;} if (!canRequest(link)){return;}
bool needSSL = (link.protocol == "https"); bool needSSL = (link.protocol == "https");
INFO_MSG("Retrieving %s", link.getUrl().c_str());
H.Clean(); H.Clean();
// Reconnect if needed // Reconnect if needed
if (!proxied || needSSL){ if (!proxied || needSSL){
@ -138,11 +142,12 @@ namespace HTTP{
/// Makes at most 5 attempts, and will wait no longer than 5 seconds without receiving data. /// Makes at most 5 attempts, and will wait no longer than 5 seconds without receiving data.
bool Downloader::get(const HTTP::URL &link, uint8_t maxRecursiveDepth){ bool Downloader::get(const HTTP::URL &link, uint8_t maxRecursiveDepth){
if (!canRequest(link)){return false;} if (!canRequest(link)){return false;}
unsigned int loop = 6; // max 5 attempts unsigned int loop = retryCount+1; // max 5 attempts
while (--loop){// loop while we are unsuccessful while (--loop){// loop while we are unsuccessful
MEDIUM_MSG("Retrieving %s (%lu/%lu)", link.getUrl().c_str(), retryCount-loop+1, retryCount);
doRequest(link); doRequest(link);
uint64_t reqTime = Util::bootSecs(); uint64_t reqTime = Util::bootSecs();
while (getSocket() && Util::bootSecs() < reqTime + 5){ while (getSocket() && Util::bootSecs() < reqTime + dataTimeout){
// No data? Wait for a second or so. // No data? Wait for a second or so.
if (!getSocket().spool()){ if (!getSocket().spool()){
if (progressCallback != 0){ if (progressCallback != 0){
@ -170,12 +175,14 @@ namespace HTTP{
} }
return true; // Success! return true; // Success!
} }
// reset the 5 second timeout // reset the data timeout
reqTime = Util::bootSecs(); reqTime = Util::bootSecs();
} }
if (getSocket()){ if (getSocket()){
FAIL_MSG("Timeout while retrieving %s", link.getUrl().c_str()); FAIL_MSG("Timeout while retrieving %s (%lu/%lu)", link.getUrl().c_str(), retryCount-loop+1, retryCount);
return false; getSocket().close();
}else{
FAIL_MSG("Lost connection while retrieving %s (%lu/%lu)", link.getUrl().c_str(), retryCount-loop+1, retryCount);
} }
Util::sleep(500); // wait a bit before retrying Util::sleep(500); // wait a bit before retrying
} }
@ -185,13 +192,14 @@ namespace HTTP{
bool Downloader::post(const HTTP::URL &link, const std::string &payload, bool sync, uint8_t maxRecursiveDepth){ bool Downloader::post(const HTTP::URL &link, const std::string &payload, bool sync, uint8_t maxRecursiveDepth){
if (!canRequest(link)){return false;} if (!canRequest(link)){return false;}
unsigned int loop = 6; // max 5 attempts unsigned int loop = retryCount; // max 5 attempts
while (--loop){// loop while we are unsuccessful while (--loop){// loop while we are unsuccessful
MEDIUM_MSG("Posting to %s (%lu/%lu)", link.getUrl().c_str(), retryCount-loop+1, retryCount);
doRequest(link, "POST", payload); doRequest(link, "POST", payload);
//Not synced? Ignore the response and immediately return false. //Not synced? Ignore the response and immediately return false.
if (!sync){return false;} if (!sync){return false;}
uint64_t reqTime = Util::bootSecs(); uint64_t reqTime = Util::bootSecs();
while (getSocket() && Util::bootSecs() < reqTime + 5){ while (getSocket() && Util::bootSecs() < reqTime + dataTimeout){
// No data? Wait for a second or so. // No data? Wait for a second or so.
if (!getSocket().spool()){ if (!getSocket().spool()){
if (progressCallback != 0){ if (progressCallback != 0){
@ -219,7 +227,7 @@ namespace HTTP{
} }
return true; // Success! return true; // Success!
} }
// reset the 5 second timeout // reset the data timeout
reqTime = Util::bootSecs(); reqTime = Util::bootSecs();
} }
if (getSocket()){ if (getSocket()){

View file

@ -6,6 +6,7 @@ namespace HTTP{
public: public:
Downloader(); Downloader();
std::string &data(); std::string &data();
const std::string &const_data() const;
void doRequest(const HTTP::URL &link, const std::string &method="", const std::string &body=""); void doRequest(const HTTP::URL &link, const std::string &method="", const std::string &body="");
bool get(const std::string &link); bool get(const std::string &link);
bool get(const HTTP::URL &link, uint8_t maxRecursiveDepth = 6); bool get(const HTTP::URL &link, uint8_t maxRecursiveDepth = 6);
@ -22,6 +23,7 @@ namespace HTTP{
bool canRequest(const HTTP::URL &link); bool canRequest(const HTTP::URL &link);
Parser &getHTTP(); Parser &getHTTP();
Socket::Connection &getSocket(); Socket::Connection &getSocket();
uint32_t retryCount, dataTimeout;
private: private:
std::map<std::string, std::string> extraHeaders; ///< Holds extra headers to sent with request std::map<std::string, std::string> extraHeaders; ///< Holds extra headers to sent with request

View file

@ -156,6 +156,14 @@ uint32_t HTTP::URL::getDefaultPort() const{
return 0; return 0;
} }
/// Returns the file extension of the URL, or an empty string if none.
std::string HTTP::URL::getExt() const{
if (path.rfind('.') == std::string::npos){
return "";
}
return path.substr(path.rfind('.')+1);
}
/// Returns the full URL in string format /// Returns the full URL in string format
std::string HTTP::URL::getUrl() const{ std::string HTTP::URL::getUrl() const{
std::string ret; std::string ret;

View file

@ -78,6 +78,7 @@ namespace HTTP{
URL(const std::string &url = ""); URL(const std::string &url = "");
uint32_t getPort() const; uint32_t getPort() const;
uint32_t getDefaultPort() const; uint32_t getDefaultPort() const;
std::string getExt() const;
std::string getUrl() const; std::string getUrl() const;
std::string getFilePath() const; std::string getFilePath() const;
std::string getBareUrl() const; std::string getBareUrl() const;

View file

@ -173,10 +173,6 @@ namespace Mist {
std::string givenStream = config->getString("streamname"); std::string givenStream = config->getString("streamname");
if (streamName == "") { if (streamName == "") {
streamName = givenStream; streamName = givenStream;
}else{
if (givenStream.find("+") != std::string::npos){
streamName += givenStream.substr(givenStream.find("+"));
}
} }
srcConn = Socket::Connection(host, port, true); srcConn = Socket::Connection(host, port, true);
if (!srcConn.connected()){ if (!srcConn.connected()){