From: Maxim Mamontov Date: Sun, 20 Oct 2013 12:44:53 +0000 (+0300) Subject: Simplified NETTRANSACT. X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/5ec330c9443a3ad42258159e11013c116d9c4978 Simplified NETTRANSACT. --- diff --git a/stglibs/srvconf.lib/netunit.cpp b/stglibs/srvconf.lib/netunit.cpp index 9955d2e5..b7aa4f73 100644 --- a/stglibs/srvconf.lib/netunit.cpp +++ b/stglibs/srvconf.lib/netunit.cpp @@ -75,9 +75,7 @@ NETTRANSACT::NETTRANSACT(const std::string & s, uint16_t p, port(p), login(l), password(pwd), - outerSocket(-1), - RxCallBack(NULL), - dataRxCallBack(NULL) + outerSocket(-1) { } //--------------------------------------------------------------------------- @@ -123,13 +121,12 @@ if (connect(outerSocket, (struct sockaddr *)&outerAddr, sizeof(outerAddr)) < 0) return st_ok; } //--------------------------------------------------------------------------- -int NETTRANSACT::Disconnect() +void NETTRANSACT::Disconnect() { close(outerSocket); -return 0; } //--------------------------------------------------------------------------- -int NETTRANSACT::Transact(const char * data) +int NETTRANSACT::Transact(const char * request, CALLBACK callback, void * data) { int ret; if ((ret = TxHeader()) != st_ok) @@ -168,13 +165,13 @@ if ((ret = RxLoginSAnswer()) != st_ok) return ret; } -if ((ret = TxData(data)) != st_ok) +if ((ret = TxData(request)) != st_ok) { Disconnect(); return ret; } -if ((ret = RxDataAnswer()) != st_ok) +if ((ret = RxDataAnswer(callback, data)) != st_ok) { Disconnect(); return ret; @@ -388,7 +385,7 @@ for (int j = 0; j < l; j++) return 0; } //--------------------------------------------------------------------------- -int NETTRANSACT::RxDataAnswer() +int NETTRANSACT::RxDataAnswer(CALLBACK callback, void * data) { BLOWFISH_CTX ctx; EnDecodeInit(password.c_str(), PASSWD_LEN, &ctx); @@ -425,8 +422,8 @@ while (true) if (chunk.length() > MAX_XML_CHUNK_LENGTH || final) { - if (RxCallBack != NULL) - if (!RxCallBack(dataRxCallBack, chunk, final)) + if (callback) + if (!callback(chunk, final, data)) return st_xml_parse_error; chunk.clear(); } @@ -435,9 +432,3 @@ while (true) return st_ok; } } -//--------------------------------------------------------------------------- -void NETTRANSACT::SetRxCallback(void * data, RxCallback_t cb) -{ -RxCallBack = cb; -dataRxCallBack = data; -} diff --git a/stglibs/srvconf.lib/netunit.h b/stglibs/srvconf.lib/netunit.h index fb2a1e28..dc705803 100644 --- a/stglibs/srvconf.lib/netunit.h +++ b/stglibs/srvconf.lib/netunit.h @@ -31,21 +31,19 @@ #include -typedef bool (* RxCallback_t)(void *, const std::string &, bool); - //--------------------------------------------------------------------------- class NETTRANSACT { public: + typedef bool (* CALLBACK)(const std::string &, bool, void *); + NETTRANSACT(const std::string & server, uint16_t port, const std::string & login, const std::string & password); - int Transact(const char * data); + int Transact(const char * request, CALLBACK f, void * data); const std::string & GetError() const { return errorMsg; } - void SetRxCallback(void * data, RxCallback_t cb); - int Connect(); - int Disconnect(); + void Disconnect(); private: int TxHeader(); int RxHeaderAnswer(); @@ -58,15 +56,13 @@ private: int TxData(const char * text); int TxData(char * data); - int RxDataAnswer(); + int RxDataAnswer(CALLBACK f, void * data); std::string server; uint16_t port; std::string login; std::string password; int outerSocket; - RxCallback_t RxCallBack; - void * dataRxCallBack; std::string errorMsg; }; //--------------------------------------------------------------------------- diff --git a/stglibs/srvconf.lib/servconf.cpp b/stglibs/srvconf.lib/servconf.cpp index ab8e5e79..16878f18 100644 --- a/stglibs/srvconf.lib/servconf.cpp +++ b/stglibs/srvconf.lib/servconf.cpp @@ -87,12 +87,12 @@ private: std::string errorMsg; XML_Parser parser; - static bool ParserRecv(void * data, const std::string & chunk, bool final); - static bool SimpleRecv(void * data, const std::string & chunk, bool final); + static bool ParserRecv(const std::string & chunk, bool final, void * data); + static bool SimpleRecv(const std::string & chunk, bool final, void * data); int ExecImpl(const std::string & request, PARSER & cp); }; -bool SERVCONF::IMPL::ParserRecv(void * data, const std::string & chunk, bool final) +bool SERVCONF::IMPL::ParserRecv(const std::string & chunk, bool final, void * data) { SERVCONF::IMPL * sc = static_cast(data); @@ -108,7 +108,7 @@ if (XML_Parse(sc->parser, chunk.c_str(), chunk.length(), final) == XML_STATUS_ER return true; } -bool SERVCONF::IMPL::SimpleRecv(void * data, const std::string & chunk, bool /*final*/) +bool SERVCONF::IMPL::SimpleRecv(const std::string & chunk, bool /*final*/, void * data) { *static_cast(data) += chunk; return true; @@ -319,7 +319,6 @@ SERVCONF::IMPL::IMPL(const std::string & server, uint16_t port, : nt( server, port, login, password ) { parser = XML_ParserCreate(NULL); -nt.SetRxCallback(this, ParserRecv); } //----------------------------------------------------------------------------- void SERVCONF::IMPL::Start(void * data, const char * el, const char ** attr) @@ -351,47 +350,34 @@ if ((ret = nt.Connect()) != st_ok) errorMsg = nt.GetError(); return ret; } -if ((ret = nt.Transact(request.c_str())) != st_ok) - { - errorMsg = nt.GetError(); - return ret; - } -if ((ret = nt.Disconnect()) != st_ok) +if ((ret = nt.Transact(request.c_str(), ParserRecv, this)) != st_ok) { errorMsg = nt.GetError(); return ret; } +nt.Disconnect(); return st_ok; } -int SERVCONF::IMPL::RawXML(const std::string & request, RAW_XML::CALLBACK f, void * data) +int SERVCONF::IMPL::RawXML(const std::string & request, RAW_XML::CALLBACK callback, void * data) { -std::string response; -nt.SetRxCallback(&response, SimpleRecv); int ret = 0; if ((ret = nt.Connect()) != st_ok) { - nt.SetRxCallback(this, ParserRecv); errorMsg = nt.GetError(); - f(false, errorMsg, "", data); + callback(false, errorMsg, "", data); return ret; } -if ((ret = nt.Transact(request.c_str())) != st_ok) - { - nt.SetRxCallback(this, ParserRecv); - errorMsg = nt.GetError(); - f(false, errorMsg, "", data); - return ret; - } -if ((ret = nt.Disconnect()) != st_ok) +std::string response; +if ((ret = nt.Transact(request.c_str(), SimpleRecv, &response)) != st_ok) { - nt.SetRxCallback(this, ParserRecv); errorMsg = nt.GetError(); - f(false, errorMsg, "", data); + callback(false, errorMsg, "", data); return ret; } -nt.SetRxCallback(this, ParserRecv); -f(true, "", response, data); + +nt.Disconnect(); +callback(true, "", response, data); return st_ok; }