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 "chrome/browser/extensions/api/socket/udp_socket.h" | 5 #include "chrome/browser/extensions/api/socket/udp_socket.h" |
6 | 6 |
7 #include "chrome/browser/extensions/api/api_resource.h" | 7 #include "chrome/browser/extensions/api/api_resource.h" |
8 #include "chrome/browser/extensions/api/api_resource_event_notifier.h" | 8 #include "chrome/browser/extensions/api/api_resource_event_notifier.h" |
9 #include "net/base/ip_endpoint.h" | 9 #include "net/base/ip_endpoint.h" |
10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 io_buffer = new net::IOBuffer(count); | 88 io_buffer = new net::IOBuffer(count); |
89 result = socket_.Read(io_buffer.get(), count, | 89 result = socket_.Read(io_buffer.get(), count, |
90 base::Bind(&UDPSocket::OnReadComplete, base::Unretained(this), | 90 base::Bind(&UDPSocket::OnReadComplete, base::Unretained(this), |
91 io_buffer)); | 91 io_buffer)); |
92 } while (false); | 92 } while (false); |
93 | 93 |
94 if (result != net::ERR_IO_PENDING) | 94 if (result != net::ERR_IO_PENDING) |
95 OnReadComplete(io_buffer, result); | 95 OnReadComplete(io_buffer, result); |
96 } | 96 } |
97 | 97 |
98 void UDPSocket::Write(scoped_refptr<net::IOBuffer> io_buffer, | 98 int UDPSocket::WriteImpl(net::IOBuffer* io_buffer, |
99 int byte_count, | 99 int io_buffer_size, |
100 const CompletionCallback& callback) { | 100 const net::CompletionCallback& callback) { |
101 DCHECK(!callback.is_null()); | 101 if (!socket_.is_connected()) |
102 | 102 return net::ERR_SOCKET_NOT_CONNECTED; |
103 if (!write_callback_.is_null()) { | 103 else |
104 // TODO(penghuang): Put requests in a pending queue to support multiple | 104 return socket_.Write(io_buffer, io_buffer_size, callback); |
105 // write calls. | |
106 callback.Run(net::ERR_IO_PENDING); | |
107 return; | |
108 } else { | |
109 write_callback_ = callback; | |
110 } | |
111 | |
112 int result = net::ERR_FAILED; | |
113 do { | |
114 if (!socket_.is_connected()) { | |
115 result = net::ERR_SOCKET_NOT_CONNECTED; | |
116 break; | |
117 } | |
118 | |
119 result = socket_.Write( | |
120 io_buffer.get(), byte_count, | |
121 base::Bind(&UDPSocket::OnWriteComplete, base::Unretained(this))); | |
122 } while (false); | |
123 | |
124 if (result != net::ERR_IO_PENDING) | |
125 OnWriteComplete(result); | |
126 } | 105 } |
127 | 106 |
128 void UDPSocket::RecvFrom(int count, | 107 void UDPSocket::RecvFrom(int count, |
129 const RecvFromCompletionCallback& callback) { | 108 const RecvFromCompletionCallback& callback) { |
130 DCHECK(!callback.is_null()); | 109 DCHECK(!callback.is_null()); |
131 | 110 |
132 if (!recv_from_callback_.is_null()) { | 111 if (!recv_from_callback_.is_null()) { |
133 callback.Run(net::ERR_IO_PENDING, NULL, NULL, 0); | 112 callback.Run(net::ERR_IO_PENDING, NULL, NULL, 0); |
134 return; | 113 return; |
135 } else { | 114 } else { |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 OnSendToComplete(result); | 185 OnSendToComplete(result); |
207 } | 186 } |
208 | 187 |
209 void UDPSocket::OnReadComplete(scoped_refptr<net::IOBuffer> io_buffer, | 188 void UDPSocket::OnReadComplete(scoped_refptr<net::IOBuffer> io_buffer, |
210 int result) { | 189 int result) { |
211 DCHECK(!read_callback_.is_null()); | 190 DCHECK(!read_callback_.is_null()); |
212 read_callback_.Run(result, io_buffer); | 191 read_callback_.Run(result, io_buffer); |
213 read_callback_.Reset(); | 192 read_callback_.Reset(); |
214 } | 193 } |
215 | 194 |
216 void UDPSocket::OnWriteComplete(int result) { | |
217 DCHECK(!write_callback_.is_null()); | |
218 write_callback_.Run(result); | |
219 write_callback_.Reset(); | |
220 } | |
221 | |
222 void UDPSocket::OnRecvFromComplete(scoped_refptr<net::IOBuffer> io_buffer, | 195 void UDPSocket::OnRecvFromComplete(scoped_refptr<net::IOBuffer> io_buffer, |
223 scoped_refptr<IPEndPoint> address, | 196 scoped_refptr<IPEndPoint> address, |
224 int result) { | 197 int result) { |
225 DCHECK(!recv_from_callback_.is_null()); | 198 DCHECK(!recv_from_callback_.is_null()); |
226 std::string ip; | 199 std::string ip; |
227 int port = 0; | 200 int port = 0; |
228 if (result > 0 && address.get()) { | 201 if (result > 0 && address.get()) { |
229 IPEndPointToStringAndPort(address->data, &ip, &port); | 202 IPEndPointToStringAndPort(address->data, &ip, &port); |
230 } | 203 } |
231 recv_from_callback_.Run(result, io_buffer, ip, port); | 204 recv_from_callback_.Run(result, io_buffer, ip, port); |
232 recv_from_callback_.Reset(); | 205 recv_from_callback_.Reset(); |
233 } | 206 } |
234 | 207 |
235 void UDPSocket::OnSendToComplete(int result) { | 208 void UDPSocket::OnSendToComplete(int result) { |
236 DCHECK(!send_to_callback_.is_null()); | 209 DCHECK(!send_to_callback_.is_null()); |
237 send_to_callback_.Run(result); | 210 send_to_callback_.Run(result); |
238 send_to_callback_.Reset(); | 211 send_to_callback_.Reset(); |
239 } | 212 } |
240 | 213 |
241 } // namespace extensions | 214 } // namespace extensions |
OLD | NEW |