Skip to content
This repository has been archived by the owner on Apr 18, 2024. It is now read-only.

Commit

Permalink
Fix problem related to control packet state transitions
Browse files Browse the repository at this point in the history
Ref PR #62
  • Loading branch information
bihanssen committed Dec 18, 2017
1 parent 10de846 commit 9b98770
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
10 changes: 2 additions & 8 deletions include/common/internal/transport/h5_transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,27 +130,21 @@ class InitializedExitCriterias : public ExitCriterias
public:
bool syncConfigSent;
bool syncConfigRspReceived;
bool syncConfigReceived;
bool syncConfigRspSent;

InitializedExitCriterias()
: ExitCriterias(),
syncConfigSent(false),
syncConfigRspReceived(false),
syncConfigReceived(false),
syncConfigRspSent(false) {}
syncConfigRspReceived(false) {}

bool isFullfilled() const override
{
return ioResourceError || close || (syncConfigSent && syncConfigRspReceived && syncConfigReceived && syncConfigRspSent);
return ioResourceError || close || (syncConfigSent && syncConfigRspReceived);
}

void reset() override
{
ExitCriterias::reset();
syncConfigSent = false;
syncConfigRspSent = false;
syncConfigReceived = false;
syncConfigRspReceived = false;
};

Expand Down
32 changes: 16 additions & 16 deletions src/common/transport/h5_transport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@

// Constants use for state machine states UNINITIALIZED and INITIALIZED
const auto NON_ACTIVE_STATE_TIMEOUT = std::chrono::milliseconds(250); // Duration to wait until resending a packet
const uint8_t PACKET_RETRANSMISSIONS = 4; // Number of times to send reliable packets before giving in
const uint8_t PACKET_RETRANSMISSIONS = 6; // Number of times to send reliable packets before giving in

// Other constants
const auto OPEN_WAIT_TIMEOUT = std::chrono::milliseconds(2000); // Duration to wait for state ACTIVE after open is called
Expand Down Expand Up @@ -316,14 +316,14 @@ void H5Transport::processPacket(std::vector<uint8_t> &packet)

if (isSyncConfigPacket)
{
exit->syncConfigReceived = true;
sendControlPacket(CONTROL_PKT_SYNC_CONFIG_RESPONSE);
exit->syncConfigRspSent = true;
syncWaitCondition.notify_all();
}

if (isSyncPacket) {
if (isSyncPacket)
{
sendControlPacket(CONTROL_PKT_SYNC_RESPONSE);
syncWaitCondition.notify_all();
}
}
else if (currentState == STATE_ACTIVE)
Expand All @@ -335,6 +335,11 @@ void H5Transport::processPacket(std::vector<uint8_t> &packet)
exit->syncReceived = true;
syncWaitCondition.notify_all();
}

if (isSyncConfigPacket)
{
sendControlPacket(CONTROL_PKT_SYNC_CONFIG_RESPONSE);
}
}
}
else if (packet_type == VENDOR_SPECIFIC_PACKET)
Expand Down Expand Up @@ -522,11 +527,12 @@ void H5Transport::setupStateMachine()
uint8_t syncRetransmission = PACKET_RETRANSMISSIONS;
std::unique_lock<std::mutex> syncGuard(syncMutex);

while (!exit->isFullfilled() && syncRetransmission--)
while (!exit->isFullfilled() && syncRetransmission > 0)
{
sendControlPacket(CONTROL_PKT_SYNC);
exit->syncSent = true;
syncWaitCondition.wait_for(syncGuard, NON_ACTIVE_STATE_TIMEOUT);
syncRetransmission--;
}

if (exit->isFullfilled())
Expand All @@ -548,22 +554,16 @@ void H5Transport::setupStateMachine()
std::unique_lock<std::mutex> syncGuard(syncMutex);

// Send a package immediately
sendControlPacket(CONTROL_PKT_SYNC_CONFIG);
exit->syncConfigSent = true;

while (!exit->isFullfilled() && syncRetransmission > 0)
{
auto status = syncWaitCondition.wait_for(syncGuard, NON_ACTIVE_STATE_TIMEOUT);

if (status == std::cv_status::timeout)
{
sendControlPacket(CONTROL_PKT_SYNC_CONFIG);
syncRetransmission--;
}
sendControlPacket(CONTROL_PKT_SYNC_CONFIG);
exit->syncConfigSent = true;
syncWaitCondition.wait_for(syncGuard, NON_ACTIVE_STATE_TIMEOUT);
syncRetransmission--;
}

if (exit->syncConfigSent && exit->syncConfigRspReceived
&& exit->syncConfigReceived && exit->syncConfigRspSent)
if (exit->syncConfigSent && exit->syncConfigRspReceived)
{
return STATE_ACTIVE;
}
Expand Down

0 comments on commit 9b98770

Please sign in to comment.