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

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

Issue 2757723002: Update ICE protocol to handle closed channel (Closed)
Patch Set: . Created 3 years, 9 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
« no previous file with comments | « remoting/protocol/stream_message_pipe_adapter.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/stream_message_pipe_adapter.h" 5 #include "remoting/protocol/stream_message_pipe_adapter.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/callback_helpers.h" 10 #include "base/callback_helpers.h"
11 #include "base/memory/ptr_util.h" 11 #include "base/memory/ptr_util.h"
12 #include "net/base/net_errors.h" 12 #include "net/base/net_errors.h"
13 #include "remoting/base/buffered_socket_writer.h" 13 #include "remoting/base/buffered_socket_writer.h"
14 #include "remoting/base/compound_buffer.h" 14 #include "remoting/base/compound_buffer.h"
15 #include "remoting/protocol/message_serialization.h" 15 #include "remoting/protocol/message_serialization.h"
16 #include "remoting/protocol/p2p_stream_socket.h" 16 #include "remoting/protocol/p2p_stream_socket.h"
17 #include "remoting/protocol/stream_channel_factory.h" 17 #include "remoting/protocol/stream_channel_factory.h"
18 18
19 namespace remoting { 19 namespace remoting {
20 namespace protocol { 20 namespace protocol {
21 21
22 StreamMessagePipeAdapter::StreamMessagePipeAdapter( 22 StreamMessagePipeAdapter::StreamMessagePipeAdapter(
23 std::unique_ptr<P2PStreamSocket> socket, 23 std::unique_ptr<P2PStreamSocket> socket,
24 const ErrorCallback& error_callback) 24 const ErrorCallback& error_callback)
25 : socket_(std::move(socket)), 25 : socket_(std::move(socket)), error_callback_(error_callback) {
26 error_callback_(error_callback),
27 writer_(new BufferedSocketWriter()) {
28 DCHECK(socket_); 26 DCHECK(socket_);
29 DCHECK(!error_callback_.is_null()); 27 DCHECK(error_callback_);
30
31 writer_->Start(
32 base::Bind(&P2PStreamSocket::Write, base::Unretained(socket_.get())),
33 base::Bind(&StreamMessagePipeAdapter::CloseOnError,
34 base::Unretained(this)));
35 } 28 }
36 29
37 StreamMessagePipeAdapter::~StreamMessagePipeAdapter() {} 30 StreamMessagePipeAdapter::~StreamMessagePipeAdapter() {}
38 31
39 void StreamMessagePipeAdapter::Start(EventHandler* event_handler) { 32 void StreamMessagePipeAdapter::Start(EventHandler* event_handler) {
40 reader_.StartReading(socket_.get(), 33 DCHECK(event_handler);
41 base::Bind(&EventHandler::OnMessageReceived, 34 event_handler_ = event_handler;
42 base::Unretained(event_handler)), 35
43 base::Bind(&StreamMessagePipeAdapter::CloseOnError, 36 writer_ = base::MakeUnique<BufferedSocketWriter>();
44 base::Unretained(this))); 37 writer_->Start(
45 event_handler->OnMessagePipeOpen(); 38 base::Bind(&P2PStreamSocket::Write, base::Unretained(socket_.get())),
39 base::Bind(&StreamMessagePipeAdapter::CloseOnError,
40 base::Unretained(this)));
41
42 reader_ = base::MakeUnique<MessageReader>();
43 reader_->StartReading(socket_.get(),
44 base::Bind(&EventHandler::OnMessageReceived,
45 base::Unretained(event_handler_)),
46 base::Bind(&StreamMessagePipeAdapter::CloseOnError,
47 base::Unretained(this)));
48
49 event_handler_->OnMessagePipeOpen();
46 } 50 }
47 51
48 void StreamMessagePipeAdapter::Send(google::protobuf::MessageLite* message, 52 void StreamMessagePipeAdapter::Send(google::protobuf::MessageLite* message,
49 const base::Closure& done) { 53 const base::Closure& done) {
50 if (writer_) 54 if (writer_)
51 writer_->Write(SerializeAndFrameMessage(*message), done); 55 writer_->Write(SerializeAndFrameMessage(*message), done);
52 } 56 }
53 57
54 void StreamMessagePipeAdapter::CloseOnError(int error) { 58 void StreamMessagePipeAdapter::CloseOnError(int error) {
55 // Stop writing on error. 59 // Stop reading and writing on error.
56 writer_.reset(); 60 writer_.reset();
61 reader_.reset();
57 62
58 if (!error_callback_.is_null()) 63 if (error == 0) {
64 event_handler_->OnMessagePipeClosed();
65 } else if (error_callback_) {
59 base::ResetAndReturn(&error_callback_).Run(error); 66 base::ResetAndReturn(&error_callback_).Run(error);
67 }
60 } 68 }
61 69
62 StreamMessageChannelFactoryAdapter::StreamMessageChannelFactoryAdapter( 70 StreamMessageChannelFactoryAdapter::StreamMessageChannelFactoryAdapter(
63 StreamChannelFactory* stream_channel_factory, 71 StreamChannelFactory* stream_channel_factory,
64 const ErrorCallback& error_callback) 72 const ErrorCallback& error_callback)
65 : stream_channel_factory_(stream_channel_factory), 73 : stream_channel_factory_(stream_channel_factory),
66 error_callback_(error_callback) {} 74 error_callback_(error_callback) {}
67 75
68 StreamMessageChannelFactoryAdapter::~StreamMessageChannelFactoryAdapter() {} 76 StreamMessageChannelFactoryAdapter::~StreamMessageChannelFactoryAdapter() {}
69 77
(...skipping 16 matching lines...) Expand all
86 if (!socket) { 94 if (!socket) {
87 error_callback_.Run(net::ERR_FAILED); 95 error_callback_.Run(net::ERR_FAILED);
88 return; 96 return;
89 } 97 }
90 callback.Run(base::MakeUnique<StreamMessagePipeAdapter>(std::move(socket), 98 callback.Run(base::MakeUnique<StreamMessagePipeAdapter>(std::move(socket),
91 error_callback_)); 99 error_callback_));
92 } 100 }
93 101
94 } // namespace protocol 102 } // namespace protocol
95 } // namespace remoting 103 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/protocol/stream_message_pipe_adapter.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698