Use a mutex to make only 1 enet_peer_send happening

In reality we need mutex to protect enet_host_service, but it leads
to seriously increased locked time
This commit is contained in:
Benau 2018-03-09 23:00:47 +08:00
parent d538dfc7e6
commit 5756fd5f48
7 changed files with 17 additions and 7 deletions

View File

@ -174,6 +174,7 @@ void Network::broadcastPacket(NetworkString *data, bool reliable)
ENetPacket* packet = enet_packet_create(data->getData(), data->size() + 1,
reliable ? ENET_PACKET_FLAG_RELIABLE
: ENET_PACKET_FLAG_UNSEQUENCED);
auto lock = acquireEnetLock();
enet_host_broadcast(m_host, 0, packet);
} // broadcastPacket

View File

@ -31,6 +31,7 @@
#define WIN32_LEAN_AND_MEAN
#include <enet/enet.h>
#include <mutex>
#include <stdio.h>
class BareNetworkString;
@ -47,6 +48,8 @@ private:
/** ENet host interfacing sockets. */
ENetHost* m_host;
std::mutex m_enet_mutex;
/** Where to log packets. If NULL for FILE* logging is disabled. */
static Synchronised<FILE*> m_log_file;
@ -69,6 +72,9 @@ public:
void broadcastPacket(NetworkString *data,
bool reliable = true);
// ------------------------------------------------------------------------
std::unique_lock<std::mutex> acquireEnetLock()
{ return std::unique_lock<std::mutex>(m_enet_mutex); }
// ------------------------------------------------------------------------
/** Returns a pointer to the ENet host object. */
ENetHost* getENetHost() { return m_host; }
}; // class Network

View File

@ -20,8 +20,6 @@
#include "network/event.hpp"
#include "network/protocol.hpp"
#include "network/stk_host.hpp"
#include "network/stk_peer.hpp"
#include "utils/log.hpp"
#include "utils/profiler.hpp"
#include "utils/time.hpp"

View File

@ -8,7 +8,7 @@
#include "modes/world.hpp"
#include "network/event.hpp"
#include "network/game_setup.hpp"
#include "network/protocol_manager.hpp"
#include "network/network_config.hpp"
#include "network/stk_host.hpp"
#include "network/stk_peer.hpp"

View File

@ -762,7 +762,8 @@ void STKHost::mainLoop()
Event* stk_event = NULL;
if (event.type == ENET_EVENT_TYPE_CONNECT)
{
auto stk_peer = std::make_shared<STKPeer>(event.peer);
auto stk_peer =
std::make_shared<STKPeer>(event.peer, m_network);
m_peers[event.peer] = stk_peer;
stk_event = new Event(&event, stk_peer);
TransportAddress addr(event.peer->address);

View File

@ -29,8 +29,8 @@
/** Constructor for an empty peer.
*/
STKPeer::STKPeer(ENetPeer *enet_peer)
: m_peer_address(enet_peer->address)
STKPeer::STKPeer(ENetPeer *enet_peer, Network* network)
: m_peer_address(enet_peer->address), m_network(network)
{
m_enet_peer = enet_peer;
m_is_authorised = false;
@ -72,6 +72,7 @@ void STKPeer::sendPacket(NetworkString *data, bool reliable)
data->getTotalSize(),
(reliable ? ENET_PACKET_FLAG_RELIABLE
: ENET_PACKET_FLAG_UNSEQUENCED));
auto lock = m_network->acquireEnetLock();
enet_peer_send(m_enet_peer, 0, packet);
} // sendPacket

View File

@ -31,6 +31,7 @@
#include <vector>
class Network;
class NetworkPlayerProfile;
class NetworkString;
class TransportAddress;
@ -59,8 +60,10 @@ protected:
TransportAddress m_peer_address;
Network* m_network;
public:
STKPeer(ENetPeer *enet_peer);
STKPeer(ENetPeer *enet_peer, Network* network);
virtual ~STKPeer();
virtual void sendPacket(NetworkString *data,