netTest.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 "sim/simBase.h"
  24. #include "platform/event.h"
  25. #include "network/netConnection.h"
  26. #include "io/bitStream.h"
  27. #include "network/netObject.h"
  28. class SimpleMessageEvent : public NetEvent
  29. {
  30. typedef NetEvent Parent;
  31. char *msg;
  32. public:
  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. class SimpleNetObject : public NetObject
  54. {
  55. typedef NetObject Parent;
  56. public:
  57. char message[256];
  58. SimpleNetObject()
  59. {
  60. mNetFlags.set(ScopeAlways | Ghostable);
  61. dStrcpy(message, "Hello World!");
  62. }
  63. U32 packUpdate(NetConnection *conn, U32 mask, BitStream *stream)
  64. {
  65. stream->writeString(message);
  66. return 0;
  67. }
  68. void unpackUpdate(NetConnection *conn, BitStream *stream)
  69. {
  70. stream->readString(message);
  71. Con::printf("Got message: %s", message);
  72. }
  73. void setMessage(const char *msg)
  74. {
  75. setMaskBits(1);
  76. dStrcpy(message, msg);
  77. }
  78. DECLARE_CONOBJECT(SimpleNetObject);
  79. };
  80. IMPLEMENT_CO_NETOBJECT_V1(SimpleNetObject);
  81. /*! Sets the object message to the provide string
  82. @param msg The string you wish as the message
  83. @return No return value
  84. */
  85. ConsoleMethodWithDocs( SimpleNetObject, setMessage, void, 3, 3, (string msg))
  86. {
  87. object->setMessage(argv[2]);
  88. }
  89. /*! Send a SimpleNetObject message to the specified connection
  90. */
  91. ConsoleFunctionWithDocs( msg, void, 3, 3, (NetConnection id, string message))
  92. {
  93. NetConnection *con = (NetConnection *) Sim::findObject(argv[1]);
  94. if(con)
  95. con->postNetEvent(new SimpleMessageEvent(argv[2]));
  96. }