Skip to content

Commit

Permalink
Fixes interleaved channel crashing
Browse files Browse the repository at this point in the history
  • Loading branch information
icecube45 committed Mar 21, 2020
1 parent d875304 commit b69278a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
4 changes: 3 additions & 1 deletion include/f1x/aasdk/Messenger/MessageInStream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include <f1x/aasdk/Messenger/ICryptor.hpp>
#include <f1x/aasdk/Messenger/FrameHeader.hpp>
#include <f1x/aasdk/Messenger/FrameSize.hpp>

#include <f1x/aasdk/Messenger/FrameType.hpp>
namespace f1x
{
namespace aasdk
Expand Down Expand Up @@ -51,6 +51,8 @@ class MessageInStream: public IMessageInStream, public std::enable_shared_from_t
FrameType recentFrameType_;
ReceivePromise::Pointer promise_;
Message::Pointer message_;

std::map<messenger::ChannelId, Message::Pointer> channel_assembly_buffers;
};

}
Expand Down
31 changes: 20 additions & 11 deletions src/Messenger/MessageInStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#include <f1x/aasdk/Messenger/MessageInStream.hpp>
#include <f1x/aasdk/Error/Error.hpp>

#include <iostream>
namespace f1x
{
namespace aasdk
Expand Down Expand Up @@ -63,19 +63,28 @@ void MessageInStream::startReceive(ReceivePromise::Pointer promise)
void MessageInStream::receiveFrameHeaderHandler(const common::DataConstBuffer& buffer)
{
FrameHeader frameHeader(buffer);

if(message_ == nullptr)
if(message_ != nullptr && message_->getChannelId() != frameHeader.getChannelId())
{
message_ = std::make_shared<Message>(frameHeader.getChannelId(), frameHeader.getEncryptionType(), frameHeader.getMessageType());
// we have interleaved channels, stop working on the old one and store it for later; Switch to the new one
channel_assembly_buffers[message_->getChannelId()] = message_;
message_ = nullptr;
// message_.reset();
// promise_->reject(error::Error(error::ErrorCode::MESSENGER_INTERTWINED_CHANNELS));
// promise_.reset();
// return;
}
else if(message_->getChannelId() != frameHeader.getChannelId())
{
message_.reset();
promise_->reject(error::Error(error::ErrorCode::MESSENGER_INTERTWINED_CHANNELS));
promise_.reset();
return;
auto prevBuffer = channel_assembly_buffers.find(frameHeader.getChannelId());
if(prevBuffer != channel_assembly_buffers.end()){ // is there previous data in our map?
if(frameHeader.getType()!=FrameType::FIRST) //only use the data if we're not on a new frame, otherwise disregard
message_ = prevBuffer->second;
else{
message_ = std::make_shared<Message>(frameHeader.getChannelId(), frameHeader.getEncryptionType(), frameHeader.getMessageType());
}
channel_assembly_buffers.erase(prevBuffer); // get rid of the previously stored data because it's now our working data.
}
else if(message_ == nullptr){
message_ = std::make_shared<Message>(frameHeader.getChannelId(), frameHeader.getEncryptionType(), frameHeader.getMessageType());
}

recentFrameType_ = frameHeader.getType();
const size_t frameSize = FrameSize::getSizeOf(frameHeader.getType() == FrameType::FIRST ? FrameSizeType::EXTENDED : FrameSizeType::SHORT);

Expand Down

0 comments on commit b69278a

Please sign in to comment.