From e058b3a35fb02c1a91df37bf171b8840c2e86981 Mon Sep 17 00:00:00 2001 From: csoler Date: Sun, 31 Oct 2021 18:00:43 +0100 Subject: [PATCH] fixed memory error --- libretroshare/src/friend_server/fsclient.cc | 17 ++++-- libretroshare/src/friend_server/fsmanager.cc | 4 +- retroshare-friendserver/src/friendserver.cc | 57 +++++++++++++++++++ retroshare-friendserver/src/friendserver.h | 17 ++++++ retroshare-friendserver/src/network.cc | 10 ++++ retroshare-friendserver/src/network.h | 1 + .../src/retroshare-friendserver.cc | 12 ---- 7 files changed, 98 insertions(+), 20 deletions(-) diff --git a/libretroshare/src/friend_server/fsclient.cc b/libretroshare/src/friend_server/fsclient.cc index 9b2459b7c..b5f385d65 100644 --- a/libretroshare/src/friend_server/fsclient.cc +++ b/libretroshare/src/friend_server/fsclient.cc @@ -109,8 +109,8 @@ bool FsClient::sendItem(const std::string& address,uint16_t port,RsItem *item,st } FsSerializer *fss = new FsSerializer; - RsSerialiser rss; - rss.addSerialType(fss); + RsSerialiser *rss = new RsSerialiser(); // deleted by ~pqistreamer() + rss->addSerialType(fss); FsSerializer().serialise(item,data,&size); write(CreateSocket,data,size); // shouldn't we use the pqistreamer in R/W mode instead? @@ -119,8 +119,9 @@ bool FsClient::sendItem(const std::string& address,uint16_t port,RsItem *item,st // Now attempt to read and deserialize anything that comes back from that connexion until it gets closed by the server. - FsBioInterface bio(CreateSocket); - pqithreadstreamer p(this,&rss,RsPeerId(),&bio,BIN_FLAGS_READABLE | BIN_FLAGS_NO_DELETE); + FsBioInterface *bio = new FsBioInterface(CreateSocket); // deleted by ~pqistreamer() + + pqithreadstreamer p(this,rss,RsPeerId(),bio,BIN_FLAGS_READABLE | BIN_FLAGS_NO_DELETE | BIN_FLAGS_NO_CLOSE); p.start(); uint32_t ss; @@ -139,13 +140,17 @@ bool FsClient::sendItem(const std::string& address,uint16_t port,RsItem *item,st std::cerr << *item << std::endl; } - if(!bio.isactive()) // socket has probably closed + if(!bio->isactive()) // socket has probably closed { - RsDbg() << "(client side) Socket has been closed by server. Killing pqistreamer and closing socket." ; + RsDbg() << "(client side) Socket has been closed by server."; + RsDbg() << " Stopping/killing pqistreamer" ; p.fullstop(); + RsDbg() << " Closing socket." ; close(CreateSocket); CreateSocket=0; + + RsDbg() << " Exiting loop." ; break; } diff --git a/libretroshare/src/friend_server/fsmanager.cc b/libretroshare/src/friend_server/fsmanager.cc index 876ae8170..199bb99d7 100644 --- a/libretroshare/src/friend_server/fsmanager.cc +++ b/libretroshare/src/friend_server/fsmanager.cc @@ -4,9 +4,9 @@ RsFriendServer *rsFriendServer = nullptr; -static const rstime_t MIN_DELAY_BETWEEN_FS_REQUESTS = 30; +static const rstime_t MIN_DELAY_BETWEEN_FS_REQUESTS = 30; static const rstime_t MAX_DELAY_BETWEEN_FS_REQUESTS = 3600; -static const uint32_t DEFAULT_FRIENDS_TO_REQUEST = 10; +static const uint32_t DEFAULT_FRIENDS_TO_REQUEST = 10; static const std::string DEFAULT_FRIEND_SERVER_ADDRESS = "127.0.0.1"; static const uint16_t DEFAULT_FRIEND_SERVER_PORT = 2017; diff --git a/retroshare-friendserver/src/friendserver.cc b/retroshare-friendserver/src/friendserver.cc index 32d3ea8a2..7464129f8 100644 --- a/retroshare-friendserver/src/friendserver.cc +++ b/retroshare-friendserver/src/friendserver.cc @@ -3,6 +3,10 @@ #include "friendserver.h" #include "friend_server/fsitem.h" +static const rstime_t MAXIMUM_PEER_INACTIVE_DELAY = 600; +static const rstime_t DELAY_BETWEEN_TWO_AUTOWASH = 60; +static const rstime_t DELAY_BETWEEN_TWO_DEBUG_PRINT = 10; + void FriendServer::threadTick() { // Listen to the network interface, capture incoming data etc. @@ -33,6 +37,23 @@ void FriendServer::threadTick() } delete item; } + + static rstime_t last_autowash_TS = time(nullptr); + rstime_t now = time(nullptr); + + if(last_autowash_TS + DELAY_BETWEEN_TWO_AUTOWASH < now) + { + last_autowash_TS = now; + autoWash(); + } + + static rstime_t last_debugprint_TS = time(nullptr); + + if(last_debugprint_TS + DELAY_BETWEEN_TWO_DEBUG_PRINT < now) + { + last_debugprint_TS = now; + debugPrint(); + } } void FriendServer::handleClientPublish(const RsFriendServerClientPublishItem *item) @@ -67,3 +88,39 @@ void FriendServer::run() while(!shouldStop()) { threadTick() ; } } +void FriendServer::autoWash() +{ + rstime_t now = time(nullptr); + + for(std::map::iterator it(mCurrentClientPeers.begin());it!=mCurrentClientPeers.end();) + { + if(it->second.last_connection_TS + MAXIMUM_PEER_INACTIVE_DELAY < now) + { + RsDbg() << "Removing client peer " << it->first << " because it's inactive for more than " << MAXIMUM_PEER_INACTIVE_DELAY << " seconds." ; + auto tmp = it; + ++tmp; + mCurrentClientPeers.erase(it); + it = tmp; + } + } +} + +void FriendServer::debugPrint() +{ + RsDbg() << "========== FriendServer statistics ============"; + RsDbg() << " Base directory: "<< mBaseDirectory; + RsDbg() << " Network interface: "; + RsDbg() << " Current peers: " << mCurrentClientPeers.size() ; + + rstime_t now = time(nullptr); + + for(auto& it:mCurrentClientPeers) + RsDbg() << " " << it.first << ": " << "last contact: " << now - it.second.last_connection_TS; + + RsDbg() << "==============================================="; + +} + + + + diff --git a/retroshare-friendserver/src/friendserver.h b/retroshare-friendserver/src/friendserver.h index 83e7eaaa7..51917cb5a 100644 --- a/retroshare-friendserver/src/friendserver.h +++ b/retroshare-friendserver/src/friendserver.h @@ -29,19 +29,36 @@ class RsFriendServerClientRemoveItem; class RsFriendServerClientPublishItem; +struct PeerInfo +{ + std::string short_certificate; + rstime_t last_connection_TS; +}; + class FriendServer : public RsTickingThread { public: FriendServer(const std::string& base_directory); private: + // overloads RsTickingThread + virtual void threadTick() override; virtual void run() override; + // Own algorithmics + void handleClientRemove(const RsFriendServerClientRemoveItem *item); void handleClientPublish(const RsFriendServerClientPublishItem *item); + void autoWash(); + void debugPrint(); + + // Local members + FsNetworkInterface *mni; std::string mBaseDirectory; + + std::map mCurrentClientPeers; }; diff --git a/retroshare-friendserver/src/network.cc b/retroshare-friendserver/src/network.cc index 18ef4c7fa..243981717 100644 --- a/retroshare-friendserver/src/network.cc +++ b/retroshare-friendserver/src/network.cc @@ -246,6 +246,16 @@ void FsNetworkInterface::closeConnection(const RsPeerId& peer_id) mConnections.erase(it); } +void FsNetworkInterface::debugPrint() +{ + RsDbg() << " " << mClintListn ; // listening socket + RsDbg() << " Connections: " << mConnections.size() ; + + for(auto& it:mConnections) + RsDbg() << " " << it.first << ": from \"" << sockaddr_storage_tostring(*(sockaddr_storage*)(&it.second.client_address)) << "\", socket=" << it.second.socket ; + + std::map mConnections; +} diff --git a/retroshare-friendserver/src/network.h b/retroshare-friendserver/src/network.h index 5729b515c..b92c60a2e 100644 --- a/retroshare-friendserver/src/network.h +++ b/retroshare-friendserver/src/network.h @@ -49,6 +49,7 @@ public: // basic functionality void closeConnection(const RsPeerId& peer_id); + void debugPrint(); // Implements PQInterface diff --git a/retroshare-friendserver/src/retroshare-friendserver.cc b/retroshare-friendserver/src/retroshare-friendserver.cc index 4c5bfc61c..585d400a9 100644 --- a/retroshare-friendserver/src/retroshare-friendserver.cc +++ b/retroshare-friendserver/src/retroshare-friendserver.cc @@ -58,20 +58,8 @@ int main(int argc, char* argv[]) fs.start(); while(fs.isRunning()) - { std::this_thread::sleep_for(std::chrono::seconds(2)); -// // send one request for testing to see what happens -// -// RsFriendServerClientPublishItem *item = new RsFriendServerClientPublishItem(); -// item->long_invite = std::string("[Long Invite]"); -// item->n_requested_friends = 10; -// -// std::cerr << "Sending fake request item for testing..." << std::endl; -// FsClient(std::string("127.0.0.1")).sendItem(item); -// -// std::this_thread::sleep_for(std::chrono::seconds(4)); - } return 0; }