RemoteEvent.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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> localOnlyEvents;
  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)
  53. return false;
  54. if (!checkRemoteEvent(mEventType))
  55. {
  56. LOGWARNING("Remote event " + toString(mEventType) + " not allowed");
  57. return false;
  58. }
  59. remoteEventSender = sender;
  60. if (!mEntityID)
  61. sendEvent(mEventType, mEventData);
  62. else
  63. {
  64. if (scene)
  65. {
  66. Entity* entity = scene->getEntity(mEntityID);
  67. if (!entity)
  68. LOGWARNING("Entity ID " + toString(mEntityID) + " not found for remote event " + toString(mEventType));
  69. else
  70. sendEvent(entity, mEventType, mEventData);
  71. }
  72. else
  73. LOGERROR("No scene for entity event");
  74. }
  75. remoteEventSender = 0;
  76. return true;
  77. }
  78. void registerLocalOnlyEvent(StringHash eventType)
  79. {
  80. localOnlyEvents.insert(eventType);
  81. }
  82. void registerLocalOnlyEvent(const std::string& name)
  83. {
  84. registerHash(name);
  85. localOnlyEvents.insert(StringHash(name));
  86. }
  87. bool checkRemoteEvent(StringHash eventType)
  88. {
  89. return localOnlyEvents.find(eventType) == localOnlyEvents.end();
  90. }
  91. Connection* getRemoteEventSender()
  92. {
  93. return remoteEventSender;
  94. }