From 25ddbe099df7d03bd6b3a9599279d92e2e02bfd2 Mon Sep 17 00:00:00 2001 From: csoler Date: Sun, 7 Nov 2021 15:16:24 +0100 Subject: [PATCH] fixed two-ways communication between client and server --- libretroshare/src/friend_server/fsclient.cc | 45 +++++++-------------- retroshare-friendserver/src/friendserver.cc | 17 ++------ retroshare-friendserver/src/network.cc | 8 ++-- 3 files changed, 22 insertions(+), 48 deletions(-) diff --git a/libretroshare/src/friend_server/fsclient.cc b/libretroshare/src/friend_server/fsclient.cc index 860b7a0c3..8e809337a 100644 --- a/libretroshare/src/friend_server/fsclient.cc +++ b/libretroshare/src/friend_server/fsclient.cc @@ -127,12 +127,8 @@ bool FsClient::sendItem(const std::string& address,uint16_t port,RsItem *item,st 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? - - RsDbg() << "Item sent. Waiting for response..." ; - - // Now attempt to read and deserialize anything that comes back from that connexion until it gets closed by the server. +// FsSerializer().serialise(item,data,&size); +// write(CreateSocket,data,size); // shouldn't we use the pqistreamer in R/W mode instead? FsBioInterface *bio = new FsBioInterface(CreateSocket); // deleted by ~pqistreamer() @@ -141,7 +137,10 @@ bool FsClient::sendItem(const std::string& address,uint16_t port,RsItem *item,st uint32_t ss; p.SendItem(item,ss); - bool should_close = false; + + RsDbg() << "Item sent. Waiting for response..." ; + + // Now attempt to read and deserialize anything that comes back from that connexion until it gets closed by the server. while(true) { @@ -157,37 +156,21 @@ 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. " ; - should_close = true; - break; - } - - if(!bio->isactive()) // socket has probably closed - { - RsDbg() << "(client side) Socket has been closed by server."; - should_close =true; - break; - } + RsDbg() << "End of transmission. " ; + break; } else std::this_thread::sleep_for(std::chrono::milliseconds(200)); } - if(should_close) - { - RsDbg() << " Stopping/killing pqistreamer" ; - p.fullstop(); + RsDbg() << " Stopping/killing pqistreamer" ; + p.fullstop(); - RsDbg() << " Closing socket." ; - close(CreateSocket); - CreateSocket=0; + RsDbg() << " Closing socket." ; + close(CreateSocket); + CreateSocket=0; - RsDbg() << " Exiting loop." ; - } + RsDbg() << " Exiting loop." ; return true; } diff --git a/retroshare-friendserver/src/friendserver.cc b/retroshare-friendserver/src/friendserver.cc index ab2ec5c1d..be445540d 100644 --- a/retroshare-friendserver/src/friendserver.cc +++ b/retroshare-friendserver/src/friendserver.cc @@ -64,6 +64,8 @@ void FriendServer::threadTick() void FriendServer::handleClientPublish(const RsFriendServerClientPublishItem *item) { + // We always respond with exactly one item, be it an error item or a list of friends to connect to. + try { RsDbg() << "Received a client publish item from " << item->PeerId() << ":"; @@ -107,19 +109,6 @@ void FriendServer::handleClientPublish(const RsFriendServerClientPublishItem *it mni->SendItem(status_item); return; } - - // Close client connection from server side, to tell the client that nothing more is coming. - - RsDbg() << "Sending end-of-stream item to " << item->PeerId() ; - - RsFriendServerStatusItem *status_item = new RsFriendServerStatusItem; - status_item->status = RsFriendServerStatusItem::END_OF_TRANSMISSION; - status_item->PeerId(item->PeerId()); - - mni->SendItem(status_item); - - RsDbg() << "Closing client connection." ; - mni->closeConnection(item->PeerId()); } std::map FriendServer::computeListOfFriendInvites(uint32_t nb_reqs_invites, const RsPeerId &pid, const RsPgpFingerprint &fpr) @@ -332,7 +321,7 @@ void FriendServer::autoWash() std::list to_remove; - for(std::map::iterator it(mCurrentClientPeers.begin());it!=mCurrentClientPeers.end();) + for(std::map::iterator it(mCurrentClientPeers.begin());it!=mCurrentClientPeers.end();++it) 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." ; diff --git a/retroshare-friendserver/src/network.cc b/retroshare-friendserver/src/network.cc index aea66cfa7..ef69f8cb1 100644 --- a/retroshare-friendserver/src/network.cc +++ b/retroshare-friendserver/src/network.cc @@ -240,20 +240,22 @@ void FsNetworkInterface::locked_closeConnection(const RsPeerId& peer_id) 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) + { + RsErr() << *item; delete item; + } it->second.incoming_items.clear(); } // Close the socket and delete everything. + close(it->second.socket); it->second.pqi_thread->fullstop(); it->second.bio->close(); - close(it->second.socket); - delete it->second.pqi_thread; mConnections.erase(it);