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/proto/mux.pb.h"
10 #include "remoting/protocol/buffered_socket_writer.h"
11 #include "remoting/protocol/message_reader.h"
12 #include "remoting/protocol/stream_channel_factory.h"
17 class ChannelMultiplexer
: public StreamChannelFactory
{
19 static const char kMuxChannelName
[];
21 // |factory| is used to create the channel upon which to multiplex.
22 ChannelMultiplexer(StreamChannelFactory
* factory
,
23 const std::string
& base_channel_name
);
24 virtual ~ChannelMultiplexer();
26 // StreamChannelFactory interface.
27 virtual void CreateChannel(const std::string
& name
,
28 const ChannelCreatedCallback
& callback
) override
;
29 virtual void CancelChannelCreation(const std::string
& name
) override
;
32 struct PendingChannel
;
35 friend class MuxChannel
;
37 // Callback for |base_channel_| creation.
38 void OnBaseChannelReady(scoped_ptr
<net::StreamSocket
> socket
);
40 // Helper to create channels asynchronously.
41 void DoCreatePendingChannels();
43 // Helper method used to create channels.
44 MuxChannel
* GetOrCreateChannel(const std::string
& name
);
46 // Error handling callback for |writer_|.
47 void OnWriteFailed(int error
);
49 // Failed write notifier, queued asynchronously by OnWriteFailed().
50 void NotifyWriteFailed(const std::string
& name
);
52 // Callback for |reader_;
53 void OnIncomingPacket(scoped_ptr
<MultiplexPacket
> packet
,
54 const base::Closure
& done_task
);
56 // Called by MuxChannel.
57 bool DoWrite(scoped_ptr
<MultiplexPacket
> packet
,
58 const base::Closure
& done_task
);
60 // Factory used to create |base_channel_|. Set to NULL once creation is
61 // finished or failed.
62 StreamChannelFactory
* base_channel_factory_
;
64 // Name of the underlying channel.
65 std::string base_channel_name_
;
67 // The channel over which to multiplex.
68 scoped_ptr
<net::StreamSocket
> base_channel_
;
70 // List of requested channels while we are waiting for |base_channel_|.
71 std::list
<PendingChannel
> pending_channels_
;
74 std::map
<std::string
, MuxChannel
*> channels_
;
76 // Channels are added to |channels_by_receive_id_| only after we receive
77 // receive_id from the remote peer.
78 std::map
<int, MuxChannel
*> channels_by_receive_id_
;
80 BufferedSocketWriter writer_
;
81 ProtobufMessageReader
<MultiplexPacket
> reader_
;
83 base::WeakPtrFactory
<ChannelMultiplexer
> weak_factory_
;
85 DISALLOW_COPY_AND_ASSIGN(ChannelMultiplexer
);
88 } // namespace protocol
89 } // namespace remoting
92 #endif // REMOTING_PROTOCOL_CHANNEL_MULTIPLEXER_H_