Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(148)

Side by Side Diff: remoting/protocol/message_reader.cc

Issue 6271004: Changed MessageReader so that it doesn't read from the socket if there are (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: proper handling of empty messages Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/protocol/message_reader.h" 5 #include "remoting/protocol/message_reader.h"
6 6
7 #include "base/message_loop.h" 7 #include "base/message_loop.h"
8 #include "net/base/io_buffer.h" 8 #include "net/base/io_buffer.h"
9 #include "net/base/net_errors.h" 9 #include "net/base/net_errors.h"
10 #include "net/socket/socket.h" 10 #include "net/socket/socket.h"
11 #include "remoting/base/compound_buffer.h" 11 #include "remoting/base/compound_buffer.h"
12 #include "remoting/proto/internal.pb.h" 12 #include "remoting/proto/internal.pb.h"
13 13
14 namespace remoting { 14 namespace remoting {
15 namespace protocol { 15 namespace protocol {
16 16
17 static const int kReadBufferSize = 4096; 17 static const int kReadBufferSize = 4096;
18 18
19 class MessageReader::DoneTaskHandler
20 : public base::RefCountedThreadSafe<MessageReader::DoneTaskHandler> {
21 public:
22 DoneTaskHandler(MessageReader* message_reader)
23 : message_loop_(MessageLoop::current()),
24 message_reader_(message_reader) {
25 }
26
27 ~DoneTaskHandler() { }
28
29 void ReleaseReader() {
30 DCHECK_EQ(message_loop_, MessageLoop::current());
31 AutoLock auto_lock(lock_);
32 message_reader_ = NULL;
33 }
34
35 void OnDone(CompoundBuffer* buffer) {
awong 2011/01/20 20:06:38 2 thoughts: 1) I wonder if we shouldn't delete t
Sergey Ulanov 2011/01/20 21:55:57 It would be great if we could use WeakPtr here, bu
awong 2011/01/20 22:08:02 Good point. Though I think if you do the post bac
Sergey Ulanov 2011/01/21 03:45:05 Destructor may be called on a different thread, an
awong 2011/01/21 19:19:33 Something about this still does not feel right. I
36 delete buffer;
37 ProcessDoneEvent();
38 }
39
40 private:
41 void ProcessDoneEvent() {
42 if (MessageLoop::current() != message_loop_) {
43 message_loop_->PostTask(FROM_HERE, NewRunnableMethod(
44 this, &DoneTaskHandler::ProcessDoneEvent));
45 return;
46 }
47
48 AutoLock auto_lock(lock_);
49 if (message_reader_) {
50 message_reader_->OnMessageDone();
51 }
52 }
53
54 // Network message loop on which this object is created and which is
55 // used to call MessageReader::OnMessageDone().
56 MessageLoop* message_loop_;
57
58 // MessageReader that owns this object.
59 MessageReader* message_reader_;
60
61 // Must be locked whenever |message_reader_| is accessed
62 // (ReleaseReader() may be called on any thread).
63 Lock lock_;
64 };
65
19 MessageReader::MessageReader() 66 MessageReader::MessageReader()
20 : socket_(NULL), 67 : socket_(NULL),
68 read_pending_(false),
69 pending_messages_(0),
21 closed_(false), 70 closed_(false),
22 ALLOW_THIS_IN_INITIALIZER_LIST( 71 ALLOW_THIS_IN_INITIALIZER_LIST(
23 read_callback_(this, &MessageReader::OnRead)) { 72 read_callback_(this, &MessageReader::OnRead)) {
24 } 73 }
25 74
26 MessageReader::~MessageReader() { 75 MessageReader::~MessageReader() {
76 done_task_handler_->ReleaseReader();
27 } 77 }
28 78
29 void MessageReader::Init(net::Socket* socket, 79 void MessageReader::Init(net::Socket* socket,
30 MessageReceivedCallback* callback) { 80 MessageReceivedCallback* callback) {
31 message_received_callback_.reset(callback); 81 message_received_callback_.reset(callback);
32 DCHECK(socket); 82 DCHECK(socket);
33 socket_ = socket; 83 socket_ = socket;
84 done_task_handler_ = new DoneTaskHandler(this);
34 DoRead(); 85 DoRead();
35 } 86 }
36 87
37 void MessageReader::DoRead() { 88 void MessageReader::DoRead() {
38 while (!closed_) { 89 // Don't try to read again if there is another read pending or we
90 // have messages that we haven't finished processing yet.
91 while (!closed_ && !read_pending_ && pending_messages_ == 0) {
Alpha Left Google 2011/01/20 20:54:52 The flag of |read_pending| is confusing to me. Acc
Sergey Ulanov 2011/01/20 21:55:57 It is here mainly for the second iteration, after
39 read_buffer_ = new net::IOBuffer(kReadBufferSize); 92 read_buffer_ = new net::IOBuffer(kReadBufferSize);
40 int result = socket_->Read( 93 int result = socket_->Read(
41 read_buffer_, kReadBufferSize, &read_callback_); 94 read_buffer_, kReadBufferSize, &read_callback_);
42 HandleReadResult(result); 95 HandleReadResult(result);
43 if (result < 0)
44 break;
45 } 96 }
46 } 97 }
47 98
48 void MessageReader::OnRead(int result) { 99 void MessageReader::OnRead(int result) {
100 DCHECK(read_pending_);
101 read_pending_ = false;
102
49 if (!closed_) { 103 if (!closed_) {
50 HandleReadResult(result); 104 HandleReadResult(result);
51 DoRead(); 105 DoRead();
52 } 106 }
53 } 107 }
54 108
55 void MessageReader::HandleReadResult(int result) { 109 void MessageReader::HandleReadResult(int result) {
56 if (result > 0) { 110 if (result > 0) {
57 OnDataReceived(read_buffer_, result); 111 OnDataReceived(read_buffer_, result);
58 } else { 112 } else {
59 if (result == net::ERR_CONNECTION_CLOSED) { 113 if (result == net::ERR_CONNECTION_CLOSED) {
60 closed_ = true; 114 closed_ = true;
61 } else if (result != net::ERR_IO_PENDING) { 115 } else if (result == net::ERR_IO_PENDING) {
116 read_pending_ = true;
117 } else {
62 LOG(ERROR) << "Read() returned error " << result; 118 LOG(ERROR) << "Read() returned error " << result;
63 } 119 }
64 } 120 }
65 } 121 }
66 122
67 void MessageReader::OnDataReceived(net::IOBuffer* data, int data_size) { 123 void MessageReader::OnDataReceived(net::IOBuffer* data, int data_size) {
68 message_decoder_.AddData(data, data_size); 124 message_decoder_.AddData(data, data_size);
69 125
70 while (true) { 126 while (true) {
71 CompoundBuffer buffer; 127 CompoundBuffer* buffer = message_decoder_.GetNextMessage();
72 if (!message_decoder_.GetNextMessage(&buffer)) 128 if (!buffer)
73 break; 129 break;
74 130
75 message_received_callback_->Run(&buffer); 131 pending_messages_++;
Alpha Left Google 2011/01/20 20:54:52 I think there's a bug here. Incrementing here will
Sergey Ulanov 2011/01/20 21:55:57 Good catch! Will also add unittest for this case.
132 message_received_callback_->Run(buffer, NewRunnableMethod(
133 done_task_handler_.get(), &DoneTaskHandler::OnDone, buffer));
76 } 134 }
77 } 135 }
78 136
137 void MessageReader::OnMessageDone() {
138 pending_messages_--;
139 DCHECK_GE(pending_messages_, 0);
140
141 DoRead(); // Start next read if neccessary.
142 }
143
79 } // namespace protocol 144 } // namespace protocol
80 } // namespace remoting 145 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698