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/channel_factory.h"
12 #include "remoting/protocol/message_reader.h"
17 class ChannelMultiplexer
: public ChannelFactory
{
19 static const char kMuxChannelName
[];
21 // |factory| is used to create the channel upon which to multiplex.
22 ChannelMultiplexer(ChannelFactory
* factory
,
23 const std::string
& base_channel_name
);
24 virtual ~ChannelMultiplexer();
26 // ChannelFactory interface.
27 virtual void CreateStreamChannel(
28 const std::string
& name
,
29 const StreamChannelCallback
& callback
) OVERRIDE
;
30 virtual void CreateDatagramChannel(
31 const std::string
& name
,
32 const DatagramChannelCallback
& callback
) OVERRIDE
;
33 virtual void CancelChannelCreation(const std::string
& name
) OVERRIDE
;
36 struct PendingChannel
;
39 friend class MuxChannel
;
41 // Callback for |base_channel_| creation.
42 void OnBaseChannelReady(scoped_ptr
<net::StreamSocket
> socket
);
44 // Helper to create channels asynchronously.
45 void DoCreatePendingChannels();
47 // Helper method used to create channels.
48 MuxChannel
* GetOrCreateChannel(const std::string
& name
);
50 // Error handling callback for |writer_|.
51 void OnWriteFailed(int error
);
53 // Failed write notifier, queued asynchronously by OnWriteFailed().
54 void NotifyWriteFailed(const std::string
& name
);
56 // Callback for |reader_;
57 void OnIncomingPacket(scoped_ptr
<MultiplexPacket
> packet
,
58 const base::Closure
& done_task
);
60 // Called by MuxChannel.
61 bool DoWrite(scoped_ptr
<MultiplexPacket
> packet
,
62 const base::Closure
& done_task
);
64 // Factory used to create |base_channel_|. Set to NULL once creation is
65 // finished or failed.
66 ChannelFactory
* base_channel_factory_
;
68 // Name of the underlying channel.
69 std::string base_channel_name_
;
71 // The channel over which to multiplex.
72 scoped_ptr
<net::StreamSocket
> base_channel_
;
74 // List of requested channels while we are waiting for |base_channel_|.
75 std::list
<PendingChannel
> pending_channels_
;
78 std::map
<std::string
, MuxChannel
*> channels_
;
80 // Channels are added to |channels_by_receive_id_| only after we receive
81 // receive_id from the remote peer.
82 std::map
<int, MuxChannel
*> channels_by_receive_id_
;
84 BufferedSocketWriter writer_
;
85 ProtobufMessageReader
<MultiplexPacket
> reader_
;
87 base::WeakPtrFactory
<ChannelMultiplexer
> weak_factory_
;
89 DISALLOW_COPY_AND_ASSIGN(ChannelMultiplexer
);
92 } // namespace protocol
93 } // namespace remoting
96 #endif // REMOTING_PROTOCOL_CHANNEL_MULTIPLEXER_H_