Fixed several buffer segfaults when multiple viewers are connected.

This commit is contained in:
Thulinma 2013-03-26 15:57:53 +01:00
parent 97974409a6
commit 35df73e042
3 changed files with 31 additions and 18 deletions

View file

@ -126,6 +126,7 @@ bool DTSC::Stream::parsePacket(Socket::Buffer & buffer){
std::string wholepacket = buffer.remove(len + 8);
metadata = JSON::fromDTMI((unsigned char*)wholepacket.c_str() + 8, len, i);
metadata.removeMember("moreheader");
metadata.netPrepare();
if ( !buffer.available(8)){
return false;
}
@ -332,7 +333,7 @@ void DTSC::Stream::updateHeaders(){
metadata.removeMember("frags");
metadata.removeMember("lastms");
metadata.removeMember("missed_frags");
metadata.toPacked();
metadata.netPrepare();
return;
}
metadata["keytime"].shrink(keyframes.size() - 2);
@ -370,7 +371,7 @@ void DTSC::Stream::updateHeaders(){
metadata["lastms"] = buffers[keyframes[0].b]["time"].asInt();
metadata["buffer_window"] = (long long int)buffertime;
metadata["live"] = true;
metadata.toPacked();
metadata.netPrepare();
updateRingHeaders();
}
}
@ -592,7 +593,7 @@ void DTSC::File::readHeader(int pos){
}
}
metadata["vod"] = true;
metadata.toPacked();
metadata.netPrepare();
}
/// Reads the packet available at the current file position.

View file

@ -464,22 +464,16 @@ std::string JSON::Value::toPacked(){
}
return r;
}
;
//toPacked
/// Packs any object-type JSON::Value to a std::string for transfer over the network, including proper DTMI header.
/// Non-object-types will print an error to stderr and return an empty string.
/// This function returns a reference to an internal buffer where the prepared data is kept.
/// The internal buffer is *not* made stale if any changes occur inside the object - subsequent calls to toPacked() will clear the buffer.
std::string & JSON::Value::toNetPacked(){
static std::string emptystring;
//check if this is legal
/// Pre-packs any object-type JSON::Value to a std::string for transfer over the network, including proper DTMI header.
/// Non-object-types will print an error to stderr.
/// The internal buffer is guaranteed to be up-to-date after this function is called.
void JSON::Value::netPrepare(){
if (myType != OBJECT){
fprintf(stderr, "Error: Only objects may be NetPacked!\n");
return emptystring;
return;
}
//if sneaky storage doesn't contain correct data, re-calculate it
if (strVal.size() == 0 || strVal[0] != 'D' || strVal[1] != 'T'){
std::string packed = toPacked();
strVal.resize(packed.size() + 8);
//insert proper header for this type of data
@ -494,6 +488,23 @@ std::string & JSON::Value::toNetPacked(){
//copy the rest of the string
memcpy((void*)(strVal.c_str() + 8), packed.c_str(), packed.size());
}
/// Packs any object-type JSON::Value to a std::string for transfer over the network, including proper DTMI header.
/// Non-object-types will print an error to stderr and return an empty string.
/// This function returns a reference to an internal buffer where the prepared data is kept.
/// The internal buffer is *not* made stale if any changes occur inside the object - subsequent calls to toPacked() will clear the buffer,
/// calls to netPrepare will guarantee it is up-to-date.
std::string & JSON::Value::toNetPacked(){
static std::string emptystring;
//check if this is legal
if (myType != OBJECT){
fprintf(stderr, "Error: Only objects may be NetPacked!\n");
return emptystring;
}
//if sneaky storage doesn't contain correct data, re-calculate it
if (strVal.size() == 0 || strVal[0] != 'D' || strVal[1] != 'T'){
netPrepare();
}
return strVal;
}

View file

@ -70,6 +70,7 @@ namespace JSON {
Value & operator[](unsigned int i);
//handy functions and others
std::string toPacked();
void netPrepare();
std::string & toNetPacked();
std::string toString();
std::string toPrettyString(int indentation = 0);