RemoteEvent.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Log.h"
  25. #include "ReplicationUtils.h"
  26. #include "Scene.h"
  27. #include "StringUtils.h"
  28. #include <set>
  29. #include "DebugNew.h"
  30. static std::set<StringHash> remoteEvents;
  31. Connection* remoteEventSender = 0;
  32. void RemoteEvent::write(Serializer& dest) const
  33. {
  34. dest.writeUShort(mFrameNumber);
  35. if (mEntityID)
  36. dest.writeUInt(mEntityID);
  37. dest.writeStringHash(mEventType);
  38. dest.writeVariantMap(mEventData);
  39. }
  40. void RemoteEvent::read(Deserializer& source, bool hasEntity)
  41. {
  42. mFrameNumber = source.readUShort();
  43. if (hasEntity)
  44. mEntityID = source.readUInt();
  45. else
  46. mEntityID = 0;
  47. mEventType = source.readStringHash();
  48. mEventData = source.readVariantMap();
  49. }
  50. bool RemoteEvent::dispatch(Connection* sender, Scene* scene)
  51. {
  52. if ((!sender) || (!scene))
  53. return false;
  54. if (!checkRemoteEvent(mEventType))
  55. {
  56. LOGWARNING("Remote event " + toString(mEventType) + " not allowed");
  57. return false;
  58. }
  59. // Note: because the Connection object is not known in the Scene library, we must use Scene as the sender
  60. remoteEventSender = sender;
  61. if (!mEntityID)
  62. scene->sendEvent(mEventType, mEventData);
  63. else
  64. {
  65. Entity* entity = scene->getEntity(mEntityID);
  66. if (!entity)
  67. LOGWARNING("Entity ID " + toString(mEntityID) + " not found for remote event " + toString(mEventType));
  68. else
  69. scene->sendEvent(entity, mEventType, mEventData);
  70. }
  71. remoteEventSender = 0;
  72. return true;
  73. }
  74. void registerRemoteEvent(StringHash eventType)
  75. {
  76. remoteEvents.insert(eventType);
  77. }
  78. void registerRemoteEvent(const std::string& name)
  79. {
  80. registerHash(name, false);
  81. remoteEvents.insert(StringHash(name));
  82. }
  83. void unregisterRemoteEvent(StringHash eventType)
  84. {
  85. remoteEvents.erase(eventType);
  86. }
  87. void unregisterRemoteEvent(const std::string& name)
  88. {
  89. remoteEvents.erase(StringHash(name));
  90. }
  91. void unregisterAllRemoteEvents()
  92. {
  93. remoteEvents.clear();
  94. }
  95. bool checkRemoteEvent(StringHash eventType)
  96. {
  97. return remoteEvents.find(eventType) != remoteEvents.end();
  98. }
  99. Connection* getRemoteEventSender()
  100. {
  101. return remoteEventSender;
  102. }