| 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 "ppapi/shared_impl/private/udp_socket_private_impl.h" | 5 #include "ppapi/shared_impl/private/udp_socket_private_impl.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 | 10 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 | 34 |
| 35 UDPSocketPrivateImpl::~UDPSocketPrivateImpl() { | 35 UDPSocketPrivateImpl::~UDPSocketPrivateImpl() { |
| 36 } | 36 } |
| 37 | 37 |
| 38 thunk::PPB_UDPSocket_Private_API* | 38 thunk::PPB_UDPSocket_Private_API* |
| 39 UDPSocketPrivateImpl::AsPPB_UDPSocket_Private_API() { | 39 UDPSocketPrivateImpl::AsPPB_UDPSocket_Private_API() { |
| 40 return this; | 40 return this; |
| 41 } | 41 } |
| 42 | 42 |
| 43 int32_t UDPSocketPrivateImpl::Bind(const PP_NetAddress_Private* addr, | 43 int32_t UDPSocketPrivateImpl::Bind(const PP_NetAddress_Private* addr, |
| 44 PP_CompletionCallback callback) { | 44 ApiCallbackType callback) { |
| 45 if (!addr || !callback.func) | 45 if (!addr) |
| 46 return PP_ERROR_BADARGUMENT; | 46 return PP_ERROR_BADARGUMENT; |
| 47 if (bound_ || closed_) | 47 if (bound_ || closed_) |
| 48 return PP_ERROR_FAILED; | 48 return PP_ERROR_FAILED; |
| 49 if (TrackedCallback::IsPending(bind_callback_)) | 49 if (TrackedCallback::IsPending(bind_callback_)) |
| 50 return PP_ERROR_INPROGRESS; | 50 return PP_ERROR_INPROGRESS; |
| 51 // TODO(dmichael): use some other strategy for determining if an | |
| 52 // operation is in progress | |
| 53 | 51 |
| 54 bind_callback_ = new TrackedCallback(this, callback); | 52 bind_callback_ = callback; |
| 55 | 53 |
| 56 // Send the request, the browser will call us back via BindACK. | 54 // Send the request, the browser will call us back via BindACK. |
| 57 SendBind(*addr); | 55 SendBind(*addr); |
| 58 return PP_OK_COMPLETIONPENDING; | 56 return PP_OK_COMPLETIONPENDING; |
| 59 } | 57 } |
| 60 | 58 |
| 61 PP_Bool UDPSocketPrivateImpl::GetBoundAddress(PP_NetAddress_Private* addr) { | 59 PP_Bool UDPSocketPrivateImpl::GetBoundAddress(PP_NetAddress_Private* addr) { |
| 62 if (!addr || !bound_ || closed_) | 60 if (!addr || !bound_ || closed_) |
| 63 return PP_FALSE; | 61 return PP_FALSE; |
| 64 | 62 |
| 65 *addr = bound_addr_; | 63 *addr = bound_addr_; |
| 66 return PP_TRUE; | 64 return PP_TRUE; |
| 67 } | 65 } |
| 68 | 66 |
| 69 int32_t UDPSocketPrivateImpl::RecvFrom(char* buffer, | 67 int32_t UDPSocketPrivateImpl::RecvFrom(char* buffer, |
| 70 int32_t num_bytes, | 68 int32_t num_bytes, |
| 71 PP_CompletionCallback callback) { | 69 ApiCallbackType callback) { |
| 72 if (!buffer || num_bytes <= 0 || !callback.func) | 70 if (!buffer || num_bytes <= 0) |
| 73 return PP_ERROR_BADARGUMENT; | 71 return PP_ERROR_BADARGUMENT; |
| 74 if (!bound_) | 72 if (!bound_) |
| 75 return PP_ERROR_FAILED; | 73 return PP_ERROR_FAILED; |
| 76 if (TrackedCallback::IsPending(recvfrom_callback_)) | 74 if (TrackedCallback::IsPending(recvfrom_callback_)) |
| 77 return PP_ERROR_INPROGRESS; | 75 return PP_ERROR_INPROGRESS; |
| 78 | 76 |
| 79 read_buffer_ = buffer; | 77 read_buffer_ = buffer; |
| 80 bytes_to_read_ = std::min(num_bytes, kMaxReadSize); | 78 bytes_to_read_ = std::min(num_bytes, kMaxReadSize); |
| 81 recvfrom_callback_ = new TrackedCallback(this, callback); | 79 recvfrom_callback_ = callback; |
| 82 | 80 |
| 83 // Send the request, the browser will call us back via RecvFromACK. | 81 // Send the request, the browser will call us back via RecvFromACK. |
| 84 SendRecvFrom(bytes_to_read_); | 82 SendRecvFrom(bytes_to_read_); |
| 85 return PP_OK_COMPLETIONPENDING; | 83 return PP_OK_COMPLETIONPENDING; |
| 86 } | 84 } |
| 87 | 85 |
| 88 PP_Bool UDPSocketPrivateImpl::GetRecvFromAddress(PP_NetAddress_Private* addr) { | 86 PP_Bool UDPSocketPrivateImpl::GetRecvFromAddress(PP_NetAddress_Private* addr) { |
| 89 if (!addr) | 87 if (!addr) |
| 90 return PP_FALSE; | 88 return PP_FALSE; |
| 91 | 89 |
| 92 *addr = recvfrom_addr_; | 90 *addr = recvfrom_addr_; |
| 93 return PP_TRUE; | 91 return PP_TRUE; |
| 94 } | 92 } |
| 95 | 93 |
| 96 int32_t UDPSocketPrivateImpl::SendTo(const char* buffer, | 94 int32_t UDPSocketPrivateImpl::SendTo(const char* buffer, |
| 97 int32_t num_bytes, | 95 int32_t num_bytes, |
| 98 const PP_NetAddress_Private* addr, | 96 const PP_NetAddress_Private* addr, |
| 99 PP_CompletionCallback callback) { | 97 ApiCallbackType callback) { |
| 100 if (!buffer || num_bytes <= 0 || !addr || !callback.func) | 98 if (!buffer || num_bytes <= 0 || !addr) |
| 101 return PP_ERROR_BADARGUMENT; | 99 return PP_ERROR_BADARGUMENT; |
| 102 if (!bound_) | 100 if (!bound_) |
| 103 return PP_ERROR_FAILED; | 101 return PP_ERROR_FAILED; |
| 104 if (TrackedCallback::IsPending(sendto_callback_)) | 102 if (TrackedCallback::IsPending(sendto_callback_)) |
| 105 return PP_ERROR_INPROGRESS; | 103 return PP_ERROR_INPROGRESS; |
| 106 | 104 |
| 107 if (num_bytes > kMaxWriteSize) | 105 if (num_bytes > kMaxWriteSize) |
| 108 num_bytes = kMaxWriteSize; | 106 num_bytes = kMaxWriteSize; |
| 109 | 107 |
| 110 sendto_callback_ = new TrackedCallback(this, callback); | 108 sendto_callback_ = callback; |
| 111 | 109 |
| 112 // Send the request, the browser will call us back via SendToACK. | 110 // Send the request, the browser will call us back via SendToACK. |
| 113 SendSendTo(std::string(buffer, num_bytes), *addr); | 111 SendSendTo(std::string(buffer, num_bytes), *addr); |
| 114 return PP_OK_COMPLETIONPENDING; | 112 return PP_OK_COMPLETIONPENDING; |
| 115 } | 113 } |
| 116 | 114 |
| 117 void UDPSocketPrivateImpl::Close() { | 115 void UDPSocketPrivateImpl::Close() { |
| 118 if(closed_) | 116 if(closed_) |
| 119 return; | 117 return; |
| 120 | 118 |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 arraysize(bound_addr_.data) * sizeof(*bound_addr_.data)); | 195 arraysize(bound_addr_.data) * sizeof(*bound_addr_.data)); |
| 198 } | 196 } |
| 199 | 197 |
| 200 void UDPSocketPrivateImpl::PostAbortIfNecessary( | 198 void UDPSocketPrivateImpl::PostAbortIfNecessary( |
| 201 scoped_refptr<TrackedCallback>* callback) { | 199 scoped_refptr<TrackedCallback>* callback) { |
| 202 if (callback->get()) | 200 if (callback->get()) |
| 203 (*callback)->PostAbort(); | 201 (*callback)->PostAbort(); |
| 204 } | 202 } |
| 205 | 203 |
| 206 } // namespace ppapi | 204 } // namespace ppapi |
| OLD | NEW |