X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fio%2Fsg_netChat.cxx;h=3baecdf8b64b564dc0fed0aa0e239d91a921c56b;hb=70c5d605641b628039f75cb8761ce783a17a5bdf;hp=fcced48eca9abb0963e8c23e13faf7dec6c008f3;hpb=5b734d8c527c291cccf7c366fd4b3a741409fb82;p=simgear.git diff --git a/simgear/io/sg_netChat.cxx b/simgear/io/sg_netChat.cxx index fcced48e..3baecdf8 100644 --- a/simgear/io/sg_netChat.cxx +++ b/simgear/io/sg_netChat.cxx @@ -16,7 +16,7 @@ You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA For further information visit http://plib.sourceforge.net @@ -25,21 +25,31 @@ #include -#include // for strdup +#include +#include +#include namespace simgear { void -NetChat::setTerminator (const char* t) +NetChat::setTerminator(const std::string& t) { - if (terminator) delete[] terminator; - terminator = strdup(t); + terminator = t; + bytesToCollect = -1; } const char* -NetChat::getTerminator (void) +NetChat::getTerminator() const { - return terminator; + return terminator.c_str(); +} + + +void +NetChat::setByteCount(int count) +{ + terminator.clear(); + bytesToCollect = count; } // return the size of the largest prefix of needle at the end @@ -48,15 +58,15 @@ NetChat::getTerminator (void) #define MAX(a,b) (((a)>(b))?(a):(b)) static int -find_prefix_at_end (const NetBuffer& haystack, const char* needle) +find_prefix_at_end(const NetBuffer& haystack, const std::string& needle) { const char* hd = haystack.getData(); int hl = haystack.getLength(); - int nl = strlen(needle); + int nl = needle.length(); for (int i = MAX (nl-hl, 0); i < nl; i++) { //if (haystack.compare (needle, hl-(nl-i), nl-i) == 0) { - if (memcmp(needle, &hd[hl-(nl-i)], nl-i) == 0) { + if (memcmp(needle.c_str(), &hd[hl-(nl-i)], nl-i) == 0) { return (nl-i); } } @@ -64,12 +74,12 @@ find_prefix_at_end (const NetBuffer& haystack, const char* needle) } static int -find_terminator (const NetBuffer& haystack, const char* needle) +find_terminator(const NetBuffer& haystack, const std::string& needle) { - if (needle && *needle) + if( !needle.empty() ) { const char* data = haystack.getData(); - const char* ptr = strstr(data,needle); + const char* ptr = strstr(data,needle.c_str()); if (ptr != NULL) return(ptr-data); } @@ -89,17 +99,25 @@ NetChat::handleBufferRead (NetBuffer& in_buffer) // necessary because we might read several data+terminator combos // with a single recv(). - while (in_buffer.getLength()) { - + while (in_buffer.getLength()) { // special case where we're not using a terminator - if (terminator == 0 || *terminator == 0) { - collectIncomingData (in_buffer.getData(),in_buffer.getLength()); - in_buffer.remove (); - return; + if ( terminator.empty() ) { + if ( bytesToCollect > 0) { + const int toRead = std::min(in_buffer.getLength(), bytesToCollect); + collectIncomingData(in_buffer.getData(), toRead); + in_buffer.remove(0, toRead); + bytesToCollect -= toRead; + if (bytesToCollect == 0) { // read all requested bytes + foundTerminator(); + } + } else { // read the whole lot + collectIncomingData (in_buffer.getData(),in_buffer.getLength()); + in_buffer.remove (); + } + + continue; } - int terminator_len = strlen(terminator); - int index = find_terminator ( in_buffer, terminator ) ; // 3 cases: @@ -113,7 +131,7 @@ NetChat::handleBufferRead (NetBuffer& in_buffer) if (index != -1) { // we found the terminator collectIncomingData ( in_buffer.getData(), index ) ; - in_buffer.remove (0, index + terminator_len); + in_buffer.remove (0, index + terminator.length()); foundTerminator(); } else { // check for a prefix of the terminator