OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "mojo/services/network/tcp_connected_socket_impl.h" | 5 #include "mojo/services/network/tcp_connected_socket_impl.h" |
6 | 6 |
7 #include "base/message_loop/message_loop.h" | |
8 #include "mojo/services/network/net_adapters.h" | |
9 #include "net/base/net_errors.h" | |
10 | |
7 namespace mojo { | 11 namespace mojo { |
8 | 12 |
9 TCPConnectedSocketImpl::TCPConnectedSocketImpl( | 13 TCPConnectedSocketImpl::TCPConnectedSocketImpl( |
10 scoped_ptr<net::TCPSocket> socket, | 14 scoped_ptr<net::TCPSocket> socket, |
11 ScopedDataPipeConsumerHandle send_stream, | 15 ScopedDataPipeConsumerHandle send_stream, |
12 ScopedDataPipeProducerHandle receive_stream) | 16 ScopedDataPipeProducerHandle receive_stream) |
13 : socket_(socket.Pass()), | 17 : socket_(socket.Pass()), |
14 send_stream_(send_stream.Pass()), | 18 send_stream_(send_stream.Pass()), |
15 receive_stream_(receive_stream.Pass()) { | 19 receive_stream_(receive_stream.Pass()), |
20 weak_ptr_factory_(this) { | |
21 // Queue up async communication. | |
22 ReceiveMore(); | |
23 SendMore(); | |
16 } | 24 } |
17 | 25 |
18 TCPConnectedSocketImpl::~TCPConnectedSocketImpl() { | 26 TCPConnectedSocketImpl::~TCPConnectedSocketImpl() { |
19 } | 27 } |
20 | 28 |
29 void TCPConnectedSocketImpl::ReceiveMore() { | |
30 DCHECK(!pending_receive_.get()); | |
31 | |
32 uint32_t num_bytes; | |
33 MojoResult result = NetToMojoPendingBuffer::BeginWrite( | |
34 &receive_stream_, &pending_receive_, &num_bytes); | |
35 if (result == MOJO_RESULT_SHOULD_WAIT) { | |
36 // The pipe is full. We need to wait for it to have more space. | |
37 receive_handle_watcher_.Start( | |
38 receive_stream_.get(), | |
39 MOJO_HANDLE_SIGNAL_WRITABLE, MOJO_DEADLINE_INDEFINITE, | |
40 base::Bind(&TCPConnectedSocketImpl::OnReceiveStreamReady, | |
41 weak_ptr_factory_.GetWeakPtr())); | |
42 return; | |
43 } else if (result != MOJO_RESULT_OK) { | |
44 // The receive stream is in a bad state. | |
45 // TODO(darin): How should this be communicated to our client? | |
46 return; | |
47 } | |
48 | |
49 // Mojo is ready for the receive. | |
50 CHECK_GT(static_cast<uint32_t>(std::numeric_limits<int>::max()), num_bytes); | |
51 scoped_refptr<net::IOBuffer> buf( | |
52 new NetToMojoIOBuffer(pending_receive_.get())); | |
53 int read_result = socket_->Read( | |
54 buf.get(), static_cast<int>(num_bytes), | |
55 base::Bind(&TCPConnectedSocketImpl::DidReceive, base::Unretained(this), | |
56 false)); | |
57 if (read_result == net::ERR_IO_PENDING) { | |
58 // Pending I/O, wait for result in DidReceive(). | |
59 } else if (read_result >= 0) { | |
60 // Synchronous data ready. | |
61 DidReceive(true, read_result); | |
62 } else { | |
63 // Some kind of error. | |
64 } | |
65 } | |
66 | |
67 void TCPConnectedSocketImpl::OnReceiveStreamReady(MojoResult result) { | |
68 // TODO(darin): Handle a bad |result| value. | |
69 ReceiveMore(); | |
70 } | |
71 | |
72 void TCPConnectedSocketImpl::DidReceive(bool completed_synchronously, | |
73 int result) { | |
74 if (result < 0) { | |
75 // Error. | |
76 pending_receive_ = NULL; // Closes the pipe (owned by the pending write). | |
77 // TODO(brettw) notify the caller of an error? | |
78 return; | |
79 } | |
80 | |
81 receive_stream_ = pending_receive_->Complete(result); | |
82 pending_receive_ = NULL; | |
83 | |
84 // Schedule more reading. | |
85 if (completed_synchronously) { | |
86 // Don't recursively call ReceiveMore if this is a sync read. | |
87 base::MessageLoop::current()->PostTask( | |
88 FROM_HERE, | |
89 base::Bind(&TCPConnectedSocketImpl::ReceiveMore, | |
90 weak_ptr_factory_.GetWeakPtr())); | |
91 } else { | |
92 ReceiveMore(); | |
93 } | |
94 } | |
95 | |
96 void TCPConnectedSocketImpl::SendMore() { | |
97 uint32_t num_bytes = 0; | |
98 MojoResult result = MojoToNetPendingBuffer::BeginRead( | |
99 &send_stream_, &pending_send_, &num_bytes); | |
100 if (result == MOJO_RESULT_SHOULD_WAIT) { | |
101 // Data not ready, wait for it. | |
102 send_handle_watcher_.Start( | |
103 send_stream_.get(), | |
104 MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE, | |
105 base::Bind(&TCPConnectedSocketImpl::OnSendStreamReady, | |
106 weak_ptr_factory_.GetWeakPtr())); | |
107 return; | |
108 } else if (result != MOJO_RESULT_OK) { | |
109 // TODO(brettw) notify caller of error. | |
110 return; | |
111 } | |
112 | |
113 // Got a buffer from Mojo, give it to the socket. Note that the sockets may | |
114 // do partial writes. | |
115 scoped_refptr<net::IOBuffer> buf(new MojoToNetIOBuffer(pending_send_.get())); | |
116 int write_result = socket_->Write( | |
117 buf.get(), static_cast<int>(num_bytes), | |
118 base::Bind(&TCPConnectedSocketImpl::DidSend, base::Unretained(this), | |
119 false)); | |
120 if (write_result == net::ERR_IO_PENDING) { | |
121 // Pending I/O, wait for result in DidSend(). | |
122 } else if (write_result >= 0) { | |
123 // Synchronous data consumed. | |
124 DidSend(true, write_result); | |
125 } | |
126 } | |
127 | |
128 void TCPConnectedSocketImpl::OnSendStreamReady(MojoResult result) { | |
129 // TODO(brettw): Handle a bad |result| value. | |
130 SendMore(); | |
131 } | |
132 | |
133 void TCPConnectedSocketImpl::DidSend(bool completed_synchronously, | |
134 int result) { | |
135 if (result < 0) { | |
136 // TODO(brettw) report error. | |
yzshen1
2014/10/15 21:07:44
Do we want to close the pipe in this case? We don'
| |
137 return; | |
138 } | |
139 | |
140 // Take back ownership of the stream and free the IOBuffer. | |
141 send_stream_ = pending_send_->Complete(result); | |
142 pending_send_ = NULL; | |
143 | |
144 // Schedule more writing. | |
145 if (completed_synchronously) { | |
146 // Don't recursively call SendMore if this is a sync read. | |
147 base::MessageLoop::current()->PostTask( | |
148 FROM_HERE, | |
149 base::Bind(&TCPConnectedSocketImpl::SendMore, | |
150 weak_ptr_factory_.GetWeakPtr())); | |
151 } else { | |
152 SendMore(); | |
153 } | |
154 } | |
155 | |
21 } // namespace mojo | 156 } // namespace mojo |
OLD | NEW |