OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/protocol/message_reader.h" | 5 #include "remoting/protocol/message_reader.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
(...skipping 20 matching lines...) Expand all Loading... |
31 | 31 |
32 MessageReader::~MessageReader() { | 32 MessageReader::~MessageReader() { |
33 } | 33 } |
34 | 34 |
35 void MessageReader::SetMessageReceivedCallback( | 35 void MessageReader::SetMessageReceivedCallback( |
36 const MessageReceivedCallback& callback) { | 36 const MessageReceivedCallback& callback) { |
37 DCHECK(CalledOnValidThread()); | 37 DCHECK(CalledOnValidThread()); |
38 message_received_callback_ = callback; | 38 message_received_callback_ = callback; |
39 } | 39 } |
40 | 40 |
41 void MessageReader::StartReading(net::Socket* socket) { | 41 void MessageReader::StartReading( |
| 42 net::Socket* socket, |
| 43 const ReadFailedCallback& read_failed_callback) { |
42 DCHECK(CalledOnValidThread()); | 44 DCHECK(CalledOnValidThread()); |
43 DCHECK(socket); | 45 DCHECK(socket); |
| 46 DCHECK(!read_failed_callback.is_null()); |
| 47 |
44 socket_ = socket; | 48 socket_ = socket; |
| 49 read_failed_callback_ = read_failed_callback; |
45 DoRead(); | 50 DoRead(); |
46 } | 51 } |
47 | 52 |
48 void MessageReader::DoRead() { | 53 void MessageReader::DoRead() { |
49 DCHECK(CalledOnValidThread()); | 54 DCHECK(CalledOnValidThread()); |
50 // Don't try to read again if there is another read pending or we | 55 // Don't try to read again if there is another read pending or we |
51 // have messages that we haven't finished processing yet. | 56 // have messages that we haven't finished processing yet. |
52 while (!closed_ && !read_pending_ && pending_messages_ == 0) { | 57 bool read_succeeded = true; |
| 58 while (read_succeeded && !closed_ && !read_pending_ && |
| 59 pending_messages_ == 0) { |
53 read_buffer_ = new net::IOBuffer(kReadBufferSize); | 60 read_buffer_ = new net::IOBuffer(kReadBufferSize); |
54 int result = socket_->Read( | 61 int result = socket_->Read( |
55 read_buffer_.get(), | 62 read_buffer_.get(), |
56 kReadBufferSize, | 63 kReadBufferSize, |
57 base::Bind(&MessageReader::OnRead, weak_factory_.GetWeakPtr())); | 64 base::Bind(&MessageReader::OnRead, weak_factory_.GetWeakPtr())); |
58 HandleReadResult(result); | 65 |
| 66 HandleReadResult(result, &read_succeeded); |
59 } | 67 } |
60 } | 68 } |
61 | 69 |
62 void MessageReader::OnRead(int result) { | 70 void MessageReader::OnRead(int result) { |
63 DCHECK(CalledOnValidThread()); | 71 DCHECK(CalledOnValidThread()); |
64 DCHECK(read_pending_); | 72 DCHECK(read_pending_); |
65 read_pending_ = false; | 73 read_pending_ = false; |
66 | 74 |
67 if (!closed_) { | 75 if (!closed_) { |
68 HandleReadResult(result); | 76 bool read_succeeded; |
69 DoRead(); | 77 HandleReadResult(result, &read_succeeded); |
| 78 if (read_succeeded) |
| 79 DoRead(); |
70 } | 80 } |
71 } | 81 } |
72 | 82 |
73 void MessageReader::HandleReadResult(int result) { | 83 void MessageReader::HandleReadResult(int result, bool* read_succeeded) { |
74 DCHECK(CalledOnValidThread()); | 84 DCHECK(CalledOnValidThread()); |
75 if (closed_) | 85 if (closed_) |
76 return; | 86 return; |
77 | 87 |
| 88 *read_succeeded = true; |
| 89 |
78 if (result > 0) { | 90 if (result > 0) { |
79 OnDataReceived(read_buffer_.get(), result); | 91 OnDataReceived(read_buffer_.get(), result); |
| 92 *read_succeeded = true; |
80 } else if (result == net::ERR_IO_PENDING) { | 93 } else if (result == net::ERR_IO_PENDING) { |
81 read_pending_ = true; | 94 read_pending_ = true; |
82 } else { | 95 } else { |
83 if (result != net::ERR_CONNECTION_CLOSED) { | 96 DCHECK_LT(result, 0); |
84 LOG(ERROR) << "Read() returned error " << result; | 97 |
85 } | |
86 // Stop reading after any error. | 98 // Stop reading after any error. |
87 closed_ = true; | 99 closed_ = true; |
| 100 *read_succeeded = false; |
| 101 |
| 102 LOG(ERROR) << "Read() returned error " << result; |
| 103 read_failed_callback_.Run(result); |
88 } | 104 } |
89 } | 105 } |
90 | 106 |
91 void MessageReader::OnDataReceived(net::IOBuffer* data, int data_size) { | 107 void MessageReader::OnDataReceived(net::IOBuffer* data, int data_size) { |
92 DCHECK(CalledOnValidThread()); | 108 DCHECK(CalledOnValidThread()); |
93 message_decoder_.AddData(data, data_size); | 109 message_decoder_.AddData(data, data_size); |
94 | 110 |
95 // Get list of all new messages first, and then call the callback | 111 // Get list of all new messages first, and then call the callback |
96 // for all of them. | 112 // for all of them. |
97 while (true) { | 113 while (true) { |
(...skipping 21 matching lines...) Expand all Loading... |
119 DCHECK(CalledOnValidThread()); | 135 DCHECK(CalledOnValidThread()); |
120 pending_messages_--; | 136 pending_messages_--; |
121 DCHECK_GE(pending_messages_, 0); | 137 DCHECK_GE(pending_messages_, 0); |
122 | 138 |
123 // Start next read if necessary. | 139 // Start next read if necessary. |
124 DoRead(); | 140 DoRead(); |
125 } | 141 } |
126 | 142 |
127 } // namespace protocol | 143 } // namespace protocol |
128 } // namespace remoting | 144 } // namespace remoting |
OLD | NEW |