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:
@ -134,12 +134,16 @@ CServerHandler::~CServerHandler()
|
|||||||
|
|
||||||
void CServerHandler::handleConnection(std::shared_ptr<EnetConnection> _c)
|
void CServerHandler::handleConnection(std::shared_ptr<EnetConnection> _c)
|
||||||
{
|
{
|
||||||
|
if(!c)
|
||||||
|
{
|
||||||
c = std::make_shared<CConnection>(_c, NAME, uuid);
|
c = std::make_shared<CConnection>(_c, NAME, uuid);
|
||||||
c->handler = std::make_shared<boost::thread>(&CServerHandler::threadHandleConnection, this);
|
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)
|
||||||
{
|
{
|
||||||
|
if(c && c->getEnetConnection() == _c)
|
||||||
state = EClientState::DISCONNECTING;
|
state = EClientState::DISCONNECTING;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -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)
|
||||||
{
|
{
|
||||||
|
@ -32,6 +32,7 @@ EnetConnection::EnetConnection(ENetHost * client, const std::string & host, ui16
|
|||||||
|
|
||||||
EnetConnection::~EnetConnection()
|
EnetConnection::~EnetConnection()
|
||||||
{
|
{
|
||||||
|
if(isOpen())
|
||||||
close();
|
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)
|
||||||
|
@ -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
|
||||||
|
@ -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;
|
||||||
|
@ -59,14 +59,18 @@ 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())
|
||||||
|
throw std::logic_error("Write in closed connection");
|
||||||
|
|
||||||
enetConnection->write(data, size);
|
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())
|
||||||
|
throw std::logic_error("Read from closed connection");
|
||||||
|
|
||||||
enetConnection->read(data, size);
|
enetConnection->read(data, size);
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
@ -162,8 +162,11 @@ void CVCMIServer::handleDisconnection(std::shared_ptr<EnetConnection> _c)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
assert(c);
|
assert(c);
|
||||||
|
{
|
||||||
|
boost::unique_lock<boost::recursive_mutex> myLock(mx);
|
||||||
c->close();
|
c->close();
|
||||||
connections -= c;
|
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);
|
||||||
|
Reference in New Issue
Block a user