use recv(...,MSG_DONTWAIT), since read() may return multiple times the same data apparently

This commit is contained in:
csoler 2021-11-04 20:52:38 +01:00
parent 01da2fbe9e
commit 62655779e5
3 changed files with 19 additions and 6 deletions

View file

@ -39,7 +39,7 @@ int FsBioInterface::tick()
char inBuffer[1025]; char inBuffer[1025];
memset(inBuffer,0,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) if(readbytes == 0)
{ {

View file

@ -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 << "Got a response item: " << std::endl;
std::cerr << *item << std::endl; std::cerr << *item << std::endl;
should_close = true; // always close the socket after one packet
if(dynamic_cast<RsFriendServerStatusItem*>(item) != nullptr) if(dynamic_cast<RsFriendServerStatusItem*>(item) != nullptr)
{ {
RsDbg() << "End of transmission. " ; RsDbg() << "End of transmission. " ;
@ -171,7 +173,8 @@ bool FsClient::sendItem(const std::string& address,uint16_t port,RsItem *item,st
break; break;
} }
} }
std::this_thread::sleep_for(std::chrono::milliseconds(200)); else
std::this_thread::sleep_for(std::chrono::milliseconds(200));
} }
if(should_close) if(should_close)

View file

@ -93,11 +93,19 @@ void FsNetworkInterface::threadTick()
// 2 - tick all streamers // 2 - tick all streamers
std::list<RsPeerId> to_close;
RS_STACK_MUTEX(mFsNiMtx); RS_STACK_MUTEX(mFsNiMtx);
for(auto& it:mConnections) 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) static RsPeerId makePeerId(int t)
@ -216,17 +224,19 @@ void FsNetworkInterface::closeConnection(const RsPeerId& peer_id)
{ {
RS_STACK_MUTEX(mFsNiMtx); RS_STACK_MUTEX(mFsNiMtx);
RsDbg() << "Closing connection to virtual peer " << peer_id ;
const auto& it = mConnections.find(peer_id); const auto& it = mConnections.find(peer_id);
if(it == mConnections.end()) 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; return;
} }
if(!it->second.incoming_items.empty()) 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) for(auto& item:it->second.incoming_items)
delete item; delete item;