OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef REMOTING_JINGLE_GLUE_JINGLE_CHANNEL_H_ | 5 #ifndef REMOTING_JINGLE_GLUE_JINGLE_CHANNEL_H_ |
6 #define REMOTING_JINGLE_GLUE_JINGLE_CHANNEL_H_ | 6 #define REMOTING_JINGLE_GLUE_JINGLE_CHANNEL_H_ |
7 | 7 |
8 #include <deque> | 8 #include <deque> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/condition_variable.h" | 12 #include "base/condition_variable.h" |
13 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
14 #include "base/lock.h" | 14 #include "base/lock.h" |
15 #include "base/ref_counted.h" | 15 #include "base/ref_counted.h" |
16 #include "base/scoped_ptr.h" | 16 #include "base/scoped_ptr.h" |
| 17 #include "base/task.h" |
17 #include "third_party/libjingle/source/talk/base/sigslot.h" | 18 #include "third_party/libjingle/source/talk/base/sigslot.h" |
18 | 19 |
19 namespace base { | 20 namespace base { |
20 class WaitableEvent; | 21 class WaitableEvent; |
21 } // namespace base | 22 } // namespace base |
22 | 23 |
23 namespace talk_base { | 24 namespace talk_base { |
24 class StreamInterface; | 25 class StreamInterface; |
25 } // namespace talk_base | 26 } // namespace talk_base |
26 | 27 |
(...skipping 25 matching lines...) Expand all Loading... |
52 // Called when a new packet is received. | 53 // Called when a new packet is received. |
53 virtual void OnPacketReceived(JingleChannel* channel, | 54 virtual void OnPacketReceived(JingleChannel* channel, |
54 scoped_refptr<media::DataBuffer> data) = 0; | 55 scoped_refptr<media::DataBuffer> data) = 0; |
55 }; | 56 }; |
56 | 57 |
57 virtual ~JingleChannel(); | 58 virtual ~JingleChannel(); |
58 | 59 |
59 // Puts data to the write buffer. | 60 // Puts data to the write buffer. |
60 virtual void Write(scoped_refptr<media::DataBuffer> data); | 61 virtual void Write(scoped_refptr<media::DataBuffer> data); |
61 | 62 |
62 // Closes the tunnel. | 63 // Closes the tunnel. If specified, |closed_task| is executed after the |
| 64 // connection is successfully closed. |
63 virtual void Close(); | 65 virtual void Close(); |
| 66 virtual void Close(Task* closed_task); |
64 | 67 |
65 // Current state of the tunnel. | 68 // Current state of the tunnel. |
66 State state() const { return state_; } | 69 State state() const { return state_; } |
67 | 70 |
68 // JID of the other end of the channel. | 71 // JID of the other end of the channel. |
69 const std::string& jid() const { return jid_; } | 72 const std::string& jid() const { return jid_; } |
70 | 73 |
71 // Number of bytes currently stored in the write buffer. | 74 // Number of bytes currently stored in the write buffer. |
72 size_t write_buffer_size(); | 75 size_t write_buffer_size(); |
73 | 76 |
74 protected: | 77 protected: |
75 // Needs access to constructor, Init(). | 78 // Needs access to constructor, Init(). |
76 friend class JingleClient; | 79 friend class JingleClient; |
77 | 80 |
78 // Constructor used by unit test only. | 81 // Constructor used by unit test only. |
79 // TODO(hclam): Have to suppress warnings in MSVC. | 82 // TODO(hclam): Have to suppress warnings in MSVC. |
80 JingleChannel(); | 83 JingleChannel(); |
81 | 84 |
82 // Used by JingleClient to create an instance of the channel. |callback| | 85 // Used by JingleClient to create an instance of the channel. |callback| |
83 // must not be NULL. | 86 // must not be NULL. |
84 explicit JingleChannel(Callback* callback); | 87 explicit JingleChannel(Callback* callback); |
85 | 88 |
86 // Initialized the channel. Ownership of the |stream| is transfered to | 89 // Initialized the channel. Ownership of the |stream| is transfered to |
87 // caller. Ownership of |thread| is not. | 90 // caller. Ownership of |thread| is not. |
88 void Init(JingleThread* thread, talk_base::StreamInterface* stream, | 91 void Init(JingleThread* thread, talk_base::StreamInterface* stream, |
89 const std::string& jid); | 92 const std::string& jid); |
90 void SetState(State state); | |
91 | |
92 JingleThread* thread_; | |
93 scoped_ptr<talk_base::StreamInterface> stream_; | |
94 State state_; | |
95 | 93 |
96 private: | 94 private: |
| 95 friend class JingleChannelTest; |
97 FRIEND_TEST_ALL_PREFIXES(JingleChannelTest, Init); | 96 FRIEND_TEST_ALL_PREFIXES(JingleChannelTest, Init); |
98 FRIEND_TEST_ALL_PREFIXES(JingleChannelTest, Write); | 97 FRIEND_TEST_ALL_PREFIXES(JingleChannelTest, Write); |
99 FRIEND_TEST_ALL_PREFIXES(JingleChannelTest, Read); | 98 FRIEND_TEST_ALL_PREFIXES(JingleChannelTest, Read); |
100 FRIEND_TEST_ALL_PREFIXES(JingleChannelTest, Close); | |
101 | 99 |
102 typedef std::deque<scoped_refptr<media::DataBuffer> > DataQueue; | 100 typedef std::deque<scoped_refptr<media::DataBuffer> > DataQueue; |
103 | 101 |
104 // Event handler for the stream. It passes stream events from the stream | 102 // Event handler for the stream. It passes stream events from the stream |
105 // to JingleChannel. | 103 // to JingleChannel. |
106 class EventHandler : public sigslot::has_slots<> { | 104 class EventHandler : public sigslot::has_slots<> { |
107 protected: | 105 protected: |
108 explicit EventHandler(JingleChannel* channel) : channel_(channel) {} | 106 explicit EventHandler(JingleChannel* channel) : channel_(channel) {} |
109 | 107 |
110 // Constructor used only by unit test. | 108 // Constructor used only by unit test. |
(...skipping 14 matching lines...) Expand all Loading... |
125 int events, int error); | 123 int events, int error); |
126 | 124 |
127 // Writes data from the buffer to the stream. Called | 125 // Writes data from the buffer to the stream. Called |
128 // from OnStreamEvent() in the jingle thread. | 126 // from OnStreamEvent() in the jingle thread. |
129 void DoWrite(); | 127 void DoWrite(); |
130 | 128 |
131 // Reads data from the stream and puts it to the read buffer. | 129 // Reads data from the stream and puts it to the read buffer. |
132 // Called from OnStreamEvent() in the jingle thread. | 130 // Called from OnStreamEvent() in the jingle thread. |
133 void DoRead(); | 131 void DoRead(); |
134 | 132 |
135 void DoClose(base::WaitableEvent* done_event); | 133 // Used by Close() to actually close the channel. |
| 134 void DoClose(); |
| 135 |
| 136 // Updates state and calels |callback_| if neccessary. |
| 137 void SetState(State new_state); |
| 138 |
| 139 // The thread this channel runs on. |
| 140 JingleThread* thread_; |
| 141 |
| 142 // The stream of this channel. |
| 143 scoped_ptr<talk_base::StreamInterface> stream_; |
| 144 |
| 145 // Current state of the channel. |
| 146 State state_; |
136 | 147 |
137 // Callback that is called on channel events. Initialized in the constructor. | 148 // Callback that is called on channel events. Initialized in the constructor. |
| 149 // Must not be called if |closed_| is set to true. |
138 Callback* callback_; | 150 Callback* callback_; |
139 | 151 |
| 152 // |closed_| must be set to true after Close() is called. |state_lock_| must |
| 153 // be locked whenever closed_ is accessed. |
| 154 Lock state_lock_; |
| 155 bool closed_; |
| 156 scoped_ptr<Task> closed_task_; |
| 157 |
140 // Event handler for stream events. | 158 // Event handler for stream events. |
141 EventHandler event_handler_; | 159 EventHandler event_handler_; |
142 | 160 |
143 // Jid of the other end of the channel. | 161 // Jid of the other end of the channel. |
144 std::string jid_; | 162 std::string jid_; |
145 | 163 |
146 // Write buffer. |write_lock_| should be locked when accessing |write_queue_| | 164 // Write buffer. |write_lock_| should be locked when accessing |write_queue_| |
147 // and |write_buffer_size_|, but isn't necessary for |current_write_buf_|. | 165 // and |write_buffer_size_|, but isn't necessary for |current_write_buf_|. |
148 // |current_write_buf_| is accessed only by the jingle thread. | 166 // |current_write_buf_| is accessed only by the jingle thread. |
149 // |write_buffer_size_| stores number of bytes currently in |write_queue_| | 167 // |write_buffer_size_| stores number of bytes currently in |write_queue_| |
150 // and in |current_write_buf_|. | 168 // and in |current_write_buf_|. |
151 DataQueue write_queue_; | 169 DataQueue write_queue_; |
152 size_t write_buffer_size_; | 170 size_t write_buffer_size_; |
153 Lock write_lock_; | 171 Lock write_lock_; |
154 scoped_refptr<media::DataBuffer> current_write_buf_; | 172 scoped_refptr<media::DataBuffer> current_write_buf_; |
155 size_t current_write_buf_pos_; | 173 size_t current_write_buf_pos_; |
156 | 174 |
157 DISALLOW_COPY_AND_ASSIGN(JingleChannel); | 175 DISALLOW_COPY_AND_ASSIGN(JingleChannel); |
158 }; | 176 }; |
159 | 177 |
160 } // namespace remoting | 178 } // namespace remoting |
161 | 179 |
162 #endif // REMOTING_JINGLE_GLUE_JINGLE_CHANNEL_H_ | 180 #endif // REMOTING_JINGLE_GLUE_JINGLE_CHANNEL_H_ |
OLD | NEW |