Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / remoting / protocol / channel_multiplexer.h
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 "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"
13
14 namespace remoting {
15 namespace protocol {
16
17 class ChannelMultiplexer : public StreamChannelFactory {
18  public:
19   static const char kMuxChannelName[];
20
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();
25
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;
30
31  private:
32   struct PendingChannel;
33   class MuxChannel;
34   class MuxSocket;
35   friend class MuxChannel;
36
37   // Callback for |base_channel_| creation.
38   void OnBaseChannelReady(scoped_ptr<net::StreamSocket> socket);
39
40   // Helper to create channels asynchronously.
41   void DoCreatePendingChannels();
42
43   // Helper method used to create channels.
44   MuxChannel* GetOrCreateChannel(const std::string& name);
45
46   // Error handling callback for |writer_|.
47   void OnWriteFailed(int error);
48
49   // Failed write notifier, queued asynchronously by OnWriteFailed().
50   void NotifyWriteFailed(const std::string& name);
51
52   // Callback for |reader_;
53   void OnIncomingPacket(scoped_ptr<MultiplexPacket> packet,
54                         const base::Closure& done_task);
55
56   // Called by MuxChannel.
57   bool DoWrite(scoped_ptr<MultiplexPacket> packet,
58                const base::Closure& done_task);
59
60   // Factory used to create |base_channel_|. Set to NULL once creation is
61   // finished or failed.
62   StreamChannelFactory* base_channel_factory_;
63
64   // Name of the underlying channel.
65   std::string base_channel_name_;
66
67   // The channel over which to multiplex.
68   scoped_ptr<net::StreamSocket> base_channel_;
69
70   // List of requested channels while we are waiting for |base_channel_|.
71   std::list<PendingChannel> pending_channels_;
72
73   int next_channel_id_;
74   std::map<std::string, MuxChannel*> channels_;
75
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_;
79
80   BufferedSocketWriter writer_;
81   ProtobufMessageReader<MultiplexPacket> reader_;
82
83   base::WeakPtrFactory<ChannelMultiplexer> weak_factory_;
84
85   DISALLOW_COPY_AND_ASSIGN(ChannelMultiplexer);
86 };
87
88 }  // namespace protocol
89 }  // namespace remoting
90
91
92 #endif  // REMOTING_PROTOCOL_CHANNEL_MULTIPLEXER_H_