1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-30 04:30:42 +02:00

Some code changes

This commit is contained in:
nordsoft 2023-01-18 20:09:19 +04:00
parent 576fd23531
commit 091cf9cd2f
6 changed files with 34 additions and 23 deletions

View File

@ -134,13 +134,17 @@ CServerHandler::~CServerHandler()
void CServerHandler::handleConnection(std::shared_ptr<EnetConnection> _c)
{
c = std::make_shared<CConnection>(_c, NAME, uuid);
c->handler = std::make_shared<boost::thread>(&CServerHandler::threadHandleConnection, this);
if(!c)
{
c = std::make_shared<CConnection>(_c, NAME, uuid);
c->handler = std::make_shared<boost::thread>(&CServerHandler::threadHandleConnection, this);
}
}
void CServerHandler::handleDisconnection(std::shared_ptr<EnetConnection> _c)
{
state = EClientState::DISCONNECTING;
if(c && c->getEnetConnection() == _c)
state = EClientState::DISCONNECTING;
}
void CServerHandler::resetStateForLobby(const StartInfo::EMode mode, const std::vector<std::string> * names)
@ -818,7 +822,7 @@ void CServerHandler::threadHandleConnection()
}
}
//catch only asio exceptions
catch(const boost::system::system_error & e)
catch(const std::logic_error & e)
{
if(state == EClientState::DISCONNECTING)
{

View File

@ -32,7 +32,8 @@ EnetConnection::EnetConnection(ENetHost * client, const std::string & host, ui16
EnetConnection::~EnetConnection()
{
close();
if(isOpen())
close();
kill();
}
@ -178,8 +179,6 @@ void EnetService::monitor()
handleConnection(connecting.front());
connecting.pop_front();
}
if(service)
enet_host_flush(service);
std::this_thread::sleep_for(std::chrono::milliseconds(MONITOR_INTERVAL));
}
}
@ -200,9 +199,9 @@ bool EnetService::valid() const
void EnetService::poll()
{
setThreadName("EnetService::poll");
ENetEvent event;
while(doPolling)
{
ENetEvent event;
if(enet_host_service(service, &event, POLL_INTERVAL) > 0)
{
switch(event.type)

View File

@ -19,9 +19,7 @@ private:
std::list<ENetPacket*> packets;
int channel = 0;
ENetPeer * peer = nullptr;
std::atomic<bool> connected;
std::mutex mutexRead, mutexWrite;
std::atomic<bool> connected, locked;
public:
EnetConnection(ENetPeer * peer);
@ -32,11 +30,14 @@ public:
void open();
void close();
void kill();
const ENetPeer * getPeer() const;
void dispatch(ENetPacket * packet);
void write(const void * data, unsigned size);
void read(void * data, unsigned size);
std::mutex mutexRead, mutexWrite;
};
class DLL_LINKAGE EnetService

View File

@ -13,7 +13,7 @@
using namespace Load;
Progress::Progress(): _progress(std::numeric_limits<Type>::min())
Progress::Progress(): _progress(std::numeric_limits<Type>::min()), _step(std::numeric_limits<int>::min()), _maxSteps(std::numeric_limits<int>::min())
{
setupSteps(100);
}
@ -45,8 +45,8 @@ void Progress::reset(int s)
void Progress::finish()
{
_progress = _target = std::numeric_limits<Type>::max();
_step = std::numeric_limits<Type>::min();
_maxSteps = std::numeric_limits<Type>::min();
_step = std::numeric_limits<int>::min();
_maxSteps = std::numeric_limits<int>::min();
}
void Progress::setupSteps(int s)
@ -59,10 +59,10 @@ void Progress::setupStepsTill(int s, Type p)
if(finished())
return;
if(_step > std::numeric_limits<Type>::min())
if(_step > std::numeric_limits<int>::min())
_progress = get();
_step = std::numeric_limits<Type>::min();
_step = std::numeric_limits<int>::min();
_maxSteps = s;
_target = p;

View File

@ -59,15 +59,19 @@ CConnection::CConnection(std::shared_ptr<EnetConnection> _c, std::string Name, s
int CConnection::write(const void * data, unsigned size)
{
if(connected)
enetConnection->write(data, size);
if(!enetConnection->isOpen())
throw std::logic_error("Write in closed connection");
enetConnection->write(data, size);
return size;
}
int CConnection::read(void * data, unsigned size)
{
if(connected)
enetConnection->read(data, size);
if(!enetConnection->isOpen())
throw std::logic_error("Read from closed connection");
enetConnection->read(data, size);
return size;
}

View File

@ -162,8 +162,11 @@ void CVCMIServer::handleDisconnection(std::shared_ptr<EnetConnection> _c)
}
}
assert(c);
c->close();
connections -= c;
{
boost::unique_lock<boost::recursive_mutex> myLock(mx);
c->close();
connections -= c;
}
if(connections.empty() || hostClient == c)
{
state = EServerState::SHUTDOWN;
@ -384,7 +387,7 @@ void CVCMIServer::threadHandleClient(std::shared_ptr<CConnection> c)
{
pack = c->retrievePack();
}
catch(boost::system::system_error & e)
catch(const std::logic_error & e)
{
logNetwork->error("Network error receiving a pack. Connection %s dies. What happened: %s", c->toString(), e.what());
hangingConnections.insert(c);