Skip to content

Commit e968576

Browse files
author
Alexandre jublot
committed
fix: client was not waiting for packet to be sent/received to destroy itself
1 parent a9b23d6 commit e968576

4 files changed

Lines changed: 28 additions & 9 deletions

File tree

src/include/tcp/ClientImpl.hpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,17 @@ namespace polymorph::network::tcp
8787
/**
8888
* @property Atomic boolean to know if the ClientImpl is already sending a packet
8989
*/
90-
std::atomic<bool> _writeInProgress;
90+
std::atomic<bool> _writeInProgress = false;
91+
92+
/**
93+
* @property Atomic boolean to know if the ClientImpl is receiving a packet
94+
*/
95+
std::atomic<bool> _receiveInProgress = false;
96+
97+
/**
98+
* @property Atomic boolean to know if the ClientImpl is already sending a packet
99+
*/
100+
std::atomic<bool> _stopping = false;
91101

92102
/**
93103
* @property Mutex to lock the send queue and ensure thread safety

src/src/tcp/ClientImpl.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,15 @@ void polymorph::network::tcp::ClientImpl::_doSend()
109109

110110
void polymorph::network::tcp::ClientImpl::_doReceive()
111111
{
112+
_receiveInProgress = false;
112113
_socket.async_receive(asio::buffer(_internalBuffer), [this](const asio::error_code &error, std::size_t bytesReceived) {
113-
if (error == asio::error::operation_aborted || error == asio::error::eof)
114+
if (error == asio::error::operation_aborted || error == asio::error::eof || _stopping)
114115
return;
115116
if (error) {
116117
std::cerr << "Error while receiving packet: " << error.message() << std::endl;
117118
return;
118119
}
120+
_receiveInProgress = true;
119121
_receiveBuffer.insert(_receiveBuffer.end(), _internalBuffer.begin(), _internalBuffer.begin() + bytesReceived);
120122
while (_receiveBuffer.size() > sizeof(PacketHeader) ) {
121123
auto header = SerializerTrait<PacketHeader>::deserialize(_receiveBuffer);
@@ -144,8 +146,8 @@ polymorph::network::tcp::ClientImpl::~ClientImpl()
144146

145147
if (!_context.stopped())
146148
_context.stop();
147-
148-
while (_writeInProgress) {
149+
_stopping = true;
150+
while (_writeInProgress || _receiveInProgress) {
149151
std::this_thread::sleep_for(std::chrono::milliseconds(1));
150152
}
151153
}

src/src/udp/ClientImpl.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ polymorph::network::udp::ClientImpl::~ClientImpl()
5353
std::promise<void> promise;
5454
auto future = promise.get_future();
5555

56-
while (isWriteInProgress() || isReceiveInProgress()) {
57-
std::this_thread::sleep_for(std::chrono::milliseconds(1));
58-
}
5956
Client::send<DisconnectionDto>(polymorph::network::DisconnectionDto::opId, dto, [&promise](const PacketHeader &header, const DisconnectionDto &payload) {
6057
promise.set_value();
6158
});
6259
future.wait_for(std::chrono::seconds(1));
6360
if (!_context.stopped())
6461
_context.stop();
62+
while (isWriteInProgress() || isReceiveInProgress()) {
63+
std::this_thread::sleep_for(std::chrono::milliseconds(1));
64+
}
6565
}
6666

6767
void polymorph::network::udp::ClientImpl::_ackReceived(const asio::ip::udp::endpoint &to,

tests/main.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
#include <functional>
33
#include <any>
44
#include <map>
5+
#include <asio/ip/address_v4.hpp>
6+
57
#include "polymorph/network/udp/Client.hpp"
68
#include "polymorph/network/udp/Server.hpp"
7-
#include "polymorph/network/udp/AConnector.hpp"
89
#include "e2e-tests/utils.hpp"
910

10-
11+
/*
1112
int main(){
1213
//checks
1314
std::uint16_t input_data = 42;
@@ -54,4 +55,10 @@ int main(){
5455
server.send(2, input_data);
5556
PNL_WAIT(PNL_TIME_OUT)
5657
return 0;
58+
}
59+
*/
60+
61+
int main() {
62+
asio::ip::make_address_v4("127.0.0.1");
63+
return 0;
5764
}

0 commit comments

Comments
 (0)