#ifndef REMOTING_PROTOCOL_CHANNEL_MULTIPLEXER_H_
#define REMOTING_PROTOCOL_CHANNEL_MULTIPLEXER_H_
#include <map>
#include <memory>
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "remoting/base/buffered_socket_writer.h"
#include "remoting/proto/mux.pb.h"
#include "remoting/protocol/message_reader.h"
#include "remoting/protocol/stream_channel_factory.h"
namespace remoting::protocol {
class ChannelMultiplexer : public StreamChannelFactory {
public:
static const char kMuxChannelName[];
ChannelMultiplexer(StreamChannelFactory* factory,
const std::string& base_channel_name);
ChannelMultiplexer(const ChannelMultiplexer&) = delete;
ChannelMultiplexer& operator=(const ChannelMultiplexer&) = delete;
~ChannelMultiplexer() override;
void CreateChannel(const std::string& name,
ChannelCreatedCallback callback) override;
void CancelChannelCreation(const std::string& name) override;
private:
struct PendingChannel;
class MuxChannel;
class MuxSocket;
friend class MuxChannel;
void OnBaseChannelReady(std::unique_ptr<P2PStreamSocket> socket);
void DoCreatePendingChannels();
MuxChannel* GetOrCreateChannel(const std::string& name);
void OnBaseChannelError(int error);
void NotifyBaseChannelError(const std::string& name, int error);
void OnIncomingPacket(std::unique_ptr<CompoundBuffer> buffer);
void DoWrite(std::unique_ptr<MultiplexPacket> packet,
base::OnceClosure done_task,
const net::NetworkTrafficAnnotationTag& traffic_annotation);
raw_ptr<StreamChannelFactory> base_channel_factory_;
std::string base_channel_name_;
std::unique_ptr<P2PStreamSocket> base_channel_;
std::list<PendingChannel> pending_channels_;
int next_channel_id_;
std::map<std::string, std::unique_ptr<MuxChannel>> channels_;
std::map<int, raw_ptr<MuxChannel, CtnExperimental>> channels_by_receive_id_;
BufferedSocketWriter writer_;
MessageReader reader_;
base::WeakPtrFactory<ChannelMultiplexer> weak_factory_{this};
};
}
#endif