Fixed several buffer segfaults when multiple viewers are connected.
This commit is contained in:
parent
97974409a6
commit
35df73e042
3 changed files with 31 additions and 18 deletions
|
@ -126,6 +126,7 @@ bool DTSC::Stream::parsePacket(Socket::Buffer & buffer){
|
||||||
std::string wholepacket = buffer.remove(len + 8);
|
std::string wholepacket = buffer.remove(len + 8);
|
||||||
metadata = JSON::fromDTMI((unsigned char*)wholepacket.c_str() + 8, len, i);
|
metadata = JSON::fromDTMI((unsigned char*)wholepacket.c_str() + 8, len, i);
|
||||||
metadata.removeMember("moreheader");
|
metadata.removeMember("moreheader");
|
||||||
|
metadata.netPrepare();
|
||||||
if ( !buffer.available(8)){
|
if ( !buffer.available(8)){
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -332,7 +333,7 @@ void DTSC::Stream::updateHeaders(){
|
||||||
metadata.removeMember("frags");
|
metadata.removeMember("frags");
|
||||||
metadata.removeMember("lastms");
|
metadata.removeMember("lastms");
|
||||||
metadata.removeMember("missed_frags");
|
metadata.removeMember("missed_frags");
|
||||||
metadata.toPacked();
|
metadata.netPrepare();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
metadata["keytime"].shrink(keyframes.size() - 2);
|
metadata["keytime"].shrink(keyframes.size() - 2);
|
||||||
|
@ -370,7 +371,7 @@ void DTSC::Stream::updateHeaders(){
|
||||||
metadata["lastms"] = buffers[keyframes[0].b]["time"].asInt();
|
metadata["lastms"] = buffers[keyframes[0].b]["time"].asInt();
|
||||||
metadata["buffer_window"] = (long long int)buffertime;
|
metadata["buffer_window"] = (long long int)buffertime;
|
||||||
metadata["live"] = true;
|
metadata["live"] = true;
|
||||||
metadata.toPacked();
|
metadata.netPrepare();
|
||||||
updateRingHeaders();
|
updateRingHeaders();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -592,7 +593,7 @@ void DTSC::File::readHeader(int pos){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
metadata["vod"] = true;
|
metadata["vod"] = true;
|
||||||
metadata.toPacked();
|
metadata.netPrepare();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reads the packet available at the current file position.
|
/// Reads the packet available at the current file position.
|
||||||
|
|
41
lib/json.cpp
41
lib/json.cpp
|
@ -464,13 +464,36 @@ std::string JSON::Value::toPacked(){
|
||||||
}
|
}
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
;
|
|
||||||
//toPacked
|
//toPacked
|
||||||
|
|
||||||
|
/// 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;
|
||||||
|
}
|
||||||
|
std::string packed = toPacked();
|
||||||
|
strVal.resize(packed.size() + 8);
|
||||||
|
//insert proper header for this type of data
|
||||||
|
if (isMember("datatype")){
|
||||||
|
memcpy((void*)strVal.c_str(), "DTPD", 4);
|
||||||
|
}else{
|
||||||
|
memcpy((void*)strVal.c_str(), "DTSC", 4);
|
||||||
|
}
|
||||||
|
//insert the packet length at bytes 4-7
|
||||||
|
unsigned int size = htonl(packed.size());
|
||||||
|
memcpy((void*)(strVal.c_str() + 4), (void*) &size, 4);
|
||||||
|
//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.
|
/// 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.
|
/// 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.
|
/// 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.
|
/// 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(){
|
std::string & JSON::Value::toNetPacked(){
|
||||||
static std::string emptystring;
|
static std::string emptystring;
|
||||||
//check if this is legal
|
//check if this is legal
|
||||||
|
@ -480,19 +503,7 @@ std::string & JSON::Value::toNetPacked(){
|
||||||
}
|
}
|
||||||
//if sneaky storage doesn't contain correct data, re-calculate it
|
//if sneaky storage doesn't contain correct data, re-calculate it
|
||||||
if (strVal.size() == 0 || strVal[0] != 'D' || strVal[1] != 'T'){
|
if (strVal.size() == 0 || strVal[0] != 'D' || strVal[1] != 'T'){
|
||||||
std::string packed = toPacked();
|
netPrepare();
|
||||||
strVal.resize(packed.size() + 8);
|
|
||||||
//insert proper header for this type of data
|
|
||||||
if (isMember("datatype")){
|
|
||||||
memcpy((void*)strVal.c_str(), "DTPD", 4);
|
|
||||||
}else{
|
|
||||||
memcpy((void*)strVal.c_str(), "DTSC", 4);
|
|
||||||
}
|
|
||||||
//insert the packet length at bytes 4-7
|
|
||||||
unsigned int size = htonl(packed.size());
|
|
||||||
memcpy((void*)(strVal.c_str() + 4), (void*) &size, 4);
|
|
||||||
//copy the rest of the string
|
|
||||||
memcpy((void*)(strVal.c_str() + 8), packed.c_str(), packed.size());
|
|
||||||
}
|
}
|
||||||
return strVal;
|
return strVal;
|
||||||
}
|
}
|
||||||
|
|
|
@ -70,6 +70,7 @@ namespace JSON {
|
||||||
Value & operator[](unsigned int i);
|
Value & operator[](unsigned int i);
|
||||||
//handy functions and others
|
//handy functions and others
|
||||||
std::string toPacked();
|
std::string toPacked();
|
||||||
|
void netPrepare();
|
||||||
std::string & toNetPacked();
|
std::string & toNetPacked();
|
||||||
std::string toString();
|
std::string toString();
|
||||||
std::string toPrettyString(int indentation = 0);
|
std::string toPrettyString(int indentation = 0);
|
||||||
|
|
Loading…
Add table
Reference in a new issue