Better onFail handling, better HTTP failure handling

# Conflicts:
#	src/output/output.cpp
This commit is contained in:
Thulinma 2018-11-13 16:35:48 +01:00
parent 109995809d
commit d695b4e5a6
3 changed files with 25 additions and 44 deletions

View file

@ -208,8 +208,7 @@ namespace Mist{
if(Triggers::shouldTrigger("CONN_PLAY", streamName)){ if(Triggers::shouldTrigger("CONN_PLAY", streamName)){
std::string payload = streamName+"\n" + getConnectedHost() +"\n"+capa["name"].asStringRef()+"\n"+reqUrl; std::string payload = streamName+"\n" + getConnectedHost() +"\n"+capa["name"].asStringRef()+"\n"+reqUrl;
if (!Triggers::doTrigger("CONN_PLAY", payload, streamName)){ if (!Triggers::doTrigger("CONN_PLAY", payload, streamName)){
INFO_MSG("Shutting down due to CONN_PLAY trigger rejection"); onFail("Not allowed to play (CONN_PLAY)");
onFail();
} }
} }
doSync(true); doSync(true);
@ -288,8 +287,7 @@ namespace Mist{
if (tmpEx.getSync() == 1){ if (tmpEx.getSync() == 1){
std::string payload = streamName+"\n" + getConnectedHost() +"\n" + JSON::Value((long long)crc).asString() + "\n"+capa["name"].asStringRef()+"\n"+reqUrl+"\n"+tmpEx.getSessId(); std::string payload = streamName+"\n" + getConnectedHost() +"\n" + JSON::Value((long long)crc).asString() + "\n"+capa["name"].asStringRef()+"\n"+reqUrl+"\n"+tmpEx.getSessId();
if (!Triggers::doTrigger("USER_NEW", payload, streamName)){ if (!Triggers::doTrigger("USER_NEW", payload, streamName)){
MEDIUM_MSG("Closing connection because denied by USER_NEW trigger"); onFail("Not allowed to play (USER_NEW)");
onFail();
tmpEx.setSync(100);//100 = denied tmpEx.setSync(100);//100 = denied
}else{ }else{
tmpEx.setSync(10);//10 = accepted tmpEx.setSync(10);//10 = accepted
@ -297,12 +295,10 @@ namespace Mist{
} }
//100 = denied //100 = denied
if (tmpEx.getSync() == 100){ if (tmpEx.getSync() == 100){
onFail(); onFail("Not allowed to play (USER_NEW cache)");
MEDIUM_MSG("Closing connection because denied by USER_NEW sync byte");
} }
if (tmpEx.getSync() == 0 || tmpEx.getSync() == 2){ if (tmpEx.getSync() == 0 || tmpEx.getSync() == 2){
onFail(); onFail("Not allowed to play (USER_NEW timeout)", true);
FAIL_MSG("Closing connection because sync byte timeout!");
} }
//anything else = accepted //anything else = accepted
}else{ }else{
@ -1017,8 +1013,7 @@ namespace Mist{
if (targetParams.count("recstopunix")){ if (targetParams.count("recstopunix")){
long long stopUnix = atoll(targetParams["recstopunix"].c_str()); long long stopUnix = atoll(targetParams["recstopunix"].c_str());
if (stopUnix < unixStreamBegin){ if (stopUnix < unixStreamBegin){
FAIL_MSG("Recording stop time is earlier than stream begin - aborting"); onFail("Recording stop time is earlier than stream begin - aborting", true);
onFail();
return; return;
}else{ }else{
targetParams["recstop"] = JSON::Value((long long)((stopUnix - unixStreamBegin)*1000)).asString(); targetParams["recstop"] = JSON::Value((long long)((stopUnix - unixStreamBegin)*1000)).asString();
@ -1028,8 +1023,7 @@ namespace Mist{
if (targetParams.count("recstop")){ if (targetParams.count("recstop")){
long long endRec = atoll(targetParams["recstop"].c_str()); long long endRec = atoll(targetParams["recstop"].c_str());
if (endRec < 0 || endRec < startTime()){ if (endRec < 0 || endRec < startTime()){
FAIL_MSG("Entire recording range is in the past"); onFail("Entire recording range is in the past", true);
onFail();
return; return;
} }
INFO_MSG("Recording will stop at %lld", endRec); INFO_MSG("Recording will stop at %lld", endRec);
@ -1039,8 +1033,7 @@ namespace Mist{
long long startRec = atoll(targetParams["recstart"].c_str()); long long startRec = atoll(targetParams["recstart"].c_str());
if (startRec > 0 && startRec > myMeta.tracks[mainTrack].lastms){ if (startRec > 0 && startRec > myMeta.tracks[mainTrack].lastms){
if (myMeta.vod){ if (myMeta.vod){
FAIL_MSG("Recording start past end of non-live source"); onFail("Recording start past end of non-live source", true);
onFail();
return; return;
} }
INFO_MSG("Waiting for stream to reach recording starting point"); INFO_MSG("Waiting for stream to reach recording starting point");
@ -1080,8 +1073,7 @@ namespace Mist{
long long stopUnix = atoll(targetParams["stopunix"].c_str()); long long stopUnix = atoll(targetParams["stopunix"].c_str());
if (stopUnix < 0){stopUnix += Util::epoch();} if (stopUnix < 0){stopUnix += Util::epoch();}
if (stopUnix < unixStreamBegin){ if (stopUnix < unixStreamBegin){
FAIL_MSG("Stop time is earlier than stream begin - aborting"); onFail("Stop time is earlier than stream begin - aborting", true);
onFail();
return; return;
}else{ }else{
targetParams["stop"] = JSON::Value((long long)((stopUnix - unixStreamBegin)*1000)).asString(); targetParams["stop"] = JSON::Value((long long)((stopUnix - unixStreamBegin)*1000)).asString();
@ -1091,8 +1083,7 @@ namespace Mist{
if (targetParams.count("stop")){ if (targetParams.count("stop")){
long long endRec = atoll(targetParams["stop"].c_str()); long long endRec = atoll(targetParams["stop"].c_str());
if (endRec < 0 || endRec < startTime()){ if (endRec < 0 || endRec < startTime()){
FAIL_MSG("Entire range is in the past"); onFail("Entire range is in the past", true);
onFail();
return; return;
} }
INFO_MSG("Playback will stop at %lld", endRec); INFO_MSG("Playback will stop at %lld", endRec);
@ -1102,8 +1093,7 @@ namespace Mist{
long long startRec = atoll(targetParams["start"].c_str()); long long startRec = atoll(targetParams["start"].c_str());
if (startRec > 0 && startRec > myMeta.tracks[mainTrack].lastms){ if (startRec > 0 && startRec > myMeta.tracks[mainTrack].lastms){
if (myMeta.vod){ if (myMeta.vod){
FAIL_MSG("Playback start past end of non-live source"); onFail("Playback start past end of non-live source", true);
onFail();
return; return;
} }
INFO_MSG("Waiting for stream to reach playback starting point"); INFO_MSG("Waiting for stream to reach playback starting point");
@ -1665,12 +1655,7 @@ namespace Mist{
if (statsPage.getData()){ if (statsPage.getData()){
/*LTS-START*/ /*LTS-START*/
if (!statsPage.isAlive()){ if (!statsPage.isAlive()){
INFO_MSG("Shutting down on controller request"); onFail("Shutting down on controller request");
if (!onFinish()){
myConn.close();
}else{
disconnect();
}
return; return;
} }
/*LTS-END*/ /*LTS-END*/

View file

@ -46,13 +46,6 @@ namespace Mist {
data.sendTo(myConn); data.sendTo(myConn);
} }
void OutDTSC::sendError(const std::string &msg){
JSON::Value err;
err["cmd"] = "error";
err["msg"] = msg;
sendCmd(err);
}
void OutDTSC::sendOk(const std::string &msg){ void OutDTSC::sendOk(const std::string &msg){
JSON::Value err; JSON::Value err;
err["cmd"] = "ok"; err["cmd"] = "ok";
@ -147,6 +140,14 @@ namespace Mist {
} }
} }
void OutDTSC::onFail(const std::string & msg, bool critical){
JSON::Value err;
err["cmd"] = "error";
err["msg"] = msg;
sendCmd(err);
Output::onFail(msg, critical);
}
void OutDTSC::onRequest(){ void OutDTSC::onRequest(){
while (myConn.Received().available(8)){ while (myConn.Received().available(8)){
if (myConn.Received().copy(4) == "DTCM"){ if (myConn.Received().copy(4) == "DTCM"){
@ -168,8 +169,7 @@ namespace Mist {
}else if (myConn.Received().copy(4) == "DTSC"){ }else if (myConn.Received().copy(4) == "DTSC"){
//Header packet //Header packet
if (!isPushing()){ if (!isPushing()){
sendError("DTSC_HEAD ignored: you are not cleared for pushing data!"); onFail("DTSC_HEAD ignored: you are not cleared for pushing data!", true);
onFail();
return; return;
} }
std::string toRec = myConn.Received().copy(8); std::string toRec = myConn.Received().copy(8);
@ -183,8 +183,7 @@ namespace Mist {
sendOk(rep.str()); sendOk(rep.str());
}else if (myConn.Received().copy(4) == "DTP2"){ }else if (myConn.Received().copy(4) == "DTP2"){
if (!isPushing()){ if (!isPushing()){
sendError("DTSC_V2 ignored: you are not cleared for pushing data!"); onFail("DTSC_V2 ignored: you are not cleared for pushing data!", true);
onFail();
return; return;
} }
// Data packet // Data packet
@ -194,15 +193,13 @@ namespace Mist {
std::string dataPacket = myConn.Received().remove(8+rSize); std::string dataPacket = myConn.Received().remove(8+rSize);
DTSC::Packet inPack(dataPacket.data(), dataPacket.size(), true); DTSC::Packet inPack(dataPacket.data(), dataPacket.size(), true);
if (!myMeta.tracks.count(inPack.getTrackId())){ if (!myMeta.tracks.count(inPack.getTrackId())){
sendError("DTSC_V2 received for a track that was not announced in the DTSC_HEAD!"); onFail("DTSC_V2 received for a track that was not announced in the DTSC_HEAD!", true);
onFail();
return; return;
} }
bufferLivePacket(inPack); bufferLivePacket(inPack);
}else{ }else{
//Invalid //Invalid
sendError("Invalid packet header received. Aborting."); onFail("Invalid packet header received. Aborting.", true);
onFail();
return; return;
} }
} }
@ -221,8 +218,7 @@ namespace Mist {
std::string passString = dScan.getMember("password").asString(); std::string passString = dScan.getMember("password").asString();
Util::sanitizeName(streamName); Util::sanitizeName(streamName);
if (!allowPush(passString)){ if (!allowPush(passString)){
sendError("Push not allowed - stream and/or password incorrect"); onFail("Push not allowed - stream and/or password incorrect", true);
onFail();
return; return;
} }
sendOk("You're cleared for pushing! DTSC_HEAD please?"); sendOk("You're cleared for pushing! DTSC_HEAD please?");

View file

@ -11,9 +11,9 @@ namespace Mist {
void sendNext(); void sendNext();
void sendHeader(); void sendHeader();
void initialSeek(); void initialSeek();
void onFail(const std::string & msg, bool critical = false);
void stats(bool force = false); void stats(bool force = false);
void sendCmd(const JSON::Value &data); void sendCmd(const JSON::Value &data);
void sendError(const std::string &msg);
void sendOk(const std::string &msg); void sendOk(const std::string &msg);
private: private:
unsigned int lastActive;///<Time of last sending of data. unsigned int lastActive;///<Time of last sending of data.