From 62655779e55c96f7d1b2f291b015a9ba981b4a50 Mon Sep 17 00:00:00 2001 From: csoler Date: Thu, 4 Nov 2021 20:52:38 +0100 Subject: [PATCH] use recv(...,MSG_DONTWAIT), since read() may return multiple times the same data apparently --- libretroshare/src/friend_server/fsbio.cc | 2 +- libretroshare/src/friend_server/fsclient.cc | 5 ++++- retroshare-friendserver/src/network.cc | 18 ++++++++++++++---- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/libretroshare/src/friend_server/fsbio.cc b/libretroshare/src/friend_server/fsbio.cc index 6668a59c9..e5be9389b 100644 --- a/libretroshare/src/friend_server/fsbio.cc +++ b/libretroshare/src/friend_server/fsbio.cc @@ -39,7 +39,7 @@ int FsBioInterface::tick() char inBuffer[1025]; memset(inBuffer,0,1025); - int readbytes = read(mCLintConnt, inBuffer, sizeof(inBuffer)); + ssize_t readbytes = recv(mCLintConnt, inBuffer, sizeof(inBuffer),MSG_DONTWAIT); if(readbytes == 0) { diff --git a/libretroshare/src/friend_server/fsclient.cc b/libretroshare/src/friend_server/fsclient.cc index e76e75d71..e9bc3d325 100644 --- a/libretroshare/src/friend_server/fsclient.cc +++ b/libretroshare/src/friend_server/fsclient.cc @@ -157,6 +157,8 @@ bool FsClient::sendItem(const std::string& address,uint16_t port,RsItem *item,st std::cerr << "Got a response item: " << std::endl; std::cerr << *item << std::endl; + should_close = true; // always close the socket after one packet + if(dynamic_cast(item) != nullptr) { RsDbg() << "End of transmission. " ; @@ -171,7 +173,8 @@ bool FsClient::sendItem(const std::string& address,uint16_t port,RsItem *item,st break; } } - std::this_thread::sleep_for(std::chrono::milliseconds(200)); + else + std::this_thread::sleep_for(std::chrono::milliseconds(200)); } if(should_close) diff --git a/retroshare-friendserver/src/network.cc b/retroshare-friendserver/src/network.cc index f02cb6395..aa449e646 100644 --- a/retroshare-friendserver/src/network.cc +++ b/retroshare-friendserver/src/network.cc @@ -93,11 +93,19 @@ void FsNetworkInterface::threadTick() // 2 - tick all streamers + std::list to_close; + RS_STACK_MUTEX(mFsNiMtx); for(auto& it:mConnections) - it.second.pqi_thread->tick(); + if(it.second.bio->isactive()) + it.second.pqi_thread->tick(); + else + to_close.push_back(it.first); - rstime::rs_usleep(1000*200); + for(const auto& pid:to_close) + closeConnection(pid); + + std::this_thread::sleep_for(std::chrono::milliseconds(200)); } static RsPeerId makePeerId(int t) @@ -216,17 +224,19 @@ void FsNetworkInterface::closeConnection(const RsPeerId& peer_id) { RS_STACK_MUTEX(mFsNiMtx); + RsDbg() << "Closing connection to virtual peer " << peer_id ; + const auto& it = mConnections.find(peer_id); if(it == mConnections.end()) { - RsErr() << "Cannot close connection to peer " << peer_id << ": no pending sockets available." ; + RsErr() << " Cannot close connection to peer " << peer_id << ": no pending sockets available." ; return; } if(!it->second.incoming_items.empty()) { - RsErr() << "Trying to close an incoming connection with incoming items still pending! The items will be lost." << std::endl; + RsErr() << " Trying to close an incoming connection with incoming items still pending! The items will be lost." << std::endl; for(auto& item:it->second.incoming_items) delete item;