1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef REMOTING_PROTOCOL_CHANNEL_MULTIPLEXER_H_ 6 #define REMOTING_PROTOCOL_CHANNEL_MULTIPLEXER_H_ 7 8 #include <memory> 9 10 #include "base/macros.h" 11 #include "base/memory/weak_ptr.h" 12 #include "net/traffic_annotation/network_traffic_annotation.h" 13 #include "remoting/base/buffered_socket_writer.h" 14 #include "remoting/proto/mux.pb.h" 15 #include "remoting/protocol/message_reader.h" 16 #include "remoting/protocol/stream_channel_factory.h" 17 18 namespace remoting { 19 namespace protocol { 20 21 class ChannelMultiplexer : public StreamChannelFactory { 22 public: 23 static const char kMuxChannelName[]; 24 25 // |factory| is used to create the channel upon which to multiplex. 26 ChannelMultiplexer(StreamChannelFactory* factory, 27 const std::string& base_channel_name); 28 ~ChannelMultiplexer() override; 29 30 // StreamChannelFactory interface. 31 void CreateChannel(const std::string& name, 32 ChannelCreatedCallback callback) override; 33 void CancelChannelCreation(const std::string& name) override; 34 35 private: 36 struct PendingChannel; 37 class MuxChannel; 38 class MuxSocket; 39 friend class MuxChannel; 40 41 // Callback for |base_channel_| creation. 42 void OnBaseChannelReady(std::unique_ptr<P2PStreamSocket> socket); 43 44 // Helper to create channels asynchronously. 45 void DoCreatePendingChannels(); 46 47 // Helper method used to create channels. 48 MuxChannel* GetOrCreateChannel(const std::string& name); 49 50 // Error handling callback for |reader_| and |writer_|. 51 void OnBaseChannelError(int error); 52 53 // Propagates base channel error to channel |name|, queued asynchronously by 54 // OnBaseChannelError(). 55 void NotifyBaseChannelError(const std::string& name, int error); 56 57 // Callback for |reader_; 58 void OnIncomingPacket(std::unique_ptr<CompoundBuffer> buffer); 59 60 // Called by MuxChannel. 61 void DoWrite(std::unique_ptr<MultiplexPacket> packet, 62 base::OnceClosure done_task, 63 const net::NetworkTrafficAnnotationTag& traffic_annotation); 64 65 // Factory used to create |base_channel_|. Set to nullptr once creation is 66 // finished or failed. 67 StreamChannelFactory* base_channel_factory_; 68 69 // Name of the underlying channel. 70 std::string base_channel_name_; 71 72 // The channel over which to multiplex. 73 std::unique_ptr<P2PStreamSocket> base_channel_; 74 75 // List of requested channels while we are waiting for |base_channel_|. 76 std::list<PendingChannel> pending_channels_; 77 78 int next_channel_id_; 79 std::map<std::string, std::unique_ptr<MuxChannel>> channels_; 80 81 // Channels are added to |channels_by_receive_id_| only after we receive 82 // receive_id from the remote peer. 83 std::map<int, MuxChannel*> channels_by_receive_id_; 84 85 BufferedSocketWriter writer_; 86 MessageReader reader_; 87 88 base::WeakPtrFactory<ChannelMultiplexer> weak_factory_{this}; 89 90 DISALLOW_COPY_AND_ASSIGN(ChannelMultiplexer); 91 }; 92 93 } // namespace protocol 94 } // namespace remoting 95 96 97 #endif // REMOTING_PROTOCOL_CHANNEL_MULTIPLEXER_H_ 98