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.
5 #ifndef REMOTING_PROTOCOL_CHANNEL_MULTIPLEXER_H_
6 #define REMOTING_PROTOCOL_CHANNEL_MULTIPLEXER_H_
8 #include "base/memory/weak_ptr.h"
9 #include "remoting/base/buffered_socket_writer.h"
10 #include "remoting/proto/mux.pb.h"
11 #include "remoting/protocol/message_reader.h"
12 #include "remoting/protocol/protobuf_message_parser.h"
13 #include "remoting/protocol/stream_channel_factory.h"
18 class ChannelMultiplexer
: public StreamChannelFactory
{
20 static const char kMuxChannelName
[];
22 // |factory| is used to create the channel upon which to multiplex.
23 ChannelMultiplexer(StreamChannelFactory
* factory
,
24 const std::string
& base_channel_name
);
25 ~ChannelMultiplexer() override
;
27 // StreamChannelFactory interface.
28 void CreateChannel(const std::string
& name
,
29 const ChannelCreatedCallback
& callback
) override
;
30 void CancelChannelCreation(const std::string
& name
) override
;
33 struct PendingChannel
;
36 friend class MuxChannel
;
38 // Callback for |base_channel_| creation.
39 void OnBaseChannelReady(scoped_ptr
<P2PStreamSocket
> socket
);
41 // Helper to create channels asynchronously.
42 void DoCreatePendingChannels();
44 // Helper method used to create channels.
45 MuxChannel
* GetOrCreateChannel(const std::string
& name
);
47 // Error handling callback for |reader_| and |writer_|.
48 void OnBaseChannelError(int error
);
50 // Propagates base channel error to channel |name|, queued asynchronously by
51 // OnBaseChannelError().
52 void NotifyBaseChannelError(const std::string
& name
, int error
);
54 // Callback for |reader_;
55 void OnIncomingPacket(scoped_ptr
<MultiplexPacket
> packet
,
56 const base::Closure
& done_task
);
58 // Called by MuxChannel.
59 void DoWrite(scoped_ptr
<MultiplexPacket
> packet
,
60 const base::Closure
& done_task
);
62 // Factory used to create |base_channel_|. Set to nullptr once creation is
63 // finished or failed.
64 StreamChannelFactory
* base_channel_factory_
;
66 // Name of the underlying channel.
67 std::string base_channel_name_
;
69 // The channel over which to multiplex.
70 scoped_ptr
<P2PStreamSocket
> base_channel_
;
72 // List of requested channels while we are waiting for |base_channel_|.
73 std::list
<PendingChannel
> pending_channels_
;
76 std::map
<std::string
, MuxChannel
*> channels_
;
78 // Channels are added to |channels_by_receive_id_| only after we receive
79 // receive_id from the remote peer.
80 std::map
<int, MuxChannel
*> channels_by_receive_id_
;
82 BufferedSocketWriter writer_
;
83 MessageReader reader_
;
84 ProtobufMessageParser
<MultiplexPacket
> parser_
;
86 base::WeakPtrFactory
<ChannelMultiplexer
> weak_factory_
;
88 DISALLOW_COPY_AND_ASSIGN(ChannelMultiplexer
);
91 } // namespace protocol
92 } // namespace remoting
95 #endif // REMOTING_PROTOCOL_CHANNEL_MULTIPLEXER_H_