Skip to content

Commit 7ddb2bc

Browse files
committed
NetCommand: make sure to steal unique_ptr owner on onReceive() if packet is for the command
1 parent d1d3c03 commit 7ddb2bc

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

sources/network/C_netCommand.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,11 @@ NetCommandResults NetMTUCommand::onReceive(std::unique_ptr<ProtocolPacket>& pack
108108
case States::WAITING_RESPONSE:
109109
if (packet->retrieveHeaderId().value() == NET_INTERNAL_ID_MTU_ASK_RESPONSE)
110110
{
111+
std::unique_ptr packetOwned{std::move(packet)};
112+
111113
//Extract the target MTU
112114
uint16_t targetMTU;
113-
if (rules::RValid<uint16_t>({packet->packet(), &targetMTU}).end() || !packet->endReached())
115+
if (rules::RValid<uint16_t>({packetOwned->packet(), &targetMTU}).end() || !packetOwned->endReached())
114116
{
115117
//Invalid packet
116118
std::cout << "MTU: Invalid packet" << std::endl;
@@ -168,6 +170,8 @@ NetCommandResults NetMTUCommand::onReceive(std::unique_ptr<ProtocolPacket>& pack
168170
case States::WAITING:
169171
if (packet->retrieveHeaderId().value() == NET_INTERNAL_ID_MTU_TEST_RESPONSE)
170172
{
173+
packet.reset();
174+
171175
this->g_currentMTU = this->g_targetMTU;
172176

173177
if (this->g_tryCount == 0 || this->g_currentMTU == this->g_maximumMTU)
@@ -187,7 +191,6 @@ NetCommandResults NetMTUCommand::onReceive(std::unique_ptr<ProtocolPacket>& pack
187191

188192
this->_g_timeout = std::chrono::milliseconds::zero();
189193
this->g_state = States::DISCOVER;
190-
break;
191194
}
192195
break;
193196
default:
@@ -396,10 +399,12 @@ NetCommandResults NetConnectCommand::onReceive(std::unique_ptr<ProtocolPacket>&
396399
return NetCommandResults::WORKING;
397400
}
398401

402+
std::unique_ptr packetOwned{std::move(packet)};
403+
399404
std::cout << "receiving handshake response" << std::endl;
400405

401406
std::string handshake;
402-
if (rules::RValid<std::string>({packet->packet(), &handshake}).end() || !packet->endReached())
407+
if (rules::RValid<std::string>({packetOwned->packet(), &handshake}).end() || !packetOwned->endReached())
403408
{
404409
std::cout << "handshake failed" << std::endl;
405410
client.getStatus().setNetworkStatus(ClientStatus::NetworkStatus::DISCONNECTED);
@@ -430,10 +435,13 @@ NetCommandResults NetConnectCommand::onReceive(std::unique_ptr<ProtocolPacket>&
430435
return NetCommandResults::WORKING;
431436
}
432437

438+
std::unique_ptr packetOwned{std::move(packet)};
439+
433440
auto& info = client.getCryptInfo();
434441

435-
auto const readPos = packet->getReadPos();
436-
BIO_write(static_cast<BIO*>(info._rbio), packet->getData() + readPos, packet->getDataSize() - readPos);
442+
auto const readPos = packetOwned->getReadPos();
443+
BIO_write(static_cast<BIO*>(info._rbio), packetOwned->getData() + readPos,
444+
packetOwned->getDataSize() - readPos);
437445

438446
std::cout << "Crypt: received some data" << std::endl;
439447

sources/network/C_server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ void ServerNetFluxUdp::processClients()
215215

216216
//Handle commands
217217
if (this->g_commandsUpdateTick >= FGE_NET_CMD_UPDATE_TICK_MS)
218-
{
218+
{ //TODO: move it to the transmit thread ?
219219
auto& commands = it->second._commands;
220220
if (!commands.empty())
221221
{

0 commit comments

Comments
 (0)