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 #include "remoting/host/client_connection.h" | 5 #include "remoting/host/client_connection.h" |
6 | 6 |
7 #include "google/protobuf/message.h" | 7 #include "google/protobuf/message.h" |
8 #include "media/base/data_buffer.h" | 8 #include "media/base/data_buffer.h" |
9 #include "remoting/base/protocol_decoder.h" | 9 #include "remoting/base/protocol_decoder.h" |
10 #include "remoting/base/protocol_util.h" | 10 #include "remoting/base/protocol_util.h" |
(...skipping 20 matching lines...) Expand all Loading... |
31 } | 31 } |
32 | 32 |
33 ClientConnection::~ClientConnection() { | 33 ClientConnection::~ClientConnection() { |
34 // TODO(hclam): When we shut down the viewer we may have to close the | 34 // TODO(hclam): When we shut down the viewer we may have to close the |
35 // jingle channel. | 35 // jingle channel. |
36 } | 36 } |
37 | 37 |
38 void ClientConnection::SendInitClientMessage(int width, int height) { | 38 void ClientConnection::SendInitClientMessage(int width, int height) { |
39 DCHECK_EQ(loop_, MessageLoop::current()); | 39 DCHECK_EQ(loop_, MessageLoop::current()); |
40 DCHECK(!update_stream_size_); | 40 DCHECK(!update_stream_size_); |
41 DCHECK(channel_.get()); | 41 |
| 42 // If we are disconnected then return. |
| 43 if (!channel_) |
| 44 return; |
42 | 45 |
43 HostMessage msg; | 46 HostMessage msg; |
44 msg.mutable_init_client()->set_width(width); | 47 msg.mutable_init_client()->set_width(width); |
45 msg.mutable_init_client()->set_height(height); | 48 msg.mutable_init_client()->set_height(height); |
46 DCHECK(msg.IsInitialized()); | 49 DCHECK(msg.IsInitialized()); |
47 channel_->Write(SerializeAndFrameMessage(msg)); | 50 channel_->Write(SerializeAndFrameMessage(msg)); |
48 } | 51 } |
49 | 52 |
50 void ClientConnection::SendBeginUpdateStreamMessage() { | 53 void ClientConnection::SendBeginUpdateStreamMessage() { |
51 DCHECK_EQ(loop_, MessageLoop::current()); | 54 DCHECK_EQ(loop_, MessageLoop::current()); |
52 DCHECK(channel_.get()); | 55 |
| 56 // If we are disconnected then return. |
| 57 if (!channel_) |
| 58 return; |
53 | 59 |
54 HostMessage msg; | 60 HostMessage msg; |
55 msg.mutable_begin_update_stream(); | 61 msg.mutable_begin_update_stream(); |
56 DCHECK(msg.IsInitialized()); | 62 DCHECK(msg.IsInitialized()); |
57 | 63 |
58 scoped_refptr<DataBuffer> data = SerializeAndFrameMessage(msg); | 64 scoped_refptr<DataBuffer> data = SerializeAndFrameMessage(msg); |
59 DCHECK(!update_stream_size_); | 65 DCHECK(!update_stream_size_); |
60 update_stream_size_ += data->GetDataSize(); | 66 update_stream_size_ += data->GetDataSize(); |
61 channel_->Write(data); | 67 channel_->Write(data); |
62 } | 68 } |
63 | 69 |
64 void ClientConnection::SendUpdateStreamPacketMessage( | 70 void ClientConnection::SendUpdateStreamPacketMessage( |
65 const UpdateStreamPacketHeader* header, | 71 const UpdateStreamPacketHeader* header, |
66 scoped_refptr<DataBuffer> data) { | 72 scoped_refptr<DataBuffer> data) { |
67 DCHECK_EQ(loop_, MessageLoop::current()); | 73 DCHECK_EQ(loop_, MessageLoop::current()); |
68 DCHECK(channel_.get()); | 74 |
| 75 // If we are disconnected then return. |
| 76 if (!channel_) |
| 77 return; |
69 | 78 |
70 HostMessage msg; | 79 HostMessage msg; |
71 msg.mutable_update_stream_packet()->mutable_header()->CopyFrom(*header); | 80 msg.mutable_update_stream_packet()->mutable_header()->CopyFrom(*header); |
72 // TODO(hclam): This introduce one memory copy. Eliminate it. | 81 // TODO(hclam): This introduce one memory copy. Eliminate it. |
73 msg.mutable_update_stream_packet()->set_data( | 82 msg.mutable_update_stream_packet()->set_data( |
74 data->GetData(), data->GetDataSize()); | 83 data->GetData(), data->GetDataSize()); |
75 DCHECK(msg.IsInitialized()); | 84 DCHECK(msg.IsInitialized()); |
76 | 85 |
77 scoped_refptr<DataBuffer> encoded_data = SerializeAndFrameMessage(msg); | 86 scoped_refptr<DataBuffer> encoded_data = SerializeAndFrameMessage(msg); |
78 update_stream_size_ += data->GetDataSize(); | 87 update_stream_size_ += data->GetDataSize(); |
79 channel_->Write(encoded_data); | 88 channel_->Write(encoded_data); |
80 } | 89 } |
81 | 90 |
82 void ClientConnection::SendEndUpdateStreamMessage() { | 91 void ClientConnection::SendEndUpdateStreamMessage() { |
83 DCHECK_EQ(loop_, MessageLoop::current()); | 92 DCHECK_EQ(loop_, MessageLoop::current()); |
84 DCHECK(channel_.get()); | 93 |
| 94 // If we are disconnected then return. |
| 95 if (!channel_) |
| 96 return; |
85 | 97 |
86 HostMessage msg; | 98 HostMessage msg; |
87 msg.mutable_end_update_stream(); | 99 msg.mutable_end_update_stream(); |
88 DCHECK(msg.IsInitialized()); | 100 DCHECK(msg.IsInitialized()); |
89 | 101 |
90 scoped_refptr<DataBuffer> data = SerializeAndFrameMessage(msg); | 102 scoped_refptr<DataBuffer> data = SerializeAndFrameMessage(msg); |
91 update_stream_size_ += data->GetDataSize(); | 103 update_stream_size_ += data->GetDataSize(); |
92 channel_->Write(data); | 104 channel_->Write(data); |
93 | 105 |
94 // Here's some logic to help finding the average update stream size. | 106 // Here's some logic to help finding the average update stream size. |
(...skipping 14 matching lines...) Expand all Loading... |
109 return 0; | 121 return 0; |
110 int average_size = size_in_queue_ / size_queue_.size(); | 122 int average_size = size_in_queue_ / size_queue_.size(); |
111 if (!average_size) | 123 if (!average_size) |
112 return 0; | 124 return 0; |
113 return channel_->write_buffer_size() / average_size; | 125 return channel_->write_buffer_size() / average_size; |
114 } | 126 } |
115 | 127 |
116 void ClientConnection::Disconnect() { | 128 void ClientConnection::Disconnect() { |
117 DCHECK_EQ(loop_, MessageLoop::current()); | 129 DCHECK_EQ(loop_, MessageLoop::current()); |
118 | 130 |
119 DCHECK(channel_.get()); | 131 // If there is a channel then close it and release the reference. |
120 channel_->Close(); | 132 if (channel_) { |
| 133 channel_->Close(); |
| 134 channel_ = NULL; |
| 135 } |
121 } | 136 } |
122 | 137 |
123 void ClientConnection::OnStateChange(JingleChannel* channel, | 138 void ClientConnection::OnStateChange(JingleChannel* channel, |
124 JingleChannel::State state) { | 139 JingleChannel::State state) { |
125 DCHECK(channel); | 140 DCHECK(channel); |
126 loop_->PostTask(FROM_HERE, | 141 loop_->PostTask(FROM_HERE, |
127 NewRunnableMethod(this, &ClientConnection::StateChangeTask, state)); | 142 NewRunnableMethod(this, &ClientConnection::StateChangeTask, state)); |
128 } | 143 } |
129 | 144 |
130 void ClientConnection::OnPacketReceived(JingleChannel* channel, | 145 void ClientConnection::OnPacketReceived(JingleChannel* channel, |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 DCHECK(decoder_.get()); | 179 DCHECK(decoder_.get()); |
165 ClientMessageList list; | 180 ClientMessageList list; |
166 decoder_->ParseClientMessages(data, &list); | 181 decoder_->ParseClientMessages(data, &list); |
167 | 182 |
168 // Then submit the messages to the handler. | 183 // Then submit the messages to the handler. |
169 DCHECK(handler_); | 184 DCHECK(handler_); |
170 handler_->HandleMessages(this, &list); | 185 handler_->HandleMessages(this, &list); |
171 } | 186 } |
172 | 187 |
173 } // namespace remoting | 188 } // namespace remoting |
OLD | NEW |