1
0
mirror of https://github.com/solemnwarning/directplay-lite synced 2024-12-30 16:45:37 +01:00

Maintain worker_pool only when DirectPlay8Peer is initialised.

This ensures any worker threads have stopped by the time we finish
returning to STATE_NEW when closing.
This commit is contained in:
Daniel Collins 2018-10-06 10:51:49 +01:00
parent 818adcbba1
commit 8db9aa277f
2 changed files with 13 additions and 11 deletions

View File

@ -45,12 +45,9 @@ DirectPlay8Peer::DirectPlay8Peer(std::atomic<unsigned int> *global_refcount):
udp_socket(-1),
listener_socket(-1),
discovery_socket(-1),
worker_pool(THREADS_PER_POOL, MAX_HANDLES_PER_POOL),
worker_pool(NULL),
udp_sq(udp_socket_event)
{
worker_pool.add_handle(udp_socket_event, [this]() { handle_udp_socket_event(); });
worker_pool.add_handle(other_socket_event, [this]() { handle_other_socket_event(); });
AddRef();
}
@ -60,9 +57,6 @@ DirectPlay8Peer::~DirectPlay8Peer()
{
Close(0);
}
worker_pool.remove_handle(other_socket_event);
worker_pool.remove_handle(udp_socket_event);
}
HRESULT DirectPlay8Peer::QueryInterface(REFIID riid, void **ppvObject)
@ -117,6 +111,11 @@ HRESULT DirectPlay8Peer::Initialize(PVOID CONST pvUserContext, CONST PFNDPNMESSA
message_handler = pfn;
message_handler_ctx = pvUserContext;
worker_pool = new HandleHandlingPool(THREADS_PER_POOL, MAX_HANDLES_PER_POOL);
worker_pool->add_handle(udp_socket_event, [this]() { handle_udp_socket_event(); });
worker_pool->add_handle(other_socket_event, [this]() { handle_other_socket_event(); });
WSADATA wd;
if(WSAStartup(MAKEWORD(2,2), &wd) != 0)
{
@ -1400,6 +1399,9 @@ HRESULT DirectPlay8Peer::Close(CONST DWORD dwFlags)
Sleep(50);
}
delete worker_pool;
worker_pool = NULL;
if(discovery_socket != -1)
{
closesocket(discovery_socket);
@ -2240,7 +2242,7 @@ void DirectPlay8Peer::peer_accept(std::unique_lock<std::mutex> &l)
peers.insert(std::make_pair(peer_id, peer));
worker_pool.add_handle(peer->event, [this, peer_id]() { io_peer_triggered(peer_id); });
worker_pool->add_handle(peer->event, [this, peer_id]() { io_peer_triggered(peer_id); });
}
bool DirectPlay8Peer::peer_connect(Peer::PeerState initial_state, uint32_t remote_ip, uint16_t remote_port, DPNID player_id)
@ -2279,7 +2281,7 @@ bool DirectPlay8Peer::peer_connect(Peer::PeerState initial_state, uint32_t remot
peers.insert(std::make_pair(peer_id, peer));
worker_pool.add_handle(peer->event, [this, peer_id]() { io_peer_triggered(peer_id); });
worker_pool->add_handle(peer->event, [this, peer_id]() { io_peer_triggered(peer_id); });
return true;
}
@ -2330,7 +2332,7 @@ void DirectPlay8Peer::peer_destroy(std::unique_lock<std::mutex> &l, unsigned int
player_to_peer_id.erase(peer->player_id);
}
worker_pool.remove_handle(peer->event);
worker_pool->remove_handle(peer->event);
closesocket(peer->sock);

View File

@ -60,7 +60,7 @@ class DirectPlay8Peer: public IDirectPlay8Peer
EventObject udp_socket_event;
EventObject other_socket_event;
HandleHandlingPool worker_pool;
HandleHandlingPool *worker_pool;
SendQueue udp_sq;