netExamples.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 "sim/netConnection.h"
  25. #include "core/stream/bitStream.h"
  26. #include "sim/netObject.h"
  27. #include "console/engineAPI.h"
  28. class SimpleMessageEvent : public NetEvent
  29. {
  30. char *msg;
  31. public:
  32. typedef NetEvent Parent;
  33. SimpleMessageEvent(const char *message = NULL)
  34. {
  35. if(message)
  36. msg = dStrdup(message);
  37. else
  38. msg = NULL;
  39. }
  40. ~SimpleMessageEvent()
  41. { dFree(msg); }
  42. virtual void pack(NetConnection* /*ps*/, BitStream *bstream)
  43. { bstream->writeString(msg); }
  44. virtual void write(NetConnection*, BitStream *bstream)
  45. { bstream->writeString(msg); }
  46. virtual void unpack(NetConnection* /*ps*/, BitStream *bstream)
  47. { char buf[256]; bstream->readString(buf); msg = dStrdup(buf); }
  48. virtual void process(NetConnection *)
  49. { Con::printf("RMSG %d %s", mSourceId, msg); }
  50. DECLARE_CONOBJECT(SimpleMessageEvent);
  51. };
  52. IMPLEMENT_CO_NETEVENT_V1(SimpleMessageEvent);
  53. ConsoleDocClass( SimpleMessageEvent,
  54. "@brief A very simple example of a network event.\n\n"
  55. "This object exists purely for instructional purposes. It is primarily "
  56. "geared toward developers that wish to understand the inner-working of "
  57. "Torque 3D's networking system. This is not intended for actual game "
  58. "development.\n\n "
  59. "@see NetEvent for the inner workings of network events\n\n"
  60. "@ingroup Networking\n");
  61. DefineEngineStaticMethod( SimpleMessageEvent, msg, void, (NetConnection* con, const char* message),,
  62. "@brief Send a SimpleMessageEvent message to the specified connection.\n\n"
  63. "The far end that receives the message will print the message out to the console.\n"
  64. "@param con The unique ID of the connection to transmit to\n"
  65. "@param message The string containing the message to transmit\n\n"
  66. "@tsexample\n"
  67. "// Send a message to the other end of the given NetConnection\n"
  68. "SimpleMessageEvent::msg( %conn, \"A message from me!\");\n\n"
  69. "// The far end will see the following in the console\n"
  70. "// (Note: The number may be something other than 1796 as it is the SimObjectID\n"
  71. "// of the received event)\n"
  72. "// \n"
  73. "// RMSG 1796 A message from me!\n"
  74. "@endtsexample\n\n"
  75. )
  76. {
  77. //NetConnection *con = (NetConnection *) Sim::findObject(argv[1]);
  78. if(con)
  79. con->postNetEvent(new SimpleMessageEvent(message));
  80. }
  81. //ConsoleFunction( msg, void, 3, 3, "(NetConnection id, string message)"
  82. // "Send a SimpleNetObject message to the specified connection.")
  83. //{
  84. // NetConnection *con = (NetConnection *) Sim::findObject(argv[1]);
  85. // if(con)
  86. // con->postNetEvent(new SimpleMessageEvent(argv[2]));
  87. //}
  88. class SimpleNetObject : public NetObject
  89. {
  90. typedef NetObject Parent;
  91. public:
  92. char message[256];
  93. SimpleNetObject()
  94. {
  95. mNetFlags.set(ScopeAlways | Ghostable);
  96. dStrcpy(message, "Hello World!", 256);
  97. }
  98. U32 packUpdate(NetConnection *conn, U32 mask, BitStream *stream)
  99. {
  100. stream->writeString(message);
  101. return 0;
  102. }
  103. void unpackUpdate(NetConnection *conn, BitStream *stream)
  104. {
  105. stream->readString(message);
  106. Con::printf("Got message: %s", message);
  107. }
  108. void setMessage(const char *msg)
  109. {
  110. setMaskBits(1);
  111. dStrcpy(message, msg, 256);
  112. }
  113. DECLARE_CONOBJECT(SimpleNetObject);
  114. };
  115. IMPLEMENT_CO_NETOBJECT_V1(SimpleNetObject);
  116. ConsoleDocClass( SimpleNetObject,
  117. "@brief A very simple example of a class derived from NetObject.\n\n"
  118. "This object exists purely for instructional purposes. It is primarily "
  119. "geared toward developers that wish to understand the inner-working of "
  120. "Torque 3D's networking system. This is not intended for actual game "
  121. "development.\n\n "
  122. "@tsexample\n"
  123. "// On the server, create a new SimpleNetObject. This is a ghost always\n"
  124. "// object so it will be immediately ghosted to all connected clients.\n"
  125. "$s = new SimpleNetObject();\n\n"
  126. "// All connected clients will see the following in their console:\n"
  127. "// \n"
  128. "// Got message: Hello World!\n"
  129. "@endtsexample\n\n"
  130. "@see NetObject for a full breakdown of this example object\n"
  131. "@ingroup Networking\n");
  132. DefineEngineMethod( SimpleNetObject, setMessage, void, (const char* msg),,
  133. "@brief Sets the internal message variable.\n\n"
  134. "SimpleNetObject is set up to automatically transmit this new message to "
  135. "all connected clients. It will appear in the clients' console.\n"
  136. "@param msg The new message to send\n\n"
  137. "@tsexample\n"
  138. "// On the server, create a new SimpleNetObject. This is a ghost always\n"
  139. "// object so it will be immediately ghosted to all connected clients.\n"
  140. "$s = new SimpleNetObject();\n\n"
  141. "// All connected clients will see the following in their console:\n"
  142. "// \n"
  143. "// Got message: Hello World!\n\n"
  144. "// Now again on the server, change the message. This will cause it to\n"
  145. "// be sent to all connected clients.\n"
  146. "$s.setMessage(\"A new message from me!\");\n\n"
  147. "// All connected clients will now see in their console:\n"
  148. "// \n"
  149. "// Go message: A new message from me!\n"
  150. "@endtsexample\n\n"
  151. )
  152. {
  153. object->setMessage(msg);
  154. }
  155. //ConsoleMethod( SimpleNetObject, setMessage, void, 3, 3, "(string msg)")
  156. //{
  157. // object->setMessage(argv[2]);
  158. //}