| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "ppapi/proxy/udp_socket_resource_base.h" | 5 #include "ppapi/proxy/udp_socket_resource_base.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cstring> | 8 #include <cstring> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 namespace ppapi { | 21 namespace ppapi { |
| 22 namespace proxy { | 22 namespace proxy { |
| 23 | 23 |
| 24 const int32_t UDPSocketResourceBase::kMaxReadSize = 128 * 1024; | 24 const int32_t UDPSocketResourceBase::kMaxReadSize = 128 * 1024; |
| 25 const int32_t UDPSocketResourceBase::kMaxWriteSize = 128 * 1024; | 25 const int32_t UDPSocketResourceBase::kMaxWriteSize = 128 * 1024; |
| 26 const int32_t UDPSocketResourceBase::kMaxSendBufferSize = | 26 const int32_t UDPSocketResourceBase::kMaxSendBufferSize = |
| 27 1024 * UDPSocketResourceBase::kMaxWriteSize; | 27 1024 * UDPSocketResourceBase::kMaxWriteSize; |
| 28 const int32_t UDPSocketResourceBase::kMaxReceiveBufferSize = | 28 const int32_t UDPSocketResourceBase::kMaxReceiveBufferSize = |
| 29 1024 * UDPSocketResourceBase::kMaxReadSize; | 29 1024 * UDPSocketResourceBase::kMaxReadSize; |
| 30 const size_t UDPSocketResourceBase::kPluginReceiveBufferSlots = 32u; | 30 const size_t UDPSocketResourceBase::kPluginReceiveBufferSlots = 32u; |
| 31 const size_t UDPSocketResourceBase::kPluginSendBufferSlots = 8u; |
| 31 | 32 |
| 32 UDPSocketResourceBase::UDPSocketResourceBase(Connection connection, | 33 UDPSocketResourceBase::UDPSocketResourceBase(Connection connection, |
| 33 PP_Instance instance, | 34 PP_Instance instance, |
| 34 bool private_api) | 35 bool private_api) |
| 35 : PluginResource(connection, instance), | 36 : PluginResource(connection, instance), |
| 36 private_api_(private_api), | 37 private_api_(private_api), |
| 37 bound_(false), | 38 bound_(false), |
| 38 closed_(false), | 39 closed_(false), |
| 39 read_buffer_(NULL), | 40 read_buffer_(NULL), |
| 40 bytes_to_read_(-1), | 41 bytes_to_read_(-1), |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 | 178 |
| 178 int32_t UDPSocketResourceBase::SendToImpl( | 179 int32_t UDPSocketResourceBase::SendToImpl( |
| 179 const char* buffer, | 180 const char* buffer, |
| 180 int32_t num_bytes, | 181 int32_t num_bytes, |
| 181 const PP_NetAddress_Private* addr, | 182 const PP_NetAddress_Private* addr, |
| 182 scoped_refptr<TrackedCallback> callback) { | 183 scoped_refptr<TrackedCallback> callback) { |
| 183 if (!buffer || num_bytes <= 0 || !addr) | 184 if (!buffer || num_bytes <= 0 || !addr) |
| 184 return PP_ERROR_BADARGUMENT; | 185 return PP_ERROR_BADARGUMENT; |
| 185 if (!bound_) | 186 if (!bound_) |
| 186 return PP_ERROR_FAILED; | 187 return PP_ERROR_FAILED; |
| 187 if (TrackedCallback::IsPending(sendto_callback_)) | 188 if (sendto_callbacks_.size() == kPluginSendBufferSlots) |
| 188 return PP_ERROR_INPROGRESS; | 189 return PP_ERROR_INPROGRESS; |
| 189 | 190 |
| 190 if (num_bytes > kMaxWriteSize) | 191 if (num_bytes > kMaxWriteSize) |
| 191 num_bytes = kMaxWriteSize; | 192 num_bytes = kMaxWriteSize; |
| 192 | 193 |
| 193 sendto_callback_ = callback; | 194 sendto_callbacks_.push(callback); |
| 194 | 195 |
| 195 // Send the request, the browser will call us back via SendToReply. | 196 // Send the request, the browser will call us back via SendToReply. |
| 196 Call<PpapiPluginMsg_UDPSocket_SendToReply>( | 197 Call<PpapiPluginMsg_UDPSocket_SendToReply>( |
| 197 BROWSER, | 198 BROWSER, |
| 198 PpapiHostMsg_UDPSocket_SendTo(std::string(buffer, num_bytes), *addr), | 199 PpapiHostMsg_UDPSocket_SendTo(std::string(buffer, num_bytes), *addr), |
| 199 base::Bind(&UDPSocketResourceBase::OnPluginMsgSendToReply, | 200 base::Bind(&UDPSocketResourceBase::OnPluginMsgSendToReply, |
| 200 base::Unretained(this)), | 201 base::Unretained(this)), |
| 201 callback); | 202 callback); |
| 202 return PP_OK_COMPLETIONPENDING; | 203 return PP_OK_COMPLETIONPENDING; |
| 203 } | 204 } |
| 204 | 205 |
| 205 void UDPSocketResourceBase::CloseImpl() { | 206 void UDPSocketResourceBase::CloseImpl() { |
| 206 if(closed_) | 207 if(closed_) |
| 207 return; | 208 return; |
| 208 | 209 |
| 209 bound_ = false; | 210 bound_ = false; |
| 210 closed_ = true; | 211 closed_ = true; |
| 211 | 212 |
| 212 Post(BROWSER, PpapiHostMsg_UDPSocket_Close()); | 213 Post(BROWSER, PpapiHostMsg_UDPSocket_Close()); |
| 213 | 214 |
| 214 PostAbortIfNecessary(&bind_callback_); | 215 PostAbortIfNecessary(&bind_callback_); |
| 215 PostAbortIfNecessary(&recvfrom_callback_); | 216 PostAbortIfNecessary(&recvfrom_callback_); |
| 216 PostAbortIfNecessary(&sendto_callback_); | 217 while (!sendto_callbacks_.empty()) { |
| 218 scoped_refptr<TrackedCallback> callback = sendto_callbacks_.front(); |
| 219 sendto_callbacks_.pop(); |
| 220 PostAbortIfNecessary(&callback); |
| 221 } |
| 217 | 222 |
| 218 read_buffer_ = NULL; | 223 read_buffer_ = NULL; |
| 219 bytes_to_read_ = -1; | 224 bytes_to_read_ = -1; |
| 220 } | 225 } |
| 221 | 226 |
| 222 void UDPSocketResourceBase::OnReplyReceived( | 227 void UDPSocketResourceBase::OnReplyReceived( |
| 223 const ResourceMessageReplyParams& params, | 228 const ResourceMessageReplyParams& params, |
| 224 const IPC::Message& msg) { | 229 const IPC::Message& msg) { |
| 225 PPAPI_BEGIN_MESSAGE_MAP(UDPSocketResourceBase, msg) | 230 PPAPI_BEGIN_MESSAGE_MAP(UDPSocketResourceBase, msg) |
| 226 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL( | 231 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL( |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 read_buffer_ = NULL; | 304 read_buffer_ = NULL; |
| 300 bytes_to_read_ = -1; | 305 bytes_to_read_ = -1; |
| 301 recvfrom_addr_resource_ = NULL; | 306 recvfrom_addr_resource_ = NULL; |
| 302 | 307 |
| 303 RunCallback(recvfrom_callback_, result); | 308 RunCallback(recvfrom_callback_, result); |
| 304 } | 309 } |
| 305 | 310 |
| 306 void UDPSocketResourceBase::OnPluginMsgSendToReply( | 311 void UDPSocketResourceBase::OnPluginMsgSendToReply( |
| 307 const ResourceMessageReplyParams& params, | 312 const ResourceMessageReplyParams& params, |
| 308 int32_t bytes_written) { | 313 int32_t bytes_written) { |
| 309 if (!TrackedCallback::IsPending(sendto_callback_)) | 314 scoped_refptr<TrackedCallback> callback = sendto_callbacks_.front(); |
| 315 sendto_callbacks_.pop(); |
| 316 if (!TrackedCallback::IsPending(callback)) |
| 310 return; | 317 return; |
| 311 | 318 |
| 312 if (params.result() == PP_OK) | 319 if (params.result() == PP_OK) |
| 313 RunCallback(sendto_callback_, bytes_written); | 320 RunCallback(callback, bytes_written); |
| 314 else | 321 else |
| 315 RunCallback(sendto_callback_, params.result()); | 322 RunCallback(callback, params.result()); |
| 316 } | 323 } |
| 317 | 324 |
| 318 void UDPSocketResourceBase::RunCallback(scoped_refptr<TrackedCallback> callback, | 325 void UDPSocketResourceBase::RunCallback(scoped_refptr<TrackedCallback> callback, |
| 319 int32_t pp_result) { | 326 int32_t pp_result) { |
| 320 callback->Run(ConvertNetworkAPIErrorForCompatibility(pp_result, | 327 callback->Run(ConvertNetworkAPIErrorForCompatibility(pp_result, |
| 321 private_api_)); | 328 private_api_)); |
| 322 } | 329 } |
| 323 | 330 |
| 324 int32_t UDPSocketResourceBase::SetRecvFromOutput( | 331 int32_t UDPSocketResourceBase::SetRecvFromOutput( |
| 325 int32_t browser_result, | 332 int32_t browser_result, |
| (...skipping 18 matching lines...) Expand all Loading... |
| 344 if (result == PP_OK && !data.empty()) | 351 if (result == PP_OK && !data.empty()) |
| 345 memcpy(output_buffer, data.c_str(), data.size()); | 352 memcpy(output_buffer, data.c_str(), data.size()); |
| 346 | 353 |
| 347 recvfrom_addr_ = addr; | 354 recvfrom_addr_ = addr; |
| 348 | 355 |
| 349 return result == PP_OK ? static_cast<int32_t>(data.size()) : result; | 356 return result == PP_OK ? static_cast<int32_t>(data.size()) : result; |
| 350 } | 357 } |
| 351 | 358 |
| 352 } // namespace proxy | 359 } // namespace proxy |
| 353 } // namespace ppapi | 360 } // namespace ppapi |
| OLD | NEW |