netTest.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "console/simBase.h"
  24. #include "platform/event.h"
  25. #include "sim/netConnection.h"
  26. #include "core/stream/bitStream.h"
  27. #include "sim/netObject.h"
  28. #include "console/engineAPI.h"
  29. class SimpleMessageEvent : public NetEvent
  30. {
  31. char *msg;
  32. public:
  33. typedef NetEvent Parent;
  34. SimpleMessageEvent(const char *message = NULL)
  35. {
  36. if(message)
  37. msg = dStrdup(message);
  38. else
  39. msg = NULL;
  40. }
  41. ~SimpleMessageEvent()
  42. { dFree(msg); }
  43. virtual void pack(NetConnection* /*ps*/, BitStream *bstream)
  44. { bstream->writeString(msg); }
  45. virtual void write(NetConnection*, BitStream *bstream)
  46. { bstream->writeString(msg); }
  47. virtual void unpack(NetConnection* /*ps*/, BitStream *bstream)
  48. { char buf[256]; bstream->readString(buf); msg = dStrdup(buf); }
  49. virtual void process(NetConnection *)
  50. { Con::printf("RMSG %d %s", mSourceId, msg); }
  51. DECLARE_CONOBJECT(SimpleMessageEvent);
  52. };
  53. IMPLEMENT_CO_NETEVENT_V1(SimpleMessageEvent);
  54. ConsoleDocClass( SimpleMessageEvent,
  55. "@brief A very simple example of a network event.\n\n"
  56. "This object exists purely for instructional purposes. It is primarily "
  57. "geared toward developers that wish to understand the inner-working of "
  58. "Torque 3D's networking system. This is not intended for actual game "
  59. "development.\n\n "
  60. "@see NetEvent for the inner workings of network events\n\n"
  61. "@ingroup Networking\n");
  62. DefineEngineStaticMethod( SimpleMessageEvent, msg, void, (NetConnection* con, const char* message),,
  63. "@brief Send a SimpleMessageEvent message to the specified connection.\n\n"
  64. "The far end that receives the message will print the message out to the console.\n"
  65. "@param con The unique ID of the connection to transmit to\n"
  66. "@param message The string containing the message to transmit\n\n"
  67. "@tsexample\n"
  68. "// Send a message to the other end of the given NetConnection\n"
  69. "SimpleMessageEvent::msg( %conn, \"A message from me!\");\n\n"
  70. "// The far end will see the following in the console\n"
  71. "// (Note: The number may be something other than 1796 as it is the SimObjectID\n"
  72. "// of the received event)\n"
  73. "// \n"
  74. "// RMSG 1796 A message from me!\n"
  75. "@endtsexample\n\n"
  76. )
  77. {
  78. //NetConnection *con = (NetConnection *) Sim::findObject(argv[1]);
  79. if(con)
  80. con->postNetEvent(new SimpleMessageEvent(message));
  81. }
  82. //ConsoleFunction( msg, void, 3, 3, "(NetConnection id, string message)"
  83. // "Send a SimpleNetObject message to the specified connection.")
  84. //{
  85. // NetConnection *con = (NetConnection *) Sim::findObject(argv[1]);
  86. // if(con)
  87. // con->postNetEvent(new SimpleMessageEvent(argv[2]));
  88. //}
  89. class SimpleNetObject : public NetObject
  90. {
  91. typedef NetObject Parent;
  92. public:
  93. char message[256];
  94. SimpleNetObject()
  95. {
  96. mNetFlags.set(ScopeAlways | Ghostable);
  97. dStrcpy(message, "Hello World!");
  98. }
  99. U32 packUpdate(NetConnection *conn, U32 mask, BitStream *stream)
  100. {
  101. stream->writeString(message);
  102. return 0;
  103. }
  104. void unpackUpdate(NetConnection *conn, BitStream *stream)
  105. {
  106. stream->readString(message);
  107. Con::printf("Got message: %s", message);
  108. }
  109. void setMessage(const char *msg)
  110. {
  111. setMaskBits(1);
  112. dStrcpy(message, msg);
  113. }
  114. DECLARE_CONOBJECT(SimpleNetObject);
  115. };
  116. IMPLEMENT_CO_NETOBJECT_V1(SimpleNetObject);
  117. ConsoleDocClass( SimpleNetObject,
  118. "@brief A very simple example of a class derived from NetObject.\n\n"
  119. "This object exists purely for instructional purposes. It is primarily "
  120. "geared toward developers that wish to understand the inner-working of "
  121. "Torque 3D's networking system. This is not intended for actual game "
  122. "development.\n\n "
  123. "@tsexample\n"
  124. "// On the server, create a new SimpleNetObject. This is a ghost always\n"
  125. "// object so it will be immediately ghosted to all connected clients.\n"
  126. "$s = new SimpleNetObject();\n\n"
  127. "// All connected clients will see the following in their console:\n"
  128. "// \n"
  129. "// Got message: Hello World!\n"
  130. "@endtsexample\n\n"
  131. "@see NetObject for a full breakdown of this example object\n"
  132. "@ingroup Networking\n");
  133. DefineEngineMethod( SimpleNetObject, setMessage, void, (const char* msg),,
  134. "@brief Sets the internal message variable.\n\n"
  135. "SimpleNetObject is set up to automatically transmit this new message to "
  136. "all connected clients. It will appear in the clients' console.\n"
  137. "@param msg The new message to send\n\n"
  138. "@tsexample\n"
  139. "// On the server, create a new SimpleNetObject. This is a ghost always\n"
  140. "// object so it will be immediately ghosted to all connected clients.\n"
  141. "$s = new SimpleNetObject();\n\n"
  142. "// All connected clients will see the following in their console:\n"
  143. "// \n"
  144. "// Got message: Hello World!\n\n"
  145. "// Now again on the server, change the message. This will cause it to\n"
  146. "// be sent to all connected clients.\n"
  147. "$s.setMessage(\"A new message from me!\");\n\n"
  148. "// All connected clients will now see in their console:\n"
  149. "// \n"
  150. "// Go message: A new message from me!\n"
  151. "@endtsexample\n\n"
  152. )
  153. {
  154. object->setMessage(msg);
  155. }
  156. //ConsoleMethod( SimpleNetObject, setMessage, void, 3, 3, "(string msg)")
  157. //{
  158. // object->setMessage(argv[2]);
  159. //}