1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-21 17:17:06 +02:00

Some improvements for enet

This commit is contained in:
nordsoft 2023-01-19 19:20:46 +04:00
parent 5713651e09
commit 95b0e11da4
3 changed files with 27 additions and 9 deletions

View File

@ -610,7 +610,7 @@ void CServerHandler::startGameplay(CGameState * gameState)
state = EClientState::GAMEPLAY;
//store settings to continue game
if(!isServerLocal() && isGuest())
if(false && !isServerLocal() && isGuest())
{
Settings saveSession = settings.write["server"]["reconnect"];
saveSession->Bool() = true;

View File

@ -13,14 +13,13 @@
VCMI_LIB_NAMESPACE_BEGIN
EnetConnection::EnetConnection(ENetPeer * _peer):
peer(_peer)
peer(_peer), connected(false), channel(1)
{
connected = false;
}
EnetConnection::EnetConnection(ENetHost * client, const std::string & host, ui16 port)
EnetConnection::EnetConnection(ENetHost * client, const std::string & host, ui16 port):
connected(false), channel(0)
{
connected = false;
ENetAddress address;
enet_address_set_host(&address, host.c_str());
address.port = port;
@ -53,7 +52,7 @@ void EnetConnection::close()
{
std::lock_guard<std::mutex> guard(mutexWrite);
connected = false;
enet_peer_disconnect(peer, 0);
enet_peer_disconnect_later(peer, 0);
}
void EnetConnection::kill()
@ -81,10 +80,19 @@ void EnetConnection::write(const void * data, unsigned size)
return;
std::lock_guard<std::mutex> guard(mutexWrite);
ENetPacket * packet = enet_packet_create(data, size, ENET_PACKET_FLAG_RELIABLE);
ENetPacket * packet = enet_packet_create(data, size, mode);
enet_peer_send(peer, channel, packet);
}
void EnetConnection::setMode(bool reliable)
{
std::lock_guard<std::mutex> guard(mutexWrite);
if(reliable)
mode = ENET_PACKET_FLAG_RELIABLE;
else
mode = ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT;
}
void EnetConnection::read(void * data, unsigned size)
{
if(!size)
@ -102,8 +110,8 @@ void EnetConnection::read(void * data, unsigned size)
auto * packet = packets.front();
packets.pop_front();
if(packet->dataLength > 0)
memcpy(data, packet->data, packet->dataLength);
assert(packet->dataLength > 0);
memcpy(data, packet->data, packet->dataLength);
assert(size == packet->dataLength);
enet_packet_destroy(packet);
@ -193,6 +201,12 @@ void EnetService::stop()
threadPolling.reset();
}
void EnetService::setMode(bool reliable)
{
for(auto c : active)
c->setMode(reliable);
}
bool EnetService::valid() const
{
return flagValid;

View File

@ -19,6 +19,7 @@ private:
std::list<ENetPacket*> packets;
int channel = 0;
enet_uint32 mode = ENET_PACKET_FLAG_RELIABLE;
ENetPeer * peer = nullptr;
std::atomic<bool> connected;
@ -34,6 +35,7 @@ public:
const ENetPeer * getPeer() const;
void dispatch(ENetPacket * packet);
void setMode(bool reliable);
void write(const void * data, unsigned size);
void read(void * data, unsigned size);
@ -77,6 +79,8 @@ public:
bool valid() const;
void setMode(bool reliable);
virtual void handleDisconnection(std::shared_ptr<EnetConnection>) = 0;
virtual void handleConnection(std::shared_ptr<EnetConnection>) = 0;
};