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