OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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_JINGLE_GLUE_JINGLE_CHANNEL_H_ |
| 6 #define REMOTING_JINGLE_GLUE_JINGLE_CHANNEL_H_ |
| 7 |
| 8 #include <deque> |
| 9 #include <string> |
| 10 |
| 11 #include "base/basictypes.h" |
| 12 #include "base/condition_variable.h" |
| 13 #include "base/lock.h" |
| 14 #include "base/ref_counted.h" |
| 15 #include "base/scoped_ptr.h" |
| 16 #include "talk/base/sigslot.h" |
| 17 #include "testing/gtest/include/gtest/gtest_prod.h" |
| 18 |
| 19 namespace base { |
| 20 class WaitableEvent; |
| 21 } // namespace base |
| 22 |
| 23 namespace talk_base { |
| 24 class StreamInterface; |
| 25 } // namespace talk_base |
| 26 |
| 27 namespace media { |
| 28 class Buffer; |
| 29 class DataBuffer; |
| 30 } // namespace media |
| 31 |
| 32 namespace remoting { |
| 33 class JingleThread; |
| 34 |
| 35 class JingleChannel : public base::RefCountedThreadSafe<JingleChannel> { |
| 36 public: |
| 37 enum State { |
| 38 INITIALIZING, |
| 39 CONNECTING, |
| 40 OPEN, |
| 41 CLOSED, |
| 42 FAILED, |
| 43 }; |
| 44 |
| 45 class Callback { |
| 46 public: |
| 47 virtual ~Callback() {} |
| 48 |
| 49 // Called when state of the connection is changed. |
| 50 virtual void OnStateChange(JingleChannel* channel, State state) = 0; |
| 51 |
| 52 // Called when a new packet is received. |
| 53 virtual void OnPacketReceived(JingleChannel* channel, |
| 54 scoped_refptr<media::DataBuffer> data) = 0; |
| 55 }; |
| 56 |
| 57 virtual ~JingleChannel(); |
| 58 |
| 59 // Puts data to the write buffer. |
| 60 virtual void Write(scoped_refptr<media::DataBuffer> data); |
| 61 |
| 62 // Closes the tunnel. |
| 63 virtual void Close(); |
| 64 |
| 65 // Current state of the tunnel. |
| 66 State state() { return state_; } |
| 67 |
| 68 // JID of the other end of the channel. |
| 69 const std::string& jid() { return jid_; } |
| 70 |
| 71 // Number of bytes currently stored in the write buffer. |
| 72 size_t write_buffer_size(); |
| 73 |
| 74 protected: |
| 75 // Needs access to constructor, Init(). |
| 76 friend class JingleClient; |
| 77 |
| 78 // Constructor used by unit test only. |
| 79 // TODO(hclam): Have to suppress warnnings in MSVC. |
| 80 JingleChannel(); |
| 81 |
| 82 // Used by JingleClient to create an instance of the channel. |callback| |
| 83 // must not be NULL. |
| 84 JingleChannel(Callback* callback); |
| 85 |
| 86 // Initialized the channel. Ownership of the |stream| is transfered to |
| 87 // caller. Ownership of |thread| is not. |
| 88 void Init(JingleThread* thread, talk_base::StreamInterface* stream, |
| 89 const std::string& jid); |
| 90 void SetState(State state); |
| 91 |
| 92 JingleThread* thread_; |
| 93 scoped_ptr<talk_base::StreamInterface> stream_; |
| 94 State state_; |
| 95 |
| 96 private: |
| 97 FRIEND_TEST(JingleChannelTest, Init); |
| 98 FRIEND_TEST(JingleChannelTest, Write); |
| 99 FRIEND_TEST(JingleChannelTest, Read); |
| 100 FRIEND_TEST(JingleChannelTest, Close); |
| 101 |
| 102 typedef std::deque<scoped_refptr<media::DataBuffer> > DataQueue; |
| 103 |
| 104 // Event handler for the stream. It passes stream events from the stream |
| 105 // to JingleChannel. |
| 106 class EventHandler : public sigslot::has_slots<> { |
| 107 protected: |
| 108 EventHandler(JingleChannel* channel) |
| 109 : channel_(channel) { } |
| 110 |
| 111 // Constructor used only by unit test. |
| 112 EventHandler() : channel_(NULL) {} |
| 113 |
| 114 void OnStreamEvent(talk_base::StreamInterface* stream, |
| 115 int events, int error) { |
| 116 channel_->OnStreamEvent(stream, events, error); |
| 117 } |
| 118 friend class JingleChannel; |
| 119 private: |
| 120 JingleChannel* channel_; |
| 121 }; |
| 122 friend class EventHandler; |
| 123 |
| 124 // Event handler for the stream. |
| 125 void OnStreamEvent(talk_base::StreamInterface* stream, |
| 126 int events, int error); |
| 127 |
| 128 // Writes data from the buffer to the stream. Called |
| 129 // from OnStreamEvent() in the jingle thread. |
| 130 void DoWrite(); |
| 131 |
| 132 // Reads data from the stream and puts it to the read buffer. |
| 133 // Called from OnStreamEvent() in the jingle thread. |
| 134 void DoRead(); |
| 135 |
| 136 void DoClose(base::WaitableEvent* done_event); |
| 137 |
| 138 Callback* callback_; |
| 139 EventHandler event_handler_; |
| 140 std::string jid_; |
| 141 |
| 142 // Write buffer. |write_lock_| should be locked when accessing |write_queue_| |
| 143 // and |write_buffer_size_|, but isn't neccessary for |current_write_buf_|. |
| 144 // |current_write_buf_| is accessed only by the jingle thread. |
| 145 // |write_buffer_size_| stores number of bytes currently in |write_queue_| |
| 146 // and in |current_write_buf_|. |
| 147 DataQueue write_queue_; |
| 148 size_t write_buffer_size_; |
| 149 Lock write_lock_; |
| 150 scoped_refptr<media::DataBuffer> current_write_buf_; |
| 151 size_t current_write_buf_pos_; |
| 152 |
| 153 DISALLOW_COPY_AND_ASSIGN(JingleChannel); |
| 154 }; |
| 155 |
| 156 } // namespace remoting |
| 157 |
| 158 #endif // REMOTING_JINGLE_GLUE_JINGLE_CHANNEL_H_ |
OLD | NEW |