blob: 12d1d933a481e8d154134b275fb126c7ba31853e [file] [log] [blame]
* Copyright (c) 2021 The WebRTC project authors. All Rights Reserved.
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
#include <stddef.h>
#include <stdint.h>
#include <map>
#include <string>
#include <unordered_map>
#include "absl/strings/string_view.h"
#include "api/array_view.h"
#include "net/dcsctp/common/sequence_numbers.h"
#include "net/dcsctp/packet/chunk/forward_tsn_common.h"
#include "net/dcsctp/packet/data.h"
#include "net/dcsctp/rx/reassembly_streams.h"
namespace dcsctp {
// Handles reassembly of incoming data when interleaved message sending
// is not enabled on the association, i.e. when RFC8260 is not in use and
// RFC4960 is to be followed.
class TraditionalReassemblyStreams : public ReassemblyStreams {
TraditionalReassemblyStreams(absl::string_view log_prefix,
OnAssembledMessage on_assembled_message)
: log_prefix_(log_prefix), on_assembled_message_(on_assembled_message) {}
int Add(UnwrappedTSN tsn, Data data) override;
size_t HandleForwardTsn(
UnwrappedTSN new_cumulative_ack_tsn,
rtc::ArrayView<const AnyForwardTsnChunk::SkippedStream> skipped_streams)
void ResetStreams(rtc::ArrayView<const StreamID> stream_ids) override;
using ChunkMap = std::map<UnwrappedTSN, Data>;
// Base class for `UnorderedStream` and `OrderedStream`.
class StreamBase {
explicit StreamBase(TraditionalReassemblyStreams* parent)
: parent_(*parent) {}
size_t AssembleMessage(const ChunkMap::iterator start,
const ChunkMap::iterator end);
TraditionalReassemblyStreams& parent_;
// Manages all received data for a specific unordered stream, and assembles
// messages when possible.
class UnorderedStream : StreamBase {
explicit UnorderedStream(TraditionalReassemblyStreams* parent)
: StreamBase(parent) {}
int Add(UnwrappedTSN tsn, Data data);
// Returns the number of bytes removed from the queue.
size_t EraseTo(UnwrappedTSN tsn);
// Given an iterator to any chunk within the map, try to assemble a message
// into `reassembled_messages` containing it and - if successful - erase
// those chunks from the stream chunks map.
// Returns the number of bytes that were assembled.
size_t TryToAssembleMessage(ChunkMap::iterator iter);
ChunkMap chunks_;
// Manages all received data for a specific ordered stream, and assembles
// messages when possible.
class OrderedStream : StreamBase {
explicit OrderedStream(TraditionalReassemblyStreams* parent)
: StreamBase(parent), next_ssn_(ssn_unwrapper_.Unwrap(SSN(0))) {}
int Add(UnwrappedTSN tsn, Data data);
size_t EraseTo(SSN ssn);
void Reset() {
next_ssn_ = ssn_unwrapper_.Unwrap(SSN(0));
// Try to assemble one or several messages in order from the stream.
// Returns the number of bytes assembled if a message was assembled.
size_t TryToAssembleMessage();
size_t TryToAssembleMessages();
// This must be an ordered container to be able to iterate in SSN order.
std::map<UnwrappedSSN, ChunkMap> chunks_by_ssn_;
UnwrappedSSN::Unwrapper ssn_unwrapper_;
UnwrappedSSN next_ssn_;
const std::string log_prefix_;
// Callback for when a message has been assembled.
const OnAssembledMessage on_assembled_message_;
// All unordered and ordered streams, managing not-yet-assembled data.
std::unordered_map<StreamID, UnorderedStream, StreamID::Hasher>
std::unordered_map<StreamID, OrderedStream, StreamID::Hasher>
} // namespace dcsctp