Revert of Revert of Roll android_tools f6e2370:2a860d8 (patchset #1 id:1 of https...
[chromium-blink-merge.git] / remoting / protocol / channel_multiplexer.h
blobc83b0f3b9616ea6e3352a0aed34888ae2385ab96
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/protobuf_message_parser.h"
13 #include "remoting/protocol/stream_channel_factory.h"
15 namespace remoting {
16 namespace protocol {
18 class ChannelMultiplexer : public StreamChannelFactory {
19 public:
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;
32 private:
33 struct PendingChannel;
34 class MuxChannel;
35 class MuxSocket;
36 friend class MuxChannel;
38 // Callback for |base_channel_| creation.
39 void OnBaseChannelReady(scoped_ptr<net::StreamSocket> 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 |writer_|.
48 void OnWriteFailed(int error);
50 // Failed write notifier, queued asynchronously by OnWriteFailed().
51 void NotifyWriteFailed(const std::string& name);
53 // Callback for |reader_;
54 void OnIncomingPacket(scoped_ptr<MultiplexPacket> packet,
55 const base::Closure& done_task);
57 // Called by MuxChannel.
58 bool DoWrite(scoped_ptr<MultiplexPacket> packet,
59 const base::Closure& done_task);
61 // Factory used to create |base_channel_|. Set to nullptr once creation is
62 // finished or failed.
63 StreamChannelFactory* base_channel_factory_;
65 // Name of the underlying channel.
66 std::string base_channel_name_;
68 // The channel over which to multiplex.
69 scoped_ptr<net::StreamSocket> base_channel_;
71 // List of requested channels while we are waiting for |base_channel_|.
72 std::list<PendingChannel> pending_channels_;
74 int next_channel_id_;
75 std::map<std::string, MuxChannel*> channels_;
77 // Channels are added to |channels_by_receive_id_| only after we receive
78 // receive_id from the remote peer.
79 std::map<int, MuxChannel*> channels_by_receive_id_;
81 BufferedSocketWriter writer_;
82 MessageReader reader_;
83 ProtobufMessageParser<MultiplexPacket> parser_;
85 base::WeakPtrFactory<ChannelMultiplexer> weak_factory_;
87 DISALLOW_COPY_AND_ASSIGN(ChannelMultiplexer);
90 } // namespace protocol
91 } // namespace remoting
94 #endif // REMOTING_PROTOCOL_CHANNEL_MULTIPLEXER_H_