Index: native_client_sdk/src/examples/api/vpn_provider/vpn_provider_helper.cc |
diff --git a/native_client_sdk/src/examples/api/vpn_provider/vpn_provider_helper.cc b/native_client_sdk/src/examples/api/vpn_provider/vpn_provider_helper.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..1cdbcd80bd5caafdb51f86b3b634081b6027099c |
--- /dev/null |
+++ b/native_client_sdk/src/examples/api/vpn_provider/vpn_provider_helper.cc |
@@ -0,0 +1,123 @@ |
+// Copyright 2016 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 "vpn_provider_helper.h" |
+ |
+#include <sstream> |
+ |
+#include "ppapi/cpp/var_dictionary.h" |
+ |
+VpnProviderHelper::VpnProviderHelper(pp::Instance* instance) |
+ : instance_(instance), |
+ thread_(pp::InstanceHandle(instance)), |
+ vpn_(pp::InstanceHandle(instance)), |
+ factory_(this), |
+ send_packet_pending_(false) { |
+ thread_.Start(); |
+} |
+ |
+void VpnProviderHelper::Init() { |
+ thread_.message_loop().PostWork( |
+ factory_.NewCallback(&VpnProviderHelper::InitOnThread)); |
+} |
+ |
+void VpnProviderHelper::Bind(const std::string& config, |
+ const std::string& name) { |
+ thread_.message_loop().PostWork( |
+ factory_.NewCallback(&VpnProviderHelper::BindOnThread, config, config)); |
binji
2016/07/18 18:46:28
should be "...config, name"?
adrian.belgun
2016/07/19 13:57:22
Done. It worked as written because currently the c
|
+} |
+ |
+void VpnProviderHelper::SendPacket(const pp::Var& data) { |
+ thread_.message_loop().PostWork( |
+ factory_.NewCallback(&VpnProviderHelper::SendPacketOnThread, data)); |
+} |
+ |
+void VpnProviderHelper::InitOnThread(int32_t result) { |
+ instance_->PostMessage("NaCl: VpnProviderHelper::InitOnThread()"); |
+ |
+ // Initial Callaback registration |
emaxx
2016/07/18 13:46:58
nit: s/Callaback/callback/
adrian.belgun
2016/07/19 13:57:22
Done.
|
+ vpn_.ReceivePacket(factory_.NewCallbackWithOutput( |
+ &VpnProviderHelper::ReceivePacketCompletionCallback)); |
+} |
+ |
+void VpnProviderHelper::BindOnThread(int32_t result, |
+ const std::string& config, |
+ const std::string& name) { |
+ instance_->PostMessage("NaCl: VpnProviderHelper::BindOnThread()"); |
+ |
+ vpn_.Bind(config, name, |
+ factory_.NewCallback(&VpnProviderHelper::BindCompletionCallback)); |
+} |
+ |
+void VpnProviderHelper::SendPacketOnThread(int32_t result, |
+ const pp::Var& data) { |
+ if (!send_packet_pending_) { |
+ send_packet_pending_ = true; |
+ vpn_.SendPacket( |
+ data, |
+ factory_.NewCallback(&VpnProviderHelper::SendPacketCompletionCallback)); |
+ } else { |
+ std::stringstream ss; |
+ ss << "NaCl: VpnProviderHelper::SendPacketOnThread: " |
+ << "Queueing Packet"; |
+ instance_->PostMessage(ss.str()); |
+ |
+ send_packet_queue_.push(data); |
+ } |
+} |
+ |
+void VpnProviderHelper::BindCompletionCallback(int32_t result) { |
+ std::stringstream ss; |
+ ss << "NaCl: VpnProviderHelper::BindCompletionCallback(" << result << ")"; |
+ instance_->PostMessage(ss.str()); |
+ |
+ pp::VarDictionary dict; |
+ dict.Set("cmd", "bindSucces"); |
emaxx
2016/07/18 13:46:58
nit: s/bindSucces/bindSuccess/
(I'm wondering how
adrian.belgun
2016/07/19 13:57:22
Done.
|
+ instance_->PostMessage(dict); |
+} |
+ |
+void VpnProviderHelper::ReceivePacketCompletionCallback(int32_t result, |
+ const pp::Var& packet) { |
+ std::stringstream ss; |
+ ss << "NaCl: VpnProviderHelper::ReceivePacketCompletionCallback(" << result |
+ << ")"; |
+ instance_->PostMessage(ss.str()); |
+ |
+ /* This is the place where the developer would unpack the packet from the |
+ * pp:Var and send it the their implementation. Example code below. |
+ * |
+ * pp::VarDictionary message; |
+ * message.Set(pp::Var("operation"), pp::Var("write")); |
emaxx
2016/07/18 13:46:58
nit: not sure whether you put conversions to pp::V
adrian.belgun
2016/07/19 13:57:22
Done. Initially there were for clarity, removed th
|
+ * message.Set(pp::Var("payload"), pp::Var(packet)); |
+ * SendMessageToTun((struct PP_Var*)&message.pp_var()); |
+ */ |
+ |
+ // Re-register callback |
+ if (result == PP_OK) { |
+ vpn_.ReceivePacket(factory_.NewCallbackWithOutput( |
+ &VpnProviderHelper::ReceivePacketCompletionCallback)); |
+ } |
+} |
+ |
+void VpnProviderHelper::SendPacketCompletionCallback(int32_t result) { |
+ std::stringstream ss; |
+ ss << "NaCl: VpnProviderHelper::SendPacketCompletionCallback(" << result |
+ << ")"; |
+ instance_->PostMessage(ss.str()); |
+ |
+ // If we have queued packets send them before accepting new ones |
+ if (!send_packet_queue_.empty()) { |
+ std::stringstream ss; |
+ ss << "NaCl: VpnProviderHelper::SendPacketCompletionCallback: " |
+ << "Sending queued packed."; |
+ instance_->PostMessage(ss.str()); |
+ |
+ vpn_.SendPacket( |
+ send_packet_queue_.front(), |
+ factory_.NewCallback(&VpnProviderHelper::SendPacketCompletionCallback)); |
+ send_packet_queue_.pop(); |
+ } else { |
+ send_packet_pending_ = false; |
+ } |
+} |