main_ppapi.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. #include <cstdio>
  11. #include <string>
  12. #include "ppapi/cpp/instance.h"
  13. #include "ppapi/cpp/module.h"
  14. #include "ppapi/cpp/var.h"
  15. // KMJ
  16. #include "ppapi/c/private/ppb_udp_socket_private.h"
  17. #include "RakPeerInterface.h"
  18. #include "RakNetTypes.h"
  19. #include "RakSleep.h"
  20. using namespace RakNet;
  21. /// The Instance class. One of these exists for each instance of your NaCl
  22. /// module on the web page. The browser will ask the Module object to create
  23. /// a new Instance for each occurence of the <embed> tag that has these
  24. /// attributes:
  25. /// type="application/x-nacl"
  26. /// src="hello_tutorial.nmf"
  27. /// To communicate with the browser, you must override HandleMessage() for
  28. /// receiving messages from the browser, and use PostMessage() to send messages
  29. /// back to the browser. Note that this interface is asynchronous.
  30. class ConnectivityTestInstance : public pp::Instance {
  31. RakPeerInterface *rakPeer;
  32. public:
  33. /// The constructor creates the plugin-side instance.
  34. /// @param[in] instance the handle to the browser-side plugin instance.
  35. explicit ConnectivityTestInstance(PP_Instance instance) : pp::Instance(instance)
  36. {
  37. // PostMessage(pp::Var("ConnectivityTestInstance constructor"));
  38. /*
  39. RakNet::ConnectionAttemptResult car = rakPeer->Connect("127.0.0.1", 1234, "Rumpelstiltskin", (int) strlen("Rumpelstiltskin"));
  40. RakAssert(car==RakNet::CONNECTION_ATTEMPT_STARTED);
  41. RakSleep(100);
  42. RakNet::Packet* p;
  43. for (p=rakPeer->Receive(); p; rakPeer->DeallocatePacket(p), p=rakPeer->Receive())
  44. {}
  45. */
  46. }
  47. virtual ~ConnectivityTestInstance() {}
  48. /// Handler for messages coming in from the browser via postMessage(). The
  49. /// @a var_message can contain anything: a JSON string; a string that encodes
  50. /// method names and arguments; etc. For example, you could use
  51. /// JSON.stringify in the browser to create a message that contains a method
  52. /// name and some parameters, something like this:
  53. /// var json_message = JSON.stringify({ "myMethod" : "3.14159" });
  54. /// nacl_module.postMessage(json_message);
  55. /// On receipt of this message in @a var_message, you could parse the JSON to
  56. /// retrieve the method name, match it to a function call, and then call it
  57. /// with the parameter.
  58. /// @param[in] var_message The message posted by the browser.
  59. virtual void HandleMessage(const pp::Var& var_message) {
  60. /*
  61. if (!var_message.is_string())
  62. return;
  63. std::string message = var_message.AsString();
  64. pp::Var var_reply;
  65. if (message == kHelloString) {
  66. var_reply = pp::Var(kReplyString);
  67. PostMessage(var_reply);
  68. }
  69. */
  70. // Just some do nothing code so I can set a breakpoint;
  71. rakPeer=0;
  72. /*
  73. rakPeer=RakNet::RakPeerInterface::GetInstance();
  74. RakNet::SocketDescriptor socketDescriptor(0,0);
  75. socketDescriptor.socketFamily=AF_INET;
  76. StartupResult sr = rakPeer->Startup(8,&socketDescriptor, 1);
  77. if (sr==RakNet::RAKNET_STARTED)
  78. {
  79. PostMessage(pp::Var("RAKNET_STARTED"));
  80. }
  81. else
  82. {
  83. PostMessage(pp::Var("RakNet did not start"));
  84. }
  85. RakNet::ConnectionAttemptResult car = rakPeer->Connect("127.0.0.1", 1234, "Rumpelstiltskin", (int) strlen("Rumpelstiltskin"));
  86. RakAssert(car==RakNet::CONNECTION_ATTEMPT_STARTED);
  87. RakSleep(0);
  88. RakNet::Packet* p;
  89. for (p=rakPeer->Receive(); p; rakPeer->DeallocatePacket(p), p=rakPeer->Receive())
  90. {}
  91. */
  92. }
  93. };
  94. /// The Module class. The browser calls the CreateInstance() method to create
  95. /// an instance of your NaCl module on the web page. The browser creates a new
  96. /// instance for each <embed> tag with type="application/x-nacl".
  97. class ConnectivityTestModule : public pp::Module {
  98. public:
  99. ConnectivityTestModule() : pp::Module() {}
  100. virtual ~ConnectivityTestModule() {}
  101. /// Create and return a ConnectivityTestInstance object.
  102. /// @param[in] instance The browser-side instance.
  103. /// @return the plugin-side instance.
  104. virtual pp::Instance* CreateInstance(PP_Instance instance) {
  105. return new ConnectivityTestInstance(instance);
  106. }
  107. };
  108. namespace pp {
  109. /// Factory function called by the browser when the module is first loaded.
  110. /// The browser keeps a singleton of this module. It calls the
  111. /// CreateInstance() method on the object you return to make instances. There
  112. /// is one instance per <embed> tag on the page. This is the main binding
  113. /// point for your NaCl module with the browser.
  114. Module* CreateModule() {
  115. return new ConnectivityTestModule();
  116. }
  117. } // namespace pp
粤ICP备19079148号