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

Unified Diff: ppapi/shared_impl/udp_socket_impl.cc

Issue 8688002: PPB_TCPSocket_Private/PPB_UDPSocket_Private are exposed to Browser (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Deleted NaCl tests. Created 9 years, 1 month 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 side-by-side diff with in-line comments
Download patch
Index: ppapi/shared_impl/udp_socket_impl.cc
diff --git a/ppapi/shared_impl/udp_socket_impl.cc b/ppapi/shared_impl/udp_socket_impl.cc
new file mode 100644
index 0000000000000000000000000000000000000000..9088294e217203aedbe0d759fd8a5109b64a5de9
--- /dev/null
+++ b/ppapi/shared_impl/udp_socket_impl.cc
@@ -0,0 +1,186 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "ppapi/shared_impl/udp_socket_impl.h"
+
+#include "base/logging.h"
+#include "ppapi/c/pp_errors.h"
+
+namespace ppapi {
+
+const int32_t UDPSocketImpl::kMaxReadSize = 1024 * 1024;
+const int32_t UDPSocketImpl::kMaxWriteSize = 1024 * 1024;
+
+UDPSocketImpl::UDPSocketImpl(const HostResource& resource, uint32 socket_id)
+ : Resource(resource) {
+ Init(socket_id);
+}
+
+UDPSocketImpl::UDPSocketImpl(PP_Instance instance, uint32 socket_id)
+ : Resource(instance) {
+ Init(socket_id);
+}
+
+UDPSocketImpl::~UDPSocketImpl() {
+ Close();
yzshen1 2011/11/28 20:59:16 You shouldn't call it here. (See comment in ~TCPSo
ygorshenin 2011/11/29 18:30:09 Done.
+}
+
+void UDPSocketImpl::Init(uint32 socket_id) {
yzshen1 2011/11/28 20:59:16 Sort the methods, please.
ygorshenin 2011/11/29 18:30:09 Done.
+ DCHECK(socket_id != 0);
+ socket_id_ = socket_id;
+ binded_ = false;
+ closed_ = false;
+ bind_callback_ = PP_BlockUntilComplete();
+ recvfrom_callback_ = PP_BlockUntilComplete();
+ sendto_callback_ = PP_BlockUntilComplete();
+ read_buffer_ = NULL;
+ bytes_to_read_ = -1;
+
+ recvfrom_addr_.size = 0;
+ memset(recvfrom_addr_.data, 0, sizeof(recvfrom_addr_.data));
dmichael (off chromium) 2011/11/28 20:38:43 sizeof->arraysize
yzshen1 2011/11/28 20:59:16 Add include, please.
ygorshenin 2011/11/29 18:30:09 Done.
ygorshenin 2011/11/29 18:30:09 Done.
+}
+
+thunk::PPB_UDPSocket_Private_API*
+UDPSocketImpl::AsPPB_UDPSocket_Private_API() {
+ return this;
+}
+
+int32_t UDPSocketImpl::Bind(const PP_NetAddress_Private* addr,
+ PP_CompletionCallback callback) {
+ if (!addr || !callback.func)
+ return PP_ERROR_BADARGUMENT;
+ if (binded_ || closed_)
+ return PP_ERROR_FAILED;
+ if (bind_callback_.func)
dmichael (off chromium) 2011/11/28 20:38:43 Here, too, I think it would be better to use some
ygorshenin 2011/11/29 18:30:09 Added TODO comment.
+ return PP_ERROR_INPROGRESS;
+
+ bind_callback_ = callback;
+
+ // Send the request, the browser will call us back via BindACK.
+ SendBind(*addr);
+ return PP_OK_COMPLETIONPENDING;
+}
+
+int32_t UDPSocketImpl::RecvFrom(char* buffer,
+ int32_t num_bytes,
+ PP_CompletionCallback callback) {
+ if (!buffer || num_bytes <= 0 || !callback.func)
+ return PP_ERROR_BADARGUMENT;
+ if (!binded_)
+ return PP_ERROR_FAILED;
+ if (recvfrom_callback_.func)
+ return PP_ERROR_INPROGRESS;
+
+ read_buffer_ = buffer;
+ bytes_to_read_ = std::min(num_bytes, kMaxReadSize);
yzshen1 2011/11/28 20:59:16 add include.
ygorshenin 2011/11/29 18:30:09 Done.
+ recvfrom_callback_ = callback;
+
+ // Send the request, the browser will call us back via RecvFromACK.
+ SendRecvFrom(bytes_to_read_);
+ return PP_OK_COMPLETIONPENDING;
+}
+
+PP_Bool UDPSocketImpl::GetRecvFromAddress(PP_NetAddress_Private* addr) {
+ if (!addr)
+ return PP_FALSE;
+
+ *addr = recvfrom_addr_;
+ return PP_TRUE;
+}
+
+int32_t UDPSocketImpl::SendTo(const char* buffer,
+ int32_t num_bytes,
+ const PP_NetAddress_Private* addr,
+ PP_CompletionCallback callback) {
+ if (!buffer || num_bytes <= 0 || !addr || !callback.func)
+ return PP_ERROR_BADARGUMENT;
+ if (!binded_)
+ return PP_ERROR_FAILED;
+ if (sendto_callback_.func)
+ return PP_ERROR_INPROGRESS;
+
+ if (num_bytes > kMaxWriteSize)
+ num_bytes = kMaxWriteSize;
+
+ sendto_callback_ = callback;
+
+ // Send the request, the browser will call us back via SendToACK.
+ SendSendTo(std::string(buffer, num_bytes), *addr);
+ return PP_OK_COMPLETIONPENDING;
+}
+
+void UDPSocketImpl::Close() {
+ if(closed_)
+ return;
+
+ binded_ = false;
+ closed_ = true;
+
+ SendClose();
+
+ socket_id_ = 0;
+
+ PostAbortAndClearIfNecessary(&bind_callback_);
+ PostAbortAndClearIfNecessary(&recvfrom_callback_);
+ PostAbortAndClearIfNecessary(&sendto_callback_);
+}
+
+void UDPSocketImpl::OnBindCompleted(bool succeeded) {
+ if (!bind_callback_.func) {
+ NOTREACHED();
+ return;
+ }
+
+ if (succeeded)
+ binded_ = true;
+
+ PP_RunAndClearCompletionCallback(&bind_callback_,
+ succeeded ? PP_OK : PP_ERROR_FAILED);
+}
+
+void UDPSocketImpl::OnRecvFromCompleted(bool succeeded,
+ const std::string& data,
+ const PP_NetAddress_Private& addr) {
+ if (!recvfrom_callback_.func || !read_buffer_) {
+ NOTREACHED();
+ return;
+ }
+
+ if (succeeded) {
+ CHECK_LE(static_cast<int32_t>(data.size()), bytes_to_read_);
+ if (!data.empty())
+ memcpy(read_buffer_, data.c_str(), data.size());
+ }
+ read_buffer_ = NULL;
+ bytes_to_read_ = -1;
+ recvfrom_addr_ = addr;
+
+ PP_RunAndClearCompletionCallback(
+ &recvfrom_callback_,
+ succeeded ? static_cast<int32_t>(data.size()) :
+ static_cast<int32_t>(PP_ERROR_FAILED));
+}
+
+void UDPSocketImpl::OnSendToCompleted(bool succeeded, int32_t bytes_written) {
+ if (!sendto_callback_.func) {
+ NOTREACHED();
+ return;
+ }
+
+ PP_RunAndClearCompletionCallback(
+ &sendto_callback_,
+ succeeded ? bytes_written : static_cast<int32_t>(PP_ERROR_FAILED));
+}
+
+void UDPSocketImpl::PostAbortAndClearIfNecessary(
+ PP_CompletionCallback* callback) {
+ DCHECK(callback);
+
+ if (callback->func) {
+ PostAbort(*callback);
+ *callback = PP_BlockUntilComplete();
+ }
+}
+
+} // namespace ppapi

Powered by Google App Engine
This is Rietveld 408576698