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 30 matching lines...) Expand all Loading... |
41 MessageReader::~MessageReader() { | 41 MessageReader::~MessageReader() { |
42 } | 42 } |
43 | 43 |
44 void MessageReader::DoRead() { | 44 void MessageReader::DoRead() { |
45 DCHECK(CalledOnValidThread()); | 45 DCHECK(CalledOnValidThread()); |
46 // Don't try to read again if there is another read pending or we | 46 // Don't try to read again if there is another read pending or we |
47 // have messages that we haven't finished processing yet. | 47 // have messages that we haven't finished processing yet. |
48 while (!closed_ && !read_pending_ && pending_messages_ == 0) { | 48 while (!closed_ && !read_pending_ && pending_messages_ == 0) { |
49 read_buffer_ = new net::IOBuffer(kReadBufferSize); | 49 read_buffer_ = new net::IOBuffer(kReadBufferSize); |
50 int result = socket_->Read( | 50 int result = socket_->Read( |
51 read_buffer_, kReadBufferSize, | 51 read_buffer_.get(), |
| 52 kReadBufferSize, |
52 base::Bind(&MessageReader::OnRead, weak_factory_.GetWeakPtr())); | 53 base::Bind(&MessageReader::OnRead, weak_factory_.GetWeakPtr())); |
53 HandleReadResult(result); | 54 HandleReadResult(result); |
54 } | 55 } |
55 } | 56 } |
56 | 57 |
57 void MessageReader::OnRead(int result) { | 58 void MessageReader::OnRead(int result) { |
58 DCHECK(CalledOnValidThread()); | 59 DCHECK(CalledOnValidThread()); |
59 DCHECK(read_pending_); | 60 DCHECK(read_pending_); |
60 read_pending_ = false; | 61 read_pending_ = false; |
61 | 62 |
62 if (!closed_) { | 63 if (!closed_) { |
63 HandleReadResult(result); | 64 HandleReadResult(result); |
64 DoRead(); | 65 DoRead(); |
65 } | 66 } |
66 } | 67 } |
67 | 68 |
68 void MessageReader::HandleReadResult(int result) { | 69 void MessageReader::HandleReadResult(int result) { |
69 DCHECK(CalledOnValidThread()); | 70 DCHECK(CalledOnValidThread()); |
70 if (closed_) | 71 if (closed_) |
71 return; | 72 return; |
72 | 73 |
73 if (result > 0) { | 74 if (result > 0) { |
74 OnDataReceived(read_buffer_, result); | 75 OnDataReceived(read_buffer_.get(), result); |
75 } else if (result == net::ERR_IO_PENDING) { | 76 } else if (result == net::ERR_IO_PENDING) { |
76 read_pending_ = true; | 77 read_pending_ = true; |
77 } else { | 78 } else { |
78 if (result != net::ERR_CONNECTION_CLOSED) { | 79 if (result != net::ERR_CONNECTION_CLOSED) { |
79 LOG(ERROR) << "Read() returned error " << result; | 80 LOG(ERROR) << "Read() returned error " << result; |
80 } | 81 } |
81 // Stop reading after any error. | 82 // Stop reading after any error. |
82 closed_ = true; | 83 closed_ = true; |
83 } | 84 } |
84 } | 85 } |
(...skipping 27 matching lines...) Expand all Loading... |
112 DCHECK(CalledOnValidThread()); | 113 DCHECK(CalledOnValidThread()); |
113 pending_messages_--; | 114 pending_messages_--; |
114 DCHECK_GE(pending_messages_, 0); | 115 DCHECK_GE(pending_messages_, 0); |
115 | 116 |
116 // Start next read if necessary. | 117 // Start next read if necessary. |
117 DoRead(); | 118 DoRead(); |
118 } | 119 } |
119 | 120 |
120 } // namespace protocol | 121 } // namespace protocol |
121 } // namespace remoting | 122 } // namespace remoting |
OLD | NEW |