From 51d3a12ab31045c7109125d978800d0f61c72cc0 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 26 Jan 2022 11:50:43 +0300 Subject: [PATCH] WIP --- backends/libcommuni/CMakeLists.txt | 8 +-- backends/swiften/CMakeLists.txt | 4 +- backends/swiften/main.cpp | 30 ++--------- backends/template/CMakeLists.txt | 6 +-- backends/template/main.cpp | 8 +-- backends/template/plugin.cpp | 23 ++------ backends/template/plugin.h | 11 +--- backends/twitter/CMakeLists.txt | 2 +- backends/twitter/TwitterPlugin.cpp | 48 +++-------------- backends/twitter/TwitterPlugin.h | 22 +++----- backends/twitter/main.cpp | 6 +-- include/transport/NetworkPlugin.h | 19 ++++--- plugin/cpp/CMakeLists.txt | 6 ++- {libtransport => plugin/cpp}/HTTPRequest.cpp | 0 .../cpp}/HTTPRequestQueue.cpp | 0 {libtransport => plugin/cpp}/MySQLBackend.cpp | 0 {libtransport => plugin/cpp}/PQXXBackend.cpp | 0 .../cpp}/SQLite3Backend.cpp | 0 .../cpp}/StorageBackend.cpp | 0 plugin/cpp/networkplugin.cpp | 53 ++++++++----------- 20 files changed, 70 insertions(+), 176 deletions(-) rename {libtransport => plugin/cpp}/HTTPRequest.cpp (100%) rename {libtransport => plugin/cpp}/HTTPRequestQueue.cpp (100%) rename {libtransport => plugin/cpp}/MySQLBackend.cpp (100%) rename {libtransport => plugin/cpp}/PQXXBackend.cpp (100%) rename {libtransport => plugin/cpp}/SQLite3Backend.cpp (100%) rename {libtransport => plugin/cpp}/StorageBackend.cpp (100%) diff --git a/backends/libcommuni/CMakeLists.txt b/backends/libcommuni/CMakeLists.txt index 0d3ee4ccc..e727aeabb 100644 --- a/backends/libcommuni/CMakeLists.txt +++ b/backends/libcommuni/CMakeLists.txt @@ -13,15 +13,15 @@ add_executable(spectrum2_libcommuni_backend ${SRC}) if(NOT WIN32) if(ENABLE_QT4) - target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} Qt4::QtNetwork Qt4::QtCore transport pthread) + target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} Qt4::QtNetwork Qt4::QtCore transport-plugin pthread) else() - target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} Qt5::Network Qt5::Core transport pthread) + target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} Qt5::Network Qt5::Core transport-plugin pthread) endif() else() if(ENABLE_QT4) - target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} Qt4::QtNetwork Qt4::QtCore transport) + target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} Qt4::QtNetwork Qt4::QtCore transport-plugin) else() - target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} Qt5::Network Qt5::Core transport) + target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} Qt5::Network Qt5::Core transport-plugin) endif() endif() diff --git a/backends/swiften/CMakeLists.txt b/backends/swiften/CMakeLists.txt index fa8db5d87..32dd12a5a 100644 --- a/backends/swiften/CMakeLists.txt +++ b/backends/swiften/CMakeLists.txt @@ -3,9 +3,9 @@ file(GLOB SRC *.cpp) add_executable(spectrum2_swiften_backend ${SRC}) if(NOT WIN32) - target_link_libraries(spectrum2_swiften_backend transport pthread ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES}) + target_link_libraries(spectrum2_swiften_backend transport-plugin pthread ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES}) else() - target_link_libraries(spectrum2_swiften_backend transport ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES}) + target_link_libraries(spectrum2_swiften_backend transport-plugin ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES}) endif() install(TARGETS spectrum2_swiften_backend RUNTIME DESTINATION ${CMAKE_INSTALL_LIBEXECDIR}) diff --git a/backends/swiften/main.cpp b/backends/swiften/main.cpp index 9d6dc4b89..aeb6f0e6d 100644 --- a/backends/swiften/main.cpp +++ b/backends/swiften/main.cpp @@ -80,36 +80,15 @@ class SwiftenPlugin : public NetworkPlugin, Swift::XMPPParserClient { Swift::XMPPParser *m_xmppParser; Swift::FullPayloadParserFactoryCollection m_collection2; - SwiftenPlugin(Config *config, Swift::SimpleEventLoop *loop, const std::string &host, int port) : NetworkPlugin() { + SwiftenPlugin(Config *config, const std::string &host, int port) : NetworkPlugin() { this->config = config; m_firstPing = true; - m_factories = new Swift::BoostNetworkFactories(loop); - m_conn = m_factories->getConnectionFactory()->createConnection(); - m_conn->onDataRead.connect(boost::bind(&SwiftenPlugin::_handleDataRead, this, _1)); - m_conn->connect(Swift::HostAddressPort(*(Swift::HostAddress::fromString(host)), port)); serializer = new Swift::XMPPSerializer(&collection, Swift::ClientStreamType, false); m_xmppParser = new Swift::XMPPParser(this, &m_collection2, m_factories->getXMLParserFactory()); m_xmppParser->parse(""); LOG4CXX_INFO(logger, "Starting the plugin."); - } - - // NetworkPlugin uses this method to send the data to networkplugin server - void sendData(const std::string &string) { - m_conn->write(Swift::createSafeByteArray(string)); - } - - // This method has to call handleDataRead with all received data from network plugin server - void _handleDataRead(std::shared_ptr data) { - if (m_firstPing) { - m_firstPing = false; - NetworkPlugin::PluginConfig cfg; - cfg.setRawXML(true); - cfg.setNeedRegistration(false); - sendConfig(cfg); - } - std::string d(data->begin(), data->end()); - handleDataRead(d); + connect(host, std::to_string(port)); } void handleStreamStart(const Swift::ProtocolHeader&) {} @@ -446,10 +425,7 @@ int main (int argc, char* argv[]) { Logging::initBackendLogging(cfg); - Swift::SimpleEventLoop eventLoop; - loop_ = &eventLoop; - np = new SwiftenPlugin(cfg, &eventLoop, host, port); - loop_->run(); + np = new SwiftenPlugin(cfg, host, port); return 0; } diff --git a/backends/template/CMakeLists.txt b/backends/template/CMakeLists.txt index 37828416d..66490e639 100644 --- a/backends/template/CMakeLists.txt +++ b/backends/template/CMakeLists.txt @@ -4,12 +4,12 @@ add_executable(spectrum2_template_backend ${SRC}) if(CMAKE_COMPILER_IS_GNUCXX) if(NOT WIN32) - target_link_libraries(spectrum2_template_backend transport pthread ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES}) + target_link_libraries(spectrum2_template_backend transport-plugin pthread ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES}) else() - target_link_libraries(spectrum2_template_backend transport ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES}) + target_link_libraries(spectrum2_template_backend transport-plugin ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES}) endif() else() - target_link_libraries(spectrum2_template_backend transport ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES}) + target_link_libraries(spectrum2_template_backend transport-plugin ${PROTOBUF_LIBRARY} ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES}) endif() #install(TARGETS spectrum2_template_backend RUNTIME DESTINATION bin) diff --git a/backends/template/main.cpp b/backends/template/main.cpp index 5f9df1a81..1a7324d86 100644 --- a/backends/template/main.cpp +++ b/backends/template/main.cpp @@ -5,9 +5,6 @@ #include "transport/NetworkPlugin.h" #include "transport/Logging.h" -// Swiften -#include "Swiften/Swiften.h" - #ifndef _WIN32 // for signal handler #include "unistd.h" @@ -17,6 +14,7 @@ #endif // Boost #include +#include using namespace boost::filesystem; using namespace boost::program_options; using namespace Transport; @@ -60,9 +58,7 @@ int main (int argc, char* argv[]) { Logging::initBackendLogging(cfg); - Swift::SimpleEventLoop eventLoop; - Plugin p(cfg, &eventLoop, host, port); - eventLoop.run(); + Plugin p(cfg, host, port); return 0; } diff --git a/backends/template/plugin.cpp b/backends/template/plugin.cpp index 57b828487..447476a22 100644 --- a/backends/template/plugin.cpp +++ b/backends/template/plugin.cpp @@ -4,36 +4,19 @@ #include "transport/NetworkPlugin.h" #include "transport/Logging.h" -// Swiften -#include "Swiften/Swiften.h" - // Boost #include +#include using namespace boost::filesystem; using namespace boost::program_options; using namespace Transport; DEFINE_LOGGER(logger, "Backend Template"); -Plugin::Plugin(Config *config, Swift::SimpleEventLoop *loop, const std::string &host, int port) : NetworkPlugin() { +Plugin::Plugin(Config *config, const std::string &host, int port) : NetworkPlugin() { this->config = config; - m_factories = new Swift::BoostNetworkFactories(loop); - m_conn = m_factories->getConnectionFactory()->createConnection(); - m_conn->onDataRead.connect(boost::bind(&Plugin::_handleDataRead, this, _1)); - m_conn->connect(Swift::HostAddressPort(*(Swift::HostAddress::fromString(host)), port)); - LOG4CXX_INFO(logger, "Starting the plugin."); -} - -// NetworkPlugin uses this method to send the data to networkplugin server -void Plugin::sendData(const std::string &string) { - m_conn->write(Swift::createSafeByteArray(string)); -} - -// This method has to call handleDataRead with all received data from network plugin server -void Plugin::_handleDataRead(std::shared_ptr data) { - std::string d(data->begin(), data->end()); - handleDataRead(d); + connect(host, std::to_string(port)); } void Plugin::handleLoginRequest(const std::string &user, const std::string &legacyName, const std::string &password, const std::map &settings) { diff --git a/backends/template/plugin.h b/backends/template/plugin.h index be2d876d8..9dc56d7db 100644 --- a/backends/template/plugin.h +++ b/backends/template/plugin.h @@ -1,13 +1,11 @@ #pragma once -#include "Swiften/Swiften.h" - #include "transport/Config.h" #include "transport/NetworkPlugin.h" class Plugin : public Transport::NetworkPlugin { public: - Plugin(Transport::Config *config, Swift::SimpleEventLoop *loop, const std::string &host, int port); + Plugin(Transport::Config *config, const std::string &host, int port); // NetworkPlugin uses this method to send the data to networkplugin server void sendData(const std::string &string); @@ -23,12 +21,5 @@ class Plugin : public Transport::NetworkPlugin { void handleBuddyRemovedRequest(const std::string &user, const std::string &buddyName, const std::vector &groups); private: - // This method has to call handleDataRead with all received data from network plugin server - void _handleDataRead(std::shared_ptr data); - - private: - Swift::BoostNetworkFactories *m_factories; - Swift::BoostIOServiceThread m_boostIOServiceThread; - std::shared_ptr m_conn; Transport::Config *config; }; diff --git a/backends/twitter/CMakeLists.txt b/backends/twitter/CMakeLists.txt index 93a3af353..b3b2097c5 100644 --- a/backends/twitter/CMakeLists.txt +++ b/backends/twitter/CMakeLists.txt @@ -8,7 +8,7 @@ find_package(CURL) if(CURL_FOUND) message(STATUS "Using curl ${CURL_VERSION_STRING}: ${CURL_INCLUDE_DIRS} ${CURL_LIBRARIES}") - target_link_libraries(spectrum2_twitter_backend transport JsonCpp::JsonCpp ${CURL_LIBRARIES} ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES}) + target_link_libraries(spectrum2_twitter_backend transport-plugin JsonCpp::JsonCpp ${CURL_LIBRARIES} ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES}) else() message(FATAL_ERROR "curl not found") endif() diff --git a/backends/twitter/TwitterPlugin.cpp b/backends/twitter/TwitterPlugin.cpp index 59d901872..e1c52f53d 100644 --- a/backends/twitter/TwitterPlugin.cpp +++ b/backends/twitter/TwitterPlugin.cpp @@ -10,12 +10,10 @@ #include "Requests/DestroyFriendRequest.h" #include "Requests/RetweetRequest.h" #include "Requests/ProfileImageRequest.h" -#include "Swiften/StringCodecs/Hexify.h" DEFINE_LOGGER(logger, "Twitter Backend"); TwitterPlugin *np = NULL; -Swift::SimpleEventLoop *loop_; // Event Loop const std::string OLD_APP_KEY = "PCWAdQpyyR12ezp2fVwEhw"; const std::string OLD_APP_SECRET = "EveLmCXJIg2R7BTCpm6OWV8YyX49nI0pxnYXh7JMvDg"; @@ -35,7 +33,7 @@ static int cmp(std::string a, std::string b) } -TwitterPlugin::TwitterPlugin(Config *config, Swift::SimpleEventLoop *loop, StorageBackend *storagebackend, const std::string &host, int port) : NetworkPlugin() +TwitterPlugin::TwitterPlugin(Config *config, StorageBackend *storagebackend, const std::string &host, int port) : NetworkPlugin() { this->config = config; this->storagebackend = storagebackend; @@ -68,26 +66,17 @@ TwitterPlugin::TwitterPlugin(Config *config, Swift::SimpleEventLoop *loop, Stora OAUTH_SECRET = "twitter_oauth_secret"; MODE = "mode"; - m_factories = new Swift::BoostNetworkFactories(loop); - m_conn = m_factories->getConnectionFactory()->createConnection(); - m_conn->onDataRead.connect(boost::bind(&TwitterPlugin::_handleDataRead, this, _1)); - m_conn->connect(Swift::HostAddressPort(*(Swift::HostAddress::fromString(host)), port)); - - tp = new ThreadPool(loop_, 10); - LOG4CXX_INFO(logger, "Fetch timeout is set to " << CONFIG_INT_DEFAULTED(config, "twitter.fetch_timeout", 90000)); - tweet_timer = m_factories->getTimerFactory()->createTimer(CONFIG_INT_DEFAULTED(config, "twitter.fetch_timeout", 90000)); - message_timer = m_factories->getTimerFactory()->createTimer(CONFIG_INT_DEFAULTED(config, "twitter.fetch_timeout", 90000)); + io = std::make_unique(); + tweet_timer = std::make_unique(io, boost::posix_time::seconds(CONFIG_INT_DEFAULTED(config, "twitter.fetch_timeout", 90000))); + message_timer = std::make_unique(io, boost::posix_time::seconds(CONFIG_INT_DEFAULTED(config, "twitter.fetch_timeout", 90000))); - tweet_timer->onTick.connect(boost::bind(&TwitterPlugin::pollForTweets, this)); - //message_timer->onTick.connect(boost::bind(&TwitterPlugin::pollForDirectMessages, this)); - - tweet_timer->start(); - message_timer->start(); - cryptoProvider = std::shared_ptr(Swift::PlatformCryptoProvider::create()); + tweet_timer->async_wait(boost::bind(&TwitterPlugin::pollForTweets, this)); + //message_timer->async_wait(boost::bind(&TwitterPlugin::pollForDirectMessages, this)); LOG4CXX_INFO(logger, "Starting the plugin."); + connect(host, std::to_string(port)); } TwitterPlugin::~TwitterPlugin() @@ -95,29 +84,6 @@ TwitterPlugin::~TwitterPlugin() delete storagebackend; std::set::iterator it; for (it = onlineUsers.begin() ; it != onlineUsers.end() ; it++) delete userdb[*it].sessions; - delete tp; -} - -// Send data to NetworkPlugin server -void TwitterPlugin::sendData(const std::string &string) -{ - m_conn->write(Swift::createSafeByteArray(string)); -} - -// Receive date from the NetworkPlugin server and invoke the appropirate payload handler (implement in the NetworkPlugin class) -void TwitterPlugin::_handleDataRead(std::shared_ptr data) -{ - if (m_firstPing) { - m_firstPing = false; - // Users can join the network without registering if we allow - // one user to connect multiple IRC networks. - NetworkPlugin::PluginConfig cfg; - cfg.setNeedPassword(false); - sendConfig(cfg); - } - - std::string d(data->begin(), data->end()); - handleDataRead(d); } // User trying to login into his twitter account diff --git a/backends/twitter/TwitterPlugin.h b/backends/twitter/TwitterPlugin.h index 967df7fc3..13866e5d2 100644 --- a/backends/twitter/TwitterPlugin.h +++ b/backends/twitter/TwitterPlugin.h @@ -7,9 +7,7 @@ #include "transport/MySQLBackend.h" #include "transport/PQXXBackend.h" #include "transport/StorageBackend.h" -#include "transport/ThreadPool.h" -#include #ifndef _WIN32 #include "unistd.h" #include "signal.h" @@ -17,6 +15,7 @@ #include "sys/signal.h" #endif #include +#include #include #include @@ -30,8 +29,7 @@ #include #include #include -#include -#include + using namespace boost::filesystem; using namespace boost::program_options; using namespace Transport; @@ -40,27 +38,20 @@ using namespace Transport; class TwitterPlugin; extern TwitterPlugin *np; -extern Swift::SimpleEventLoop *loop_; // Event Loop class TwitterPlugin : public NetworkPlugin { public: - Swift::BoostNetworkFactories *m_factories; - Swift::BoostIOServiceThread m_boostIOServiceThread; - std::shared_ptr m_conn; - std::shared_ptr cryptoProvider; - Swift::Timer::ref tweet_timer; - Swift::Timer::ref message_timer; + std::unique_ptr tweet_timer; + std::unique_ptr message_timer; + std::unique_ptr io; StorageBackend *storagebackend; - TwitterPlugin(Config *config, Swift::SimpleEventLoop *loop, StorageBackend *storagebackend, const std::string &host, int port); + TwitterPlugin(Config *config, StorageBackend *storagebackend, const std::string &host, int port); ~TwitterPlugin(); // Send data to NetworkPlugin server void sendData(const std::string &string); - - // Receive date from the NetworkPlugin server and invoke the appropirate payload handler (implement in the NetworkPlugin class) - void _handleDataRead(std::shared_ptr data); // User trying to login into his twitter account void handleLoginRequest(const std::string &user, const std::string &legacyName, const std::string &password, const std::map &settings); @@ -153,7 +144,6 @@ class TwitterPlugin : public NetworkPlugin { boost::mutex dblock, userlock; - ThreadPool *tp; std::set onlineUsers; struct UserData { diff --git a/backends/twitter/main.cpp b/backends/twitter/main.cpp index 5d4d61dad..27f285b3e 100644 --- a/backends/twitter/main.cpp +++ b/backends/twitter/main.cpp @@ -52,11 +52,7 @@ int main (int argc, char* argv[]) { LOG4CXX_ERROR(logger, "Can't connect to database!"); return -1; } - - Swift::SimpleEventLoop eventLoop; - loop_ = &eventLoop; - np = new TwitterPlugin(cfg, &eventLoop, storagebackend, host, port); - loop_->run(); + np = new TwitterPlugin(cfg, storagebackend, host, port); return 0; } diff --git a/include/transport/NetworkPlugin.h b/include/transport/NetworkPlugin.h index eecf9497d..a696bb5bd 100644 --- a/include/transport/NetworkPlugin.h +++ b/include/transport/NetworkPlugin.h @@ -27,6 +27,8 @@ #include #include +#include + namespace Transport { /// Represents Spectrum2 legacy network plugin. @@ -50,7 +52,6 @@ class NetworkPlugin { void setExtraFields(const std::vector &fields) { m_extraFields = fields; } void setRawXML(bool rawXML = false) { m_rawXML = rawXML; } void disableJIDEscaping() { m_disableJIDEscaping = true; } - private: bool m_needPassword; bool m_needRegistration; @@ -62,15 +63,17 @@ class NetworkPlugin { friend class NetworkPlugin; }; - /// Creates new NetworkPlugin and connects the Spectrum2 NetworkPluginServer. - /// \param loop Event loop. - /// \param host Host where Spectrum2 NetworkPluginServer runs. - /// \param port Port. + /// Creates new NetworkPlugin NetworkPlugin(); /// Destructor. virtual ~NetworkPlugin(); + /// Connects the Spectrum2 NetworkPluginServer. + /// \param host Host where Spectrum2 NetworkPluginServer runs. + /// \param port Port ("http" or "8080") + void connect(const std::string &host, const std::string &port); + void sendConfig(const PluginConfig &cfg); void sendRawXML(std::string &xml); @@ -266,8 +269,8 @@ class NetworkPlugin { virtual void handleMemoryUsage(double &res, double &shared) {res = 0; shared = 0;} virtual void handleExitRequest() { exit(1); } - void handleDataRead(std::string &data); - virtual void sendData(const std::string &string) {} + void handleDataRead(std::istream &data); + void sendData(const std::string& string); void checkPing(); @@ -292,7 +295,7 @@ class NetworkPlugin { std::string m_data; bool m_pingReceived; double m_init_res; - + boost::asio::ip::tcp::iostream stream; }; } diff --git a/plugin/cpp/CMakeLists.txt b/plugin/cpp/CMakeLists.txt index 370d02b5e..f8ae7d0db 100644 --- a/plugin/cpp/CMakeLists.txt +++ b/plugin/cpp/CMakeLists.txt @@ -18,10 +18,12 @@ if(CMAKE_COMPILER_IS_GNUCXX) endif() endif() +find_package(CURL) + if(NOT WIN32) - target_link_libraries(transport-plugin ${PROTOBUF_LIBRARY} ${LOG4CXX_LIBRARIES} ${Boost_LIBRARIES}) + target_link_libraries(transport-plugin ${PROTOBUF_LIBRARY} ${LOG4CXX_LIBRARIES} ${Boost_LIBRARIES} ${PQXX_LIBRARIES} ${CURL_LIBRARIES} ${PQ_LIBRARY} ${SQLITE3_LIBRARIES} ${MYSQL_LIBRARIES}) else() - target_link_libraries(transport-plugin ${PROTOBUF_LIBRARY} ${LOG4CXX_LIBRARIES} ${Boost_LIBRARIES} ws2_32.lib) + target_link_libraries(transport-plugin ${PROTOBUF_LIBRARY} ${LOG4CXX_LIBRARIES} ${Boost_LIBRARIES} ${PQXX_LIBRARIES} ${CURL_LIBRARIES} ${PQ_LIBRARY} ${SQLITE3_LIBRARIES} ${MYSQL_LIBRARIES} ws2_32.lib) endif() set_target_properties(transport-plugin PROPERTIES diff --git a/libtransport/HTTPRequest.cpp b/plugin/cpp/HTTPRequest.cpp similarity index 100% rename from libtransport/HTTPRequest.cpp rename to plugin/cpp/HTTPRequest.cpp diff --git a/libtransport/HTTPRequestQueue.cpp b/plugin/cpp/HTTPRequestQueue.cpp similarity index 100% rename from libtransport/HTTPRequestQueue.cpp rename to plugin/cpp/HTTPRequestQueue.cpp diff --git a/libtransport/MySQLBackend.cpp b/plugin/cpp/MySQLBackend.cpp similarity index 100% rename from libtransport/MySQLBackend.cpp rename to plugin/cpp/MySQLBackend.cpp diff --git a/libtransport/PQXXBackend.cpp b/plugin/cpp/PQXXBackend.cpp similarity index 100% rename from libtransport/PQXXBackend.cpp rename to plugin/cpp/PQXXBackend.cpp diff --git a/libtransport/SQLite3Backend.cpp b/plugin/cpp/SQLite3Backend.cpp similarity index 100% rename from libtransport/SQLite3Backend.cpp rename to plugin/cpp/SQLite3Backend.cpp diff --git a/libtransport/StorageBackend.cpp b/plugin/cpp/StorageBackend.cpp similarity index 100% rename from libtransport/StorageBackend.cpp rename to plugin/cpp/StorageBackend.cpp diff --git a/plugin/cpp/networkplugin.cpp b/plugin/cpp/networkplugin.cpp index cc1dba244..53f5ec568 100644 --- a/plugin/cpp/networkplugin.cpp +++ b/plugin/cpp/networkplugin.cpp @@ -24,14 +24,9 @@ #include -#ifndef WIN32 -#include -#include -#include -#else -#include -#include -#include +#include + +#ifdef _WIN32 #define getpid _getpid #endif @@ -60,6 +55,12 @@ NetworkPlugin::NetworkPlugin() { NetworkPlugin::~NetworkPlugin() { } +void NetworkPlugin::connect(const std::string& host, const std::string &port) { + stream.expires_after(boost::asio::chrono::seconds(60)); + stream.connect(host, port); + handleDataRead(stream); +} + void NetworkPlugin::sendConfig(const PluginConfig &cfg) { std::string data = "[registration]\n"; data += std::string("needPassword=") + (cfg.m_needPassword ? "1" : "0") + "\n"; @@ -496,30 +497,11 @@ void NetworkPlugin::handleChatStatePayload(const std::string &data, int type) { } } -void NetworkPlugin::handleDataRead(std::string &data) { - m_data.insert(m_data.end(), data.begin(), data.end()); - - while (m_data.size() != 0) { - unsigned int expected_size; - - if (m_data.size() >= 4) { - expected_size = *((unsigned int*) &m_data[0]); - expected_size = ntohl(expected_size); - if (m_data.size() - 4 < expected_size) - return; - } - else { - return; - } - +void NetworkPlugin::handleDataRead(std::istream& data) { + while (true) { pbnetwork::WrapperMessage wrapper; - if (wrapper.ParseFromArray(&m_data[4], expected_size) == false) { - m_data.erase(m_data.begin(), m_data.begin() + 4 + expected_size); - return; - } - m_data.erase(m_data.begin(), m_data.begin() + 4 + expected_size); - - switch(wrapper.type()) { + if (wrapper.ParseFromIstream(&data)) { + switch (wrapper.type()) { case pbnetwork::WrapperMessage_Type_TYPE_LOGIN: handleLoginPayload(wrapper.payload()); break; @@ -573,6 +555,10 @@ void NetworkPlugin::handleDataRead(std::string &data) { break; default: return; + } + } else { + LOG4CXX_ERROR(logger, "Error reading from server"); + break; } } } @@ -583,6 +569,11 @@ void NetworkPlugin::send(const std::string &data) { sendData(std::string(header, 4) + data); } +void NetworkPlugin::sendData(const std::string& data) { + stream << data; + stream.flush(); +} + void NetworkPlugin::checkPing() { if (m_pingReceived == false) { LOG4CXX_ERROR(logger, "PING request not received - exiting...");