From efb0b27f5bc659a49e330fb7970a9b1b6ae0f398 Mon Sep 17 00:00:00 2001 From: deva Date: Tue, 23 Sep 2008 08:10:37 +0000 Subject: Made fix for the 'multiple xml buffers in single network buffer' problem. --- server/src/server.cc | 36 ++++++++++++++++++++++++------------ server/src/transactionparser.cc | 13 +++++++++++++ server/src/transactionparser.h | 4 ++++ 3 files changed, 41 insertions(+), 12 deletions(-) (limited to 'server/src') diff --git a/server/src/server.cc b/server/src/server.cc index ad7065e..688a310 100644 --- a/server/src/server.cc +++ b/server/src/server.cc @@ -262,21 +262,33 @@ static void handleConnection(TCPSocket *socket) while((size = socket->read(buf, bufsize)) > 0) { - if(transaction == NULL) { - transaction = new Transaction(); - parser = new TransactionParser(transaction); - } + while(size) { + + if(transaction == NULL) { + transaction = new Transaction(); + parser = new TransactionParser(transaction); + } - printf("Got %d bytes in read loop\n", size); - if(parser->parse(buf)) { - printf("Got complete XML document\n"); - socket->write(handleTransaction(*transaction)); + printf("Got %d bytes in read loop\n", size); + size = 0; + if(parser->parse(buf)) { + printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n" + "!! Got complete XML document %d bytes used, %d bytes in current buffer.\n" + "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n", parser->usedBytes(), strlen(buf)); - delete transaction; - transaction = NULL; + if(parser->usedBytes() < strlen(buf)) { + size = strlen(buf) - parser->usedBytes(); + strcpy(buf, buf + parser->usedBytes()); + } - delete parser; - parser = NULL; + socket->write(handleTransaction(*transaction)); + + delete transaction; + transaction = NULL; + + delete parser; + parser = NULL; + } } memset(buf, 0, bufsize); } diff --git a/server/src/transactionparser.cc b/server/src/transactionparser.cc index 1dbeb39..2ba4fc4 100644 --- a/server/src/transactionparser.cc +++ b/server/src/transactionparser.cc @@ -37,6 +37,7 @@ TransactionParser::TransactionParser(Transaction *transaction) { this->transaction = transaction; done = false; + totalbytes = 0; } TransactionParser::~TransactionParser() @@ -83,18 +84,25 @@ int TransactionParser::readData(char *data, size_t size) bool TransactionParser::parse(std::string data) { + bufferbytes = data.length(); + totalbytes += bufferbytes; + if(! XML_Parse(p, (char*)data.c_str(), data.size(), false) ) { + if(XML_GetErrorCode(p) == XML_ERROR_JUNK_AFTER_DOC_ELEMENT) return true; parseError((char*)data.c_str(), data.size(), XML_ErrorString(XML_GetErrorCode(p)), (int)XML_GetCurrentLineNumber(p)); return false; } if(done) { if(! XML_Parse(p, (char*)data.c_str(), 0, true) ) { + if(XML_GetErrorCode(p) == XML_ERROR_JUNK_AFTER_DOC_ELEMENT) return true; parseError((char*)data.c_str(), 0, XML_ErrorString(XML_GetErrorCode(p)), (int)XML_GetCurrentLineNumber(p)); return false; } } + printf("Got END_OF_TEMPLATE at %ld\n", XML_GetCurrentByteIndex(p)); + return done; } @@ -106,3 +114,8 @@ void TransactionParser::parseError(char *buf, size_t len, std::string error, int fprintf(stderr, "]\n"); fflush(stderr); } + +unsigned int TransactionParser::usedBytes() +{ + return bufferbytes + (XML_GetCurrentByteIndex(p) - totalbytes); +} diff --git a/server/src/transactionparser.h b/server/src/transactionparser.h index 3f477c9..1f7d23f 100644 --- a/server/src/transactionparser.h +++ b/server/src/transactionparser.h @@ -43,10 +43,14 @@ public: bool parse(std::string data); + unsigned int usedBytes(); + protected: int readData(char *data, size_t size); private: + unsigned int bufferbytes; + unsigned int totalbytes; Transaction *transaction; bool done; }; -- cgit v1.2.3