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

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

Issue 8824006: Migrate net/socket/socket.h, net/socket/stream_socket.h to base::Bind(). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebased Created 9 years 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
« no previous file with comments | « remoting/protocol/message_reader.h ('k') | remoting/protocol/pepper_stream_channel.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/location.h" 9 #include "base/location.h"
10 #include "net/base/io_buffer.h" 10 #include "net/base/io_buffer.h"
11 #include "net/base/net_errors.h" 11 #include "net/base/net_errors.h"
12 #include "net/socket/socket.h" 12 #include "net/socket/socket.h"
13 #include "remoting/base/compound_buffer.h" 13 #include "remoting/base/compound_buffer.h"
14 #include "remoting/proto/internal.pb.h" 14 #include "remoting/proto/internal.pb.h"
15 15
16 namespace remoting { 16 namespace remoting {
17 namespace protocol { 17 namespace protocol {
18 18
19 static const int kReadBufferSize = 4096; 19 static const int kReadBufferSize = 4096;
20 20
21 MessageReader::MessageReader() 21 MessageReader::MessageReader()
22 : socket_(NULL), 22 : socket_(NULL),
23 read_pending_(false), 23 read_pending_(false),
24 pending_messages_(0), 24 pending_messages_(0),
25 closed_(false), 25 closed_(false) {
26 ALLOW_THIS_IN_INITIALIZER_LIST(
27 read_callback_(this, &MessageReader::OnRead)) {
28 } 26 }
29 27
30 MessageReader::~MessageReader() { 28 MessageReader::~MessageReader() {
31 CHECK_EQ(pending_messages_, 0); 29 CHECK_EQ(pending_messages_, 0);
32 } 30 }
33 31
34 void MessageReader::Init(net::Socket* socket, 32 void MessageReader::Init(net::Socket* socket,
35 const MessageReceivedCallback& callback) { 33 const MessageReceivedCallback& callback) {
36 message_received_callback_ = callback; 34 message_received_callback_ = callback;
37 DCHECK(socket); 35 DCHECK(socket);
38 socket_ = socket; 36 socket_ = socket;
39 DoRead(); 37 DoRead();
40 } 38 }
41 39
42 void MessageReader::DoRead() { 40 void MessageReader::DoRead() {
43 // Don't try to read again if there is another read pending or we 41 // Don't try to read again if there is another read pending or we
44 // have messages that we haven't finished processing yet. 42 // have messages that we haven't finished processing yet.
45 while (!closed_ && !read_pending_ && pending_messages_ == 0) { 43 while (!closed_ && !read_pending_ && pending_messages_ == 0) {
46 read_buffer_ = new net::IOBuffer(kReadBufferSize); 44 read_buffer_ = new net::IOBuffer(kReadBufferSize);
47 int result = socket_->Read( 45 int result = socket_->Read(
48 read_buffer_, kReadBufferSize, &read_callback_); 46 read_buffer_, kReadBufferSize, base::Bind(&MessageReader::OnRead,
47 base::Unretained(this)));
49 HandleReadResult(result); 48 HandleReadResult(result);
50 } 49 }
51 } 50 }
52 51
53 void MessageReader::OnRead(int result) { 52 void MessageReader::OnRead(int result) {
54 DCHECK(read_pending_); 53 DCHECK(read_pending_);
55 read_pending_ = false; 54 read_pending_ = false;
56 55
57 if (!closed_) { 56 if (!closed_) {
58 HandleReadResult(result); 57 HandleReadResult(result);
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 116
118 void MessageReader::ProcessDoneEvent() { 117 void MessageReader::ProcessDoneEvent() {
119 pending_messages_--; 118 pending_messages_--;
120 DCHECK_GE(pending_messages_, 0); 119 DCHECK_GE(pending_messages_, 0);
121 120
122 DoRead(); // Start next read if neccessary. 121 DoRead(); // Start next read if neccessary.
123 } 122 }
124 123
125 } // namespace protocol 124 } // namespace protocol
126 } // namespace remoting 125 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/protocol/message_reader.h ('k') | remoting/protocol/pepper_stream_channel.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698