1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-07-15 01:24:45 +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) void CServerHandler::handleConnection(std::shared_ptr<EnetConnection> _c)
{ {
c = std::make_shared<CConnection>(_c, NAME, uuid); if(!c)
c->handler = std::make_shared<boost::thread>(&CServerHandler::threadHandleConnection, this); {
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) 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) 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 only asio exceptions
catch(const boost::system::system_error & e) catch(const std::logic_error & e)
{ {
if(state == EClientState::DISCONNECTING) if(state == EClientState::DISCONNECTING)
{ {

View File

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

View File

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

View File

@ -13,7 +13,7 @@
using namespace Load; 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); setupSteps(100);
} }
@ -45,8 +45,8 @@ void Progress::reset(int s)
void Progress::finish() void Progress::finish()
{ {
_progress = _target = std::numeric_limits<Type>::max(); _progress = _target = std::numeric_limits<Type>::max();
_step = std::numeric_limits<Type>::min(); _step = std::numeric_limits<int>::min();
_maxSteps = std::numeric_limits<Type>::min(); _maxSteps = std::numeric_limits<int>::min();
} }
void Progress::setupSteps(int s) void Progress::setupSteps(int s)
@ -59,10 +59,10 @@ void Progress::setupStepsTill(int s, Type p)
if(finished()) if(finished())
return; return;
if(_step > std::numeric_limits<Type>::min()) if(_step > std::numeric_limits<int>::min())
_progress = get(); _progress = get();
_step = std::numeric_limits<Type>::min(); _step = std::numeric_limits<int>::min();
_maxSteps = s; _maxSteps = s;
_target = p; _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) int CConnection::write(const void * data, unsigned size)
{ {
if(connected) if(!enetConnection->isOpen())
enetConnection->write(data, size); throw std::logic_error("Write in closed connection");
enetConnection->write(data, size);
return size; return size;
} }
int CConnection::read(void * data, unsigned size) int CConnection::read(void * data, unsigned size)
{ {
if(connected) if(!enetConnection->isOpen())
enetConnection->read(data, size); throw std::logic_error("Read from closed connection");
enetConnection->read(data, size);
return size; return size;
} }

View File

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