fixed two-ways communication between client and server

This commit is contained in:
csoler 2021-11-07 15:16:24 +01:00
parent aff7912f41
commit 25ddbe099d
3 changed files with 22 additions and 48 deletions

View File

@ -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,28 +156,13 @@ 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<RsFriendServerStatusItem*>(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;
}
}
else
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}
if(should_close)
{
RsDbg() << " Stopping/killing pqistreamer" ;
p.fullstop();
@ -187,7 +171,6 @@ bool FsClient::sendItem(const std::string& address,uint16_t port,RsItem *item,st
CreateSocket=0;
RsDbg() << " Exiting loop." ;
}
return true;
}

View File

@ -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<std::string, bool> FriendServer::computeListOfFriendInvites(uint32_t nb_reqs_invites, const RsPeerId &pid, const RsPgpFingerprint &fpr)
@ -332,7 +321,7 @@ void FriendServer::autoWash()
std::list<RsPeerId> to_remove;
for(std::map<RsPeerId,PeerInfo>::iterator it(mCurrentClientPeers.begin());it!=mCurrentClientPeers.end();)
for(std::map<RsPeerId,PeerInfo>::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." ;

View File

@ -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);